refactor: make the whole thing more generic

This commit is contained in:
2024-04-02 16:28:57 +02:00
parent 7b648e1955
commit 651f3ad808
193 changed files with 763 additions and 521 deletions

View File

@@ -0,0 +1,77 @@
import * as Utils from "resource:///com/github/Aylur/ags/utils.js";
import Service from "resource:///com/github/Aylur/ags/service.js";
import options from "../options.js";
import { dependencies } from "../utils.js";
const KBD = options.brightnessctlKBD;
class Brightness extends Service {
static {
Service.register(
this,
{},
{
screen: ["float", "rw"],
kbd: ["int", "rw"],
},
);
}
#kbd = 0;
#kbdMax = 3;
#screen = 0;
get kbd() {
return this.#kbd;
}
get screen() {
return this.#screen;
}
set kbd(value) {
if (!dependencies(["brightnessctl"])) return;
if (value < 0 || value > this.#kbdMax) return;
Utils.execAsync(`brightnessctl -d ${KBD} s ${value} -q`)
.then(() => {
this.#kbd = value;
this.changed("kbd");
})
.catch(console.error);
}
set screen(percent) {
if (!dependencies(["gbmonctl"])) return;
if (percent < 0) percent = 0;
if (percent > 1) percent = 1;
Utils.execAsync(
`gbmonctl --prop brightness -val ${Math.min(
Math.max(Math.floor(percent * 100), 0),
100,
)}`,
)
.then(() => {
this.#screen = percent;
this.changed("screen");
})
.catch(console.error);
}
constructor() {
super();
if (dependencies(["brightnessctl"])) {
this.#kbd = Number(Utils.exec(`brightnessctl -d ${KBD} g`));
this.#kbdMax = Number(Utils.exec(`brightnessctl -d ${KBD} m`));
this.#screen =
Number(Utils.exec("brightnessctl g")) /
Number(Utils.exec("brightnessctl m"));
}
}
}
export default new Brightness();