From 1eed5db346d7f8b4cca9f09dee255a870681ee67 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Tue, 12 Nov 2024 14:36:01 -0500 Subject: [PATCH] fix(agsV2 brightness): add safety around kbd levels and related OSD --- .../ags-v2/config/services/brightness.ts | 59 ++++++++++++------- .../ags-v2/config/widgets/osd/main.tsx | 40 +++++++------ 2 files changed, 60 insertions(+), 39 deletions(-) diff --git a/nixosModules/ags-v2/config/services/brightness.ts b/nixosModules/ags-v2/config/services/brightness.ts index 652cf1e9..66d5761d 100644 --- a/nixosModules/ags-v2/config/services/brightness.ts +++ b/nixosModules/ags-v2/config/services/brightness.ts @@ -11,11 +11,10 @@ const SCREEN_ICONS: Record = { const INTERVAL = 500; - @register() class Brightness extends GObject.Object { - declare private _kbd: string; - declare private _caps: string; + declare private _kbd: string | undefined; + declare private _caps: string | undefined; declare private _screen: number; @@ -51,15 +50,28 @@ class Brightness extends GObject.Object { return this._screenIcon; } - declare private _kbdMax: number; - declare private _kbdLevel: number; + public hasKbd = false; + declare private _kbdMax: number | undefined; + declare private _kbdLevel: number | undefined; @property(Number) get kbdLevel() { + if (!this._kbdMax) { + console.error('[get kbdLevel] No Keyboard brightness'); + + return -1; + } + return this._kbdLevel; } set kbdLevel(value) { + if (!this._kbdMax || !value) { + console.error('[set kbdLevel] No Keyboard brightness'); + + return; + } + if (value < 0 || value > this._kbdMax) { return; } @@ -94,19 +106,22 @@ class Brightness extends GObject.Object { * @param o.kbd name of kbd in brightnessctl * @param o.caps name of caps_lock in brightnessctl */ - public async initService({ kbd = '', caps = '' }) { - this._kbd = kbd; - this._caps = caps; + public async initService({ kbd, caps }: { kbd?: string, caps?: string }) { try { - this._monitorKbdState(); - this._kbdMax = Number(await execAsync(`brightnessctl -d ${this._kbd} m`)); + if (kbd) { + this.hasKbd = true; + this._kbd = kbd; + this._monitorKbdState(); + this._kbdMax = Number(await execAsync(`brightnessctl -d ${this._kbd} m`)); + } + this._caps = caps; this._screen = Number(await execAsync('brightnessctl g')) / Number(await execAsync('brightnessctl m')); this.notify('screen'); } catch (_e) { - console.error('missing dependancy: brightnessctl'); + console.error('missing dependency: brightnessctl'); } } @@ -124,16 +139,18 @@ class Brightness extends GObject.Object { private _monitorKbdState() { const timer = interval(INTERVAL, () => { - execAsync(`brightnessctl -d ${this._kbd} g`).then( - (out) => { - if (parseInt(out) !== this._kbdLevel) { - this._kbdLevel = parseInt(out); - this.notify('kbd-level'); - } - }, - ).catch(() => { - timer?.cancel(); - }); + execAsync(`brightnessctl -d ${this._kbd} g`) + .then( + (out) => { + if (parseInt(out) !== this._kbdLevel) { + this._kbdLevel = parseInt(out); + this.notify('kbd-level'); + } + }, + ) + .catch(() => { + timer?.cancel(); + }); }); } diff --git a/nixosModules/ags-v2/config/widgets/osd/main.tsx b/nixosModules/ags-v2/config/widgets/osd/main.tsx index aa57556a..ea05c787 100644 --- a/nixosModules/ags-v2/config/widgets/osd/main.tsx +++ b/nixosModules/ags-v2/config/widgets/osd/main.tsx @@ -138,26 +138,30 @@ export default () => { - { - self.hook(Brightness, 'notify::kbd-level', () => { - popup('keyboard'); - }); - }} - > - - + setup={(self) => { + self.hook(Brightness, 'notify::kbd-level', () => { + popup('keyboard'); + }); + }} + > + + - v / 2)} - sensitive={bind(Brightness, 'kbdLevel').as((v) => v !== 0)} - valign={Gtk.Align.CENTER} - /> - - + (v ?? 0) / 2)} + sensitive={bind(Brightness, 'kbdLevel').as((v) => v !== 0)} + valign={Gtk.Align.CENTER} + /> + + + ) + }