diff --git a/src/extension.js b/src/extension.js index dca9e05..7ff4cc7 100644 --- a/src/extension.js +++ b/src/extension.js @@ -5,6 +5,7 @@ import { Extension } from "resource:///org/gnome/shell/extensions/extension.js"; +import { migrate } from "./migrations.js"; import * as debug from "./debug.js"; import { AccentColorSwitcher } from "./modules/AccentColorSwitcher.js"; @@ -18,6 +19,8 @@ export default class NightThemeSwitcher extends Extension { enable() { globalThis.NTS = this; + migrate(this); + debug.message("Enabling extension..."); const timer = new Timer(); diff --git a/src/meson.build b/src/meson.build index 5ef60dc..3a17d6d 100644 --- a/src/meson.build +++ b/src/meson.build @@ -16,6 +16,7 @@ configure_file( main = [ 'debug.js', 'extension.js', + 'migrations.js', 'prefs.js', ] enums = [ diff --git a/src/migrations.js b/src/migrations.js new file mode 100644 index 0000000..6f096c7 --- /dev/null +++ b/src/migrations.js @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Night Theme Switcher Contributors +// SPDX-License-Identifier: GPL-3.0-or-later + +import Gio from "gi://Gio"; + +/** + * Migrate the extension from a previous version. + * @param {ExtensionBase} extension The extension base class. + */ +export function migrate(extension) { + initializeAccentColorSettings(extension); +} + +/** + * Initialize the accent color settings to the current system accent color. + * @param {ExtensionBase} extension The extension base class. + */ +function initializeAccentColorSettings(extension) { + const accentColorSettings = extension.getSettings(`${extension.metadata["settings-schema"]}.accent-color`); + const interfaceSettings = new Gio.Settings({ schema: "org.gnome.desktop.interface" }); + + if (accentColorSettings.get_string("day") === "unset") + accentColorSettings.set_string("day", interfaceSettings.get_string("accent-color")); + + if (accentColorSettings.get_string("night") === "unset") + accentColorSettings.set_string("night", interfaceSettings.get_string("accent-color")); +} diff --git a/src/modules/AccentColorSwitcher.js b/src/modules/AccentColorSwitcher.js index 6438ed9..76738ef 100644 --- a/src/modules/AccentColorSwitcher.js +++ b/src/modules/AccentColorSwitcher.js @@ -34,7 +34,6 @@ export class AccentColorSwitcher extends Switcher { this.#timer = timer; this.#settings = settings; this.#interfaceSettings = new Gio.Settings({ schema: "org.gnome.desktop.interface" }); - this.#initSettings(); } enable() { @@ -47,13 +46,6 @@ export class AccentColorSwitcher extends Switcher { this.#disconnectSettings(); } - #initSettings() { - if (this.#settings.get_string(Time.DAY) === "unset") - this.#settings.set_string(Time.DAY, this.#interfaceSettings.get_string("accent-color")); - if (this.#settings.get_string(Time.NIGHT) === "unset") - this.#settings.set_string(Time.NIGHT, this.#interfaceSettings.get_string("accent-color")); - } - #connectSettings() { debug.message("Connecting Accent Color Switcher to settings..."); this.#settingsConnections.push({ diff --git a/src/preferences/AppearancePage.js b/src/preferences/AppearancePage.js index 3ef3cd5..fa59b19 100644 --- a/src/preferences/AppearancePage.js +++ b/src/preferences/AppearancePage.js @@ -20,12 +20,6 @@ export class AppearancePage extends Adw.PreferencesPage { constructor({ accentColorSettings, ...params } = {}) { super(params); const backgroundSettings = new Gio.Settings({ schema: "org.gnome.desktop.background" }); - const interfaceSettings = new Gio.Settings({ schema: "org.gnome.desktop.interface" }); - - if (accentColorSettings.get_string("day") === "unset") - accentColorSettings.set_string("day", interfaceSettings.get_string("accent-color")); - if (accentColorSettings.get_string("night") === "unset") - accentColorSettings.set_string("night", interfaceSettings.get_string("accent-color")); accentColorSettings.bind("day", this._day_appearance_chooser, "accent-color", Gio.SettingsBindFlags.DEFAULT); accentColorSettings.bind("night", this._night_appearance_chooser, "accent-color", Gio.SettingsBindFlags.DEFAULT); diff --git a/src/prefs.js b/src/prefs.js index 5d1f946..24d6c18 100644 --- a/src/prefs.js +++ b/src/prefs.js @@ -11,12 +11,17 @@ import Gtk from "gi://Gtk"; import { ExtensionPreferences } from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js"; +import { migrate } from "./migrations.js"; + export default class NightThemeSwitcherPreferences extends ExtensionPreferences { /** * Fill the PreferencesWindow. * @param {import("gi://Adw").default.PreferencesWindow} window The PreferencesWindow to fill. */ async fillPreferencesWindow(window) { + // Migrate from a previous version + migrate(this); + // Load resources const resource = Gio.Resource.load(GLib.build_filenamev([this.path, "resources", "preferences.gresource"])); Gio.resources_register(resource);