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>
This commit is contained in:
co-authored by
Claude Fable 5
parent
61b7cd1c12
commit
ba91bad3a4
@@ -32,6 +32,7 @@ User-installed plugins live alongside these conceptually but on disk under
|
||||
| Media | `omarchy.media` | `service`, `bar-widget` | `services/media/Service.qml`, `services/media/BarWidget.qml` |
|
||||
| Battery | `omarchy.battery` | `service` | `services/battery/Service.qml` |
|
||||
| Idle | `omarchy.idle` | `service` | `services/idle/Service.qml` |
|
||||
| Night light | `omarchy.nightlight` | `service` | `services/nightlight/Service.qml` |
|
||||
| Lock screen | `omarchy.lock` | `service` | `lock/Service.qml` |
|
||||
| OSD | `omarchy.osd` | `panel` | `osd/Osd.qml` |
|
||||
| Polkit agent | `omarchy.polkit` | `service` | `polkit/PolkitAgent.qml` |
|
||||
|
||||
@@ -1,57 +1,19 @@
|
||||
import QtQuick
|
||||
import Quickshell.Io
|
||||
import qs.Ui
|
||||
|
||||
BarIndicator {
|
||||
id: root
|
||||
|
||||
property bool nightlight: false
|
||||
readonly property var nightlightService: bar?.shell?.firstPartyServiceFor("omarchy.nightlight")
|
||||
|
||||
active: nightlight
|
||||
active: nightlightService ? nightlightService.enabled : false
|
||||
activeText: ""
|
||||
inactiveText: ""
|
||||
activeTooltipText: "Day Light"
|
||||
inactiveTooltipText: "Night Light"
|
||||
|
||||
function refresh() {
|
||||
if (!statusProc.running) statusProc.running = true
|
||||
}
|
||||
|
||||
function update(raw) {
|
||||
var data = extractData(raw)
|
||||
nightlight = data && data.enabled === true
|
||||
}
|
||||
|
||||
function toggle() {
|
||||
nightlight = !nightlight
|
||||
if (root.bar) root.bar.run("omarchy-toggle-nightlight")
|
||||
refreshTimer.restart()
|
||||
}
|
||||
|
||||
Component.onCompleted: refresh()
|
||||
|
||||
Connections {
|
||||
target: root.indicatorHost
|
||||
ignoreUnknownSignals: true
|
||||
function onRefreshRequested() { root.refresh() }
|
||||
}
|
||||
|
||||
Process {
|
||||
id: statusProc
|
||||
command: ["omarchy-toggle-nightlight", "--status"]
|
||||
stdout: StdioCollector {
|
||||
waitForEnd: true
|
||||
onStreamFinished: root.update(text)
|
||||
}
|
||||
onExited: function(exitCode) {
|
||||
if (exitCode !== 0) root.nightlight = false
|
||||
}
|
||||
}
|
||||
|
||||
Timer {
|
||||
id: refreshTimer
|
||||
interval: 1500
|
||||
onTriggered: root.refresh()
|
||||
if (root.nightlightService) root.nightlightService.setNightlight(!root.active)
|
||||
}
|
||||
|
||||
onPressed: function() { root.toggle() }
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// Temperatures below the identity point count as night light. Keep in sync
|
||||
// with bin/omarchy-toggle-nightlight, which applies the same threshold.
|
||||
var IDENTITY_TEMPERATURE = 6000
|
||||
|
||||
function temperatureFromOutput(output) {
|
||||
var match = String(output === undefined || output === null ? "" : output).match(/[0-9]+/)
|
||||
return match ? Number(match[0]) : null
|
||||
}
|
||||
|
||||
function isNightlight(temperature) {
|
||||
return temperature !== null && temperature !== undefined && temperature < IDENTITY_TEMPERATURE
|
||||
}
|
||||
|
||||
if (typeof module !== "undefined") {
|
||||
module.exports = {
|
||||
IDENTITY_TEMPERATURE: IDENTITY_TEMPERATURE,
|
||||
temperatureFromOutput: temperatureFromOutput,
|
||||
isNightlight: isNightlight
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
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"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "omarchy.nightlight",
|
||||
"name": "Night Light",
|
||||
"version": "1.0.0",
|
||||
"author": "Omarchy",
|
||||
"description": "Owns the hyprsunset night light temperature for the bar indicator and CLI.",
|
||||
"kinds": [
|
||||
"service"
|
||||
],
|
||||
"entryPoints": {
|
||||
"service": "Service.qml"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user