Extension: Move system color scheme handling outside of Timer

This commit is contained in:
Romain Vigier
2026-08-06 16:29:26 +02:00
parent c9c14ab35f
commit f2f2921ef1
2 changed files with 25 additions and 36 deletions
+5 -3
View File
@@ -5,6 +5,7 @@ import Gio from "gi://Gio";
import * as debug from "../debug.js";
import { ColorScheme } from "../enums/ColorScheme.js";
import { Time } from "../enums/Time.js";
import { Switcher } from "./Switcher.js";
@@ -80,8 +81,9 @@ export class ColorSchemeSwitcher extends Switcher {
}
#onSystemColorSchemeChanged() {
const colorScheme = this.#interfaceSettings.get_string("color-scheme");
debug.message(`System color scheme changed to ${colorScheme}.`);
this.#timer.syncTimeToColorScheme(colorScheme);
const systemColorScheme = this.#interfaceSettings.get_string("color-scheme");
const time = systemColorScheme === ColorScheme.PREFER_DARK ? Time.NIGHT : Time.DAY;
debug.message(`System color scheme changed to ${systemColorScheme}, manually setting time to ${time}.`);
this.#timer.manuallyOverrideTime(time);
}
}
+20 -33
View File
@@ -14,7 +14,6 @@ import * as MessageTray from "resource:///org/gnome/shell/ui/messageTray.js";
import * as debug from "../debug.js";
import { ColorScheme } from "../enums/ColorScheme.js";
import { Time } from "../enums/Time.js";
/**
@@ -29,19 +28,17 @@ import { Time } from "../enums/Time.js";
export class Timer extends GObject.Object {
#locationSettings;
#timeSettings;
#colorSchemeSettings;
#systemInterfaceSettings;
#systemLocationSettings;
/** @type {Time | undefined} */
#time;
/** @type {Time} */
#time = Time.UNKNOWN;
#cancellable = null;
#previousKeybinding = null;
#timeTimeoutId = null;
#geoclue = null;
#geoclueLocationConnectionId = null;
#preventTimeChangeUntilIdentical = false;
#suntimesTimeoutId = null;
#manuallySetTime = false;
#settingsConnections = [];
@@ -60,8 +57,6 @@ export class Timer extends GObject.Object {
super();
this.#locationSettings = NTS.getSettings(`${NTS.metadata["settings-schema"]}.location`);
this.#timeSettings = NTS.getSettings(`${NTS.metadata["settings-schema"]}.time`);
this.#colorSchemeSettings = NTS.getSettings(`${NTS.metadata["settings-schema"]}.color-scheme`);
this.#systemInterfaceSettings = new Gio.Settings({ schema: "org.gnome.desktop.interface" });
this.#systemLocationSettings = new Gio.Settings({ schema: "org.gnome.system.location" });
}
@@ -81,7 +76,7 @@ export class Timer extends GObject.Object {
this.#trackSuntimes();
}
this.#addKeybinding();
this.#changeTime(this.#computeTime());
this.time = this.#computeTime();
debug.message("Timer enabled.");
}
@@ -96,29 +91,19 @@ export class Timer extends GObject.Object {
debug.message("Timer disabled.");
}
/**
* @param {ColorScheme} colorScheme Color scheme to sync the time to.
*/
syncTimeToColorScheme(colorScheme) {
this.#changeTime(this.#colorSchemeToTime(colorScheme), true);
}
get time() {
return this.#time || Time.UNKNOWN;
return this.#time;
}
#changeTime(time, manual = false) {
if (time === this.#time) {
if (!manual && this.#manuallySetTime) this.#manuallySetTime = false;
return;
}
set time(time) {
if (this.#preventTimeChangeUntilIdentical && time === this.#time) this.#preventTimeChangeUntilIdentical = false;
if (!manual && time !== this.#time && this.#manuallySetTime) return;
if (time === this.#time || !Object.values(Time).includes(time) || time === Time.UNKNOWN) return;
if (this.#preventTimeChangeUntilIdentical) return;
this.#time = time;
this.#manuallySetTime = manual;
debug.message(manual ? `Time manually set to ${time}.` : `Time changed to ${time}.`);
debug.message(`Time changed to ${time}.`);
const isMonitorFullscreen = layoutManager.monitors.some((monitor) => monitor.inFullscreen);
if (this.#timeSettings.get_boolean("fullscreen-transition") || !isMonitorFullscreen) {
@@ -130,6 +115,12 @@ export class Timer extends GObject.Object {
this.notify("time");
}
manuallyOverrideTime(time) {
this.#preventTimeChangeUntilIdentical = false;
this.time = time;
this.#preventTimeChangeUntilIdentical = true;
}
#connectSettings() {
debug.message("Connecting Timer to settings...");
this.#settingsConnections.push({
@@ -177,7 +168,7 @@ export class Timer extends GObject.Object {
#trackTime() {
debug.message("Watching for time change...");
this.#timeTimeoutId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, () => {
this.#changeTime(this.#computeTime());
this.time = this.#computeTime();
return GLib.SOURCE_CONTINUE;
});
}
@@ -237,7 +228,7 @@ export class Timer extends GObject.Object {
Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW,
() => {
const time = this.time === Time.NIGHT ? Time.DAY : Time.NIGHT;
this.#changeTime(time, true);
this.time = time;
},
);
debug.message("Added keybinding.");
@@ -251,10 +242,6 @@ export class Timer extends GObject.Object {
}
}
#colorSchemeToTime(colorScheme) {
return colorScheme === this.#colorSchemeSettings.get_string("night") ? Time.NIGHT : Time.DAY;
}
#onSystemLocationStateChanged() {
this.disable();
this.enable();
@@ -340,7 +327,7 @@ export class Timer extends GObject.Object {
// Sunset happens on the day after
else if (sunrise > sunset) return hour >= sunrise || hour < sunset ? Time.DAY : Time.NIGHT;
// Sunset and Sunrise times are identical; preserve current theme
else return this.#time || this.#colorSchemeToTime(this.#systemInterfaceSettings.get_string("color-scheme"));
else return this.#time;
}
#updateSuntimes() {