Code refactoring
This commit is contained in:
+42
-28
@@ -57,14 +57,15 @@ var Nightlighter = class {
|
|||||||
disable() {
|
disable() {
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
get status() {
|
get status() {
|
||||||
if ( !this.proxy ) {
|
if ( !this.dbus_proxy ) {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return this.proxy.get_cached_property('NightLightActive').get_boolean();
|
return this.dbus_proxy.get_cached_property('NightLightActive').get_boolean();
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch(e) {
|
||||||
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.
|
||||||
@@ -76,8 +77,9 @@ var Nightlighter = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
emit() {
|
emit() {
|
||||||
if ( !this.nightlight_change_callback ) return;
|
if ( this.nightlight_change_callback ) {
|
||||||
this.nightlight_change_callback();
|
this.nightlight_change_callback();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_is_nightlight_enabled() {
|
_is_nightlight_enabled() {
|
||||||
@@ -92,8 +94,12 @@ var Nightlighter = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_listen_to_nightlight_status() {
|
_listen_to_nightlight_status() {
|
||||||
if ( this.nightlight_status_connect ) return;
|
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));
|
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() {
|
_stop_listening_to_nightlight_status() {
|
||||||
@@ -109,36 +115,44 @@ var Nightlighter = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_connect_to_dbus() {
|
_connect_to_dbus() {
|
||||||
if ( this.proxy ) return;
|
if ( !this.dbus_proxy ) {
|
||||||
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.');
|
||||||
throw new Error(message);
|
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,
|
_disconnect_from_dbus() {
|
||||||
null,
|
if ( this.dbus_proxy ) {
|
||||||
'org.gnome.SettingsDaemon.Color',
|
this.dbus_proxy == null;
|
||||||
'/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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_listen_to_nightlight_changes() {
|
_listen_to_nightlight_changes() {
|
||||||
if ( this.connect ) return;
|
if ( !this.nightlight_changes_connect ) {
|
||||||
this.connect = this.proxy.connect('g-properties-changed', this.emit.bind(this));
|
this.nightlight_changes_connect = this.dbus_proxy.connect('g-properties-changed', this.emit.bind(this));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_stop_listening_to_nightlight_changes() {
|
_stop_listening_to_nightlight_changes() {
|
||||||
if ( this.proxy && this.connect ) {
|
if ( this.dbus_proxy && this.connect ) {
|
||||||
this.proxy.disconnect(this.connect);
|
this.dbus_proxy.disconnect(this.nightlight_changes_connect);
|
||||||
this.connect == null;
|
this.nightlight_changes_connect == null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,8 +50,9 @@ var Switcher = class {
|
|||||||
enable() {
|
enable() {
|
||||||
try {
|
try {
|
||||||
this.theme = new Themer();
|
this.theme = new Themer();
|
||||||
|
this.theme.enable();
|
||||||
this.variants = Variants.guess_from(this.theme.current);
|
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 = new Nightlighter();
|
||||||
this.nightlight.enable();
|
this.nightlight.enable();
|
||||||
@@ -67,7 +68,7 @@ var Switcher = class {
|
|||||||
disable() {
|
disable() {
|
||||||
try {
|
try {
|
||||||
this.theme.current = this.variants.original;
|
this.theme.current = this.variants.original;
|
||||||
this.theme.stop_listening();
|
this.theme.disable();
|
||||||
this.nightlight.disable();
|
this.nightlight.disable();
|
||||||
}
|
}
|
||||||
catch(e) {} // Since we're disabling, we'll just ignore errors.
|
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 });
|
this.gsettings = new Gio.Settings({ schema: config.THEME_GSETTINGS_SCHEMA });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enable() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
disable() {
|
||||||
|
this._stop_listening_to_theme_changes();
|
||||||
|
}
|
||||||
|
|
||||||
get current() {
|
get current() {
|
||||||
return this.gsettings.get_string(config.THEME_GSETTINGS_PROPERTY);
|
return this.gsettings.get_string(config.THEME_GSETTINGS_PROPERTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
set current(theme) {
|
set current(theme) {
|
||||||
if ( theme === this.current ) return;
|
if ( theme !== this.current ) {
|
||||||
this.gsettings.set_string(config.THEME_GSETTINGS_PROPERTY, theme);
|
this.gsettings.set_string(config.THEME_GSETTINGS_PROPERTY, theme);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
listen(callback) {
|
subscribe(callback) {
|
||||||
this.connect = this.gsettings.connect('changed::' + config.THEME_GSETTINGS_PROPERTY, 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 ){
|
if ( this.gsettings && this.connect ){
|
||||||
this.gsettings.disconnect(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