Extension: Add accent color switching
This commit is contained in:
+7
-3
@@ -7,6 +7,7 @@ import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
|
||||
|
||||
import * as debug from "./debug.js";
|
||||
|
||||
import { AccentColorSwitcher } from "./modules/AccentColorSwitcher.js";
|
||||
import { ColorSchemeSwitcher } from "./modules/ColorSchemeSwitcher.js";
|
||||
import { CommandsSwitcher } from "./modules/CommandsSwitcher.js";
|
||||
import { Timer } from "./modules/Timer.js";
|
||||
@@ -21,9 +22,12 @@ export default class NightThemeSwitcher extends Extension {
|
||||
|
||||
const timer = new Timer();
|
||||
|
||||
[timer, new ColorSchemeSwitcher({ timer }), new CommandsSwitcher({ timer })].forEach((module) =>
|
||||
this.#modules.push(module),
|
||||
);
|
||||
[
|
||||
timer,
|
||||
new AccentColorSwitcher({ timer }),
|
||||
new ColorSchemeSwitcher({ timer }),
|
||||
new CommandsSwitcher({ timer }),
|
||||
].forEach((module) => this.#modules.push(module));
|
||||
|
||||
this.#modules.forEach((module) => module.enable());
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ icons = [
|
||||
'data/icons/nightthemeswitcher-symbolic.svg',
|
||||
]
|
||||
modules = [
|
||||
'modules/AccentColorSwitcher.js',
|
||||
'modules/ColorSchemeSwitcher.js',
|
||||
'modules/CommandsSwitcher.js',
|
||||
'modules/Switcher.js',
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
// SPDX-FileCopyrightText: Night Theme Switcher Contributors
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import Gio from "gi://Gio";
|
||||
|
||||
import * as debug from "../debug.js";
|
||||
|
||||
import { Time } from "../enums/Time.js";
|
||||
|
||||
import { Switcher } from "./Switcher.js";
|
||||
|
||||
/**
|
||||
* The Accent Color Switcher changes the system accent color according to the time.
|
||||
*/
|
||||
export class AccentColorSwitcher extends Switcher {
|
||||
#settings;
|
||||
#interfaceSettings;
|
||||
#timer;
|
||||
|
||||
#settingsConnections = [];
|
||||
|
||||
/**
|
||||
* @param {object} params Params object.
|
||||
* @param {import("./Timer.js").Timer} params.timer Timer to listen to.
|
||||
*/
|
||||
constructor({ timer }) {
|
||||
const settings = NTS.getSettings(`${NTS.metadata["settings-schema"]}.accent-color`);
|
||||
super({
|
||||
name: "Accent Color",
|
||||
timer,
|
||||
settings,
|
||||
callback: (time) => this.#onTimeChanged(time),
|
||||
});
|
||||
this.#timer = timer;
|
||||
this.#settings = settings;
|
||||
this.#interfaceSettings = new Gio.Settings({ schema: "org.gnome.desktop.interface" });
|
||||
this.#initSettings();
|
||||
}
|
||||
|
||||
enable() {
|
||||
super.enable();
|
||||
this.#connectSettings();
|
||||
}
|
||||
|
||||
disable() {
|
||||
super.disable();
|
||||
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({
|
||||
settings: this.#settings,
|
||||
id: this.#settings.connect("changed::day", this.#onAccentColorChanged.bind(this)),
|
||||
});
|
||||
this.#settingsConnections.push({
|
||||
settings: this.#settings,
|
||||
id: this.#settings.connect("changed::night", this.#onAccentColorChanged.bind(this)),
|
||||
});
|
||||
this.#settingsConnections.push({
|
||||
settings: this.#interfaceSettings,
|
||||
id: this.#interfaceSettings.connect("changed::accent-color", this.#onSystemAccentColorChanged.bind(this)),
|
||||
});
|
||||
}
|
||||
|
||||
#disconnectSettings() {
|
||||
this.#settingsConnections.forEach(({ settings, id }) => settings.disconnect(id));
|
||||
this.#settingsConnections = [];
|
||||
debug.message("Disconnected Accent Color Switcher from settings.");
|
||||
}
|
||||
|
||||
#onTimeChanged(time) {
|
||||
const accentColor = time === Time.NIGHT ? this.#settings.get_string("night") : this.#settings.get_string("day");
|
||||
this.#interfaceSettings.set_string("accent-color", accentColor);
|
||||
}
|
||||
|
||||
#onAccentColorChanged(_settings, time) {
|
||||
const accentColor = this.#settings.get_string(time);
|
||||
debug.message(`${time} accent color changed to ${accentColor}.`);
|
||||
if (time === this.#timer.time) this.#interfaceSettings.set_string("accent-color", accentColor);
|
||||
}
|
||||
|
||||
#onSystemAccentColorChanged() {
|
||||
const accentColor = this.#interfaceSettings.get_string("accent-color");
|
||||
const time = this.#timer.time;
|
||||
if (accentColor === this.#settings.get_string(time)) return;
|
||||
debug.message(`System accent colot changed to ${accentColor}, applying it to ${time}.`);
|
||||
this.#settings.set_string(time, accentColor);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user