From 65606dc617414c14c59eb7703408a4fbc23e4ccf Mon Sep 17 00:00:00 2001 From: Romain Vigier Date: Wed, 16 Oct 2019 17:06:44 +0200 Subject: [PATCH] Add support for Materia-compact and Arc themes, watch theme changes --- src/extension.js | 75 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/src/extension.js b/src/extension.js index 447dc93..120744b 100644 --- a/src/extension.js +++ b/src/extension.js @@ -20,12 +20,31 @@ this program. If not, see . const Gio = imports.gi.Gio; -let interface_settings; +let interface_settings, interface_settings_connect_id; let nightlight_active; let original_user_theme, user_theme_day, user_theme_night; let conn, proxy, proxy_connect_id; +function _connect_dbus_proxy() { + conn = Gio.bus_get_sync(Gio.BusType.SESSION, null); + if ( conn === null ) { + throw new Error('Unable to connect to the session bus'); + } + proxy = Gio.DBusProxy.new_sync( + conn, + Gio.DBusProxyFlags.GET_INVALIDATED_PROPERTIES, + null, + 'org.gnome.SettingsDaemon.Color', + '/org/gnome/SettingsDaemon/Color', + 'org.gnome.SettingsDaemon.Color', + null + ); + if ( proxy === null ) { + throw new Error('Unable to create proxy to the session bus'); + } +} + function _get_theme() { return interface_settings.get_string('gtk-theme'); } @@ -34,6 +53,25 @@ function _set_theme(theme) { interface_settings.set_string('gtk-theme', theme); } +function _build_theme_variants() { + if ( original_user_theme.includes('HighContrast') ) { + user_theme_day = 'HighContrast'; + user_theme_night = 'HighContrastInverse'; + } + else if ( original_user_theme.match(/Materia.*-compact/g) ) { + user_theme_day = original_user_theme.replace('-dark', ''); + user_theme_night = user_theme_day.replace(/(-light)?-compact/g, '-dark-compact'); + } + else if ( original_user_theme.includes('Arc') ) { + user_theme_day = original_user_theme.replace('-Dark', ''); + user_theme_night = user_theme_day.replace('-Darker', '') + '-Dark'; + } + else { + user_theme_day = original_user_theme.replace('-dark', ''); + user_theme_night = user_theme_day.replace(/(-light)?(-darker)?/g, '') + '-dark'; + } +} + function _apply_theme_variant() { const variant = proxy.get_cached_property('NightLightActive'); if ( variant.get_boolean() === nightlight_active ) return; @@ -47,49 +85,36 @@ function enable() { // Store the current user theme and build day and night variants interface_settings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' }); original_user_theme = _get_theme(); - if ( original_user_theme.includes('HighContrast') ) { - user_theme_day = 'HighContrast'; - user_theme_night = 'HighContrastInverse'; - } - else { - user_theme_day = original_user_theme.replace(/(-dark)?/gi, ''); - user_theme_night = user_theme_day.replace(/(-light)?(-darker)?/gi, '') + '-dark'; - } + _build_theme_variants(); // Connect to session bus, listen to Color changes and change theme variant try { - conn = Gio.bus_get_sync(Gio.BusType.SESSION, null); - if ( conn === null ) { - throw new Error('Unable to connect to the session bus'); - } - proxy = Gio.DBusProxy.new_sync( - conn, - Gio.DBusProxyFlags.GET_INVALIDATED_PROPERTIES, - null, - 'org.gnome.SettingsDaemon.Color', - '/org/gnome/SettingsDaemon/Color', - 'org.gnome.SettingsDaemon.Color', - null - ); - if ( proxy === null ) { - throw new Error('Unable to create proxy to the session bus'); - } + _connect_dbus_proxy(); } catch(e) { logError(e); } proxy_connect_id = proxy.connect('g-properties-changed', _apply_theme_variant); _apply_theme_variant(); + + // Listen to theme change + interface_settings_connect_id = interface_settings.connect('changed::gtk-theme', () => { + original_user_theme = _get_theme(); + _build_theme_variants(); + _apply_theme_variant(); + }); } function disable() { if ( proxy && proxy_connect_id ) { proxy.disconnect(proxy_connect_id); } + interface_settings.disconnect(interface_settings_connect_id); _set_theme(original_user_theme); interface_settings = null; + interface_settings_connect_id = null; nightlight_active = null; original_user_theme = null; user_theme_day = null;