Move theme logic to Themer, add debug output
This commit is contained in:
@@ -29,6 +29,7 @@ build: build-clean
|
|||||||
gnome-extensions pack \
|
gnome-extensions pack \
|
||||||
--extra-source=../LICENSE \
|
--extra-source=../LICENSE \
|
||||||
--extra-source=./config.js \
|
--extra-source=./config.js \
|
||||||
|
--extra-source=./utils.js \
|
||||||
--extra-source=./modules/ \
|
--extra-source=./modules/ \
|
||||||
--podir=./po/ \
|
--podir=./po/ \
|
||||||
--gettext-domain=$(UUID) \
|
--gettext-domain=$(UUID) \
|
||||||
|
|||||||
@@ -24,3 +24,5 @@ var THEME_GSETTINGS_PROPERTY = 'gtk-theme';
|
|||||||
|
|
||||||
var NIGHTLIGHT_GSETTINGS_SCHEMA = 'org.gnome.settings-daemon.plugins.color';
|
var NIGHTLIGHT_GSETTINGS_SCHEMA = 'org.gnome.settings-daemon.plugins.color';
|
||||||
var NIGHTLIGHT_GSETTINGS_PROPERTY = 'night-light-enabled';
|
var NIGHTLIGHT_GSETTINGS_PROPERTY = 'night-light-enabled';
|
||||||
|
|
||||||
|
var debug = false;
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ const { main } = imports.ui;
|
|||||||
const Me = extensionUtils.getCurrentExtension();
|
const Me = extensionUtils.getCurrentExtension();
|
||||||
const config = Me.imports.config;
|
const config = Me.imports.config;
|
||||||
|
|
||||||
|
const { log_debug } = Me.imports.utils;
|
||||||
|
|
||||||
const Gettext = imports.gettext.domain(config.EXT_UUID);
|
const Gettext = imports.gettext.domain(config.EXT_UUID);
|
||||||
const _ = Gettext.gettext;
|
const _ = Gettext.gettext;
|
||||||
|
|
||||||
@@ -38,16 +40,21 @@ if it is enabled and warns the user if that's not the case.
|
|||||||
var Nightlighter = class {
|
var Nightlighter = class {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
log_debug('Initializing Nightlighter...');
|
||||||
this.nightlight_gsettings = new Gio.Settings({ schema: config.NIGHTLIGHT_GSETTINGS_SCHEMA });
|
this.nightlight_gsettings = new Gio.Settings({ schema: config.NIGHTLIGHT_GSETTINGS_SCHEMA });
|
||||||
|
log_debug('Nightlighter initialized.');
|
||||||
}
|
}
|
||||||
|
|
||||||
enable() {
|
enable() {
|
||||||
|
log_debug('Enabling NightLighter...');
|
||||||
// As Night Light must be enabled for the extension to work, we have to monitor any change of that setting.
|
// 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 {
|
try {
|
||||||
|
this._listen_to_nightlight_status();
|
||||||
this._check_nightlight_status();
|
this._check_nightlight_status();
|
||||||
this._connect_to_dbus();
|
this._connect_to_dbus();
|
||||||
this._listen_to_nightlight_changes();
|
this._listen_to_nightlight_changes();
|
||||||
|
this.emit();
|
||||||
|
log_debug('Nightlighter enabled.');
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch(e) {
|
||||||
main.notifyError(config.EXT_NAME, e.message);
|
main.notifyError(config.EXT_NAME, e.message);
|
||||||
@@ -55,15 +62,15 @@ var Nightlighter = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
disable() {
|
disable() {
|
||||||
|
log_debug('Disabling Nightlighter...');
|
||||||
this._stop_listening_to_nightlight_status();
|
this._stop_listening_to_nightlight_status();
|
||||||
this._stop_listening_to_nightlight_changes();
|
this._stop_listening_to_nightlight_changes();
|
||||||
this._disconnect_from_dbus();
|
this._disconnect_from_dbus();
|
||||||
|
log_debug('Nightlighter disabled.');
|
||||||
}
|
}
|
||||||
|
|
||||||
get status() {
|
get status() {
|
||||||
if ( !this.dbus_proxy ) {
|
if ( this.dbus_proxy ) {
|
||||||
throw new Error();
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return this.dbus_proxy.get_cached_property('NightLightActive').get_boolean();
|
return this.dbus_proxy.get_cached_property('NightLightActive').get_boolean();
|
||||||
}
|
}
|
||||||
@@ -71,6 +78,7 @@ var Nightlighter = class {
|
|||||||
return false; // Sometimes when Night Light hasn't changed colors yet it returns an error, 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.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
subscribe(callback) {
|
subscribe(callback) {
|
||||||
this.nightlight_change_callback = callback;
|
this.nightlight_change_callback = callback;
|
||||||
@@ -99,6 +107,7 @@ var Nightlighter = class {
|
|||||||
'changed::' + config.NIGHTLIGHT_GSETTINGS_PROPERTY,
|
'changed::' + config.NIGHTLIGHT_GSETTINGS_PROPERTY,
|
||||||
this._on_nightlight_status_change.bind(this)
|
this._on_nightlight_status_change.bind(this)
|
||||||
);
|
);
|
||||||
|
log_debug('Listening to Night Light status changes...');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,16 +115,18 @@ var Nightlighter = class {
|
|||||||
if ( this.nightlight_gsettings && this.nightlight_status_connect ) {
|
if ( this.nightlight_gsettings && this.nightlight_status_connect ) {
|
||||||
this.nightlight_gsettings.disconnect(this.nightlight_status_connect);
|
this.nightlight_gsettings.disconnect(this.nightlight_status_connect);
|
||||||
this.nightlight_status_connect = null;
|
this.nightlight_status_connect = null;
|
||||||
|
log_debug('Stopped listening to Night Light status changes.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_on_nightlight_status_change() {
|
_on_nightlight_status_change() {
|
||||||
|
log_debug('Night Light status has changed.');
|
||||||
this.enable();
|
this.enable();
|
||||||
this.emit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_connect_to_dbus() {
|
_connect_to_dbus() {
|
||||||
if ( !this.dbus_proxy ) {
|
if ( !this.dbus_proxy ) {
|
||||||
|
log_debug('Connecting to DBus...');
|
||||||
const connection = Gio.bus_get_sync(Gio.BusType.SESSION, null);
|
const connection = Gio.bus_get_sync(Gio.BusType.SESSION, null);
|
||||||
if ( connection === null ) {
|
if ( connection === null ) {
|
||||||
const message = _('Unable to connect to the session bus.');
|
const message = _('Unable to connect to the session bus.');
|
||||||
@@ -140,6 +151,7 @@ var Nightlighter = class {
|
|||||||
_disconnect_from_dbus() {
|
_disconnect_from_dbus() {
|
||||||
if ( this.dbus_proxy ) {
|
if ( this.dbus_proxy ) {
|
||||||
this.dbus_proxy = null;
|
this.dbus_proxy = null;
|
||||||
|
log_debug('Disconnected from DBus.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,17 +161,20 @@ var Nightlighter = class {
|
|||||||
'g-properties-changed',
|
'g-properties-changed',
|
||||||
this._on_nightlight_change.bind(this)
|
this._on_nightlight_change.bind(this)
|
||||||
);
|
);
|
||||||
|
log_debug('Listening to Night Light changes...');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_stop_listening_to_nightlight_changes() {
|
_stop_listening_to_nightlight_changes() {
|
||||||
if ( this.dbus_proxy && this.connect ) {
|
if ( this.dbus_proxy && this.nightlight_changes_connect ) {
|
||||||
this.dbus_proxy.disconnect(this.nightlight_changes_connect);
|
this.dbus_proxy.disconnect(this.nightlight_changes_connect);
|
||||||
this.nightlight_changes_connect = null;
|
this.nightlight_changes_connect = null;
|
||||||
|
log_debug('Stopped listening to Night Light changes.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_on_nightlight_change() {
|
_on_nightlight_change() {
|
||||||
|
log_debug('Night Light has changed.');
|
||||||
this.emit();
|
this.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+18
-18
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Night Theme Switcher Gnome Shell extension
|
Night Shell Switcher Gnome Shell extension
|
||||||
|
|
||||||
Copyright (C) 2020 Romain Vigier
|
Copyright (C) 2020 Romain Vigier
|
||||||
|
|
||||||
@@ -22,9 +22,10 @@ const { main } = imports.ui;
|
|||||||
const Me = extensionUtils.getCurrentExtension();
|
const Me = extensionUtils.getCurrentExtension();
|
||||||
const config = Me.imports.config;
|
const config = Me.imports.config;
|
||||||
|
|
||||||
|
const { log_debug } = Me.imports.utils;
|
||||||
|
|
||||||
const { Nightlighter } = Me.imports.modules.Nightlighter;
|
const { Nightlighter } = Me.imports.modules.Nightlighter;
|
||||||
const { Themer } = Me.imports.modules.Themer;
|
const { Themer } = Me.imports.modules.Themer;
|
||||||
const { Variants } = Me.imports.modules.Variants;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -44,21 +45,24 @@ explicitely selected, stops listening to changes and cleans itself.
|
|||||||
var Switcher = class {
|
var Switcher = class {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
log_debug('Initializing extension...');
|
||||||
extensionUtils.initTranslations(config.EXT_UUID);
|
extensionUtils.initTranslations(config.EXT_UUID);
|
||||||
|
log_debug('Extension initialized.');
|
||||||
}
|
}
|
||||||
|
|
||||||
enable() {
|
enable() {
|
||||||
|
log_debug('Enabling extension...');
|
||||||
try {
|
try {
|
||||||
this.theme = new Themer();
|
this.theme = new Themer();
|
||||||
this.theme.enable();
|
this.theme.enable();
|
||||||
this.variants = Variants.guess_from(this.theme.current);
|
|
||||||
this.theme.subscribe(this._on_theme_change.bind(this));
|
this.theme.subscribe(this._on_theme_change.bind(this));
|
||||||
|
|
||||||
this.nightlight = new Nightlighter();
|
this.nightlight = new Nightlighter();
|
||||||
this.nightlight.enable();
|
this.nightlight.enable();
|
||||||
this.nightlight.subscribe(this._on_nightlight_change.bind(this));
|
this.nightlight.subscribe(this._on_nightlight_change.bind(this));
|
||||||
|
|
||||||
this._apply_theme_variant();
|
this.theme.set_variant(this.nightlight.status);
|
||||||
|
log_debug('Extension enabled.');
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch(e) {
|
||||||
main.notifyError(config.EXT_NAME, e.message);
|
main.notifyError(config.EXT_NAME, e.message);
|
||||||
@@ -66,38 +70,34 @@ var Switcher = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
disable() {
|
disable() {
|
||||||
|
log_debug('Disabling extension...');
|
||||||
try {
|
try {
|
||||||
this.theme.disable();
|
this.theme.disable();
|
||||||
this.nightlight.disable();
|
this.nightlight.disable();
|
||||||
this.theme.current = this.variants.original;
|
|
||||||
}
|
}
|
||||||
catch(e) {} // Since we're disabling, we'll just ignore errors.
|
catch(e) {} // Since we're disabling, we'll just ignore errors.
|
||||||
finally {
|
finally {
|
||||||
this.theme = null;
|
this.theme = null;
|
||||||
this.variants = null;
|
|
||||||
this.nightlight = null;
|
this.nightlight = null;
|
||||||
}
|
}
|
||||||
|
log_debug('Extension disabled.');
|
||||||
}
|
}
|
||||||
|
|
||||||
_apply_theme_variant() {
|
async _await_extensionManager_init() {
|
||||||
try {
|
log_debug('Waiting for the Extension Manager to be initialized...');
|
||||||
this.theme.current = this.nightlight.status ? this.variants.night : this.variants.day;
|
while ( true ) {
|
||||||
}
|
if ( main.extensionManager._initialized ) return;
|
||||||
catch(e) {
|
await null;
|
||||||
this.theme.current = this.variants.original;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_on_theme_change() {
|
_on_theme_change() {
|
||||||
const new_theme = this.theme.current;
|
this.theme.update_variants();
|
||||||
if ( new_theme !== this.variants.day && new_theme !== this.variants.night ) {
|
this.theme.set_variant(this.nightlight.status);
|
||||||
this.variants = Variants.guess_from(new_theme);
|
|
||||||
}
|
|
||||||
this._apply_theme_variant();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_on_nightlight_change() {
|
_on_nightlight_change() {
|
||||||
this._apply_theme_variant();
|
this.theme.set_variant(this.nightlight.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-5
@@ -22,6 +22,10 @@ const { Gio } = imports.gi;
|
|||||||
const Me = extensionUtils.getCurrentExtension();
|
const Me = extensionUtils.getCurrentExtension();
|
||||||
const config = Me.imports.config;
|
const config = Me.imports.config;
|
||||||
|
|
||||||
|
const { log_debug } = Me.imports.utils;
|
||||||
|
|
||||||
|
const { Variants } = Me.imports.modules.Variants;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The Themer communicates with the system to get the current theme or set a new
|
The Themer communicates with the system to get the current theme or set a new
|
||||||
@@ -31,15 +35,24 @@ one. It can also be asked to listen to theme changes.
|
|||||||
var Themer = class {
|
var Themer = class {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
log_debug('Initializing Themer...');
|
||||||
this.gsettings = new Gio.Settings({ schema: config.THEME_GSETTINGS_SCHEMA });
|
this.gsettings = new Gio.Settings({ schema: config.THEME_GSETTINGS_SCHEMA });
|
||||||
|
log_debug('Themer initialized.');
|
||||||
}
|
}
|
||||||
|
|
||||||
enable() {
|
enable() {
|
||||||
|
log_debug('Enabling Themer...');
|
||||||
this._listen_to_theme_changes();
|
this._listen_to_theme_changes();
|
||||||
|
this._update_variants();
|
||||||
|
this.emit();
|
||||||
|
log_debug('Themer enabled.');
|
||||||
}
|
}
|
||||||
|
|
||||||
disable() {
|
disable() {
|
||||||
|
log_debug('Disabling Themer...');
|
||||||
this._stop_listening_to_theme_changes();
|
this._stop_listening_to_theme_changes();
|
||||||
|
this.reset_theme();
|
||||||
|
log_debug('Themer disabled.');
|
||||||
}
|
}
|
||||||
|
|
||||||
get current() {
|
get current() {
|
||||||
@@ -49,6 +62,7 @@ var Themer = class {
|
|||||||
set current(theme) {
|
set current(theme) {
|
||||||
if ( theme !== this.current ) {
|
if ( theme !== this.current ) {
|
||||||
this.gsettings.set_string(config.THEME_GSETTINGS_PROPERTY, theme);
|
this.gsettings.set_string(config.THEME_GSETTINGS_PROPERTY, theme);
|
||||||
|
log_debug(`Theme has been set to "${theme}"`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,20 +76,55 @@ var Themer = class {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set_variant(night) {
|
||||||
|
if ( night !== undefined && this.variants ) {
|
||||||
|
this.current = night ? this.variants.night : this.variants.day;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reset_theme() {
|
||||||
|
if ( this.variants ) {
|
||||||
|
this.current = this.variants.original;
|
||||||
|
log_debug('Theme has been reset to the user\'s original variant.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
update_variants() {
|
||||||
|
if ( this.variants ) {
|
||||||
|
const new_theme = this.current;
|
||||||
|
if ( new_theme && new_theme !== this.variants.day && new_theme !== this.variants.night ) {
|
||||||
|
this._update_variants();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_update_variants() {
|
||||||
|
if ( this.current ) {
|
||||||
|
this.variants = Variants.guess_from(this.current);
|
||||||
|
log_debug('Variants updated: ' + JSON.stringify(this.variants));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_listen_to_theme_changes() {
|
_listen_to_theme_changes() {
|
||||||
if ( !this.connect ) {
|
if ( !this.theme_change_connect ) {
|
||||||
this.connect = this.gsettings.connect('changed::' + config.THEME_GSETTINGS_PROPERTY, this._on_theme_change.bind(this));
|
this.theme_change_connect = this.gsettings.connect(
|
||||||
|
'changed::' + config.THEME_GSETTINGS_PROPERTY,
|
||||||
|
this._on_theme_change.bind(this)
|
||||||
|
);
|
||||||
|
log_debug('Listening for theme changes...');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_stop_listening_to_theme_changes() {
|
_stop_listening_to_theme_changes() {
|
||||||
if ( this.gsettings && this.connect ){
|
if ( this.gsettings && this.theme_change_connect ){
|
||||||
this.gsettings.disconnect(this.connect);
|
this.gsettings.disconnect(this.theme_change_connect);
|
||||||
this.connect = null;
|
this.theme_change_connect = null;
|
||||||
|
log_debug('Stopped listening for theme changes.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_on_theme_change() {
|
_on_theme_change() {
|
||||||
|
log_debug('Theme has changed.');
|
||||||
this.emit();
|
this.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
Night Theme Switcher Gnome Shell extension
|
||||||
|
|
||||||
|
Copyright (C) 2020 Romain Vigier
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify it under
|
||||||
|
the terms of the GNU General Public License as published by the Free Software
|
||||||
|
Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with
|
||||||
|
this program. If not, see <http s ://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { extensionUtils } = imports.misc;
|
||||||
|
|
||||||
|
const Me = extensionUtils.getCurrentExtension();
|
||||||
|
const config = Me.imports.config;
|
||||||
|
|
||||||
|
var log_debug = function(message) {
|
||||||
|
if ( config.debug ) {
|
||||||
|
log(`[DEBUG] ${config.EXT_NAME}: ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user