Cursor theme switching

This commit is contained in:
Romain
2020-05-29 09:30:53 +00:00
parent f6e0b0dbf9
commit 49f58678bc
10 changed files with 579 additions and 75 deletions
+163
View File
@@ -0,0 +1,163 @@
/*
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 e = Me.imports.extension;
const { log_debug } = Me.imports.utils;
/**
* The Cursor Themer is responsible for changing the cursor theme according to
* the time.
*/
var CursorThemer = class {
enable() {
log_debug('Enabling Cursor Themer...');
this._watch_status();
this._save_original_theme();
if ( e.settingsManager.cursor_variants_enabled ) {
this._connect_settings();
this._connect_timer();
}
log_debug('Cursor Themer enabled.');
}
disable() {
log_debug('Disabling Cursor Themer...');
this._disconnect_timer();
this._disconnect_settings();
this._reset_original_theme();
this._unwatch_status();
log_debug('Cursor Themer disabled.');
}
_watch_status() {
log_debug('Watching cursor variants status...');
this._cursor_variants_status_changed_connect = e.settingsManager.connect('cursor-variants-status-changed', this._on_cursor_variants_status_changed.bind(this));
}
_unwatch_status() {
if ( this._cursor_variants_status_changed_connect ) {
e.settingsManager.disconnect(this._cursor_variants_status_changed_connect);
this._cursor_variants_status_changed_connect = null;
}
log_debug('Stopped watching cursor variants status.');
}
_connect_settings() {
log_debug('Connecting Cursor Themer to settings...');
this._cursor_variant_changed_connect = e.settingsManager.connect('cursor-variant-changed', this._on_cursor_variant_changed.bind(this));
this._cursor_theme_changed_connect = e.settingsManager.connect('cursor-theme-changed', this._on_cursor_theme_changed.bind(this));
}
_disconnect_settings() {
if ( this._cursor_variant_changed_connect ) {
e.settingsManager.disconnect(this._cursor_variant_changed_connect);
this._cursor_variant_changed_connect = null;
}
if ( this._cursor_theme_changed_connect ) {
e.settingsManager.disconnect(this._cursor_theme_changed_connect);
this._cursor_theme_changed_connect = null;
}
log_debug('Disconnected Cursor Themer from settings.');
}
_connect_timer() {
log_debug('Connecting Cursor Themer to Timer...');
this._time_changed_connect = e.timer.connect('time-changed', this._on_time_changed.bind(this));
}
_disconnect_timer() {
if ( this._time_changed_connect ) {
e.timer.disconnect(this._time_changed_connect);
this._time_changed_connect = null;
}
log_debug('Disconnected Cursor Themer from Timer.');
}
_on_cursor_variants_status_changed(settings, enabled) {
this.disable();
this.enable();
}
_on_cursor_variant_changed(settings, changed_variant_time) {
if ( changed_variant_time === e.timer.time ) {
this._set_variant(changed_variant_time);
}
}
_on_cursor_theme_changed(settings, new_theme) {
this._save_original_theme();
switch (e.timer.time) {
case 'day':
e.settingsManager.cursor_variant_day = new_theme;
break;
case 'night':
e.settingsManager.cursor_variant_night = new_theme;
}
this._set_variant(e.timer.time);
}
_on_time_changed(timer, new_time) {
this._set_variant(new_time);
}
_are_variants_up_to_date() {
return ( e.settingsManager.cursor_theme === e.settingsManager.cursor_variant_day || e.settingsManager.cursor_theme === e.settingsManager.cursor_variant_night );
}
_set_variant(time) {
log_debug(`Setting the cursor ${time} variant...`);
switch (time) {
case 'day':
e.settingsManager.cursor_theme = e.settingsManager.cursor_variant_day;
break;
case 'night':
e.settingsManager.cursor_theme = e.settingsManager.cursor_variant_night;
break;
case 'original':
e.settingsManager.cursor_theme = e.settingsManager.cursor_variant_original;
break;
}
}
_save_original_theme() {
if ( this._are_variants_up_to_date() ) {
return;
}
e.settingsManager.cursor_variant_original = e.settingsManager.cursor_theme;
}
_reset_original_theme() {
// We don't reset the theme when locking the session to prevent
// flicker on unlocking
if ( !main.screenShield.locked ) {
log_debug('Resetting to the user\'s original cursor theme...');
this._set_variant('original');
}
}
}
+93
View File
@@ -57,6 +57,10 @@ var SettingsManager = class {
this._shell_variant_day_changed_connect = this._extensionsSettings.connect('changed::shell-variant-day', this._on_shell_variant_day_changed.bind(this));
this._shell_variant_night_changed_connect = this._extensionsSettings.connect('changed::shell-variant-night', this._on_shell_variant_night_changed.bind(this));
this._shell_variant_original_changed_connect = this._extensionsSettings.connect('changed::shell-variant-original', this._on_shell_variant_original_changed.bind(this));
this._cursor_variants_status_connect = this._extensionsSettings.connect('changed::cursor-variants-enabled', this._on_cursor_variants_status_changed.bind(this));
this._cursor_variant_day_changed_connect = this._extensionsSettings.connect('changed::cursor-variant-day', this._on_cursor_variant_day_changed.bind(this));
this._cursor_variant_night_changed_connect = this._extensionsSettings.connect('changed::cursor-variant-night', this._on_cursor_variant_night_changed.bind(this));
this._cursor_variant_original_changed_connect = this._extensionsSettings.connect('changed::cursor-variant-original', this._on_cursor_variant_original_changed.bind(this));
this._time_source_changed_connect = this._extensionsSettings.connect('changed::time-source', this._on_time_source_changed.bind(this));
this._manual_time_source_changed_connect = this._extensionsSettings.connect('changed::manual-time-source', this._on_manual_time_source_changed.bind(this));
this._commands_status_connect = this._extensionsSettings.connect('changed::commands-enabled', this._on_commands_status_changed.bind(this));
@@ -66,6 +70,7 @@ var SettingsManager = class {
this._nightlight_status_connect = this._colorSettings.connect('changed::night-light-enabled', this._on_nightlight_status_changed.bind(this));
this._location_status_connect = this._locationSettings.connect('changed::enabled', this._on_location_status_changed.bind(this));
this._gtk_theme_changed_connect = this._interfaceSettings.connect('changed::gtk-theme', this._on_gtk_theme_changed.bind(this));
this._cursor_theme_changed_connect = this._interfaceSettings.connect('changed::cursor-theme', this._on_cursor_theme_changed.bind(this));
this._background_changed_connect = this._backgroundSettings.connect('changed::picture-uri', this._on_background_changed.bind(this));
if ( this._userthemesSettings ) {
this._shell_theme_changed_connect = this._userthemesSettings.connect('changed::name', this._on_shell_theme_changed.bind(this));
@@ -81,6 +86,10 @@ var SettingsManager = class {
this._extensionsSettings.disconnect(this._shell_variant_day_changed_connect);
this._extensionsSettings.disconnect(this._shell_variant_night_changed_connect);
this._extensionsSettings.disconnect(this._shell_variant_original_changed_connect);
this._extensionsSettings.disconnect(this._cursor_variants_status_connect);
this._extensionsSettings.disconnect(this._cursor_variant_day_changed_connect);
this._extensionsSettings.disconnect(this._cursor_variant_night_changed_connect);
this._extensionsSettings.disconnect(this._cursor_variant_original_changed_connect);
this._extensionsSettings.disconnect(this._time_source_changed_connect);
this._extensionsSettings.disconnect(this._manual_time_source_changed_connect);
this._extensionsSettings.disconnect(this._commands_status_connect);
@@ -180,6 +189,45 @@ var SettingsManager = class {
return this._extensionsSettings.get_boolean('manual-shell-variants');
}
/* Cursor variants settings */
get cursor_variants_enabled() {
return this._extensionsSettings.get_boolean('cursor-variants-enabled');
}
get cursor_variant_day() {
return this._extensionsSettings.get_string('cursor-variant-day') || this.cursor_theme;
}
set cursor_variant_day(value) {
if ( value !== this.cursor_variant_day ) {
this._extensionsSettings.set_string('cursor-variant-day', value);
log_debug(`The cursor day variant has been set to '${value}'.`);
}
}
get cursor_variant_night() {
return this._extensionsSettings.get_string('cursor-variant-night') || this.cursor_theme;
}
set cursor_variant_night(value) {
if ( value !== this.cursor_variant_night ) {
this._extensionsSettings.set_string('cursor-variant-night', value);
log_debug(`The cursor night variant has been set to '${value}'.`);
}
}
get cursor_variant_original() {
return this._extensionsSettings.get_string('cursor-variant-original');
}
set cursor_variant_original(value) {
if ( value !== this.cursor_variant_original ) {
this._extensionsSettings.set_string('cursor-variant-original', value);
log_debug(`The cursor original variant has been set to '${value}'.`);
}
}
/* Time source settings */
@@ -292,6 +340,20 @@ var SettingsManager = class {
}
/* Cursor theme settings */
get cursor_theme() {
return this._interfaceSettings.get_string('cursor-theme');
}
set cursor_theme(value) {
if ( value !== this.cursor_theme ) {
this._interfaceSettings.set_string('cursor-theme', value);
log_debug(`Cursor theme has been set to '${value}.'`);
}
}
/* Background settings */
get background() {
@@ -345,6 +407,29 @@ var SettingsManager = class {
}
/* Cursor variants */
_on_cursor_variants_status_changed(settings, changed_key) {
log_debug('Cursor variants have been ' + (this.cursor_variants_enabled ? 'ena' : 'disa') + 'bled.');
this.emit('cursor-variants-status-changed', this.curso_variants_enabled);
}
_on_cursor_variant_day_changed(settings, changed_key) {
log_debug(`Cursor day variant has changed to '${this.cursor_variant_day}'.`);
this.emit('cursor-variant-changed', 'day');
}
_on_cursor_variant_night_changed(settings, changed_key) {
log_debug(`Cursor night variant has changed to '${this.cursor_variant_night}'.`);
this.emit('cursor-variant-changed', 'night');
}
_on_cursor_variant_original_changed(settings, changed_key) {
log_debug(`Cursor original variant has changed to '${this.cursor_variant_original}'.`);
this.emit('cursor-variant-changed', 'original');
}
/* Time source */
_on_time_source_changed(settings, changed_key) {
@@ -408,6 +493,14 @@ var SettingsManager = class {
}
/* Cursor theme */
_on_cursor_theme_changed(settings, changed_key) {
log_debug(`Cursor theme has changed to '${this.cursor_theme}'.`);
this.emit('cursor-theme-changed', this.cursor_theme);
}
/* Background */
_on_background_changed(settings, changed_key) {
+1 -1
View File
@@ -23,7 +23,7 @@ const { main } = imports.ui;
const Me = extensionUtils.getCurrentExtension();
const e = Me.imports.extension;
const { log_debug, log_error, get_theme_dirs_paths, get_installed_shell_themes, get_shell_theme_stylesheet, apply_shell_stylesheet } = Me.imports.utils;
const { log_debug, log_error, get_installed_shell_themes, get_shell_theme_stylesheet, apply_shell_stylesheet } = Me.imports.utils;
const { ShellVariants } = Me.imports.modules.ShellVariants;
const Gettext = imports.gettext.domain(Me.metadata.uuid);