Check if Night Light is enabled

This commit is contained in:
Romain
2020-02-25 19:43:12 +00:00
parent 4bb05e57f8
commit 93b8f99d90
7 changed files with 111 additions and 28 deletions
+67 -7
View File
@@ -18,6 +18,7 @@ this program. If not, see <http s ://www.gnu.org/licenses/>.
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;
}
}
}
+9 -3
View File
@@ -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() {
+4 -4
View File
@@ -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() {