feat(ags brightness): add changing screen brightness icon

This commit is contained in:
matt1432 2023-12-07 17:25:27 -05:00
parent 32b1130779
commit e9e304aafb
4 changed files with 33 additions and 3 deletions

View file

@ -6,6 +6,11 @@ import Separator from '../../misc/separator.js';
const SPACING = 5; const SPACING = 5;
const Indicator = (props) => Icon({
...props,
binds: [['icon', Brightness, 'screen-icon']],
});
const BrightnessPercentLabel = (props) => Label({ const BrightnessPercentLabel = (props) => Label({
...props, ...props,
connections: [[Brightness, (self) => { connections: [[Brightness, (self) => {
@ -34,7 +39,7 @@ export default () => {
child: Box({ child: Box({
className: 'brightness', className: 'brightness',
children: [ children: [
Icon('display-brightness-symbolic'), Indicator(),
rev, rev,
], ],
}), }),

View file

@ -32,7 +32,7 @@ export const SpeakerOSD = (stack) => OSD({
export const ScreenBrightnessOSD = (stack) => OSD({ export const ScreenBrightnessOSD = (stack) => OSD({
stack, stack,
icon: 'display-brightness-symbolic', icon: { binds: [['icon', Brightness, 'screen-icon']] },
info: { info: {
mod: Brightness, mod: Brightness,
signal: 'screen', signal: 'screen',

View file

@ -58,7 +58,7 @@ export default () => Box({
children: [ children: [
Icon({ Icon({
className: 'slider-label', className: 'slider-label',
icon: 'display-brightness-symbolic', binds: [['icon', Brightness, 'screen-icon']],
}), }),
Slider({ Slider({

View file

@ -5,6 +5,12 @@ import { exec, execAsync } from 'resource:///com/github/Aylur/ags/utils.js';
const KBD = 'tpacpi::kbd_backlight'; const KBD = 'tpacpi::kbd_backlight';
const CAPS = 'input0::capslock'; const CAPS = 'input0::capslock';
const INTERVAL = 500; const INTERVAL = 500;
const SCREEN_ICONS = {
90: 'display-brightness-high-symbolic',
70: 'display-brightness-medium-symbolic',
20: 'display-brightness-low-symbolic',
5: 'display-brightness-off-symbolic',
};
class Brightness extends Service { class Brightness extends Service {
static { static {
@ -13,12 +19,14 @@ class Brightness extends Service {
kbd: ['float'], kbd: ['float'],
caps: ['int'], caps: ['int'],
}, { }, {
'screen-icon': ['string', 'rw'],
'caps-icon': ['string', 'rw'], 'caps-icon': ['string', 'rw'],
}); });
} }
#kbd = 0; #kbd = 0;
#screen = 0; #screen = 0;
#screenIcon = 'display-brightness-symbolic';
#caps = 0; #caps = 0;
#capsIcon = 'caps-lock-symbolic'; #capsIcon = 'caps-lock-symbolic';
@ -30,6 +38,10 @@ class Brightness extends Service {
return this.#screen; return this.#screen;
} }
get screenIcon() {
return this.#screenIcon;
}
get caps() { get caps() {
return this.#caps; return this.#caps;
} }
@ -55,6 +67,7 @@ class Brightness extends Service {
execAsync(`brightnessctl s ${percent * 100}% -q`) execAsync(`brightnessctl s ${percent * 100}% -q`)
.then(() => { .then(() => {
this.#screen = percent; this.#screen = percent;
this.#getScreenIcon();
this.emit('screen', this.#screen); this.emit('screen', this.#screen);
}) })
.catch(console.error); .catch(console.error);
@ -73,6 +86,18 @@ class Brightness extends Service {
} }
} }
#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');
}
}
}
fetchCapsState() { fetchCapsState() {
execAsync(`brightnessctl -d ${CAPS} g`) execAsync(`brightnessctl -d ${CAPS} g`)
.then((out) => { .then((out) => {