Files
David Heinemeier HanssonandClaude Fable 5 ba91bad3a4 Move Night Light onto a first-party nightlight service
Same treatment as Stay Awake: the indicator polled the toggle CLI over
a Process with a timer to paper over the race after clicking, and the
CLI ended by asking the shell to refresh every indicator over IPC. A new
omarchy.nightlight service owns hyprsunset instead - it probes the
temperature on startup, applies changes itself for in-shell toggles, and
answers on the nightlight IPC target. The indicator becomes a plain
binding. The CLI still drives hyprctl directly so keybindings, the menu,
and ssh work without the shell, but now just nudges the service to
re-probe since hyprsunset has no state file to watch.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-21 15:26:20 -07:00

116 lines
2.7 KiB
QML

import QtQuick
import Quickshell.Io
import "NightlightModel.js" as NightlightModel
Item {
id: root
// Injected by omarchy-shell (the first-party service loader).
property var shell: null
// Keep in sync with bin/omarchy-toggle-nightlight, which sets the same
// temperatures for callers outside the shell (keybindings, menu, ssh).
readonly property int nightTemperature: 4000
readonly property int dayTemperature: 6500
property bool stateLoaded: false
property var temperature: null
readonly property bool enabled: stateLoaded && NightlightModel.isNightlight(temperature)
property bool hasPendingTemperature: false
property int pendingTemperature: 0
function refresh() {
if (!statusProbe.running) statusProbe.running = true
}
function setNightlight(value) {
applyTemperature(value ? nightTemperature : dayTemperature)
}
function toggle() {
setNightlight(!enabled)
}
function applyTemperature(temp) {
root.temperature = temp
root.stateLoaded = true
if (applyProcess.running) {
root.pendingTemperature = temp
root.hasPendingTemperature = true
return
}
runApply(temp)
}
function runApply(temp) {
applyProcess.command = ["bash", "-lc",
"pgrep -x hyprsunset >/dev/null || { setsid uwsm-app -- hyprsunset >/dev/null 2>&1 & sleep 1; }; " +
"hyprctl hyprsunset temperature " + Number(temp)]
applyProcess.running = true
}
Process {
id: statusProbe
command: ["hyprctl", "hyprsunset", "temperature"]
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: {
root.temperature = NightlightModel.temperatureFromOutput(text)
root.stateLoaded = true
}
}
onExited: function(exitCode) {
if (exitCode !== 0) {
root.temperature = null
root.stateLoaded = true
}
}
}
Process {
id: applyProcess
onExited: function() {
if (root.hasPendingTemperature) {
root.hasPendingTemperature = false
root.runApply(root.pendingTemperature)
return
}
root.refresh()
}
}
Component.onCompleted: refresh()
IpcHandler {
target: "nightlight"
function status(): string {
return JSON.stringify({ enabled: root.enabled, temperature: root.temperature })
}
function refresh(): void {
root.refresh()
}
function enable(): string {
root.setNightlight(true)
return "enabled"
}
function disable(): string {
root.setNightlight(false)
return "disabled"
}
function toggle(): string {
var enabling = !root.enabled
root.setNightlight(enabling)
return enabling ? "enabled" : "disabled"
}
}
}