Merge branch 'refactor' into 'master'
Split modules into different files See merge request rmnvgr/nightthemeswitcher-gnome-shell-extension!4
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 <http s ://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
var EXT_NAME = 'Night Theme Switcher';
|
||||
var EXT_UUID = 'nightthemeswitcher@romainvigier.fr';
|
||||
|
||||
var GSETTINGS_SCHEMA = 'org.gnome.desktop.interface';
|
||||
var GSETTINGS_PROPERTY = 'gtk-theme';
|
||||
+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() {
|
||||
|
||||
@@ -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 <http s ://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <http s ://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <http s ://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user