From 337609a0aeb50fa8f0c74f541e7b783a4b27f49b Mon Sep 17 00:00:00 2001 From: Romain Date: Fri, 14 Feb 2020 10:14:39 +0000 Subject: [PATCH] Split modules into different files --- Makefile | 5 +- src/config.js | 23 ++++ src/extension.js | 146 +---------------------- src/modules/Nightlighter.js | 80 +++++++++++++ src/modules/Switcher.js | 78 ++++++++++++ src/modules/Themer.js | 51 ++++++++ src/{variants.js => modules/Variants.js} | 0 7 files changed, 236 insertions(+), 147 deletions(-) create mode 100644 src/config.js create mode 100644 src/modules/Nightlighter.js create mode 100644 src/modules/Switcher.js create mode 100644 src/modules/Themer.js rename src/{variants.js => modules/Variants.js} (100%) diff --git a/Makefile b/Makefile index b56b3d4..4e1aa28 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,8 @@ build: build-clean mkdir -p ./build gnome-extensions pack \ --extra-source=../LICENSE \ - --extra-source=./variants.js \ + --extra-source=./config.js \ + --extra-source=./modules/ \ --podir=./po/ \ --gettext-domain=$(UUID) \ --out-dir=./build \ @@ -51,7 +52,7 @@ clean: build-clean deps-clean .PHONY: test test: - cat ./src/variants.js ./tests/_variants.js.template > ./tests/_variants.js + cat ./src/modules/Variants.js ./tests/_variants.js.template > ./tests/_variants.js npm run test .PHONY: deps-install diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..3355ede --- /dev/null +++ b/src/config.js @@ -0,0 +1,23 @@ +/* +Night Theme Switcher Gnome Shell extension + +Copyright (C) 2020 Romain Vigier + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +var EXT_NAME = 'Night Theme Switcher'; +var EXT_UUID = 'nightthemeswitcher@romainvigier.fr'; + +var GSETTINGS_SCHEMA = 'org.gnome.desktop.interface'; +var GSETTINGS_PROPERTY = 'gtk-theme'; diff --git a/src/extension.js b/src/extension.js index 2e5f73a..dd2c5de 100644 --- a/src/extension.js +++ b/src/extension.js @@ -18,154 +18,10 @@ this program. If not, see . '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() { diff --git a/src/modules/Nightlighter.js b/src/modules/Nightlighter.js new file mode 100644 index 0000000..20d0816 --- /dev/null +++ b/src/modules/Nightlighter.js @@ -0,0 +1,80 @@ +/* +Night Theme Switcher Gnome Shell extension + +Copyright (C) 2020 Romain Vigier + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +const { extensionUtils } = imports.misc; +const { Gio } = imports.gi; + +const Me = extensionUtils.getCurrentExtension(); +const config = Me.imports.config; + +const Gettext = imports.gettext.domain(config.EXT_UUID); +const _ = Gettext.gettext; + + +var Nightlighter = class { + + 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); + } + } + +} diff --git a/src/modules/Switcher.js b/src/modules/Switcher.js new file mode 100644 index 0000000..7a7c258 --- /dev/null +++ b/src/modules/Switcher.js @@ -0,0 +1,78 @@ +/* +Night Theme Switcher Gnome Shell extension + +Copyright (C) 2020 Romain Vigier + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +const { extensionUtils } = imports.misc; +const { main } = imports.ui; + +const Me = extensionUtils.getCurrentExtension(); +const config = Me.imports.config; + +const { Nightlighter } = Me.imports.modules.Nightlighter; +const { Themer } = Me.imports.modules.Themer; +const { Variants } = Me.imports.modules.Variants; + + +var Switcher = class { + + constructor() { + extensionUtils.initTranslations(config.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(config.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(); + } + +} diff --git a/src/modules/Themer.js b/src/modules/Themer.js new file mode 100644 index 0000000..ac99db6 --- /dev/null +++ b/src/modules/Themer.js @@ -0,0 +1,51 @@ +/* +Night Theme Switcher Gnome Shell extension + +Copyright (C) 2020 Romain Vigier + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +const { extensionUtils } = imports.misc; +const { Gio } = imports.gi; + +const Me = extensionUtils.getCurrentExtension(); +const config = Me.imports.config; + + +var Themer = class { + + constructor() { + this.gsettings = new Gio.Settings({ schema: config.GSETTINGS_SCHEMA }); + } + + get current() { + return this.gsettings.get_string(config.GSETTINGS_PROPERTY); + } + + set current(theme) { + if ( theme === this.current ) return; + this.gsettings.set_string(config.GSETTINGS_PROPERTY, theme); + } + + listen(callback) { + this.connect = this.gsettings.connect('changed::' + config.GSETTINGS_PROPERTY, callback); + } + + stop_listening() { + if ( this.gsettings && this.connect ){ + this.gsettings.disconnect(this.connect); + } + } + +} diff --git a/src/variants.js b/src/modules/Variants.js similarity index 100% rename from src/variants.js rename to src/modules/Variants.js