Split modules into different files
This commit is contained in:
+1
-145
@@ -18,154 +18,10 @@ this program. If not, see <http s ://www.gnu.org/licenses/>.
|
||||
|
||||
'use strict';
|
||||
|
||||
const EXT_NAME = 'Night Theme Switcher';
|
||||
const EXT_UUID = 'nightthemeswitcher@romainvigier.fr';
|
||||
|
||||
const GSETTINGS_SCHEMA = 'org.gnome.desktop.interface';
|
||||
const GSETTINGS_PROPERTY = 'gtk-theme';
|
||||
|
||||
const { Gio } = imports.gi;
|
||||
const { extensionUtils } = imports.misc;
|
||||
const { main } = imports.ui;
|
||||
|
||||
const Me = extensionUtils.getCurrentExtension();
|
||||
const { Variants } = Me.imports.variants;
|
||||
|
||||
const Gettext = imports.gettext.domain(EXT_UUID);
|
||||
const _ = Gettext.gettext;
|
||||
|
||||
|
||||
class Switcher {
|
||||
|
||||
constructor() {
|
||||
extensionUtils.initTranslations(EXT_UUID);
|
||||
}
|
||||
|
||||
enable() {
|
||||
try {
|
||||
this.theme = new Themer();
|
||||
this.variants = Variants.guess_from(this.theme.current);
|
||||
this.theme.listen(this._on_theme_change.bind(this));
|
||||
|
||||
this.nightlight = new Nightlighter();
|
||||
this.nightlight.listen(this._apply_theme_variant.bind(this));
|
||||
|
||||
this._apply_theme_variant();
|
||||
}
|
||||
catch(e) {
|
||||
main.notifyError(EXT_NAME, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
disable() {
|
||||
try {
|
||||
this.theme.current = this.variants.original;
|
||||
this.theme.stop_listening();
|
||||
this.nightlight.stop_listening();
|
||||
}
|
||||
catch(e) {}
|
||||
finally {
|
||||
this.theme = null;
|
||||
this.variants = null;
|
||||
this.nightlight = null;
|
||||
}
|
||||
}
|
||||
|
||||
_apply_theme_variant() {
|
||||
this.theme.current = this.nightlight.status ? this.variants.night : this.variants.day;
|
||||
}
|
||||
|
||||
_on_theme_change() {
|
||||
const new_theme = this.theme.current;
|
||||
if ( new_theme === this.variants.day || new_theme === this.variants.night ) return;
|
||||
|
||||
this.variants = Variants.guess_from(new_theme);
|
||||
this._apply_theme_variant();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Themer {
|
||||
|
||||
constructor() {
|
||||
this.gsettings = new Gio.Settings({ schema: GSETTINGS_SCHEMA });
|
||||
}
|
||||
|
||||
get current() {
|
||||
return this.gsettings.get_string(GSETTINGS_PROPERTY);
|
||||
}
|
||||
|
||||
set current(theme) {
|
||||
if ( theme === this.current ) return;
|
||||
this.gsettings.set_string(GSETTINGS_PROPERTY, theme);
|
||||
}
|
||||
|
||||
listen(callback) {
|
||||
this.connect = this.gsettings.connect('changed::' + GSETTINGS_PROPERTY, callback);
|
||||
}
|
||||
|
||||
stop_listening() {
|
||||
if ( this.gsettings && this.connect ){
|
||||
this.gsettings.disconnect(this.connect);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Nightlighter {
|
||||
|
||||
constructor() {
|
||||
try {
|
||||
this._connect_to_dbus();
|
||||
}
|
||||
catch(e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
get status() {
|
||||
try {
|
||||
return this.proxy.get_cached_property('NightLightActive').get_boolean();
|
||||
}
|
||||
catch(e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
listen(callback) {
|
||||
this.connect = this.proxy.connect('g-properties-changed', callback);
|
||||
}
|
||||
|
||||
stop_listening() {
|
||||
if ( this.proxy && this.connect ) {
|
||||
this.proxy.disconnect(this.connect);
|
||||
}
|
||||
}
|
||||
|
||||
_connect_to_dbus() {
|
||||
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.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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
const { Switcher } = Me.imports.modules.Switcher;
|
||||
|
||||
|
||||
function init() {
|
||||
|
||||
Reference in New Issue
Block a user