Merge branch 'refactor' into 'master'
Code refactoring See merge request rmnvgr/nightthemeswitcher-gnome-shell-extension!6
This commit is contained in:
+42
-28
@@ -57,14 +57,15 @@ var Nightlighter = class {
|
||||
disable() {
|
||||
this._stop_listening_to_nightlight_status();
|
||||
this._stop_listening_to_nightlight_changes();
|
||||
this._disconnect_from_dbus();
|
||||
}
|
||||
|
||||
get status() {
|
||||
if ( !this.proxy ) {
|
||||
if ( !this.dbus_proxy ) {
|
||||
throw new Error();
|
||||
}
|
||||
try {
|
||||
return this.proxy.get_cached_property('NightLightActive').get_boolean();
|
||||
return this.dbus_proxy.get_cached_property('NightLightActive').get_boolean();
|
||||
}
|
||||
catch(e) {
|
||||
return false; // Sometimes when Night Light hasn't changed colors yet it returns an error, we consider it is inactive.
|
||||
@@ -76,8 +77,9 @@ var Nightlighter = class {
|
||||
}
|
||||
|
||||
emit() {
|
||||
if ( !this.nightlight_change_callback ) return;
|
||||
this.nightlight_change_callback();
|
||||
if ( this.nightlight_change_callback ) {
|
||||
this.nightlight_change_callback();
|
||||
}
|
||||
}
|
||||
|
||||
_is_nightlight_enabled() {
|
||||
@@ -92,8 +94,12 @@ var Nightlighter = class {
|
||||
}
|
||||
|
||||
_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));
|
||||
if ( !this.nightlight_status_connect ) {
|
||||
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() {
|
||||
@@ -109,36 +115,44 @@ var Nightlighter = class {
|
||||
}
|
||||
|
||||
_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.');
|
||||
throw new Error(message);
|
||||
if ( !this.dbus_proxy ) {
|
||||
const connection = Gio.bus_get_sync(Gio.BusType.SESSION, null);
|
||||
if ( connection === null ) {
|
||||
const message = _('Unable to connect to the session bus.');
|
||||
throw new Error(message);
|
||||
}
|
||||
this.dbus_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.dbus_proxy === null ) {
|
||||
const message = _('Unable to create proxy to the session bus.');
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
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 ) {
|
||||
const message = _('Unable to create proxy to the session bus.');
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
_disconnect_from_dbus() {
|
||||
if ( this.dbus_proxy ) {
|
||||
this.dbus_proxy == null;
|
||||
}
|
||||
}
|
||||
|
||||
_listen_to_nightlight_changes() {
|
||||
if ( this.connect ) return;
|
||||
this.connect = this.proxy.connect('g-properties-changed', this.emit.bind(this));
|
||||
if ( !this.nightlight_changes_connect ) {
|
||||
this.nightlight_changes_connect = this.dbus_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;
|
||||
if ( this.dbus_proxy && this.connect ) {
|
||||
this.dbus_proxy.disconnect(this.nightlight_changes_connect);
|
||||
this.nightlight_changes_connect == null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,8 +50,9 @@ var Switcher = class {
|
||||
enable() {
|
||||
try {
|
||||
this.theme = new Themer();
|
||||
this.theme.enable();
|
||||
this.variants = Variants.guess_from(this.theme.current);
|
||||
this.theme.listen(this._on_theme_change.bind(this));
|
||||
this.theme.subscribe(this._on_theme_change.bind(this));
|
||||
|
||||
this.nightlight = new Nightlighter();
|
||||
this.nightlight.enable();
|
||||
@@ -67,7 +68,7 @@ var Switcher = class {
|
||||
disable() {
|
||||
try {
|
||||
this.theme.current = this.variants.original;
|
||||
this.theme.stop_listening();
|
||||
this.theme.disable();
|
||||
this.nightlight.disable();
|
||||
}
|
||||
catch(e) {} // Since we're disabling, we'll just ignore errors.
|
||||
|
||||
+27
-5
@@ -34,22 +34,44 @@ var Themer = class {
|
||||
this.gsettings = new Gio.Settings({ schema: config.THEME_GSETTINGS_SCHEMA });
|
||||
}
|
||||
|
||||
enable() {
|
||||
return;
|
||||
}
|
||||
|
||||
disable() {
|
||||
this._stop_listening_to_theme_changes();
|
||||
}
|
||||
|
||||
get current() {
|
||||
return this.gsettings.get_string(config.THEME_GSETTINGS_PROPERTY);
|
||||
}
|
||||
|
||||
set current(theme) {
|
||||
if ( theme === this.current ) return;
|
||||
this.gsettings.set_string(config.THEME_GSETTINGS_PROPERTY, theme);
|
||||
if ( theme !== this.current ) {
|
||||
this.gsettings.set_string(config.THEME_GSETTINGS_PROPERTY, theme);
|
||||
}
|
||||
}
|
||||
|
||||
listen(callback) {
|
||||
this.connect = this.gsettings.connect('changed::' + config.THEME_GSETTINGS_PROPERTY, callback);
|
||||
subscribe(callback) {
|
||||
this.theme_change_callback = callback;
|
||||
}
|
||||
|
||||
stop_listening() {
|
||||
_listen_to_theme_changes() {
|
||||
if ( !this.connect ) {
|
||||
this.connect = this.gsettings.connect('changed::' + config.THEME_GSETTINGS_PROPERTY, this._on_theme_change);
|
||||
}
|
||||
}
|
||||
|
||||
_stop_listening_to_theme_changes() {
|
||||
if ( this.gsettings && this.connect ){
|
||||
this.gsettings.disconnect(this.connect);
|
||||
this.connect = null;
|
||||
}
|
||||
}
|
||||
|
||||
_on_theme_change() {
|
||||
if ( this.theme_change_callback ) {
|
||||
this.theme_change_callback();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user