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;
@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();
});
});
}

View file

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