fix(agsV2 brightness): add safety around kbd levels and related OSD

This commit is contained in:
matt1432 2024-11-12 14:36:01 -05:00
parent 99e2b17754
commit 1eed5db346
2 changed files with 60 additions and 39 deletions

View file

@ -11,11 +11,10 @@ const SCREEN_ICONS: Record<number, string> = {
const INTERVAL = 500; const INTERVAL = 500;
@register() @register()
class Brightness extends GObject.Object { class Brightness extends GObject.Object {
declare private _kbd: string; declare private _kbd: string | undefined;
declare private _caps: string; declare private _caps: string | undefined;
declare private _screen: number; declare private _screen: number;
@ -51,15 +50,28 @@ class Brightness extends GObject.Object {
return this._screenIcon; return this._screenIcon;
} }
declare private _kbdMax: number; public hasKbd = false;
declare private _kbdLevel: number; declare private _kbdMax: number | undefined;
declare private _kbdLevel: number | undefined;
@property(Number) @property(Number)
get kbdLevel() { get kbdLevel() {
if (!this._kbdMax) {
console.error('[get kbdLevel] No Keyboard brightness');
return -1;
}
return this._kbdLevel; return this._kbdLevel;
} }
set kbdLevel(value) { set kbdLevel(value) {
if (!this._kbdMax || !value) {
console.error('[set kbdLevel] No Keyboard brightness');
return;
}
if (value < 0 || value > this._kbdMax) { if (value < 0 || value > this._kbdMax) {
return; return;
} }
@ -94,19 +106,22 @@ class Brightness extends GObject.Object {
* @param o.kbd name of kbd in brightnessctl * @param o.kbd name of kbd in brightnessctl
* @param o.caps name of caps_lock in brightnessctl * @param o.caps name of caps_lock in brightnessctl
*/ */
public async initService({ kbd = '', caps = '' }) { public async initService({ kbd, caps }: { kbd?: string, caps?: string }) {
this._kbd = kbd;
this._caps = caps;
try { try {
this._monitorKbdState(); if (kbd) {
this._kbdMax = Number(await execAsync(`brightnessctl -d ${this._kbd} m`)); 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')) / this._screen = Number(await execAsync('brightnessctl g')) /
Number(await execAsync('brightnessctl m')); Number(await execAsync('brightnessctl m'));
this.notify('screen'); this.notify('screen');
} }
catch (_e) { catch (_e) {
console.error('missing dependancy: brightnessctl'); console.error('missing dependency: brightnessctl');
} }
} }
@ -124,16 +139,18 @@ class Brightness extends GObject.Object {
private _monitorKbdState() { private _monitorKbdState() {
const timer = interval(INTERVAL, () => { const timer = interval(INTERVAL, () => {
execAsync(`brightnessctl -d ${this._kbd} g`).then( execAsync(`brightnessctl -d ${this._kbd} g`)
(out) => { .then(
if (parseInt(out) !== this._kbdLevel) { (out) => {
this._kbdLevel = parseInt(out); if (parseInt(out) !== this._kbdLevel) {
this.notify('kbd-level'); this._kbdLevel = parseInt(out);
} this.notify('kbd-level');
}, }
).catch(() => { },
timer?.cancel(); )
}); .catch(() => {
timer?.cancel();
});
}); });
} }

View file

@ -138,26 +138,30 @@ export default () => {
</box> </box>
</box> </box>
<box {
name="keyboard" Brightness.hasKbd && (
css="margin-bottom: 80px;" <box
name="keyboard"
css="margin-bottom: 80px;"
setup={(self) => { setup={(self) => {
self.hook(Brightness, 'notify::kbd-level', () => { self.hook(Brightness, 'notify::kbd-level', () => {
popup('keyboard'); popup('keyboard');
}); });
}} }}
> >
<box className="osd-item widget"> <box className="osd-item widget">
<icon icon="keyboard-brightness-symbolic" /> <icon icon="keyboard-brightness-symbolic" />
<ProgressBar <ProgressBar
fraction={bind(Brightness, 'kbdLevel').as((v) => v / 2)} fraction={bind(Brightness, 'kbdLevel').as((v) => (v ?? 0) / 2)}
sensitive={bind(Brightness, 'kbdLevel').as((v) => v !== 0)} sensitive={bind(Brightness, 'kbdLevel').as((v) => v !== 0)}
valign={Gtk.Align.CENTER} valign={Gtk.Align.CENTER}
/> />
</box> </box>
</box> </box>
)
}
<box <box
name="caps" name="caps"