From 00e76220b3583e618f2b8b335adec0503ce34e8e Mon Sep 17 00:00:00 2001 From: Popa Ioan Alexandru Date: Tue, 5 Oct 2021 04:50:18 +0300 Subject: [PATCH] Support GNOME and Cinnamon --- README.md | 4 ++-- src/applythemes.py | 60 +++++++++++++++++++++++++++++++++++++++++++--- src/window.ui | 2 +- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e26b1e9..f8c3a9f 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ It also lets the user install icon and widget theme archives. - Set the cursor theme, and tweak the cursor's size - Set all these themes with a special searchable selector with previews for GTK3, icon and cursor themes - Set various options like whether buttons have images or not -- Instantaneously apply your setting changes to the running applications using [xsettingsd](https://github.com/derat/xsettingsd) (you must download xsettingsd and run it in the background) +- Instantaneously apply your setting changes to the running applications in GNOME, Cinnamon, or using [xsettingsd](https://github.com/derat/xsettingsd) (you must download xsettingsd and run it in the background) for those that don't use GTK desktop environments - Edit GTK CSS with instantaneous feedback of the changes made - Install new widget or icon themes from archives available e.g. at https://gnome-look.org @@ -32,7 +32,7 @@ In the folder with the source (obtainable e.g. by running `git clone https://git # TODOs - Add more advanced Kvantum support (e.g. handling of the Kvantum theme names, which oftentimes don't match the GTK theme names, or syncing some options) -- Implement live reloading for other XSETTINGS-compatible daemons. +- Implement live reloading for other desktop environments / XSETTINGS-compatible daemons. - Add more options to set (see [further reference](https://developer.gnome.org/gtk3/stable/GtkSettings.html)) - Add theme remover - Add a client for OCS-compatible websites like gnome-look.org, tailored for downloading (and automatically installing) themes (see [API reference]() and [a reference project](https://www.opencode.net/dfn2/pling-store-development)) diff --git a/src/applythemes.py b/src/applythemes.py index 18a256b..e782ab3 100644 --- a/src/applythemes.py +++ b/src/applythemes.py @@ -17,7 +17,7 @@ import os import subprocess -from gi.repository import GLib +from gi.repository import GLib, Gio class BaseApplyThemes: def applyThemes(self, props, gtk2Theme, gtk4Theme, kvantumTheme, cssText): @@ -79,7 +79,53 @@ def applyThemes(self, props, gtk2Theme, gtk4Theme, kvantumTheme, cssText): with open(os.path.join(GLib.get_user_config_dir(), "gtk-4.0", "gtk.css"), "w") as cssFile: cssFile.write(cssText) +class GSettingsApplyThemes(BaseApplyThemes): + def applyThemes(self, props, **kwargs): + super().applyThemes(props, **kwargs) + + self.settings.set_string("gtk-theme", props.gtk_theme_name) + self.settings.set_string("icon-theme", props.gtk_icon_theme_name) + self.settings.set_string("cursor-theme", props.gtk_cursor_theme_name or "") + self.settings.set_int("cursor-size", props.gtk_cursor_theme_size) + self.settings.set_string("gtk-key-theme", props.gtk_key_theme_name or "") + self.settings.set_string("font-name", props.gtk_font_name) + self.settings.set_double("text-scaling-factor", props.gtk_xft_dpi/(96*1024)) +class GnomeApplyThemes(GSettingsApplyThemes): + def __init__(self): + self.settings = Gio.Settings.new("org.gnome.desktop.interface") + + def applyThemes(self, props, **kwargs): + super().applyThemes(props, **kwargs) + + if props.gtk_xft_rgba != "none": + self.settings.set_string("font-rgba-order", props.gtk_xft_rgba or "rgb") + self.settings.set_string("font-antialiasing", "rgba" if props.gtk_xft_antialias else "none") + else: + self.settings.set_string("font-antialiasing", "grayscale" if props.gtk_xft_antialias else "none") + self.settings.set_string("font-hinting", props.gtk_xft_hintstyle[4:]) # drop the first 4 letters ('hint' in all cases) + + self.settings.set_boolean("overlay-scrolling", props.gtk_overlay_scrolling) + +class CinnamonApplyThemes(GSettingsApplyThemes): + def __init__(self): + self.settings = Gio.Settings.new("org.cinnamon.desktop.interface") + self.settingsFont = Gio.Settings.new("org.cinnamon.settings-daemon.plugins.xsettings") + + def applyThemes(self, props, **kwargs): + super().applyThemes(props, **kwargs) + + if props.gtk_xft_rgba != "none": + self.settingsFont.set_string("rgba-order", props.gtk_xft_rgba) + self.settingsFont.set_string("antialiasing", "rgba" if props.gtk_xft_antialias else "none") + else: + self.settingsFont.set_string("antialiasing", "grayscale" if props.gtk_xft_antialias else "none") + self.settingsFont.set_string("hinting", props.gtk_xft_hintstyle[4:]) # drop the first 4 letters ('hint' in all cases) + + self.settings.set_boolean("gtk-overlay-scrollbars", props.gtk_overlay_scrolling) + self.settings.set_boolean("buttons-have-icons", props.gtk_button_images) + self.settings.set_boolean("menus-have-icons", props.gtk_menu_images) + class XsettingsdApplyThemes(BaseApplyThemes): def __init__(self): self.confFolder = os.path.join(GLib.get_user_config_dir(), "xsettingsd") @@ -100,9 +146,10 @@ def applyThemes(self, props, **kwargs): "Xft/HintStyle": f'"{props.gtk_xft_hintstyle or "hintnone"}"', "Xft/RGBA": f'"{props.gtk_xft_rgba or "none"}"', "Xft/DPI": props.gtk_xft_dpi, - "Gtk/CursorThemeName": f'"{props.gtk_cursor_theme_name or "default"}"', + "Gtk/CursorThemeName": f'"{props.gtk_cursor_theme_name or ""}"', "Gtk/FontName": f'"{props.gtk_font_name}"', "Gtk/KeyThemeName": f'"{props.gtk_key_theme_name or ""}"', + "Gtk/OverlayScrolling": int(props.gtk_overlay_scrolling), "Gtk/MenuImages": int(props.gtk_menu_images), "Gtk/ButtonImages": int(props.gtk_button_images), } @@ -113,8 +160,15 @@ def applyThemes(self, props, **kwargs): subprocess.run(["pkill", "-HUP", "^xsettingsd$"]) +def isRunning(app): + return subprocess.call(["pidof", app], stdout=subprocess.DEVNULL) == 0 + def getThemeApplier(): - if subprocess.call(["pidof", "xsettingsd"], stdout=subprocess.DEVNULL) == 0: + if isRunning("gsd-xsettings"): + return GnomeApplyThemes() + elif isRunning("csd-xsettings"): + return CinnamonApplyThemes() + elif isRunning("xsettingsd"): return XsettingsdApplyThemes() else: return BaseApplyThemes() diff --git a/src/window.ui b/src/window.ui index 8b1cfd7..b4dc1e0 100644 --- a/src/window.ui +++ b/src/window.ui @@ -249,7 +249,7 @@ True False - Live theme reloading detected. Setting another theme for GTK2 or GTK4 won't have any effect. + Instantaneous theme changing is supported. This means that setting another theme for GTK2 or GTK4 won't have any effect. True