-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.qml
More file actions
336 lines (302 loc) · 11.4 KB
/
Copy pathService.qml
File metadata and controls
336 lines (302 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import QtQuick
import Quickshell
import Quickshell.Hyprland
import Quickshell.Io
import Quickshell.Services.UPower
import Quickshell.Wayland
import "model/RulesModel.js" as RulesModel
// Context Rules engine: "when X then Y" over the desktop's own reactive
// facts. Rules live in ~/.config/omarchy/context-rules.json (watched — an
// edit applies immediately); the enabled flag persists in the settings
// state dir. Actions are asserted while a trigger holds; on assert the
// engine snapshots the actuator's prior state and on release it restores
// exactly that, so a choice the user made before the rule fired survives
// the rule. Unused triggers stay dormant (no clock when no time rule).
Item {
id: root
// Injected by the shell service loader.
property var shell: null
readonly property string rulesFile: RulesModel.rulesPath(
Quickshell.env("XDG_CONFIG_HOME"), Quickshell.env("HOME"))
readonly property string stateFile: RulesModel.statePath(
Quickshell.env("XDG_STATE_HOME"), Quickshell.env("HOME"))
readonly property string gameModeDir: RulesModel.gameModeDir(
Quickshell.env("XDG_STATE_HOME"), Quickshell.env("HOME"))
readonly property string gameModeFlag: RulesModel.gameModeFlagPath(
Quickshell.env("XDG_STATE_HOME"), Quickshell.env("HOME"))
property string rulesText: ""
property string stateText: ""
readonly property var ruleset: RulesModel.parseRules(rulesText)
readonly property bool engineEnabled: RulesModel.parseState(stateText).enabled
// Actuator services, resolved with a bounded retry (load order at shell
// startup is not guaranteed).
property var dndService: null
property var nightlightService: null
property var idleService: null
property int resolveAttempts: 0
readonly property bool servicesResolved: dndService !== null
&& nightlightService !== null && idleService !== null
// Trigger state. Sources a ruleset does not use stay off.
readonly property bool needsTime: RulesModel.needsTrigger(ruleset.rules, "time")
readonly property bool needsClass: RulesModel.needsTrigger(ruleset.rules, "window-class")
readonly property bool needsBattery: RulesModel.needsTrigger(ruleset.rules, "on-battery")
property bool screenshare: false
readonly property string activeAppId: needsClass
&& ToplevelManager.activeToplevel && ToplevelManager.activeToplevel.appId
? String(ToplevelManager.activeToplevel.appId) : ""
readonly property int minutesNow: needsTime && clock.date
? clock.hours * 60 + clock.minutes : 0
readonly property var asserted: engineEnabled
? RulesModel.computeAsserted(ruleset.rules, {
"screenshare": screenshare,
"onBattery": needsBattery && UPower.onBattery === true,
"minutes": minutesNow,
"appId": activeAppId
})
: []
property var applied: []
property var priorState: ({})
property string lastError: ""
property string pendingProfileAction: ""
onAssertedChanged: reconcile()
function resolveServices() {
var host = shell && typeof shell.serviceFor === "function" ? shell : null
if (!host) return
if (dndService === null) dndService = host.serviceFor("omarchy.notifications")
if (nightlightService === null) nightlightService = host.serviceFor("omarchy.nightlight")
if (idleService === null) idleService = host.serviceFor("omarchy.idle")
}
function reconcile() {
var diff = RulesModel.diffActions(applied, asserted)
for (var i = 0; i < diff.exit.length; i++) releaseAction(diff.exit[i])
for (var j = 0; j < diff.enter.length; j++) assertAction(diff.enter[j])
applied = asserted.slice()
}
function assertAction(action) {
var profile = RulesModel.profileFromAction(action)
if (profile !== "") {
// Snapshot the current profile first, then set; both async.
pendingProfileAction = action
run(profileGetProcess)
return
}
if (action === "dnd" && dndService && typeof dndService.setDoNotDisturb === "function") {
priorState[action] = dndService.doNotDisturb === true
dndService.setDoNotDisturb(true)
} else if (action === "night-light" && nightlightService
&& typeof nightlightService.setNightlight === "function") {
priorState[action] = nightlightService.enabled === true
nightlightService.setNightlight(true)
} else if (action === "stay-awake" && idleService
&& typeof idleService.setIdleEnabled === "function") {
priorState[action] = idleService.idleEnabled === true
idleService.setIdleEnabled(false)
} else if (action === "game-mode") {
priorState[action] = gameModeProbe.flagPresent
if (!gameModeProbe.flagPresent) run(gameModeMkdirProcess)
}
}
function releaseAction(action) {
var prior = priorState[action]
delete priorState[action]
var profile = RulesModel.profileFromAction(action)
if (profile !== "") {
if (typeof prior === "string" && prior !== "") {
profileSetProcess.command = ["powerprofilesctl", "set", prior]
run(profileSetProcess)
}
return
}
if (action === "dnd" && dndService && typeof dndService.setDoNotDisturb === "function") {
dndService.setDoNotDisturb(prior === true)
} else if (action === "night-light" && nightlightService
&& typeof nightlightService.setNightlight === "function") {
nightlightService.setNightlight(prior === true)
} else if (action === "stay-awake" && idleService
&& typeof idleService.setIdleEnabled === "function") {
idleService.setIdleEnabled(prior === true)
} else if (action === "game-mode") {
if (prior !== true && gameModeProbe.flagPresent) run(gameModeRemoveProcess)
}
}
function setEngineEnabled(value) {
stateWriter.setText(RulesModel.buildStateJson(value === true))
}
function run(proc) {
proc.exitSeen = false
proc.running = true
}
Component.onCompleted: resolveServices()
Timer {
interval: 1000
repeat: true
running: !root.servicesResolved && root.resolveAttempts < 30
onTriggered: {
root.resolveAttempts++
root.resolveServices()
if (root.servicesResolved) root.reconcile()
}
}
SystemClock {
id: clock
enabled: root.engineEnabled && root.needsTime
precision: SystemClock.Minutes
}
Connections {
target: Hyprland
enabled: root.engineEnabled
function onRawEvent(event) {
if (event.name === "screencast" || event.name === "screencastv2") {
var state = RulesModel.parseScreencast(event.data)
if (state !== null) root.screenshare = state
}
}
}
FileView {
path: root.rulesFile
printErrors: false
watchChanges: true
onLoaded: root.rulesText = text()
onLoadFailed: root.rulesText = ""
onFileChanged: reload()
}
FileView {
path: root.stateFile
printErrors: false
watchChanges: true
onLoaded: root.stateText = text()
onLoadFailed: root.stateText = ""
onFileChanged: reload()
}
FileView {
id: stateWriter
path: root.stateFile
printErrors: false
atomicWrites: true
onSaved: reload()
onSaveFailed: root.lastError = "could not write the state file"
}
// Game-mode flag interop (shared contract with community.game-mode).
FileView {
id: gameModeProbe
property bool flagPresent: false
path: root.gameModeFlag
printErrors: false
watchChanges: true
onLoaded: flagPresent = true
onLoadFailed: flagPresent = false
onFileChanged: reload()
}
Process {
id: gameModeMkdirProcess
property bool exitSeen: false
command: ["mkdir", "-p", root.gameModeDir]
onRunningChanged: if (!running && !exitSeen) root.lastError = "mkdir could not be started"
onExited: function (exitCode) {
exitSeen = true
if (exitCode === 0) gameModeWriter.setText(gameModeWriter.flagContent)
else root.lastError = "could not create the toggles directory"
}
}
FileView {
id: gameModeWriter
// The exact strip set community.game-mode writes; kept byte-identical
// so either side can remove the other's flag cleanly.
readonly property string flagContent: [
"-- Omarchy game mode (community.game-mode plugin).",
"-- This file lives in the sourced toggles directory; deleting it (or",
"-- toggling game mode off in the bar) restores your own configuration.",
"hl.config({", " animations = {", " enabled = false,", " },", "",
" general = {", " gaps_in = 0,", " gaps_out = 0,",
" allow_tearing = true,", " },", "", " decoration = {",
" rounding = 0,", "", " blur = {", " enabled = false,",
" },", "", " shadow = {", " enabled = false,", " },",
" },", "})", ""
].join("\n")
path: root.gameModeFlag
printErrors: false
atomicWrites: true
onSaved: {
reload()
root.run(gameModeReloadProcess)
}
onSaveFailed: root.lastError = "could not write the game-mode flag"
}
Process {
id: gameModeRemoveProcess
property bool exitSeen: false
command: ["rm", "-f", root.gameModeFlag]
onRunningChanged: if (!running && !exitSeen) root.lastError = "rm could not be started"
onExited: function (exitCode) {
exitSeen = true
if (exitCode === 0) root.run(gameModeReloadProcess)
else root.lastError = "could not remove the game-mode flag"
}
}
Process {
id: gameModeReloadProcess
property bool exitSeen: false
command: ["hyprctl", "reload"]
onRunningChanged: if (!running && !exitSeen) root.lastError = "hyprctl could not be started"
onExited: function (exitCode) {
exitSeen = true
if (exitCode !== 0) root.lastError = "hyprctl reload failed"
}
}
Process {
id: profileGetProcess
property bool exitSeen: false
command: ["powerprofilesctl", "get"]
onRunningChanged: if (!running && !exitSeen) root.lastError = "powerprofilesctl could not be started"
onExited: function (exitCode) {
exitSeen = true
}
stdout: StdioCollector {
onStreamFinished: {
var action = root.pendingProfileAction
root.pendingProfileAction = ""
var target = RulesModel.profileFromAction(action)
if (target === "") return
// Only snapshot once: a re-assert while already applied must not
// capture our own value as "prior".
if (root.priorState[action] === undefined)
root.priorState[action] = text.trim()
profileSetProcess.command = ["powerprofilesctl", "set", target]
root.run(profileSetProcess)
}
}
}
Process {
id: profileSetProcess
property bool exitSeen: false
command: ["powerprofilesctl", "set", "balanced"]
onRunningChanged: if (!running && !exitSeen) root.lastError = "powerprofilesctl could not be started"
onExited: function (exitCode) {
exitSeen = true
if (exitCode !== 0) root.lastError = "powerprofilesctl set failed"
}
}
// Scriptable surface:
// omarchy-shell community.context-rules status | enable | disable
IpcHandler {
target: "community.context-rules"
function status(): string {
return JSON.stringify({
"enabled": root.engineEnabled,
"asserted": root.asserted,
"rules": root.ruleset.rules.length,
"source": root.ruleset.source,
"screenshare": root.screenshare,
"error": root.lastError
})
}
function enable(): string {
root.setEngineEnabled(true)
return "enabled"
}
function disable(): string {
root.setEngineEnabled(false)
return "disabled"
}
}
}