diff --git a/Makefile b/Makefile index 4e1aa28..2489034 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,7 @@ pot: --package-name="$(NAME)" \ --package-version="$(VERSION)" \ --output=./src/po/$(UUID).pot \ - ./src/*.js + ./src/**/*.js sed -i '1,4s/SOME DESCRIPTIVE TITLE./$(NAME)/g' ./src/po/$(UUID).pot sed -i '1,4s/YEAR/$(COPYRIGHT_YEAR)/' ./src/po/$(UUID).pot sed -i "1,4s/THE PACKAGE'S COPYRIGHT HOLDER/$(AUTHOR_NAME)/" ./src/po/$(UUID).pot diff --git a/src/config.js b/src/config.js index 3355ede..f860925 100644 --- a/src/config.js +++ b/src/config.js @@ -19,5 +19,8 @@ this program. If not, see . var EXT_NAME = 'Night Theme Switcher'; var EXT_UUID = 'nightthemeswitcher@romainvigier.fr'; -var GSETTINGS_SCHEMA = 'org.gnome.desktop.interface'; -var GSETTINGS_PROPERTY = 'gtk-theme'; +var THEME_GSETTINGS_SCHEMA = 'org.gnome.desktop.interface'; +var THEME_GSETTINGS_PROPERTY = 'gtk-theme'; + +var NIGHTLIGHT_GSETTINGS_SCHEMA = 'org.gnome.settings-daemon.plugins.color'; +var NIGHTLIGHT_GSETTINGS_PROPERTY = 'night-light-enabled'; diff --git a/src/modules/Nightlighter.js b/src/modules/Nightlighter.js index 63f1c6a..d4e8138 100644 --- a/src/modules/Nightlighter.js +++ b/src/modules/Nightlighter.js @@ -18,6 +18,7 @@ this program. If not, see . const { extensionUtils } = imports.misc; const { Gio } = imports.gi; +const { main } = imports.ui; const Me = extensionUtils.getCurrentExtension(); const config = Me.imports.config; @@ -29,39 +30,86 @@ const _ = Gettext.gettext; /* The Nightlighter establishes a connection with the session bus to get the current Night Light status. It can also be asked to listen to status changes. + +As Night Light is essential for the extension to work, it continuously checks +if it is enabled and warns the user if that's not the case. */ var Nightlighter = class { constructor() { + this.nightlight_gsettings = new Gio.Settings({ schema: config.NIGHTLIGHT_GSETTINGS_SCHEMA }); + } + + enable() { + // As Night Light must be enabled for the extension to work, we have to monitor any change of that setting. + this._listen_to_nightlight_status(); try { + this._check_nightlight_status(); this._connect_to_dbus(); + this._listen_to_nightlight_changes(); } catch(e) { - throw e; // Let's pass the error up. + main.notifyError(config.EXT_NAME, e.message); } } + disable() { + this._stop_listening_to_nightlight_status(); + this._stop_listening_to_nightlight_changes(); + } + get status() { + if ( !this.proxy ) { + throw new Error(); + } try { return this.proxy.get_cached_property('NightLightActive').get_boolean(); } catch(e) { - return false; // If Night Light is disabled, we consider it is inactive. + return false; // Sometimes when Night Light hasn't changed colors yet it returns an error, we consider it is inactive. } } - listen(callback) { - this.connect = this.proxy.connect('g-properties-changed', callback); + subscribe(callback) { + this.nightlight_change_callback = callback; } - stop_listening() { - if ( this.proxy && this.connect ) { - this.proxy.disconnect(this.connect); + emit() { + if ( !this.nightlight_change_callback ) return; + this.nightlight_change_callback(); + } + + _is_nightlight_enabled() { + return this.nightlight_gsettings.get_boolean(config.NIGHTLIGHT_GSETTINGS_PROPERTY); + } + + _check_nightlight_status() { + if ( !this._is_nightlight_enabled() ) { + const message = _('Night Light must be enabled to use this extension. Please enable it in your system settings.'); + throw new Error(message); } } + _listen_to_nightlight_status() { + if ( this.nightlight_status_connect ) return; + this.nightlight_status_connect = this.nightlight_gsettings.connect('changed::' + config.NIGHTLIGHT_GSETTINGS_PROPERTY, this._on_nightlight_status_change.bind(this)); + } + + _stop_listening_to_nightlight_status() { + if ( this.nightlight_gsettings && this.nightlight_status_connect ) { + this.nightlight_gsettings.disconnect(this.nightlight_status_connect); + this.nightlight_status_connect = null; + } + } + + _on_nightlight_status_change() { + this.enable(); + this.emit(); + } + _connect_to_dbus() { + if ( this.proxy ) return; const connection = Gio.bus_get_sync(Gio.BusType.SESSION, null); if ( connection === null ) { const message = _('Unable to connect to the session bus.'); @@ -82,4 +130,16 @@ var Nightlighter = class { } } + _listen_to_nightlight_changes() { + if ( this.connect ) return; + this.connect = this.proxy.connect('g-properties-changed', this.emit.bind(this)); + } + + _stop_listening_to_nightlight_changes() { + if ( this.proxy && this.connect ) { + this.proxy.disconnect(this.connect); + this.connect == null; + } + } + } diff --git a/src/modules/Switcher.js b/src/modules/Switcher.js index adc972f..6d09fe2 100644 --- a/src/modules/Switcher.js +++ b/src/modules/Switcher.js @@ -54,7 +54,8 @@ var Switcher = class { this.theme.listen(this._on_theme_change.bind(this)); this.nightlight = new Nightlighter(); - this.nightlight.listen(this._apply_theme_variant.bind(this)); + this.nightlight.enable(); + this.nightlight.subscribe(this._apply_theme_variant.bind(this)); this._apply_theme_variant(); } @@ -67,7 +68,7 @@ var Switcher = class { try { this.theme.current = this.variants.original; this.theme.stop_listening(); - this.nightlight.stop_listening(); + this.nightlight.disable(); } catch(e) {} // Since we're disabling, we'll just ignore errors. finally { @@ -78,7 +79,12 @@ var Switcher = class { } _apply_theme_variant() { - this.theme.current = this.nightlight.status ? this.variants.night : this.variants.day; + try { + this.theme.current = this.nightlight.status ? this.variants.night : this.variants.day; + } + catch(e) { + this.theme.current = this.variants.original; + } } _on_theme_change() { diff --git a/src/modules/Themer.js b/src/modules/Themer.js index afa9661..134dbb0 100644 --- a/src/modules/Themer.js +++ b/src/modules/Themer.js @@ -31,20 +31,20 @@ one. It can also be asked to listen to theme changes. var Themer = class { constructor() { - this.gsettings = new Gio.Settings({ schema: config.GSETTINGS_SCHEMA }); + this.gsettings = new Gio.Settings({ schema: config.THEME_GSETTINGS_SCHEMA }); } get current() { - return this.gsettings.get_string(config.GSETTINGS_PROPERTY); + return this.gsettings.get_string(config.THEME_GSETTINGS_PROPERTY); } set current(theme) { if ( theme === this.current ) return; - this.gsettings.set_string(config.GSETTINGS_PROPERTY, theme); + this.gsettings.set_string(config.THEME_GSETTINGS_PROPERTY, theme); } listen(callback) { - this.connect = this.gsettings.connect('changed::' + config.GSETTINGS_PROPERTY, callback); + this.connect = this.gsettings.connect('changed::' + config.THEME_GSETTINGS_PROPERTY, callback); } stop_listening() { diff --git a/src/po/fr.po b/src/po/fr.po index da41ec8..5918638 100644 --- a/src/po/fr.po +++ b/src/po/fr.po @@ -1,14 +1,14 @@ # Night Theme Switcher # Copyright (C) 2019 Romain Vigier # This file is distributed under the same license as the Night Theme Switcher package. -# Romain Vigier <>, 2019. +# Romain Vigier <>, 2019-2020. # msgid "" msgstr "" "Project-Id-Version: Night Theme Switcher 7\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-02-08 23:18+0100\n" -"PO-Revision-Date: 2019-11-25 00:28+0100\n" +"POT-Creation-Date: 2020-02-25 20:28+0100\n" +"PO-Revision-Date: 2020-02-25 20:31+0100\n" "Last-Translator: Romain Vigier <>\n" "Language-Team: French \n" "Language: fr\n" @@ -18,11 +18,19 @@ msgstr "" "Plural-Forms: nplurals=2; plural=(n > 1)\n" "X-Generator: Gtranslator 3.34.0\n" -#: src/extension.js:150 +#: src/modules/Nightlighter.js:86 +msgid "" +"Night Light must be enabled to use this extension. Please enable it in your " +"system settings." +msgstr "" +"Le mode nuit doit être activé pour utiliser l’extension. Veuillez l’activer " +"dans les réglages du système." + +#: src/modules/Nightlighter.js:112 msgid "Unable to connect to the session bus." msgstr "Impossible de se connecter au bus de la session." -#: src/extension.js:163 +#: src/modules/Nightlighter.js:125 msgid "Unable to create proxy to the session bus." msgstr "Impossible de créer une passerelle vers le bus de la session." diff --git a/src/po/nightthemeswitcher@romainvigier.fr.pot b/src/po/nightthemeswitcher@romainvigier.fr.pot index b3643f9..1b37fc4 100644 --- a/src/po/nightthemeswitcher@romainvigier.fr.pot +++ b/src/po/nightthemeswitcher@romainvigier.fr.pot @@ -1,14 +1,14 @@ # Night Theme Switcher -# Copyright (C) 2019 Romain Vigier +# Copyright (C) 2019, 2020 Romain Vigier # This file is distributed under the same license as the Night Theme Switcher package. -# Romain Vigier <>, 2019. +# Romain Vigier <>, 2019, 2020. # #, fuzzy msgid "" msgstr "" -"Project-Id-Version: Night Theme Switcher 12\n" +"Project-Id-Version: Night Theme Switcher 17\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-02-08 23:18+0100\n" +"POT-Creation-Date: 2020-02-25 20:28+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,10 +17,16 @@ msgstr "" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" -#: src/extension.js:150 +#: src/modules/Nightlighter.js:86 +msgid "" +"Night Light must be enabled to use this extension. Please enable it in your " +"system settings." +msgstr "" + +#: src/modules/Nightlighter.js:112 msgid "Unable to connect to the session bus." msgstr "" -#: src/extension.js:163 +#: src/modules/Nightlighter.js:125 msgid "Unable to create proxy to the session bus." msgstr ""