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