From d920c7dcd728eee35d27079a5e8ad119ce33f79f Mon Sep 17 00:00:00 2001 From: Romain Vigier Date: Sun, 20 Feb 2022 00:07:30 +0100 Subject: [PATCH] Use common Switcher class for themes and commands --- src/extension.js | 57 +++++---- src/modules/Commander.js | 88 -------------- src/modules/CursorThemer.js | 136 ---------------------- src/modules/GtkThemer.js | 143 ----------------------- src/modules/IconThemer.js | 136 ---------------------- src/modules/ShellThemer.js | 152 ------------------------ src/modules/Switcher.js | 101 ++++++++++++++++ src/modules/SwitcherCommands.js | 45 ++++++++ src/modules/SwitcherTheme.js | 197 ++++++++++++++++++++++++++++++++ 9 files changed, 370 insertions(+), 685 deletions(-) delete mode 100644 src/modules/Commander.js delete mode 100644 src/modules/CursorThemer.js delete mode 100644 src/modules/GtkThemer.js delete mode 100644 src/modules/IconThemer.js delete mode 100644 src/modules/ShellThemer.js create mode 100644 src/modules/Switcher.js create mode 100644 src/modules/SwitcherCommands.js create mode 100644 src/modules/SwitcherTheme.js diff --git a/src/extension.js b/src/extension.js index b73c45f..2660b4d 100644 --- a/src/extension.js +++ b/src/extension.js @@ -11,21 +11,18 @@ const Me = extensionUtils.getCurrentExtension(); const utils = Me.imports.utils; +const { SwitcherCommands } = Me.imports.modules.SwitcherCommands; +const { SwitcherThemeCursor, SwitcherThemeGtk, SwitcherThemeIcon, SwitcherThemeShell } = Me.imports.modules.SwitcherTheme; const { Timer } = Me.imports.modules.Timer; -const { GtkThemer } = Me.imports.modules.GtkThemer; -const { ShellThemer } = Me.imports.modules.ShellThemer; -const { IconThemer } = Me.imports.modules.IconThemer; -const { CursorThemer } = Me.imports.modules.CursorThemer; -const { Commander } = Me.imports.modules.Commander; var enabled = false; var timer = null; -var gtkThemer = null; -var shellThemer = null; -var iconThemer = null; -var cursorThemer = null; -var commander = null; +var switcherThemeGtk = null; +var switcherThemeShell = null; +var switcherThemeIcon = null; +var switcherThemeCursor = null; +var switcherCommands = null; /** @@ -52,18 +49,18 @@ function enable() { function start() { console.debug('Enabling extension...'); timer = new Timer(); - gtkThemer = new GtkThemer(); - shellThemer = new ShellThemer(); - iconThemer = new IconThemer(); - cursorThemer = new CursorThemer(); - commander = new Commander(); + switcherThemeGtk = new SwitcherThemeGtk({ timer }); + switcherThemeIcon = new SwitcherThemeIcon({ timer }); + switcherThemeShell = new SwitcherThemeShell({ timer }); + switcherThemeCursor = new SwitcherThemeCursor({ timer }); + switcherCommands = new SwitcherCommands({ timer }); timer.enable(); - gtkThemer.enable(); - shellThemer.enable(); - iconThemer.enable(); - cursorThemer.enable(); - commander.enable(); + switcherThemeGtk.enable(); + switcherThemeShell.enable(); + switcherThemeIcon.enable(); + switcherThemeCursor.enable(); + switcherCommands.enable(); enabled = true; console.debug('Extension enabled.'); @@ -76,19 +73,19 @@ function disable() { console.debug('Disabling extension...'); enabled = false; - gtkThemer.disable(); - shellThemer.disable(); - iconThemer.disable(); - cursorThemer.disable(); - commander.disable(); + switcherThemeGtk.disable(); + switcherThemeShell.disable(); + switcherThemeIcon.disable(); + switcherThemeCursor.disable(); + switcherCommands.disable(); timer.disable(); timer = null; - gtkThemer = null; - shellThemer = null; - iconThemer = null; - cursorThemer = null; - commander = null; + switcherThemeGtk = null; + switcherThemeShell = null; + switcherThemeIcon = null; + switcherThemeCursor = null; + switcherCommands = null; console.debug('Extension disabled.'); } diff --git a/src/modules/Commander.js b/src/modules/Commander.js deleted file mode 100644 index 79c9cdf..0000000 --- a/src/modules/Commander.js +++ /dev/null @@ -1,88 +0,0 @@ -// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier -// SPDX-License-Identifier: GPL-3.0-or-later - -const { GLib } = imports.gi; -const { extensionUtils } = imports.misc; - -const Me = extensionUtils.getCurrentExtension(); - -const e = Me.imports.extension; -const utils = Me.imports.utils; - -const { Time } = Me.imports.enums.Time; - - -/** - * The Commander is responsible for spawning commands according to the time. - */ -var Commander = class { - constructor() { - this._commandsSettings = extensionUtils.getSettings(utils.getSettingsSchema('commands')); - this._statusConnection = null; - this._timerConnection = null; - } - - enable() { - console.debug('Enabling Commander...'); - this._watchStatus(); - if (this._commandsSettings.get_boolean('enabled')) { - this._connectTimer(); - this._spawnCommand(e.timer.time); - } - console.debug('Commander enabled.'); - } - - disable() { - console.debug('Disabling Commander...'); - this._disconnectTimer(); - this._unwatchStatus(); - console.debug('Commander disabled.'); - } - - - _watchStatus() { - console.debug('Watching commands status...'); - this._statusConnection = this._commandsSettings.connect('changed::enabled', this._onStatusChanged.bind(this)); - } - - _unwatchStatus() { - if (this._statusConnection) { - this._commandsSettings.disconnect(this._statusConnection); - this._statusConnection = null; - } - console.debug('Stopped watching commands status.'); - } - - _connectTimer() { - console.debug('Connecting Commander to Timer...'); - this._timerConnection = e.timer.connect('time-changed', this._onTimeChanged.bind(this)); - } - - _disconnectTimer() { - if (this._timerConnection) { - e.timer.disconnect(this._timerConnection); - this._timerConnection = null; - } - console.debug('Disconnecting Commander from Timer.'); - } - - - _onStatusChanged() { - console.debug(`Commands launching has been ${this._commandsSettings.get_boolean('enabled') ? 'enabled' : 'disabled'}.`); - this.disable(); - this.enable(); - } - - _onTimeChanged() { - this._spawnCommand(); - } - - - _spawnCommand() { - if (e.timer.time === Time.UNKNOWN) - return; - const command = this._commandsSettings.get_string(e.timer.time === 'day' ? 'sunrise' : 'sunset'); - GLib.spawn_async(null, ['sh', '-c', command], null, GLib.SpawnFlags.SEARCH_PATH, null); - console.debug(`Spawned ${e.timer.time} command.`); - } -}; diff --git a/src/modules/CursorThemer.js b/src/modules/CursorThemer.js deleted file mode 100644 index d2fd135..0000000 --- a/src/modules/CursorThemer.js +++ /dev/null @@ -1,136 +0,0 @@ -// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier -// SPDX-License-Identifier: GPL-3.0-or-later - -const { Gio } = imports.gi; -const { extensionUtils } = imports.misc; -const { main } = imports.ui; - -const Me = extensionUtils.getCurrentExtension(); - -const e = Me.imports.extension; -const utils = Me.imports.utils; - -const { Time } = Me.imports.enums.Time; - - -/** - * The Cursor Themer is responsible for changing the cursor theme according to - * the time. - */ -var CursorThemer = class { - constructor() { - this._cursorVariantsSettings = extensionUtils.getSettings(utils.getSettingsSchema('cursor-variants')); - this._interfaceSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' }); - this._settingsConnections = []; - this._statusConnection = null; - this._timerConnection = null; - } - - enable() { - console.debug('Enabling Cursor Themer...'); - this._watchStatus(); - if (this._cursorVariantsSettings.get_boolean('enabled')) { - this._connectSettings(); - this._connectTimer(); - this._updateSystemCursorTheme(); - } - console.debug('Cursor Themer enabled.'); - } - - disable() { - console.debug('Disabling Cursor Themer...'); - this._disconnectTimer(); - this._disconnectSettings(); - this._unwatchStatus(); - console.debug('Cursor Themer disabled.'); - } - - - _watchStatus() { - console.debug('Watching cursor variants status...'); - this._statusConnection = this._cursorVariantsSettings.connect('changed::enabled', this._onStatusChanged.bind(this)); - } - - _unwatchStatus() { - if (this._statusConnection) { - this._cursorVariantsSettings.disconnect(this._statusConnection); - this._statusConnection = null; - } - console.debug('Stopped watching cursor variants status.'); - } - - _connectSettings() { - console.debug('Connecting Cursor Themer to settings...'); - this._settingsConnections.push({ - settings: this._cursorVariantsSettings, - id: this._cursorVariantsSettings.connect('changed::day', this._onDayVariantChanged.bind(this)), - }); - this._settingsConnections.push({ - settings: this._cursorVariantsSettings, - id: this._cursorVariantsSettings.connect('changed::night', this._onNightVariantChanged.bind(this)), - }); - this._settingsConnections.push({ - settings: this._interfaceSettings, - id: this._interfaceSettings.connect('changed::cursor-theme', this._onSystemCursorThemeChanged.bind(this)), - }); - } - - _disconnectSettings() { - this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id)); - this._settingsConnections = []; - console.debug('Disconnected Cursor Themer from settings.'); - } - - _connectTimer() { - console.debug('Connecting Cursor Themer to Timer...'); - this._timerConnection = e.timer.connect('time-changed', this._onTimeChanged.bind(this)); - } - - _disconnectTimer() { - if (this._timerConnection) { - e.timer.disconnect(this._timerConnection); - this._timerConnection = null; - } - console.debug('Disconnected Cursor Themer from Timer.'); - } - - - _onStatusChanged() { - console.debug(`Cursor variants switching has been ${this._cursorVariantsSettings.get_boolean('enabled') ? 'enabled' : 'disabled'}.`); - this.disable(); - this.enable(); - } - - _onDayVariantChanged() { - console.debug(`Day cursor variant changed to '${this._cursorVariantsSettings.get_string('day')}'.`); - this._updateSystemCursorTheme(); - } - - _onNightVariantChanged() { - console.debug(`Night cursor variant changed to '${this._cursorVariantsSettings.get_string('night')}'.`); - this._updateSystemCursorTheme(); - } - - _onSystemCursorThemeChanged() { - console.debug(`System cursor theme changed to '${this._interfaceSettings.get_string('cursor-theme')}'.`); - this._updateCurrentVariant(); - } - - _onTimeChanged() { - this._updateSystemCursorTheme(); - } - - - _updateCurrentVariant() { - if (e.timer.time === Time.UNKNOWN) - return; - this._cursorVariantsSettings.set_string(e.timer.time, this._interfaceSettings.get_string('cursor-theme')); - } - - _updateSystemCursorTheme() { - if (e.timer.time === Time.UNKNOWN) - return; - console.debug(`Setting the ${e.timer.time} cursor variant...`); - this._interfaceSettings.set_string('cursor-theme', this._cursorVariantsSettings.get_string(e.timer.time)); - } -}; diff --git a/src/modules/GtkThemer.js b/src/modules/GtkThemer.js deleted file mode 100644 index 7e40a45..0000000 --- a/src/modules/GtkThemer.js +++ /dev/null @@ -1,143 +0,0 @@ -// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier -// SPDX-License-Identifier: GPL-3.0-or-later - -const { Gio } = imports.gi; -const { extensionUtils } = imports.misc; - -const Me = extensionUtils.getCurrentExtension(); -const _ = extensionUtils.gettext; - -const e = Me.imports.extension; -const utils = Me.imports.utils; - -const { Time } = Me.imports.enums.Time; - - -/** - * The GTK Themer is responsible for changing the GTK theme according to the - * time. - * - * When the user changes its GTK theme (for example via GNOME Tweaks), it will - * try to automatically guess the day and night variants for this theme. It - * will warn the user if it is unable to guess. - * - * In manual mode, it will not attempt to update the variants. The user can - * change the GTK variants in the extension's preferences. - */ -var GtkThemer = class { - constructor() { - this._gtkVariantsSettings = extensionUtils.getSettings(utils.getSettingsSchema('gtk-variants')); - this._interfaceSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' }); - this._settingsConnections = []; - this._statusConnection = null; - this._timerConnection = null; - } - - enable() { - console.debug('Enabling GTK Themer...'); - this._watchStatus(); - if (this._gtkVariantsSettings.get_boolean('enabled')) { - this._connectSettings(); - this._connectTimer(); - this._updateSystemGtkTheme(); - } - console.debug('GTK Themer enabled.'); - } - - disable() { - console.debug('Disabling GTK Themer...'); - this._disconnectTimer(); - this._disconnectSettings(); - this._unwatchStatus(); - console.debug('GTK Themer disabled.'); - } - - - _watchStatus() { - console.debug('Watching GTK variants status...'); - this._statusConnection = this._gtkVariantsSettings.connect('changed::enabled', this._onStatusChanged.bind(this)); - } - - _unwatchStatus() { - if (this._statusConnection) { - this._gtkVariantsSettings.disconnect(this._statusConnection); - this._statusConnection = null; - } - console.debug('Stopped watching GTK variants status.'); - } - - _connectSettings() { - console.debug('Connecting GTK Themer to settings...'); - this._settingsConnections.push({ - settings: this._gtkVariantsSettings, - id: this._gtkVariantsSettings.connect('changed::day', this._onDayVariantChanged.bind(this)), - }); - this._settingsConnections.push({ - settings: this._gtkVariantsSettings, - id: this._gtkVariantsSettings.connect('changed::night', this._onNightVariantChanged.bind(this)), - }); - this._settingsConnections.push({ - settings: this._interfaceSettings, - id: this._interfaceSettings.connect('changed::gtk-theme', this._onSystemGtkThemeChanged.bind(this)), - }); - } - - _disconnectSettings() { - this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id)); - this._settingsConnections = []; - console.debug('Disconnected GTK Themer from settings.'); - } - - _connectTimer() { - console.debug('Connecting GTK Themer to Timer...'); - this._timerConnection = e.timer.connect('time-changed', this._onTimeChanged.bind(this)); - } - - _disconnectTimer() { - if (this._timerConnection) { - e.timer.disconnect(this._timerConnection); - this._timerConnection = null; - } - console.debug('Disconnected GTK Themer from Timer.'); - } - - - _onStatusChanged() { - console.debug(`GTK variants switching has been ${this._gtkVariantsSettings.get_boolean('enabled') ? 'enabled' : 'disabled'}.`); - this.disable(); - this.enable(); - } - - _onDayVariantChanged() { - console.debug(`Day GTK variant changed to '${this._gtkVariantsSettings.get_string('day')}'.`); - this._updateSystemGtkTheme(); - } - - _onNightVariantChanged() { - console.debug(`Night GTK variant changed to '${this._gtkVariantsSettings.get_string('night')}'.`); - this._updateSystemGtkTheme(); - } - - _onSystemGtkThemeChanged() { - console.debug(`System GTK theme changed to '${this._interfaceSettings.get_string('gtk-theme')}'.`); - this._updateCurrentVariant(); - } - - _onTimeChanged() { - this._updateSystemGtkTheme(); - } - - - _updateCurrentVariant() { - if (e.timer.time === Time.UNKNOWN) - return; - this._gtkVariantsSettings.set_string(e.timer.time, this._interfaceSettings.get_string('gtk-theme')); - } - - _updateSystemGtkTheme() { - if (e.timer.time === Time.UNKNOWN) - return; - console.debug(`Setting the ${e.timer.time} GTK variant...`); - this._interfaceSettings.set_string('gtk-theme', this._gtkVariantsSettings.get_string(e.timer.time)); - } -}; diff --git a/src/modules/IconThemer.js b/src/modules/IconThemer.js deleted file mode 100644 index 3ea51d1..0000000 --- a/src/modules/IconThemer.js +++ /dev/null @@ -1,136 +0,0 @@ -// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier -// SPDX-License-Identifier: GPL-3.0-or-later - -const { Gio } = imports.gi; -const { extensionUtils } = imports.misc; -const { main } = imports.ui; - -const Me = extensionUtils.getCurrentExtension(); - -const e = Me.imports.extension; -const utils = Me.imports.utils; - -const { Time } = Me.imports.enums.Time; - - -/** - * The Icon Themer is responsible for changing the icon theme according to the - * time. - */ -var IconThemer = class { - constructor() { - this._iconVariantsSettings = extensionUtils.getSettings(utils.getSettingsSchema('icon-variants')); - this._interfaceSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' }); - this._settingsConnections = []; - this._statusConnection = null; - this._timerConnection = null; - } - - enable() { - console.debug('Enabling Icon Themer...'); - this._watchStatus(); - if (this._iconVariantsSettings.get_boolean('enabled')) { - this._connectSettings(); - this._connectTimer(); - this._updateSystemIconTheme(); - } - console.debug('Icon Themer enabled.'); - } - - disable() { - console.debug('Disabling Icon Themer...'); - this._disconnectTimer(); - this._disconnectSettings(); - this._unwatchStatus(); - console.debug('Icon Themer disabled.'); - } - - - _watchStatus() { - console.debug('Watching icon variants status...'); - this._statusConnection = this._iconVariantsSettings.connect('changed::enabled', this._onStatusChanged.bind(this)); - } - - _unwatchStatus() { - if (this._statusConnection) { - this._iconVariantsSettings.disconnect(this._statusConnection); - this._statusConnection = null; - } - console.debug('Stopped watching icon variants status.'); - } - - _connectSettings() { - console.debug('Connecting Icon Themer to settings...'); - this._settingsConnections.push({ - settings: this._iconVariantsSettings, - id: this._iconVariantsSettings.connect('changed::day', this._onDayVariantChanged.bind(this)), - }); - this._settingsConnections.push({ - settings: this._iconVariantsSettings, - id: this._iconVariantsSettings.connect('changed::night', this._onNightVariantChanged.bind(this)), - }); - this._settingsConnections.push({ - settings: this._interfaceSettings, - id: this._interfaceSettings.connect('changed::icon-theme', this._onSystemIconThemeChanged.bind(this)), - }); - } - - _disconnectSettings() { - this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id)); - this._settingsConnections = []; - console.debug('Disconnected Icon Themer from settings.'); - } - - _connectTimer() { - console.debug('Connecting Icon Themer to Timer...'); - this._timerConnection = e.timer.connect('time-changed', this._onTimeChanged.bind(this)); - } - - _disconnectTimer() { - if (this._timerConnection) { - e.timer.disconnect(this._timerConnection); - this._timerConnection = null; - } - console.debug('Disconnected Icon Themer from Timer.'); - } - - - _onStatusChanged() { - console.debug(`Icon variants switching has been ${this._iconVariantsSettings.get_boolean('enabled') ? 'enabled' : 'disabled'}.`); - this.disable(); - this.enable(); - } - - _onDayVariantChanged() { - console.debug(`Day icon variant changed to '${this._iconVariantsSettings.get_string('day')}'.`); - this._updateSystemIconTheme(); - } - - _onNightVariantChanged() { - console.debug(`Night icon variant changed to '${this._iconVariantsSettings.get_string('night')}'.`); - this._updateSystemIconTheme(); - } - - _onSystemIconThemeChanged() { - console.debug(`System icon theme changed to '${this._interfaceSettings.get_string('icon-theme')}'.`); - this._updateCurrentVariant(); - } - - _onTimeChanged() { - this._updateSystemIconTheme(); - } - - - _updateCurrentVariant() { - if (e.timer.time === Time.UNKNOWN) - return; - this._iconVariantsSettings.set_string(e.timer.time, this._interfaceSettings.get_string('icon-theme')); - } - - _updateSystemIconTheme() { - if (e.timer.time === Time.UNKNOWN) - return; - console.debug(`Setting the ${e.timer.time} icon variant...`); - this._interfaceSettings.set_string('icon-theme', this._iconVariantsSettings.get_string(e.timer.time)); - } -}; diff --git a/src/modules/ShellThemer.js b/src/modules/ShellThemer.js deleted file mode 100644 index b0db55b..0000000 --- a/src/modules/ShellThemer.js +++ /dev/null @@ -1,152 +0,0 @@ -// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier -// SPDX-License-Identifier: GPL-3.0-or-later - -const { Gio } = imports.gi; -const { extensionUtils } = imports.misc; - -const Me = extensionUtils.getCurrentExtension(); -const _ = extensionUtils.gettext; - -const e = Me.imports.extension; -const utils = Me.imports.utils; - -const { Time } = Me.imports.enums.Time; - -/** - * The Shell Themer is responsible for changing the GTK theme according to the - * time. It will use the User Themes extension to do so if it is enabled. - * - * When the user changes its shell theme (for example via GNOME Tweaks), it will - * try to automatically guess the day and night variants for this theme. It - * will warn the user if it is unable to guess. - * - * In manual mode, it will not attempt to update the variants. The user can - * change the shell variants in the extension's preferences. - */ -var ShellThemer = class { - constructor() { - this._shellVariantsSettings = extensionUtils.getSettings(utils.getSettingsSchema('shell-variants')); - this._userthemesSettings = utils.getUserthemesSettings(); - this._settingsConnections = []; - this._statusConnection = null; - this._timerConnection = null; - } - - enable() { - console.debug('Enabling Shell Themer...'); - this._watchStatus(); - if (this._shellVariantsSettings.get_boolean('enabled')) { - this._connectSettings(); - this._connectTimer(); - this._updateSystemShellTheme(); - } - console.debug('Shell Themer enabled.'); - } - - disable() { - console.debug('Disabling Shell Themer...'); - this._disconnectTimer(); - this._disconnectSettings(); - this._unwatchStatus(); - console.debug('Shell Themer disabled.'); - } - - - _watchStatus() { - console.debug('Watching shell variants status...'); - this._statusConnection = this._shellVariantsSettings.connect('changed::enabled', this._onStatusChanged.bind(this)); - } - - _unwatchStatus() { - if (this._statusConnection) { - this._shellVariantsSettings.disconnect(this._statusConnection); - this._statusConnection = null; - } - console.debug('Stopped watching shell variants status.'); - } - - _connectSettings() { - console.debug('Connecting Shell Themer to settings...'); - this._settingsConnections.push({ - settings: this._shellVariantsSettings, - id: this._shellVariantsSettings.connect('changed::day', this._onDayVariantChanged.bind(this)), - }); - this._settingsConnections.push({ - settings: this._shellVariantsSettings, - id: this._shellVariantsSettings.connect('changed::night', this._onNightVariantChanged.bind(this)), - }); - if (this._userthemesSettings) { - this._settingsConnections.push({ - settings: this._userthemesSettings, - id: this._userthemesSettings.connect('changed::name', this._onSystemShellThemeChanged.bind(this)), - }); - } - } - - _disconnectSettings() { - this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id)); - this._settingsConnections = []; - console.debug('Disconnected Shell Themer from settings.'); - } - - _connectTimer() { - console.debug('Connecting Shell Themer to Timer...'); - this._timerConnection = e.timer.connect('time-changed', this._onTimeChanged.bind(this)); - } - - _disconnectTimer() { - if (this._timerConnection) { - e.timer.disconnect(this._timerConnection); - this._timerConnection = null; - } - console.debug('Disconnected Shell Themer from Timer.'); - } - - - _onStatusChanged() { - console.debug(`Shell variants switching has been ${this._shellVariantsSettings.get_boolean('enabled') ? 'enabled' : 'disabled'}.`); - this.disable(); - this.enable(); - } - - _onDayVariantChanged() { - console.debug(`Day Shell variant changed to '${this._shellVariantsSettings.get_string('day')}'.`); - this._updateSystemShellTheme(); - } - - _onNightVariantChanged() { - console.debug(`Night Shell variant changed to '${this._shellVariantsSettings.get_string('night')}'.`); - this._updateSystemShellTheme(); - } - - _onSystemShellThemeChanged(_settings, _newTheme) { - if (!this._userthemesSettings) - return; - console.debug(`System Shell theme changed to '${this._userthemesSettings.get_string('name')}'.`); - this._updateCurrentVariant(); - } - - _onTimeChanged() { - this._updateSystemShellTheme(); - } - - - _updateCurrentVariant() { - if (e.timer.time === Time.UNKNOWN || !this._userthemesSettings) - return; - this._shellVariantsSettings.set_string(e.timer.time, this._userthemesSettings.get_string('name')); - } - - _updateSystemShellTheme() { - if (e.timer.time === Time.UNKNOWN) - return; - console.debug(`Setting the ${e.timer.time} Shell variant...`); - const shellTheme = this._shellVariantsSettings.get_string(e.timer.time); - if (this._userthemesSettings) { - this._userthemesSettings.set_string('name', shellTheme); - } else { - const stylesheet = utils.getShellThemeStylesheet(shellTheme); - utils.applyShellStylesheet(stylesheet); - } - } -}; diff --git a/src/modules/Switcher.js b/src/modules/Switcher.js new file mode 100644 index 0000000..715426a --- /dev/null +++ b/src/modules/Switcher.js @@ -0,0 +1,101 @@ +// SPDX-FileCopyrightText: 2020-2022 Romain Vigier +// SPDX-License-Identifier: GPL-3.0-or-later + +const { Gio } = imports.gi; +const { extensionUtils } = imports.misc; + +const Me = extensionUtils.getCurrentExtension(); + +const utils = Me.imports.utils; + +const { Time } = Me.imports.enums.Time; + + +/** + * Function called when the time changes. + * + * @callback TimeChangedCallback + * @param {Time} time New time. + */ + + +/** + * The Switcher runs a callback function when the time changes. + * + * @param {Object} params Params object. + * @param {string} params.name Name of the switcher. + * @param {Timer} params.timer Timer to listen to. + * @param {Gio.Settings} params.settings Settings with the `enabled` key. + * @param {TimeChangedCallback} params.callback Callback function. + */ +var Switcher = class { + #name; + #timer; + #settings; + #callback; + + #statusConnection = null; + #timerConnection = null; + + constructor({ name, timer, settings, callback }) { + this.#name = name; + this.#timer = timer; + this.#settings = settings; + this.#callback = callback; + } + + enable() { + console.debug(`Enabling ${this.#name} switcher...`); + this.#watchStatus(); + if (this.#settings.get_boolean('enabled')) { + this.#connectTimer(); + this.#onTimeChanged(); + } + console.debug(`${this.#name} switcher enabled.`); + } + + disable() { + console.debug(`Disabling ${this.#name} switcher...`); + this.#disconnectTimer(); + this.#unwatchStatus(); + console.debug(`${this.#name} switcher disabled.`); + } + + + #watchStatus() { + console.debug(`Watching ${this.#name} switching status...`); + this.#statusConnection = this.#settings.connect('changed::enabled', this.#onStatusChanged.bind(this)); + } + + #unwatchStatus() { + if (this.#statusConnection) { + this.#settings.disconnect(this.#statusConnection); + this.#statusConnection = null; + } + console.debug(`Stopped watching ${this.#name} switching status.`); + } + + #connectTimer() { + console.debug(`Connecting ${this.#name} switcher to Timer...`); + this.#timerConnection = this.#timer.connect('time-changed', this.#onTimeChanged.bind(this)); + } + + #disconnectTimer() { + if (this.#timerConnection) { + this.#timer.disconnect(this.#timerConnection); + this.#timerConnection = null; + } + console.debug(`Disconnected ${this.#name} switcher from Timer.`); + } + + + #onStatusChanged() { + console.debug(`${this.#name} switching has been ${this.#settings.get_boolean('enabled') ? 'enabled' : 'disabled'}.`); + this.disable(); + this.enable(); + } + + #onTimeChanged() { + this.#callback(this.#timer.time); + } +}; diff --git a/src/modules/SwitcherCommands.js b/src/modules/SwitcherCommands.js new file mode 100644 index 0000000..0f6eda9 --- /dev/null +++ b/src/modules/SwitcherCommands.js @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2022 Romain Vigier +// SPDX-License-Identifier: GPL-3.0-or-later + +const { GLib } = imports.gi; +const { extensionUtils } = imports.misc; + +const Me = extensionUtils.getCurrentExtension(); + +const utils = Me.imports.utils; + +const { Switcher } = Me.imports.modules.Switcher; + +const { Time } = Me.imports.enums.Time; + + +/** + * The Commands Switcher spawns commands according to the time. + * + * @param {Object} params Params object. + * @param {Timer} params.timer Timer to listen to. + */ +var SwitcherCommands = class extends Switcher { + #settings; + + constructor({ timer }) { + const settings = extensionUtils.getSettings(utils.getSettingsSchema('commands')); + super({ + name: 'Command', + timer, + settings, + callback: time => this.#onTimeChanged(time), + }); + this.#settings = settings; + } + + #onTimeChanged(time) { + if (time === Time.UNKNOWN) + return; + const command = this.#settings.get_string(time === Time.DAY ? 'sunrise' : 'sunset'); + if (!command) + return; + GLib.spawn_async(null, ['sh', '-c', command], null, GLib.SpawnFlags.SEARCH_PATH, null); + console.debug(`Spawned ${time} command.`); + } +}; diff --git a/src/modules/SwitcherTheme.js b/src/modules/SwitcherTheme.js new file mode 100644 index 0000000..0a26292 --- /dev/null +++ b/src/modules/SwitcherTheme.js @@ -0,0 +1,197 @@ +// SPDX-FileCopyrightText: 2020-2022 Romain Vigier +// SPDX-License-Identifier: GPL-3.0-or-later + +const { Gio } = imports.gi; +const { extensionUtils } = imports.misc; + +const Me = extensionUtils.getCurrentExtension(); + +const utils = Me.imports.utils; + +const { Switcher } = Me.imports.modules.Switcher; + +const { Time } = Me.imports.enums.Time; + + +/** + * Function called to update the system theme when no settings exist. + * + * @callback noSettingsUpdateSystemThemeCallback + * @param {Time} time New time. + */ + + +/** + * The Theme Switcher sets the system theme according to the time, either via + * provided settings or by running a callback function. + * + * It also listens to system theme changes to update the current variant setting. + * + * @param {Object} params Params object. + * @param {string} params.name Name of the switcher. + * @param {Timer} params.timer Timer to listen to. + * @param {Gio.Settings} params.settings Settings with the `enabled`, `day` and `night` keys. + * @param {Gio.Settings=} params.systemSettings System settings containing the theme name. + * @param {string} params.themeKey Settings key of the theme name. + * @param {noSettingsUpdateSystemThemeCallback} Callback function. + */ +var SwitcherTheme = class extends Switcher { + #name; + #timer; + #settings; + #systemSettings; + #themeKey; + #noSettingsUpdateSystemThemeCallback; + + #settingsConnections = []; + + constructor({ name, timer, settings, systemSettings = null, themeKey, noSettingsUpdateSystemThemeCallback = null }) { + super({ + name, + timer, + settings, + callback: time => this.#onTimeChanged(time), + }); + this.#name = name; + this.#timer = timer; + this.#settings = settings; + this.#systemSettings = systemSettings; + this.#themeKey = themeKey; + this.#noSettingsUpdateSystemThemeCallback = noSettingsUpdateSystemThemeCallback; + } + + enable() { + if (this.#settings.get_boolean('enabled')) + this.#connectSettings(); + super.enable(); + } + + disable() { + this.#disconnectSettings(); + super.disable(); + } + + #connectSettings() { + console.debug(`Connecting ${this.#name} switcher to settings...`); + this.#settingsConnections.push({ + settings: this.#settings, + id: this.#settings.connect('changed::day', this.#onDayVariantChanged.bind(this)), + }); + this.#settingsConnections.push({ + settings: this.#settings, + id: this.#settings.connect('changed::night', this.#onNightVariantChanged.bind(this)), + }); + if (!this.#systemSettings) + return; + this.#settingsConnections.push({ + settings: this.#systemSettings, + id: this.#systemSettings.connect(`changed::${this.#themeKey}`, this.#onSystemThemeChanged.bind(this)), + }); + } + + #disconnectSettings() { + this.#settingsConnections.forEach(connection => connection.settings.disconnect(connection.id)); + this.#settingsConnections = []; + console.debug(`Disconnected ${this.#name} switcher from settings.`); + } + + + #onDayVariantChanged() { + console.debug(`Day ${this.#name} variant changed to '${this.#settings.get_string('day')}'.`); + this.#updateSystemTheme(); + } + + #onNightVariantChanged() { + console.debug(`Night ${this.#name} variant changed to '${this.#settings.get_string('night')}'.`); + this.#updateSystemTheme(); + } + + #onSystemThemeChanged() { + console.debug(`System ${this.#name} changed to '${this.#systemSettings.get_string(this.#themeKey)}'.`); + this.#updateCurrentVariant(); + } + + #onTimeChanged(_time) { + this.#updateSystemTheme(); + } + + + #updateCurrentVariant() { + if (this.#timer.time === Time.UNKNOWN || !this.#systemSettings) + return; + this.#settings.set_string(this.#timer.time, this.#systemSettings.get_string(this.#themeKey)); + } + + #updateSystemTheme() { + if (this.#timer.time === Time.UNKNOWN) + return; + console.debug(`Setting the ${this.#timer.time} ${this.#name} variant...`); + if (this.#systemSettings) + this.#systemSettings.set_string(this.#themeKey, this.#settings.get_string(this.#timer.time)); + else if (this.#noSettingsUpdateSystemThemeCallback) + this.#noSettingsUpdateSystemThemeCallback(this.#timer.time); + } +}; + + +var SwitcherThemeCursor = class extends SwitcherTheme { + constructor({ timer }) { + super({ + name: 'Cursor theme', + timer, + settings: extensionUtils.getSettings(utils.getSettingsSchema('cursor-variants')), + systemSettings: new Gio.Settings({ schema: 'org.gnome.desktop.interface' }), + themeKey: 'cursor-theme', + }); + } +}; + + +var SwitcherThemeGtk = class extends SwitcherTheme { + constructor({ timer }) { + super({ + name: 'GTK theme', + timer, + settings: extensionUtils.getSettings(utils.getSettingsSchema('gtk-variants')), + systemSettings: new Gio.Settings({ schema: 'org.gnome.desktop.interface' }), + themeKey: 'gtk-theme', + }); + } +}; + + +var SwitcherThemeIcon = class extends SwitcherTheme { + constructor({ timer }) { + super({ + name: 'Icon theme', + timer, + settings: extensionUtils.getSettings(utils.getSettingsSchema('icon-variants')), + systemSettings: new Gio.Settings({ schema: 'org.gnome.desktop.interface' }), + themeKey: 'icon-theme', + }); + } +}; + + +var SwitcherThemeShell = class extends SwitcherTheme { + #settings; + + constructor({ timer }) { + const settings = extensionUtils.getSettings(utils.getSettingsSchema('shell-variants')); + super({ + name: 'Shell theme', + timer, + settings, + systemSettings: utils.getUserthemesSettings(), + themeKey: 'name', + noSettingsUpdateSystemThemeCallback: time => this.#noSettingsUpdateSystemThemeCallback(time), + }); + this.#settings = settings; + } + + #noSettingsUpdateSystemThemeCallback(time) { + const shellTheme = this.#settings.get_string(time); + const stylesheet = utils.getShellThemeStylesheet(shellTheme); + utils.applyShellStylesheet(stylesheet); + } +};