Merge branch 'refactor/nightlighter-dbus' into 'master'
Refactor DBus calls, only emit on NightLightActive change (Fix #7) Closes #7 See merge request rmnvgr/nightthemeswitcher-gnome-shell-extension!12
This commit is contained in:
+21
-24
@@ -16,7 +16,7 @@ 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/>.
|
this program. If not, see <http s ://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const { extensionUtils } = imports.misc;
|
const { extensionUtils, fileUtils } = imports.misc;
|
||||||
const { Gio } = imports.gi;
|
const { Gio } = imports.gi;
|
||||||
const { main } = imports.ui;
|
const { main } = imports.ui;
|
||||||
|
|
||||||
@@ -28,6 +28,8 @@ 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;
|
||||||
|
|
||||||
|
const State = new Map();
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The Nightlighter establishes a connection with the session bus to get the
|
The Nightlighter establishes a connection with the session bus to get the
|
||||||
@@ -55,6 +57,7 @@ var Nightlighter = class {
|
|||||||
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();
|
||||||
|
State.set('nightlight_active', this._get_nightlight_active())
|
||||||
this.emit();
|
this.emit();
|
||||||
log_debug('Nightlighter enabled.');
|
log_debug('Nightlighter enabled.');
|
||||||
}
|
}
|
||||||
@@ -74,14 +77,7 @@ var Nightlighter = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get time() {
|
get time() {
|
||||||
if ( this.dbus_proxy ) {
|
return State.get('nightlight_active') ? 'night' : 'day';
|
||||||
try {
|
|
||||||
return this.dbus_proxy.get_cached_property('NightLightActive').get_boolean() ? 'night' : 'day';
|
|
||||||
}
|
|
||||||
catch(e) {
|
|
||||||
return 'day'; // Sometimes when Night Light hasn't changed colors yet it returns an error, we consider it is inactive.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(callback) {
|
subscribe(callback) {
|
||||||
@@ -137,21 +133,14 @@ var Nightlighter = class {
|
|||||||
_connect_to_dbus() {
|
_connect_to_dbus() {
|
||||||
if ( !this.dbus_proxy ) {
|
if ( !this.dbus_proxy ) {
|
||||||
log_debug('Connecting to DBus...');
|
log_debug('Connecting to DBus...');
|
||||||
const connection = Gio.bus_get_sync(Gio.BusType.SESSION, null);
|
const color_interface = fileUtils.loadInterfaceXML('org.gnome.SettingsDaemon.Color');
|
||||||
if ( connection === null ) {
|
const ColorProxy = Gio.DBusProxy.makeProxyWrapper(color_interface);
|
||||||
const message = _('Unable to connect to the session bus.');
|
this.dbus_proxy = new ColorProxy(
|
||||||
throw new Error(message);
|
Gio.DBus.session,
|
||||||
}
|
|
||||||
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',
|
'/org/gnome/SettingsDaemon/Color'
|
||||||
'org.gnome.SettingsDaemon.Color',
|
|
||||||
null
|
|
||||||
);
|
);
|
||||||
if ( this.dbus_proxy === null ) {
|
if ( !this.dbus_proxy ) {
|
||||||
const message = _('Unable to create proxy to the session bus.');
|
const message = _('Unable to create proxy to the session bus.');
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
@@ -184,8 +173,16 @@ var Nightlighter = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_on_nightlight_change() {
|
_on_nightlight_change() {
|
||||||
log_debug('Night Light has changed.');
|
const nightlight_active = this._get_nightlight_active();
|
||||||
this.emit();
|
if ( State.get('nightlight_active') !== nightlight_active ) {
|
||||||
|
log_debug('Night Light has become ' + (nightlight_active ? '' : 'in') + 'active.');
|
||||||
|
State.set('nightlight_active', nightlight_active);
|
||||||
|
this.emit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_get_nightlight_active() {
|
||||||
|
return this.dbus_proxy.NightLightActive
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user