| 1 |
# COPYRIGHT (C) 2020-2024 Nicotine+ Contributors |
| 2 |
# |
| 3 |
# GNU GENERAL PUBLIC LICENSE |
| 4 |
# Version 3, 29 June 2007 |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
import os |
| 20 |
import random |
| 21 |
import shutil |
| 22 |
import sys |
| 23 |
|
| 24 |
from gi.repository import Gdk |
| 25 |
from gi.repository import Gio |
| 26 |
from gi.repository import GLib |
| 27 |
from gi.repository import Gtk |
| 28 |
from gi.repository import Pango |
| 29 |
|
| 30 |
import pynicotine |
| 31 |
from pynicotine.config import config |
| 32 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 33 |
from pynicotine.gtkgui.application import GTK_GUI_FOLDER_PATH |
| 34 |
from pynicotine.gtkgui.application import LIBADWAITA_API_VERSION |
| 35 |
from pynicotine.logfacility import log |
| 36 |
from pynicotine.shares import FileTypes |
| 37 |
from pynicotine.slskmessages import UserStatus |
| 38 |
from pynicotine.utils import encode_path |
| 39 |
|
| 40 |
|
| 41 |
# Global Style # |
| 42 |
|
| 43 |
|
| 44 |
CUSTOM_CSS_PROVIDER = Gtk.CssProvider() |
| 45 |
GTK_SETTINGS = Gtk.Settings.get_default() |
| 46 |
USE_COLOR_SCHEME_PORTAL = (sys.platform not in {"win32", "darwin"} and not LIBADWAITA_API_VERSION) |
| 47 |
|
| 48 |
if USE_COLOR_SCHEME_PORTAL: |
| 49 |
# GNOME 42+ system-wide dark mode for GTK without libadwaita |
| 50 |
SETTINGS_PORTAL = None |
| 51 |
|
| 52 |
class ColorScheme: |
| 53 |
NO_PREFERENCE = 0 |
| 54 |
PREFER_DARK = 1 |
| 55 |
PREFER_LIGHT = 2 |
| 56 |
|
| 57 |
def read_color_scheme(): |
| 58 |
|
| 59 |
color_scheme = None |
| 60 |
|
| 61 |
try: |
| 62 |
result = SETTINGS_PORTAL.call_sync( |
| 63 |
method_name="Read", |
| 64 |
parameters=GLib.Variant.new_tuple( |
| 65 |
GLib.Variant.new_string("org.freedesktop.appearance"), |
| 66 |
GLib.Variant.new_string("color-scheme") |
| 67 |
), |
| 68 |
flags=Gio.DBusCallFlags.NONE, |
| 69 |
timeout_msec=1000, |
| 70 |
cancellable=None |
| 71 |
) |
| 72 |
color_scheme, = result.unpack() |
| 73 |
|
| 74 |
except Exception as error: |
| 75 |
log.add_debug("Cannot read color scheme, falling back to GTK theme preference: %s", error) |
| 76 |
|
| 77 |
return color_scheme |
| 78 |
|
| 79 |
def on_color_scheme_changed(_proxy, _sender_name, signal_name, parameters): |
| 80 |
|
| 81 |
if signal_name != "SettingChanged": |
| 82 |
return |
| 83 |
|
| 84 |
namespace, name, color_scheme, *_unused = parameters.unpack() |
| 85 |
|
| 86 |
if (config.sections["ui"]["dark_mode"] |
| 87 |
or namespace != "org.freedesktop.appearance" or name != "color-scheme"): |
| 88 |
return |
| 89 |
|
| 90 |
set_dark_mode(color_scheme == ColorScheme.PREFER_DARK) |
| 91 |
|
| 92 |
try: |
| 93 |
SETTINGS_PORTAL = Gio.DBusProxy.new_for_bus_sync( |
| 94 |
bus_type=Gio.BusType.SESSION, |
| 95 |
flags=Gio.DBusProxyFlags.NONE, |
| 96 |
info=None, |
| 97 |
name="org.freedesktop.portal.Desktop", |
| 98 |
object_path="/org/freedesktop/portal/desktop", |
| 99 |
interface_name="org.freedesktop.portal.Settings", |
| 100 |
cancellable=None |
| 101 |
) |
| 102 |
SETTINGS_PORTAL.connect("g-signal", on_color_scheme_changed) |
| 103 |
|
| 104 |
except Exception as portal_error: |
| 105 |
log.add_debug("Cannot start color scheme settings portal, falling back to GTK theme preference: %s", |
| 106 |
portal_error) |
| 107 |
USE_COLOR_SCHEME_PORTAL = False |
| 108 |
|
| 109 |
|
| 110 |
def set_dark_mode(enabled): |
| 111 |
|
| 112 |
if LIBADWAITA_API_VERSION: |
| 113 |
from gi.repository import Adw # pylint: disable=no-name-in-module |
| 114 |
|
| 115 |
color_scheme = Adw.ColorScheme.FORCE_DARK if enabled else Adw.ColorScheme.DEFAULT |
| 116 |
Adw.StyleManager.get_default().set_color_scheme(color_scheme) |
| 117 |
return |
| 118 |
|
| 119 |
if USE_COLOR_SCHEME_PORTAL and not enabled: |
| 120 |
color_scheme = read_color_scheme() |
| 121 |
|
| 122 |
if color_scheme is not None: |
| 123 |
enabled = (color_scheme == ColorScheme.PREFER_DARK) |
| 124 |
|
| 125 |
GTK_SETTINGS.props.gtk_application_prefer_dark_theme = enabled |
| 126 |
|
| 127 |
|
| 128 |
def set_use_header_bar(enabled): |
| 129 |
GTK_SETTINGS.props.gtk_dialogs_use_header = enabled |
| 130 |
|
| 131 |
|
| 132 |
def set_default_font(): |
| 133 |
|
| 134 |
if sys.platform == "win32": |
| 135 |
# Using larger font size than the default to match modern Windows apps |
| 136 |
GTK_SETTINGS.props.gtk_font_name = "Segoe UI 10" |
| 137 |
|
| 138 |
elif sys.platform == "darwin": |
| 139 |
# Using Helvetica Neue instead of the default font due to rendering |
| 140 |
# issues with the latter |
| 141 |
GTK_SETTINGS.props.gtk_font_name = "Helvetica Neue 13" |
| 142 |
|
| 143 |
else: |
| 144 |
return |
| 145 |
|
| 146 |
if GTK_API_VERSION == 3: |
| 147 |
return |
| 148 |
|
| 149 |
# Enable OS-specific font tweaks |
| 150 |
try: |
| 151 |
GTK_SETTINGS.props.gtk_font_rendering = Gtk.FontRendering.MANUAL # pylint: disable=no-member |
| 152 |
except AttributeError: |
| 153 |
# GTK <4.16 |
| 154 |
pass |
| 155 |
|
| 156 |
|
| 157 |
def set_visual_settings(isolated_mode=False): |
| 158 |
|
| 159 |
if isolated_mode: |
| 160 |
# Hide minimize/maximize buttons in isolated mode (e.g. Broadway backend) |
| 161 |
GTK_SETTINGS.props.gtk_decoration_layout = ":close" |
| 162 |
GTK_SETTINGS.props.gtk_titlebar_right_click = "none" |
| 163 |
|
| 164 |
elif sys.platform == "darwin": |
| 165 |
# Left align window controls on macOS |
| 166 |
GTK_SETTINGS.props.gtk_decoration_layout = "close,minimize,maximize:" |
| 167 |
|
| 168 |
if os.environ.get("GDK_BACKEND") == "broadway": |
| 169 |
# Animations use a lot of CPU in Broadway, disable them |
| 170 |
GTK_SETTINGS.props.gtk_enable_animations = False |
| 171 |
|
| 172 |
if GTK_API_VERSION >= 4: |
| 173 |
GTK_SETTINGS.props.gtk_cursor_blink = False |
| 174 |
|
| 175 |
set_default_font() |
| 176 |
set_dark_mode(config.sections["ui"]["dark_mode"]) |
| 177 |
set_use_header_bar(config.sections["ui"]["header_bar"]) |
| 178 |
|
| 179 |
|
| 180 |
def set_global_css(): |
| 181 |
|
| 182 |
global_css_provider = Gtk.CssProvider() |
| 183 |
css_folder_path = os.path.join(GTK_GUI_FOLDER_PATH, "css") |
| 184 |
css = bytearray() |
| 185 |
|
| 186 |
with open(encode_path(os.path.join(css_folder_path, "style.css")), "rb") as file_handle: |
| 187 |
css += file_handle.read() |
| 188 |
|
| 189 |
if GTK_API_VERSION >= 4: |
| 190 |
add_provider_func = Gtk.StyleContext.add_provider_for_display # pylint: disable=no-member |
| 191 |
display = Gdk.Display.get_default() |
| 192 |
|
| 193 |
with open(encode_path(os.path.join(css_folder_path, "style_gtk4.css")), "rb") as file_handle: |
| 194 |
css += file_handle.read() |
| 195 |
|
| 196 |
if sys.platform == "win32": |
| 197 |
with open(encode_path(os.path.join(css_folder_path, "style_gtk4_win32.css")), "rb") as file_handle: |
| 198 |
css += file_handle.read() |
| 199 |
|
| 200 |
elif sys.platform == "darwin": |
| 201 |
with open(encode_path(os.path.join(css_folder_path, "style_gtk4_darwin.css")), "rb") as file_handle: |
| 202 |
css += file_handle.read() |
| 203 |
|
| 204 |
elif os.environ.get("GDK_BACKEND") == "broadway": |
| 205 |
with open(encode_path(os.path.join(css_folder_path, "style_gtk4_broadway.css")), "rb") as file_handle: |
| 206 |
css += file_handle.read() |
| 207 |
|
| 208 |
if LIBADWAITA_API_VERSION: |
| 209 |
with open(encode_path(os.path.join(css_folder_path, "style_libadwaita.css")), "rb") as file_handle: |
| 210 |
css += file_handle.read() |
| 211 |
|
| 212 |
load_css(global_css_provider, css) |
| 213 |
|
| 214 |
else: |
| 215 |
add_provider_func = Gtk.StyleContext.add_provider_for_screen # pylint: disable=no-member |
| 216 |
display = Gdk.Screen.get_default() |
| 217 |
|
| 218 |
with open(encode_path(os.path.join(css_folder_path, "style_gtk3.css")), "rb") as file_handle: |
| 219 |
css += file_handle.read() |
| 220 |
|
| 221 |
load_css(global_css_provider, css) |
| 222 |
|
| 223 |
add_provider_func(display, global_css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) |
| 224 |
add_provider_func(display, CUSTOM_CSS_PROVIDER, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) |
| 225 |
|
| 226 |
|
| 227 |
def set_global_style(isolated_mode=False): |
| 228 |
set_visual_settings(isolated_mode) |
| 229 |
set_global_css() |
| 230 |
update_custom_css() |
| 231 |
|
| 232 |
|
| 233 |
# Icons # |
| 234 |
|
| 235 |
|
| 236 |
if GTK_API_VERSION >= 4: |
| 237 |
ICON_THEME = Gtk.IconTheme.get_for_display(Gdk.Display.get_default()) # pylint: disable=no-member |
| 238 |
else: |
| 239 |
ICON_THEME = Gtk.IconTheme.get_default() # pylint: disable=no-member |
| 240 |
|
| 241 |
CUSTOM_ICON_THEME_NAME = ".nicotine-icon-theme" |
| 242 |
FILE_TYPE_ICON_LABELS = { |
| 243 |
"application-x-executable-symbolic": _("Executable"), |
| 244 |
"folder-music-symbolic": _("Audio"), |
| 245 |
"folder-pictures-symbolic": _("Image"), |
| 246 |
"package-x-generic-symbolic": _("Archive"), |
| 247 |
"folder-documents-symbolic": _("Miscellaneous"), |
| 248 |
"folder-videos-symbolic": _("Video"), |
| 249 |
"x-office-document-symbolic": _("Document"), |
| 250 |
"emblem-documents-symbolic": _("Text") |
| 251 |
} |
| 252 |
USER_STATUS_ICON_LABELS = { |
| 253 |
"nplus-status-available": _("Online"), |
| 254 |
"nplus-status-away": _("Away"), |
| 255 |
"nplus-status-offline": _("Offline") |
| 256 |
} |
| 257 |
USER_STATUS_ICON_NAMES = { |
| 258 |
UserStatus.ONLINE: "nplus-status-available", |
| 259 |
UserStatus.AWAY: "nplus-status-away", |
| 260 |
UserStatus.OFFLINE: "nplus-status-offline" |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
def load_custom_icons(update=False): |
| 265 |
"""Load custom icon theme if one is selected.""" |
| 266 |
|
| 267 |
if update: |
| 268 |
GTK_SETTINGS.reset_property("gtk-icon-theme-name") |
| 269 |
|
| 270 |
icon_theme_path = os.path.join(config.data_folder_path, CUSTOM_ICON_THEME_NAME) |
| 271 |
icon_theme_path_encoded = encode_path(icon_theme_path) |
| 272 |
|
| 273 |
parent_icon_theme_name = GTK_SETTINGS.props.gtk_icon_theme_name |
| 274 |
|
| 275 |
if parent_icon_theme_name == CUSTOM_ICON_THEME_NAME: |
| 276 |
return |
| 277 |
|
| 278 |
try: |
| 279 |
# Create internal icon theme folder |
| 280 |
if os.path.exists(icon_theme_path_encoded): |
| 281 |
shutil.rmtree(icon_theme_path_encoded) |
| 282 |
|
| 283 |
except Exception as error: |
| 284 |
log.add_debug("Failed to remove custom icon theme folder %s: %s", (icon_theme_path, error)) |
| 285 |
return |
| 286 |
|
| 287 |
user_icon_theme_path = config.sections["ui"]["icontheme"] |
| 288 |
|
| 289 |
if not user_icon_theme_path: |
| 290 |
return |
| 291 |
|
| 292 |
user_icon_theme_path = os.path.normpath(os.path.expandvars(user_icon_theme_path)) |
| 293 |
log.add_debug("Loading custom icon theme from %s", user_icon_theme_path) |
| 294 |
|
| 295 |
theme_file_path = os.path.join(icon_theme_path, "index.theme") |
| 296 |
theme_file_contents = ( |
| 297 |
"[Icon Theme]\n" |
| 298 |
"Name=Nicotine+ Icon Theme\n" |
| 299 |
"Inherits=" + parent_icon_theme_name + "\n" |
| 300 |
"Directories=.\n" |
| 301 |
"\n" |
| 302 |
"[.]\n" |
| 303 |
"Size=16\n" |
| 304 |
"MinSize=8\n" |
| 305 |
"MaxSize=512\n" |
| 306 |
"Type=Scalable" |
| 307 |
) |
| 308 |
|
| 309 |
try: |
| 310 |
# Create internal icon theme folder |
| 311 |
os.makedirs(icon_theme_path_encoded) |
| 312 |
|
| 313 |
# Create icon theme index file |
| 314 |
with open(encode_path(theme_file_path), "w", encoding="utf-8") as file_handle: |
| 315 |
file_handle.write(theme_file_contents) |
| 316 |
|
| 317 |
except Exception as error: |
| 318 |
log.add_debug("Failed to enable custom icon theme %s: %s", (user_icon_theme_path, error)) |
| 319 |
return |
| 320 |
|
| 321 |
icon_names = ( |
| 322 |
("away", USER_STATUS_ICON_NAMES[UserStatus.AWAY]), |
| 323 |
("online", USER_STATUS_ICON_NAMES[UserStatus.ONLINE]), |
| 324 |
("offline", USER_STATUS_ICON_NAMES[UserStatus.OFFLINE]), |
| 325 |
("hilite", "nplus-tab-highlight"), |
| 326 |
("hilite3", "nplus-tab-changed"), |
| 327 |
("trayicon_away", "nplus-tray-away"), |
| 328 |
("trayicon_away", f"{pynicotine.__application_id__}-away"), |
| 329 |
("trayicon_connect", "nplus-tray-connect"), |
| 330 |
("trayicon_connect", f"{pynicotine.__application_id__}-connect"), |
| 331 |
("trayicon_disconnect", "nplus-tray-disconnect"), |
| 332 |
("trayicon_disconnect", f"{pynicotine.__application_id__}-disconnect"), |
| 333 |
("trayicon_msg", "nplus-tray-msg"), |
| 334 |
("trayicon_msg", f"{pynicotine.__application_id__}-msg"), |
| 335 |
("n", pynicotine.__application_id__), |
| 336 |
("n", f"{pynicotine.__application_id__}-symbolic") |
| 337 |
) |
| 338 |
extensions = (".png", ".svg", ".jpg", ".jpeg", ".bmp") |
| 339 |
|
| 340 |
# Move custom icons to internal icon theme location |
| 341 |
for original_name, replacement_name in icon_names: |
| 342 |
for extension in extensions: |
| 343 |
file_path = os.path.join(user_icon_theme_path, original_name + extension) |
| 344 |
file_path_encoded = encode_path(file_path) |
| 345 |
|
| 346 |
if not os.path.isfile(file_path_encoded): |
| 347 |
continue |
| 348 |
|
| 349 |
try: |
| 350 |
shutil.copyfile( |
| 351 |
file_path_encoded, |
| 352 |
encode_path(os.path.join(icon_theme_path, replacement_name + extension)) |
| 353 |
) |
| 354 |
break |
| 355 |
|
| 356 |
except OSError as error: |
| 357 |
log.add(_("Error loading custom icon %(path)s: %(error)s"), { |
| 358 |
"path": file_path, |
| 359 |
"error": error |
| 360 |
}) |
| 361 |
|
| 362 |
# Enable custom icon theme |
| 363 |
GTK_SETTINGS.props.gtk_icon_theme_name = CUSTOM_ICON_THEME_NAME |
| 364 |
|
| 365 |
|
| 366 |
def load_icons(): |
| 367 |
"""Load custom icons necessary for the application to function.""" |
| 368 |
|
| 369 |
paths = ( |
| 370 |
config.data_folder_path, # Custom internal icon theme |
| 371 |
os.path.join(GTK_GUI_FOLDER_PATH, "icons"), # Support running from folder, as well as macOS and Windows |
| 372 |
os.path.join(sys.prefix, "share", "icons") # Support Python venv |
| 373 |
) |
| 374 |
|
| 375 |
for path in paths: |
| 376 |
if GTK_API_VERSION >= 4: |
| 377 |
ICON_THEME.add_search_path(path) |
| 378 |
else: |
| 379 |
ICON_THEME.append_search_path(path) |
| 380 |
|
| 381 |
load_custom_icons() |
| 382 |
|
| 383 |
|
| 384 |
def get_flag_icon_name(country_code): |
| 385 |
|
| 386 |
if not country_code: |
| 387 |
return "" |
| 388 |
|
| 389 |
return f"nplus-flag-{country_code.lower()}" |
| 390 |
|
| 391 |
|
| 392 |
def get_file_type_icon_name(basename): |
| 393 |
|
| 394 |
_basename_no_extension, _separator, extension = basename.rpartition(".") |
| 395 |
extension = extension.lower() |
| 396 |
|
| 397 |
if extension in FileTypes.AUDIO: |
| 398 |
return "folder-music-symbolic" |
| 399 |
|
| 400 |
if extension in FileTypes.IMAGE: |
| 401 |
return "folder-pictures-symbolic" |
| 402 |
|
| 403 |
if extension in FileTypes.VIDEO: |
| 404 |
return "folder-videos-symbolic" |
| 405 |
|
| 406 |
if extension in FileTypes.ARCHIVE: |
| 407 |
return "package-x-generic-symbolic" |
| 408 |
|
| 409 |
if extension in FileTypes.DOCUMENT: |
| 410 |
return "x-office-document-symbolic" |
| 411 |
|
| 412 |
if extension in FileTypes.TEXT: |
| 413 |
return "emblem-documents-symbolic" |
| 414 |
|
| 415 |
if extension in FileTypes.EXECUTABLE: |
| 416 |
return "application-x-executable-symbolic" |
| 417 |
|
| 418 |
return "folder-documents-symbolic" |
| 419 |
|
| 420 |
|
| 421 |
def on_icon_theme_changed(*_args): |
| 422 |
load_custom_icons() |
| 423 |
|
| 424 |
|
| 425 |
ICON_THEME.connect("changed", on_icon_theme_changed) |
| 426 |
|
| 427 |
|
| 428 |
# Fonts and Colors # |
| 429 |
|
| 430 |
|
| 431 |
PANGO_STYLES = { |
| 432 |
Pango.Style.NORMAL: "normal", |
| 433 |
Pango.Style.ITALIC: "italic" |
| 434 |
} |
| 435 |
PANGO_WEIGHTS = { |
| 436 |
Pango.Weight.THIN: 100, |
| 437 |
Pango.Weight.ULTRALIGHT: 200, |
| 438 |
Pango.Weight.LIGHT: 300, |
| 439 |
Pango.Weight.SEMILIGHT: 350, |
| 440 |
Pango.Weight.BOOK: 380, |
| 441 |
Pango.Weight.NORMAL: 400, |
| 442 |
Pango.Weight.MEDIUM: 500, |
| 443 |
Pango.Weight.SEMIBOLD: 600, |
| 444 |
Pango.Weight.BOLD: 700, |
| 445 |
Pango.Weight.ULTRABOLD: 800, |
| 446 |
Pango.Weight.HEAVY: 900, |
| 447 |
Pango.Weight.ULTRAHEAVY: 1000 |
| 448 |
} |
| 449 |
USER_STATUS_COLORS = { |
| 450 |
UserStatus.ONLINE: "useronline", |
| 451 |
UserStatus.AWAY: "useraway", |
| 452 |
UserStatus.OFFLINE: "useroffline" |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
def add_css_class(widget, css_class): |
| 457 |
|
| 458 |
if GTK_API_VERSION >= 4: |
| 459 |
widget.add_css_class(css_class) # pylint: disable=no-member |
| 460 |
return |
| 461 |
|
| 462 |
widget.get_style_context().add_class(css_class) # pylint: disable=no-member |
| 463 |
|
| 464 |
|
| 465 |
def remove_css_class(widget, css_class): |
| 466 |
|
| 467 |
if GTK_API_VERSION >= 4: |
| 468 |
widget.remove_css_class(css_class) # pylint: disable=no-member |
| 469 |
return |
| 470 |
|
| 471 |
widget.get_style_context().remove_class(css_class) # pylint: disable=no-member |
| 472 |
|
| 473 |
|
| 474 |
def load_css(css_provider, data): |
| 475 |
|
| 476 |
try: |
| 477 |
css_provider.load_from_string(data.decode()) |
| 478 |
|
| 479 |
except AttributeError: |
| 480 |
try: |
| 481 |
css_provider.load_from_data(data.decode(), length=-1) |
| 482 |
|
| 483 |
except TypeError: |
| 484 |
css_provider.load_from_data(data) |
| 485 |
|
| 486 |
|
| 487 |
def _get_custom_font_css(): |
| 488 |
|
| 489 |
css = bytearray() |
| 490 |
|
| 491 |
for css_selector, font in ( |
| 492 |
("window, popover", config.sections["ui"]["globalfont"]), |
| 493 |
("treeview", config.sections["ui"]["listfont"]), |
| 494 |
("textview", config.sections["ui"]["textviewfont"]), |
| 495 |
(".chat-view textview", config.sections["ui"]["chatfont"]), |
| 496 |
(".search-view treeview", config.sections["ui"]["searchfont"]), |
| 497 |
(".transfers-view treeview", config.sections["ui"]["transfersfont"]), |
| 498 |
(".userbrowse-view treeview", config.sections["ui"]["browserfont"]) |
| 499 |
): |
| 500 |
font_description = Pango.FontDescription.from_string(font) |
| 501 |
|
| 502 |
if font_description.get_set_fields() & (Pango.FontMask.FAMILY | Pango.FontMask.SIZE): |
| 503 |
css += ( |
| 504 |
f""" |
| 505 |
{css_selector} {{ |
| 506 |
font-family: '{font_description.get_family()}'; |
| 507 |
font-size: {font_description.get_size() // 1024}pt; |
| 508 |
font-style: {PANGO_STYLES.get(font_description.get_style(), "normal")}; |
| 509 |
font-weight: {PANGO_WEIGHTS.get(font_description.get_weight(), "normal")}; |
| 510 |
}} |
| 511 |
""".encode() |
| 512 |
) |
| 513 |
|
| 514 |
return css |
| 515 |
|
| 516 |
|
| 517 |
def _is_color_valid(color_hex): |
| 518 |
return color_hex and Gdk.RGBA().parse(color_hex) |
| 519 |
|
| 520 |
|
| 521 |
def _get_custom_color_css(): |
| 522 |
|
| 523 |
css = bytearray() |
| 524 |
|
| 525 |
# User status colors |
| 526 |
online_color = config.sections["ui"]["useronline"] |
| 527 |
away_color = config.sections["ui"]["useraway"] |
| 528 |
offline_color = config.sections["ui"]["useroffline"] |
| 529 |
|
| 530 |
if _is_color_valid(online_color) and _is_color_valid(away_color) and _is_color_valid(offline_color): |
| 531 |
css += ( |
| 532 |
f""" |
| 533 |
.user-status {{ |
| 534 |
-gtk-icon-palette: success {online_color}, warning {away_color}, error {offline_color}; |
| 535 |
}} |
| 536 |
""".encode() |
| 537 |
) |
| 538 |
|
| 539 |
# Text colors |
| 540 |
treeview_text_color = config.sections["ui"]["search"] |
| 541 |
|
| 542 |
for css_selector, color in ( |
| 543 |
(".notebook-tab", config.sections["ui"]["tab_default"]), |
| 544 |
(".notebook-tab-changed", config.sections["ui"]["tab_changed"]), |
| 545 |
(".notebook-tab-highlight", config.sections["ui"]["tab_hilite"]), |
| 546 |
("entry", config.sections["ui"]["inputcolor"]), |
| 547 |
("treeview .cell:not(:disabled):not(:selected):not(.progressbar)", treeview_text_color) |
| 548 |
): |
| 549 |
if _is_color_valid(color): |
| 550 |
css += ( |
| 551 |
f""" |
| 552 |
{css_selector} {{ |
| 553 |
color: {color}; |
| 554 |
}} |
| 555 |
""".encode() |
| 556 |
) |
| 557 |
|
| 558 |
# Background colors |
| 559 |
for css_selector, color in ( |
| 560 |
("entry", config.sections["ui"]["textbg"]), |
| 561 |
): |
| 562 |
if _is_color_valid(color): |
| 563 |
css += ( |
| 564 |
f""" |
| 565 |
{css_selector} {{ |
| 566 |
background: {color}; |
| 567 |
}} |
| 568 |
""".encode() |
| 569 |
) |
| 570 |
|
| 571 |
# Reset treeview column header colors |
| 572 |
if treeview_text_color: |
| 573 |
css += ( |
| 574 |
b""" |
| 575 |
treeview header { |
| 576 |
color: initial; |
| 577 |
} |
| 578 |
""" |
| 579 |
) |
| 580 |
|
| 581 |
# Workaround for GTK bug where tree view colors don't update until moving the |
| 582 |
# cursor over the widget. Changing the color of the text caret to a random one |
| 583 |
# forces the tree view to re-render with new icon/text colors (text carets are |
| 584 |
# never visible in our tree views, so usability is unaffected). |
| 585 |
css += ( |
| 586 |
f""" |
| 587 |
treeview {{ |
| 588 |
caret-color: #{random.randint(0, 0xFFFFFF):06x}; |
| 589 |
}} |
| 590 |
|
| 591 |
treeview popover {{ |
| 592 |
caret-color: initial; |
| 593 |
}} |
| 594 |
""".encode() |
| 595 |
) |
| 596 |
|
| 597 |
return css |
| 598 |
|
| 599 |
|
| 600 |
def update_custom_css(): |
| 601 |
|
| 602 |
using_custom_icon_theme = (GTK_SETTINGS.props.gtk_icon_theme_name == CUSTOM_ICON_THEME_NAME) |
| 603 |
css = bytearray( |
| 604 |
f""" |
| 605 |
.colored-icon {{ |
| 606 |
-gtk-icon-style: {"regular" if using_custom_icon_theme else "symbolic"}; |
| 607 |
}} |
| 608 |
""".encode() |
| 609 |
) |
| 610 |
css += _get_custom_font_css() |
| 611 |
css += _get_custom_color_css() |
| 612 |
|
| 613 |
load_css(CUSTOM_CSS_PROVIDER, css) |
| 614 |
|
| 615 |
|
| 616 |
def update_tag_visuals(tag, color_id): |
| 617 |
|
| 618 |
enable_colored_usernames = config.sections["ui"]["usernamehotspots"] |
| 619 |
is_hotspot_tag = (color_id in {"useraway", "useronline", "useroffline"}) |
| 620 |
color_hex = config.sections["ui"].get(color_id) |
| 621 |
tag_props = tag.props |
| 622 |
|
| 623 |
if is_hotspot_tag and not enable_colored_usernames: |
| 624 |
color_hex = None |
| 625 |
|
| 626 |
if not color_hex: |
| 627 |
if tag_props.foreground_rgba: |
| 628 |
tag_props.foreground_rgba = None |
| 629 |
else: |
| 630 |
current_rgba = tag_props.foreground_rgba |
| 631 |
new_rgba = Gdk.RGBA() |
| 632 |
new_rgba.parse(color_hex) |
| 633 |
|
| 634 |
if current_rgba is None or not new_rgba.equal(current_rgba): |
| 635 |
tag_props.foreground_rgba = new_rgba |
| 636 |
|
| 637 |
# URLs |
| 638 |
if color_id == "urlcolor" and tag_props.underline != Pango.Underline.SINGLE: |
| 639 |
tag_props.underline = Pango.Underline.SINGLE |
| 640 |
|
| 641 |
# Hotspots |
| 642 |
if not is_hotspot_tag: |
| 643 |
return |
| 644 |
|
| 645 |
username_style = config.sections["ui"]["usernamestyle"] |
| 646 |
|
| 647 |
weight_style = Pango.Weight.BOLD if username_style == "bold" else Pango.Weight.NORMAL |
| 648 |
if tag_props.weight != weight_style: |
| 649 |
tag_props.weight = weight_style |
| 650 |
|
| 651 |
italic_style = Pango.Style.ITALIC if username_style == "italic" else Pango.Style.NORMAL |
| 652 |
if tag_props.style != italic_style: |
| 653 |
tag_props.style = italic_style |
| 654 |
|
| 655 |
underline_style = Pango.Underline.SINGLE if username_style == "underline" else Pango.Underline.NONE |
| 656 |
if tag_props.underline != underline_style: |
| 657 |
tag_props.underline = underline_style |