Files
dots-hyprland/dots/.config/quickshell/ii/services/Hyprsunset.qml
T

171 lines
5.8 KiB
QML

pragma Singleton
import QtQuick
import qs.modules.common
import Quickshell
import Quickshell.Io
import Quickshell.Hyprland
/**
* Simple hyprsunset service with automatic mode.
* In theory we don't need this because hyprsunset has a config file, but it somehow doesn't work.
* It should also be possible to control it via hyprctl, but it doesn't work consistently either so we're just killing and launching.
*/
Singleton {
id: root
property string from: Config.options?.light?.night?.from ?? "19:00"
property string to: Config.options?.light?.night?.to ?? "06:30"
property bool automatic: Config.options?.light?.night?.automatic && (Config?.ready ?? true)
property int colorTemperature: Config.options?.light?.night?.colorTemperature ?? 5000
property int gamma: 100
property bool shouldBeOn
property bool firstEvaluation: true
property bool temperatureActive: false
property int fromHour: Number(from.split(":")[0])
property int fromMinute: Number(from.split(":")[1])
property int toHour: Number(to.split(":")[0])
property int toMinute: Number(to.split(":")[1])
property int clockHour: DateTime.clock.hours
property int clockMinute: DateTime.clock.minutes
property var manualActive
property int manualActiveHour
property int manualActiveMinute
onClockMinuteChanged: reEvaluate()
onAutomaticChanged: {
root.manualActive = undefined;
root.firstEvaluation = true;
reEvaluate();
}
function inBetween(t, from, to) {
if (from < to) {
return (t >= from && t <= to);
} else {
// Wrapped around midnight
return (t >= from || t <= to);
}
}
function reEvaluate() {
const t = clockHour * 60 + clockMinute;
const from = fromHour * 60 + fromMinute;
const to = toHour * 60 + toMinute;
const manualActive = manualActiveHour * 60 + manualActiveMinute;
if (root.manualActive !== undefined && (inBetween(from, manualActive, t) || inBetween(to, manualActive, t))) {
root.manualActive = undefined;
}
root.shouldBeOn = inBetween(t, from, to);
if (firstEvaluation) {
firstEvaluation = false;
root.ensureState();
}
}
onShouldBeOnChanged: ensureState()
function ensureState() {
// console.log("[Hyprsunset] Ensuring state:", root.shouldBeOn, "Automatic mode:", root.automatic);
if (!root.automatic || root.manualActive !== undefined)
return;
if (root.shouldBeOn) {
root.enableTemperature();
} else {
root.disableTemperature();
}
}
function load() { } // Dummy to force init
function enableTemperature() {
root.temperatureActive = true;
// console.log("[Hyprsunset] Enabling");
Quickshell.execDetached(["bash", "-c", `
if pidof hyprsunset > /dev/null; then
hyprctl hyprsunset temperature ${root.colorTemperature};
else
hyprsunset --temperature ${root.colorTemperature};
fi
`]);
}
function disableTemperature() {
root.temperatureActive = false;
// console.log("[Hyprsunset] Disabling");
if (root.gamma === 100) {
Quickshell.execDetached(["bash", "-c", `pkill hyprsunset`]);
} else {
Quickshell.execDetached(["hyprctl", "hyprsunset", "identity"]);
}
}
function setGamma(gamma) {
root.gamma = Math.max(0, Math.min(100, gamma));
if (root.gamma !== 100) {
// console.log("[Hyprsunset] Enabling");
Quickshell.execDetached(["bash", "-c", `
if pidof hyprsunset > /dev/null; then
hyprctl hyprsunset gamma ${root.gamma};
else
hyprsunset --gamma ${root.gamma};
fi
`]);
} else {
if (!root.temperatureActive) {
Quickshell.execDetached(["bash", "-c", `pkill hyprsunset`]);
} else {
Quickshell.execDetached(["hyprctl", "hyprsunset", "gamma", "100"]);
}
}
}
function fetchState() {
fetchProc.running = true;
}
Process {
id: fetchProc
running: true
command: ["bash", "-c", "hyprctl hyprsunset temperature"]
stdout: StdioCollector {
id: stateCollector
onStreamFinished: {
const output = stateCollector.text.trim();
if (output.length == 0 || output.startsWith("Couldn't"))
root.temperatureActive = false;
else
root.temperatureActive = (output != "6500"); // 6500 is the default when off
// console.log("[Hyprsunset] Fetched state:", output, "->", root.temperatureActive);
}
}
}
function toggleTemperature(active = undefined) {
if (root.manualActive === undefined) {
root.manualActive = root.temperatureActive;
root.manualActiveHour = root.clockHour;
root.manualActiveMinute = root.clockMinute;
}
root.manualActive = active !== undefined ? active : !root.manualActive;
if (root.manualActive) {
root.enableTemperature();
} else {
root.disableTemperature();
}
}
// Change temp
Connections {
target: Config.options.light.night
function onColorTemperatureChanged() {
if (!root.temperatureActive) return;
Hyprland.dispatch(`hyprctl hyprsunset temperature ${Config.options.light.night.colorTemperature}`);
Quickshell.execDetached(["hyprctl", "hyprsunset", "temperature", `${Config.options.light.night.colorTemperature}`]);
}
}
}