rags/.config/ags/brightness.js (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
class BrightnessService extends Service {
// every subclass of GObject.Object has to register itself
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'],
},
);
}
// this Service assumes only one device with backlight
#interface = Utils.exec("sh -c 'ls -w1 /sys/class/backlight | head -1'");
// # prefix means private in JS
#screenValue = 0;
#max = Number(Utils.exec('brightnessctl max'));
// 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) {
if (percent < 0)
percent = 0;
if (percent > 1)
percent = 1;
Utils.execAsync(`brightnessctl set ${percent * 100}% -q`);
// the file monitor will handle the rest
}
constructor() {
super();
// setup monitor
const brightness = `/sys/class/backlight/${this.#interface}/brightness`;
Utils.monitorFile(brightness, () => this.#onChange());
// initialize
this.#onChange();
}
#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 = 'screen-changed', callback) {
return super.connect(event, callback);
}
}
// the singleton instance
const service = new BrightnessService;
// export to use in other modules
export default service;
|