Split modules into different files
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
Night Theme Switcher Gnome Shell extension
|
||||
|
||||
Copyright (C) 2019, 2020 Romain Vigier
|
||||
Copyright (C) 2020 Matti Hyttinen
|
||||
|
||||
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 Variants = class {
|
||||
|
||||
static guess_from(name) {
|
||||
const variants = {};
|
||||
variants.original = name;
|
||||
|
||||
if ( name.includes('HighContrast') ) {
|
||||
variants.day = 'HighContrast';
|
||||
variants.night = 'HighContrastInverse';
|
||||
}
|
||||
else if ( name.match(/(Canta|Materia).*-compact/g) ) {
|
||||
variants.day = name.replace(/-dark(?!er)/g, '');
|
||||
variants.night = variants.day.replace(/(-light)?-compact/g, '-dark-compact');
|
||||
}
|
||||
else if ( name.includes('Adapta') ) {
|
||||
variants.day = name.replace('-Nokto', '');
|
||||
variants.night = variants.day.replace('Adapta', 'Adapta-Nokto');
|
||||
}
|
||||
else if ( name.includes('Arc') ) {
|
||||
variants.day = name.replace(/-Dark(?!er)/g, '');
|
||||
variants.night = variants.day.replace(/Arc(-Darker)?/g, 'Arc-Dark');
|
||||
}
|
||||
else if ( name.includes('Flat-Remix-GTK') ) {
|
||||
const isSolid = name.includes('-Solid');
|
||||
const withoutBorder = name.includes('-NoBorder');
|
||||
const basename = name.split('-').slice(0, 4).join('-');
|
||||
variants.day = basename + (name.includes('-Darker') ? '-Darker' : '') + (isSolid ? '-Solid' : '');
|
||||
variants.night = basename + (name.includes('-Darkest') ? '-Darkest' : '-Dark') + (isSolid ? '-Solid' : '') + (withoutBorder ? '-NoBorder' : '');
|
||||
}
|
||||
else if ( name.includes('Layan') ) {
|
||||
variants.day = name.replace('-dark', '');
|
||||
variants.night = variants.day.replace(/Layan(-light)?/g, 'Layan-dark');
|
||||
}
|
||||
else if ( name.includes('Matcha') ) {
|
||||
variants.day = name.replace('-dark-', '-');
|
||||
variants.night = variants.day.replace(/Matcha(-light)?-/g, 'Matcha-dark-');
|
||||
}
|
||||
else if ( name.includes('Mojave') ) {
|
||||
variants.day = name.replace('-dark', '-light');
|
||||
variants.night = variants.day.replace('-light', '-dark');
|
||||
}
|
||||
else if ( name.includes('vimix') ) {
|
||||
variants.day = name.replace('-dark', '');
|
||||
variants.night = variants.day.replace(/vimix(-light)?/g, 'vimix-dark');
|
||||
}
|
||||
else {
|
||||
variants.day = name.replace(/-dark(?!er)/g, '');
|
||||
variants.night = variants.day.replace(/(-light)?(-darker)?/g, '') + '-dark';
|
||||
}
|
||||
|
||||
return variants;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user