From 4fc2986d15e6775169002adaf91ce23179314a4c Mon Sep 17 00:00:00 2001 From: retrozinndev Date: Mon, 18 Aug 2025 22:10:40 -0300 Subject: [PATCH] :boom: fix(control-center/tiles/night-light): not turning back on after disabling --- src/modules/nightlight.ts | 68 ++++++++++--------- .../control-center/tiles/NightLight.tsx | 9 ++- src/widget/control-center/tiles/Tile.tsx | 2 +- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/modules/nightlight.ts b/src/modules/nightlight.ts index edd365b..7edb1be 100644 --- a/src/modules/nightlight.ts +++ b/src/modules/nightlight.ts @@ -1,9 +1,10 @@ +import { execAsync, exec } from "ags/process"; +import { interval } from "ags/time"; +import GObject, { getter, register, setter } from "ags/gobject"; + import AstalIO from "gi://AstalIO"; import GLib from "gi://GLib?version=2.0"; -import GObject, { getter, register } from "ags/gobject"; -import { execAsync, exec } from "ags/process"; -import { interval } from "ags/time"; export { NightLight }; @@ -16,9 +17,6 @@ class NightLight extends GObject.Object { #gamma: number = 100; #identity: boolean = false; - #prevTemperature: (number|null) = null; - #prevGamma: (number|null) = null; - @getter(Number) public get temperature() { return this.#temperature; } public set temperature(newValue: number) { this.setTemperature(newValue); } @@ -34,19 +32,24 @@ class NightLight extends GObject.Object { @getter(Boolean) public get identity() { return this.#identity; } - public set identity(newValue: boolean) { - newValue ? this.applyIdentity() : this.filter(); + + @setter(Boolean) + public set identity(val: boolean) { + val ? this.applyIdentity() : this.filter(); + this.#identity = val; + this.notify("identity"); } constructor() { super(); - this.#watchInterval = interval(1000, () => { + this.#watchInterval = interval(10000, () => { execAsync("hyprctl hyprsunset temperature").then(t => { if(t.trim() !== "" && t.trim().length <= 5) { const val = Number.parseInt(t.trim()); if(this.#temperature !== val) { + this.identity = this.#temperature === this.identityTemperature; this.#temperature = val; this.notify("temperature"); } @@ -58,6 +61,7 @@ class NightLight extends GObject.Object { const val = Number.parseInt(g.trim()); if(this.#gamma !== val) { + this.identity = this.#gamma === this.maxGamma; this.#gamma = val; this.notify("gamma"); } @@ -77,7 +81,7 @@ class NightLight extends GObject.Object { } private setTemperature(value: number): void { - if(value === this.temperature) return; + if(value === this.temperature && !this.identity) return; if(value > this.maxTemperature || value < 1000) { console.error(`Night Light(hyprsunset): provided temperatue ${value @@ -85,20 +89,18 @@ class NightLight extends GObject.Object { return; } - execAsync(`hyprctl hyprsunset temperature ${value}`).then(() => { + this.dispatchAsync("temperature", value).then(() => { this.#temperature = value; this.notify("temperature"); - this.#identity = false; - this.#prevTemperature = null; - this.#prevGamma = null; + this.identity = false; }).catch((r) => console.error( `Night Light(hyprsunset): Couldn't set temperature. Stderr: ${r}` )); } private setGamma(value: number): void { - if(value === this.gamma) return; + if(value === this.gamma && !this.identity) return; if(value > this.maxGamma || value < 0) { console.error(`Night Light(hyprsunset): provided gamma ${value @@ -106,38 +108,40 @@ class NightLight extends GObject.Object { return; } - execAsync(`hyprctl hyprsunset gamma ${value}`).then(() => { + this.dispatchAsync("gamma", value).then(() => { this.#gamma = value; this.notify("gamma"); - this.#identity = false; - this.#prevTemperature = null; - this.#prevGamma = null; + this.identity = false; }).catch((r) => console.error( `Night Light(hyprsunset): Couldn't set gamma. Stderr: ${r}` )); } public applyIdentity(): void { - if(this.#identity) return; + this.dispatch("identity"); + if(!this.#identity) { + this.#identity = true; + this.notify("identity"); + } + } - this.#prevGamma = this.#gamma; - this.#prevTemperature = this.#temperature; + private dispatch(call: "temperature"|"gamma"|"identity", val?: number): string { + return exec(`hyprctl hyprsunset ${call}${val != null ? ` ${val}` : ""}`); + } - this.#identity = true; - this.temperature = this.identityTemperature; - this.gamma = this.maxGamma; + private async dispatchAsync(...[call, val]: Parameters): Promise { + return await execAsync(`hyprctl hyprsunset ${call}${val != null ? ` ${val}` : ""}`); } public filter(): void { - if(!this.#identity) return; + this.setTemperature(this.temperature); + this.setGamma(this.gamma); - this.#identity = false; - this.setTemperature(this.#prevTemperature ?? this.identityTemperature); - this.setGamma(this.#prevGamma ?? this.maxGamma); - - this.#prevTemperature = null; - this.#prevGamma = null; + if(this.#identity) { + this.#identity = false; + this.notify("identity"); + } } public saveData(): void { diff --git a/src/widget/control-center/tiles/NightLight.tsx b/src/widget/control-center/tiles/NightLight.tsx index 04852e4..bbfe9fc 100644 --- a/src/widget/control-center/tiles/NightLight.tsx +++ b/src/widget/control-center/tiles/NightLight.tsx @@ -10,11 +10,14 @@ export const TileNightLight = () => `${temp === NightLight.getDefault().identityTemperature ? - tr("control_center.tiles.night_light.default_desc") : `${temp}K`} ${ - gamma < NightLight.getDefault().maxGamma ? `(${gamma}%)` : ""}` + ], (identity, temp, gamma) => !identity ? + `${temp === NightLight.getDefault().identityTemperature ? + tr("control_center.tiles.night_light.default_desc") : `${temp}K` + } ${gamma < NightLight.getDefault().maxGamma ? `(${gamma}%)` : ""}` + : tr("control_center.tiles.disabled") )} hasArrow visible={isInstalled("hyprsunset")} onDisabled={() => NightLight.getDefault().identity = true} diff --git a/src/widget/control-center/tiles/Tile.tsx b/src/widget/control-center/tiles/Tile.tsx index 99ed057..11c85dc 100644 --- a/src/widget/control-center/tiles/Tile.tsx +++ b/src/widget/control-center/tiles/Tile.tsx @@ -110,7 +110,7 @@ class Tile extends Gtk.Box { {