From 242f29f90d501dea90489de6adf3fdf2e61689e9 Mon Sep 17 00:00:00 2001 From: Romain Vigier Date: Sun, 24 Mar 2024 11:15:40 +0100 Subject: [PATCH] Extension: Make extension global --- lint/eslintrc-extension.yml | 2 +- src/debug.js | 4 ++-- src/extension.js | 16 ++++++---------- src/modules/ColorSchemeSwitcher.js | 7 ++++++- src/modules/CommandsSwitcher.js | 4 ++-- src/modules/Timer.js | 26 +++++++++----------------- 6 files changed, 26 insertions(+), 33 deletions(-) diff --git a/lint/eslintrc-extension.yml b/lint/eslintrc-extension.yml index d955e60..eb65711 100644 --- a/lint/eslintrc-extension.yml +++ b/lint/eslintrc-extension.yml @@ -9,6 +9,6 @@ rules: argsIgnorePattern: ^(unused|_) object-curly-spacing: off globals: - NTSMetadata: writable + NTS: writable parserOptions: sourceType: module diff --git a/src/debug.js b/src/debug.js index 468393e..348bec2 100644 --- a/src/debug.js +++ b/src/debug.js @@ -7,6 +7,6 @@ * @param {string} msg Message to print. */ export function message(msg) { - if (NTSMetadata['build-type'] === 'debug') - console.log(`[${NTSMetadata.name}] ${msg}`); + if (NTS.metadata['build-type'] === 'debug') + console.log(`[${NTS.metadata.name}] ${msg}`); } diff --git a/src/extension.js b/src/extension.js index 391b57a..ec5fd8b 100644 --- a/src/extension.js +++ b/src/extension.js @@ -16,20 +16,16 @@ export default class NightThemeSwitcher extends Extension { #modules = []; enable() { - globalThis.NTSMetadata = this.metadata; + globalThis.NTS = this; debug.message('Enabling extension...'); - const timer = new Timer({ - settings: this.getSettings(`${this.metadata['settings-schema']}.time`), - colorSchemeSettings: this.getSettings(`${this.metadata['settings-schema']}.color-scheme`), - openPreferences: this.openPreferences, - }); - this.#modules.push(timer); + const timer = new Timer(); [ - new ColorSchemeSwitcher({ timer, settings: this.getSettings(`${this.metadata['settings-schema']}.color-scheme`) }), - new CommandsSwitcher({ timer, settings: this.getSettings(`${this.metadata['settings-schema']}.commands`) }), + timer, + new ColorSchemeSwitcher({ timer }), + new CommandsSwitcher({ timer }), ].forEach(module => this.#modules.push(module)); this.#modules.forEach(module => module.enable()); @@ -48,6 +44,6 @@ export default class NightThemeSwitcher extends Extension { debug.message('Extension disabled.'); - delete globalThis.NTSMetadata; + delete globalThis.NTS; } } diff --git a/src/modules/ColorSchemeSwitcher.js b/src/modules/ColorSchemeSwitcher.js index f3a7567..0792b4d 100644 --- a/src/modules/ColorSchemeSwitcher.js +++ b/src/modules/ColorSchemeSwitcher.js @@ -19,7 +19,12 @@ export class ColorSchemeSwitcher extends Switcher { #settingsConnections = []; - constructor({ timer, settings }) { + /** + * @param {object} params Params object. + * @param {Timer} params.timer Timer to listen to. + */ + constructor({ timer }) { + const settings = NTS.getSettings(`${NTS.metadata['settings-schema']}.color-scheme`); super({ name: 'Color Scheme', timer, diff --git a/src/modules/CommandsSwitcher.js b/src/modules/CommandsSwitcher.js index d815d96..bcd2737 100644 --- a/src/modules/CommandsSwitcher.js +++ b/src/modules/CommandsSwitcher.js @@ -19,9 +19,9 @@ export class CommandsSwitcher extends Switcher { /** * @param {object} params Params object. * @param {Timer} params.timer Timer to listen to. - * @param {Gio.Settings} params.settings Commands settings. */ - constructor({ timer, settings }) { + constructor({ timer }) { + const settings = NTS.getSettings(`${NTS.metadata['settings-schema']}.commands`); super({ name: 'Command', timer, diff --git a/src/modules/Timer.js b/src/modules/Timer.js index 40c34ec..030b382 100644 --- a/src/modules/Timer.js +++ b/src/modules/Timer.js @@ -32,7 +32,6 @@ export class Timer extends GObject.Object { #colorSchemeSettings; #interfaceSettings; #locationSettings; - #openPreferences; #time; #cancellable = null; @@ -53,19 +52,12 @@ export class Timer extends GObject.Object { }, this); } - /** - * @param {object} params Params object. - * @param {Gio.Settings} params.settings Timer settings. - * @param {Gio.Settings} params.colorSchemeSettings Color Scheme settings. - * @param {Function} params.openPreferences Function opening the extension preferences. - */ - constructor({ settings, colorSchemeSettings, openPreferences }) { + constructor() { super(); - this.#settings = settings; - this.#colorSchemeSettings = colorSchemeSettings; + this.#settings = NTS.getSettings(`${NTS.metadata['settings-schema']}.time`); + this.#colorSchemeSettings = NTS.getSettings(`${NTS.metadata['settings-schema']}.color-scheme`); this.#interfaceSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' }); this.#locationSettings = new Gio.Settings({ schema: 'org.gnome.system.location' }); - this.#openPreferences = openPreferences; } enable() { @@ -276,14 +268,14 @@ export class Timer extends GObject.Object { } catch (e) { const [latitude, longitude] = this.#settings.get_value('location').deepUnpack(); if (latitude >= -90 && latitude <= 90 && longitude >= -180 && longitude <= 180) { - console.error(`[${NTSMetadata.name}] Unable to retrieve the location, using the last known location instead.\n${e}`); + console.error(`[${NTS.metadata.name}] Unable to retrieve the location, using the last known location instead.\n${e}`); this.#updateSuntimes(); } else { - console.error(`[${NTSMetadata.name}] Unable to retrieve the location, using the manual schedule times instead.\n${e}`); + console.error(`[${NTS.metadata.name}] Unable to retrieve the location, using the manual schedule times instead.\n${e}`); const source = new MessageTray.Source({ - title: NTSMetadata.name, - icon: Gio.icon_new_for_string(GLib.build_filenamev([NTSMetadata.path, 'icons', 'nightthemeswitcher-symbolic.svg'])), + title: NTS.metadata.name, + icon: Gio.icon_new_for_string(GLib.build_filenamev([NTS.metadata.path, 'icons', 'nightthemeswitcher-symbolic.svg'])), }); messageTray.add(source); @@ -295,9 +287,9 @@ export class Timer extends GObject.Object { 'icon-name': 'location-services-disabled-symbolic', } ); - notification.addAction(_('Edit Manual Schedule'), () => this.#openPreferences()); + notification.addAction(_('Edit Manual Schedule'), () => NTS.openPreferences()); - notification.connect('activated', () => this.#openPreferences()); + notification.connect('activated', () => NTS.openPreferences()); source.addNotification(notification);