2024-10-29 00:11:37 -04:00
|
|
|
import { execAsync, interval } from 'astal';
|
|
|
|
import GObject, { register, property } from 'astal/gobject';
|
|
|
|
|
|
|
|
|
|
|
|
const SCREEN_ICONS: Record<number, string> = {
|
|
|
|
90: 'display-brightness-high-symbolic',
|
|
|
|
70: 'display-brightness-medium-symbolic',
|
|
|
|
20: 'display-brightness-low-symbolic',
|
|
|
|
5: 'display-brightness-off-symbolic',
|
|
|
|
};
|
|
|
|
|
|
|
|
const INTERVAL = 500;
|
|
|
|
|
|
|
|
@register()
|
2024-11-20 23:38:41 -05:00
|
|
|
export default class Brightness extends GObject.Object {
|
2024-11-12 14:36:01 -05:00
|
|
|
declare private _kbd: string | undefined;
|
|
|
|
declare private _caps: string | undefined;
|
2024-10-29 00:11:37 -04:00
|
|
|
|
|
|
|
declare private _screen: number;
|
|
|
|
|
|
|
|
@property(Number)
|
|
|
|
get screen() {
|
|
|
|
return this._screen;
|
|
|
|
};
|
|
|
|
|
|
|
|
set screen(percent) {
|
|
|
|
if (percent < 0) {
|
|
|
|
percent = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (percent > 1) {
|
|
|
|
percent = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
percent = parseFloat(percent.toFixed(2));
|
|
|
|
|
|
|
|
execAsync(`brightnessctl s ${percent * 100}% -q`)
|
|
|
|
.then(() => {
|
|
|
|
this._screen = percent;
|
|
|
|
this.notify('screen');
|
|
|
|
this._getScreenIcon();
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
|
|
|
}
|
|
|
|
|
2024-10-29 20:35:52 -04:00
|
|
|
private _screenIcon = 'display-brightness-high-symbolic';
|
2024-10-29 00:11:37 -04:00
|
|
|
|
|
|
|
@property(String)
|
|
|
|
get screenIcon() {
|
|
|
|
return this._screenIcon;
|
|
|
|
}
|
|
|
|
|
2024-11-12 14:36:01 -05:00
|
|
|
public hasKbd = false;
|
|
|
|
declare private _kbdMax: number | undefined;
|
|
|
|
declare private _kbdLevel: number | undefined;
|
2024-10-29 00:11:37 -04:00
|
|
|
|
|
|
|
@property(Number)
|
|
|
|
get kbdLevel() {
|
2024-11-12 14:36:01 -05:00
|
|
|
if (!this._kbdMax) {
|
|
|
|
console.error('[get kbdLevel] No Keyboard brightness');
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-10-29 00:11:37 -04:00
|
|
|
return this._kbdLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
set kbdLevel(value) {
|
2024-11-12 14:36:01 -05:00
|
|
|
if (!this._kbdMax || !value) {
|
|
|
|
console.error('[set kbdLevel] No Keyboard brightness');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-29 00:11:37 -04:00
|
|
|
if (value < 0 || value > this._kbdMax) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
execAsync(`brightnessctl -d ${this._kbd} s ${value} -q`)
|
|
|
|
.then(() => {
|
|
|
|
this._kbdLevel = value;
|
|
|
|
this.notify('kbd-level');
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
declare private _capsLevel: number;
|
|
|
|
|
|
|
|
@property(Number)
|
|
|
|
get capsLevel() {
|
|
|
|
return this._capsLevel;
|
|
|
|
}
|
|
|
|
|
2024-10-29 20:35:52 -04:00
|
|
|
private _capsIcon = 'caps-lock-symbolic';
|
2024-10-29 00:11:37 -04:00
|
|
|
|
|
|
|
@property(String)
|
|
|
|
get capsIcon() {
|
|
|
|
return this._capsIcon;
|
|
|
|
}
|
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
public constructor({ kbd, caps }: { kbd?: string, caps?: string } = {}) {
|
|
|
|
super();
|
2024-10-29 00:11:37 -04:00
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
try {
|
|
|
|
(async() => {
|
|
|
|
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');
|
|
|
|
})();
|
2024-10-29 00:11:37 -04:00
|
|
|
}
|
|
|
|
catch (_e) {
|
2024-11-12 14:36:01 -05:00
|
|
|
console.error('missing dependency: brightnessctl');
|
2024-10-29 00:11:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
private static _default: InstanceType<typeof Brightness> | undefined;
|
|
|
|
|
|
|
|
public static get_default({ kbd, caps }: { kbd?: string, caps?: string } = {}) {
|
|
|
|
if (!Brightness._default) {
|
|
|
|
Brightness._default = new Brightness({ kbd, caps });
|
|
|
|
}
|
|
|
|
|
|
|
|
return Brightness._default;
|
|
|
|
}
|
|
|
|
|
2024-10-29 00:11:37 -04:00
|
|
|
private _getScreenIcon() {
|
|
|
|
const brightness = this._screen * 100;
|
|
|
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
for (const threshold of [4, 19, 69, 89]) {
|
|
|
|
if (brightness > threshold + 1) {
|
|
|
|
this._screenIcon = SCREEN_ICONS[threshold + 1];
|
|
|
|
this.notify('screen-icon');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _monitorKbdState() {
|
|
|
|
const timer = interval(INTERVAL, () => {
|
2024-11-12 14:36:01 -05:00
|
|
|
execAsync(`brightnessctl -d ${this._kbd} g`)
|
|
|
|
.then(
|
|
|
|
(out) => {
|
|
|
|
if (parseInt(out) !== this._kbdLevel) {
|
|
|
|
this._kbdLevel = parseInt(out);
|
|
|
|
this.notify('kbd-level');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.catch(() => {
|
|
|
|
timer?.cancel();
|
|
|
|
});
|
2024-10-29 00:11:37 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public fetchCapsState() {
|
|
|
|
execAsync(`brightnessctl -d ${this._caps} g`)
|
|
|
|
.then((out) => {
|
|
|
|
this._capsLevel = Number(out);
|
|
|
|
this._capsIcon = this._capsLevel ?
|
|
|
|
'caps-lock-symbolic' :
|
|
|
|
'capslock-disabled-symbolic';
|
|
|
|
|
|
|
|
this.notify('caps-icon');
|
|
|
|
this.notify('caps-level');
|
|
|
|
})
|
|
|
|
.catch(logError);
|
|
|
|
}
|
|
|
|
}
|