Files
TheaninovOS/desktops/hyprland/ags/scripts/brightness.js
2023-11-15 16:09:26 +01:00

57 lines
1.5 KiB
JavaScript

import {Service, Utils} from "../imports.js"
const {exec, execAsync} = Utils
const clamp = (num, min, max) => Math.min(Math.max(num, min), max)
class BrightnessService extends Service {
static {
Service.register(this, {"screen-changed": ["float"]}, {"screen-value": ["float", "rw"]})
}
_screenValue = 0
// the getter has to be in snake_case
get screen_value() {
return this._screenValue
}
// the setter has to be in snake_case too
set screen_value(percent) {
percent = clamp(percent, 0, 1)
this._screenValue = percent
Utils.execAsync(`brightnessctl s ${percent * 100}% -q`)
.then(() => {
// signals has to be explicity emitted
this.emit("screen-changed", percent)
this.notify("screen-value")
// or use Service.changed(propName: string) which does the above two
// this.changed('screen');
})
.catch(print)
}
constructor() {
super()
const current = Number(exec("brightnessctl g"))
const max = Number(exec("brightnessctl m"))
this._screenValue = current / max
}
// overwriting connectWidget method, let's you
// change the default event that widgets connect to
connectWidget(widget, callback, event = "screen-changed") {
super.connectWidget(widget, callback, event)
}
}
// the singleton instance
const service = new BrightnessService()
// make it global for easy use with cli
globalThis.brightness = service
// export to use in other modules
export default service