From fb7d361fd8a4cd54f6dc181417f3fd4a25f0e6a7 Mon Sep 17 00:00:00 2001 From: Romain Vigier Date: Wed, 20 Nov 2019 16:38:54 +0100 Subject: [PATCH] Refactor code to be more legible --- src/extension.js | 247 ++++++++++++++++++++++++++++------------------ src/metadata.json | 2 +- 2 files changed, 151 insertions(+), 98 deletions(-) diff --git a/src/extension.js b/src/extension.js index 64a73a1..c6439d2 100644 --- a/src/extension.js +++ b/src/extension.js @@ -20,112 +20,165 @@ this program. If not, see . const Gio = imports.gi.Gio; -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; +const GSETTINGS_SCHEMA = 'org.gnome.desktop.interface'; +const GSETTINGS_PROPERTY = 'gtk-theme'; -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'); +class Extension { + + enable() { + this.theme = new Themer(); + this.variants = {}; + this.variants = this._guess_theme_variants_from(this.theme.current); + this.theme.listen(this._on_theme_change.bind(this)); + + this.nightlight = new Nightlighter(); + this.nightlight.listen(this._apply_theme_variant.bind(this)); + + this._apply_theme_variant(); } - 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'); + + disable() { + if ( this.theme && this.variants ) { + this.theme.current = this.variants.original; + } + if ( this.theme ) { + this.theme.stop_listening(); + } + this.theme = null; + this.variants = null; + + if ( this.nightlight ){ + this.nightlight.stop_listening(); + } + this.nightlight = null; } + + _apply_theme_variant() { + this.theme.current = this.nightlight.status ? this.variants.night : this.variants.day; + } + + _on_theme_change() { + const new_theme = this.theme.current; + if ( new_theme === this.variants.day || new_theme === this.variants.night ) return; + + this.variants = this._guess_theme_variants_from(); + this._apply_theme_variant(); + } + + _guess_theme_variants_from(name) { + const variants = {}; + variants.original = name; + + if ( name.includes('HighContrast') ) { + variants.day = 'HighContrast'; + variants.night = 'HighContrastInverse'; + } + else if ( name.match(/Materia.*-compact/g) ) { + variants.day = name.replace(/-dark(?!er)/g, ''); + variants.night = variants.day.replace(/(-light)?-compact/g, '-dark-compact'); + } + else if ( name.includes('Arc') ) { + variants.day = name.replace(/-Dark(?!er)/g, ''); + variants.night = variants.day.replace('-Darker', '') + '-Dark'; + } + else { + variants.day = name.replace(/-dark(?!er)/g, ''); + variants.night = variants.day.replace(/(-light)?(-darker)?/g, '') + '-dark'; + } + + return variants; + } + } -function _get_theme() { - return interface_settings.get_string('gtk-theme'); + +class Themer { + + constructor() { + try { + this.gsettings = new Gio.Settings({ schema: GSETTINGS_SCHEMA }); + } + catch(e) + { + logError('"User Themes" GNOME Shell extension must be installed') + } + } + + get current() { + return this.gsettings.get_string(GSETTINGS_PROPERTY); + } + + set current(theme) { + if ( theme === this.current ) return; + this.gsettings.set_string(GSETTINGS_PROPERTY, theme); + } + + listen(callback) { + this.connect = this.gsettings.connect(`changed::${GSETTINGS_PROPERTY}`, callback); + } + + stop_listening() { + if ( this.gsettings && this.connect ){ + this.gsettings.disconnect(this.connect); + } + } + } -function _set_theme(theme) { - if ( theme === _get_theme() ) return; - interface_settings.set_string('gtk-theme', theme); + +class Nightlighter { + + constructor() { + try { + this._connect_to_dbus(); + } + catch(e) { + logError(e); + } + } + + get status() { + try { + return this.proxy.get_cached_property('NightLightActive').get_boolean(); + } + catch(e) { + return false; + } + } + + listen(callback) { + this.connect = this.proxy.connect('g-properties-changed', callback); + } + + stop_listening() { + if ( this.proxy && this.connect ) { + this.proxy.disconnect(this.connect); + } + } + + _connect_to_dbus() { + const connection = Gio.bus_get_sync(Gio.BusType.SESSION, null); + if ( connection === null ) { + throw new Error('Unable to connect to the session bus'); + } + this.proxy = Gio.DBusProxy.new_sync( + connection, + Gio.DBusProxyFlags.GET_INVALIDATED_PROPERTIES, + null, + 'org.gnome.SettingsDaemon.Color', + '/org/gnome/SettingsDaemon/Color', + 'org.gnome.SettingsDaemon.Color', + null + ); + if ( this.proxy === null ) { + throw new Error('Unable to create proxy to the session bus'); + } + } + } -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(?!er)/g, ''); - 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(?!er)/g, ''); - user_theme_night = user_theme_day.replace('-Darker', '') + '-Dark'; - } - else { - user_theme_day = original_user_theme.replace(/-dark(?!er)/g, ''); - user_theme_night = user_theme_day.replace(/(-light)?(-darker)?/g, '') + '-dark'; - } -} -function _apply_theme_variant() { - try { - nightlight_active = proxy.get_cached_property('NightLightActive').get_boolean(); - } - catch(e) { - nightlight_active = false; - } - _set_theme(nightlight_active ? user_theme_night : user_theme_day); -} - -function init() {} - -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(); - _build_theme_variants(); - - // Connect to session bus, listen to Color changes and change theme variant - try { - _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, don't change if this is the same theme - interface_settings_connect_id = interface_settings.connect('changed::gtk-theme', () => { - const new_theme = _get_theme(); - if ( new_theme === user_theme_day || new_theme === user_theme_night ) return; - original_user_theme = new_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; - user_theme_night = null; - conn = null; - proxy = null; - proxy_connect_id = null; +function init() { + return new Extension(); } diff --git a/src/metadata.json b/src/metadata.json index d24f824..faac95c 100644 --- a/src/metadata.json +++ b/src/metadata.json @@ -3,6 +3,6 @@ "description": "Change theme to dark variant when Night Light activates", "uuid": "nightthemeswitcher@romainvigier.fr", "url": "https://gitlab.com/rmnvgr/nightthemeswitcher-gnome-shell-extension/", - "version": 5, + "version": 6, "shell-version": ["3.28", "3.30","3.32", "3.34"] }