Added on-screen-displays to indicate volume and brightness changes. (#34)
* Resolves #13 - Added on-screen-displays to indicate volume and brightness changes. * <3 Aylur * Update brightness logic for osd * Update brightness labels * Fixed typos in the settings menu component. * Added options to toggle OSD and change its orientation.
This commit is contained in:
@@ -1,84 +1,70 @@
|
||||
class BrightnessService extends Service {
|
||||
// every subclass of GObject.Object has to register itself
|
||||
// <3 Aylur for this brightness service
|
||||
import { bash, dependencies, sh } from "lib/utils"
|
||||
|
||||
if (!dependencies("brightnessctl"))
|
||||
App.quit()
|
||||
|
||||
const get = (args: string) => Number(Utils.exec(`brightnessctl ${args}`))
|
||||
const screen = await bash`ls -w1 /sys/class/backlight | head -1`
|
||||
const kbd = await bash`ls -w1 /sys/class/leds | head -1`
|
||||
|
||||
class Brightness extends Service {
|
||||
static {
|
||||
// takes three arguments
|
||||
// the class itself
|
||||
// an object defining the signals
|
||||
// an object defining its properties
|
||||
Service.register(
|
||||
this,
|
||||
{
|
||||
// 'name-of-signal': [type as a string from GObject.TYPE_<type>],
|
||||
'screen-changed': ['float'],
|
||||
},
|
||||
{
|
||||
// 'kebab-cased-name': [type as a string from GObject.TYPE_<type>, 'r' | 'w' | 'rw']
|
||||
// 'r' means readable
|
||||
// 'w' means writable
|
||||
// guess what 'rw' means
|
||||
'screen-value': ['float', 'rw'],
|
||||
},
|
||||
);
|
||||
Service.register(this, {}, {
|
||||
"screen": ["float", "rw"],
|
||||
"kbd": ["int", "rw"],
|
||||
})
|
||||
}
|
||||
|
||||
// this Service assumes only one device with backlight
|
||||
#interface = Utils.exec("sh -c 'ls -w1 /sys/class/backlight | head -1'");
|
||||
#kbdMax = get(`--device ${kbd} max`)
|
||||
#kbd = get(`--device ${kbd} get`)
|
||||
#screenMax = get("max")
|
||||
#screen = get("get") / (get("max") || 1)
|
||||
|
||||
// # prefix means private in JS
|
||||
#screenValue = 0;
|
||||
#max = Number(Utils.exec('brightnessctl max'));
|
||||
get kbd() { return this.#kbd }
|
||||
get screen() { return this.#screen }
|
||||
|
||||
// the getter has to be in snake_case
|
||||
get screen_value() {
|
||||
return this.#screenValue;
|
||||
set kbd(value) {
|
||||
if (value < 0 || value > this.#kbdMax)
|
||||
return
|
||||
|
||||
sh(`brightnessctl -d ${kbd} s ${value} -q`).then(() => {
|
||||
this.#kbd = value
|
||||
this.changed("kbd")
|
||||
})
|
||||
}
|
||||
|
||||
// the setter has to be in snake_case too
|
||||
set screen_value(percent) {
|
||||
set screen(percent) {
|
||||
if (percent < 0)
|
||||
percent = 0;
|
||||
percent = 0
|
||||
|
||||
if (percent > 1)
|
||||
percent = 1;
|
||||
percent = 1
|
||||
|
||||
Utils.execAsync(`brightnessctl set ${percent * 100}% -q`);
|
||||
// the file monitor will handle the rest
|
||||
sh(`brightnessctl set ${Math.floor(percent * 100)}% -q`).then(() => {
|
||||
this.#screen = percent
|
||||
this.changed("screen")
|
||||
})
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
super()
|
||||
|
||||
// setup monitor
|
||||
const brightness = `/sys/class/backlight/${this.#interface}/brightness`;
|
||||
Utils.monitorFile(brightness, () => this.#onChange());
|
||||
const screenPath = `/sys/class/backlight/${screen}/brightness`
|
||||
const kbdPath = `/sys/class/leds/${kbd}/brightness`
|
||||
|
||||
// initialize
|
||||
this.#onChange();
|
||||
}
|
||||
Utils.monitorFile(screenPath, async f => {
|
||||
const v = await Utils.readFileAsync(f)
|
||||
this.#screen = Number(v) / this.#screenMax
|
||||
this.changed("screen")
|
||||
})
|
||||
|
||||
#onChange() {
|
||||
this.#screenValue = Number(Utils.exec('brightnessctl get')) / this.#max;
|
||||
|
||||
// signals have to be explicitly emitted
|
||||
this.emit('changed'); // emits "changed"
|
||||
this.notify('screen-value'); // emits "notify::screen-value"
|
||||
|
||||
// or use Service.changed(propName: string) which does the above two
|
||||
// this.changed('screen-value');
|
||||
|
||||
// emit screen-changed with the percent as a parameter
|
||||
this.emit('screen-changed', this.#screenValue);
|
||||
}
|
||||
|
||||
// overwriting the connect method, let's you
|
||||
// change the default event that widgets connect to
|
||||
connect(event: string = 'screen-changed', callback: any) {
|
||||
return super.connect(event, callback);
|
||||
Utils.monitorFile(kbdPath, async f => {
|
||||
const v = await Utils.readFileAsync(f)
|
||||
this.#kbd = Number(v) / this.#kbdMax
|
||||
this.changed("kbd")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// the singleton instance
|
||||
const service = new BrightnessService;
|
||||
|
||||
// export to use in other modules
|
||||
export default service;
|
||||
export default new Brightness
|
||||
|
||||
Reference in New Issue
Block a user