quickshell-toki-night/services/Brightness.qml
Kiana Sheibani e45f412930
feat!: support exponential brightness curve
I don't need most of the fluff for handling other types of displays, and getting rid of it lets me do something a lot nicer: add an exponential-gamma brightness display.
2026-01-28 13:59:09 -05:00

93 lines
2.4 KiB
QML

pragma Singleton
pragma ComponentBehavior: Bound
import qs.config
import qs.custom
import Quickshell
import Quickshell.Io
import QtQuick
Singleton {
id: root
reloadableId: "brightness"
property real brightness: 1.0
property real maxBrightness: 0.0
function increaseBrightness(): void {
setBrightness(brightness + Config.osd.brightnessIncrement);
}
function decreaseBrightness(): void {
setBrightness(brightness - Config.osd.brightnessIncrement);
}
function setBrightness(value: real): void {
value = Math.max(0, Math.min(1, value));
if (Math.abs(brightness - value) < 0.01) return;
brightness = value;
const exp = Config.services.brightnessExp;
const raw = Math.round((value ** exp) * maxBrightness);
Quickshell.execDetached(["brightnessctl", "s", `${raw}`]);
}
Component.onCompleted: initProc.running = true
Process {
id: initProc
command: ["sh", "-c", "echo $(brightnessctl g) $(brightnessctl m)"]
stdout: StdioCollector {
onStreamFinished: {
const exp = Config.services.brightnessExp;
const [cur, max] = text.split(" ");
root.maxBrightness = parseInt(max);
root.brightness = (parseInt(cur) / root.maxBrightness) ** (1 / exp);
}
}
}
CustomShortcut {
id: brightnessUp
name: "brightnessUp"
description: "Increase brightness"
onPressed: {
root.increaseBrightness();
brightnessUpTimer.interval = 500;
brightnessUpTimer.running = true;
}
onReleased: {
brightnessUpTimer.running = false;
}
}
Timer {
id: brightnessUpTimer
repeat: true
onTriggered: {
interval = 100;
root.increaseBrightness();
}
}
CustomShortcut {
id: brightnessDown
name: "brightnessDown"
description: "Decrease brightness"
onPressed: {
root.decreaseBrightness();
brightnessDownTimer.interval = 500;
brightnessDownTimer.running = true;
}
onReleased: {
brightnessDownTimer.running = false;
}
}
Timer {
id: brightnessDownTimer
repeat: true
onTriggered: {
interval = 100;
root.decreaseBrightness();
}
}
}