perf(ags): add throttle to brightness slider

This commit is contained in:
matt1432 2023-09-12 16:44:47 -04:00
parent bb6c5c1781
commit cd38ec8c10

View file

@ -1,4 +1,4 @@
const { Box, Slider, Label, Icon } = ags.Widget; const { Box, Slider, Label, Icon, EventBox } = ags.Widget;
const { Audio } = ags.Service; const { Audio } = ags.Service;
const { exec } = ags.Utils; const { exec } = ags.Utils;
@ -10,6 +10,18 @@ const items = {
0: 'audio-volume-muted-symbolic', 0: 'audio-volume-muted-symbolic',
}; };
let throttleTimer;
const throttle = (callback, time) => {
if (throttleTimer) return;
throttleTimer = true;
setTimeout(() => {
callback();
throttleTimer = false;
}, time);
}
export const SliderBox = Box({ export const SliderBox = Box({
className: 'slider-box', className: 'slider-box',
vertical: true, vertical: true,
@ -64,13 +76,26 @@ export const SliderBox = Box({
icon: 'display-brightness-symbolic', icon: 'display-brightness-symbolic',
}), }),
Slider({ EventBox({
value: `${exec('brightnessctl get') / 2.55}`, onHover: box => box.child._canChange = false,
onChange: "brightnessctl set {}%", onHoverLost: box => box.child._canChange = true,
child: Slider({
properties: [
['canChange', true],
],
onChange: ({ value }) => {
throttle(() => exec('brightnessctl set ' + value + ' %'), 20);
},
connections: [[1000, slider => {
if (slider._canChange) {
slider.value = exec('brightnessctl get');
}
}]],
min: 0, min: 0,
max: 100, max: 255,
draw_value: false, draw_value: false,
}), }),
}),
], ],
}), }),