Repository navigation
Expand file tree
/
Copy pathService.qml
More file actions
1064 lines (950 loc) · 42.1 KB
/
Copy pathService.qml
File metadata and controls
1064 lines (950 loc) · 42.1 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Notification service for the omarchy shell.
import QtQuick
import QtQuick.Layouts
import Quickshell
import Quickshell.Io
import Quickshell.Wayland
import Quickshell.Services.Notifications
import qs.Commons
import "components"
import "NotificationLogic.js" as NotificationLogic
Item {
id: service
// Injected by omarchy-shell (the first-party service loader).
property var shell: null
property string omarchyPath: Quickshell.env("OMARCHY_PATH")
readonly property string home: Quickshell.env("HOME")
// History + DND live under XDG_STATE_HOME: they're persistent user state
// (the notifications received, the last-set DND preference), not
// regeneratable cache that a `rm -rf ~/.cache` should wipe.
readonly property string stateDir: home + "/.local/state/omarchy/"
readonly property string settingsPath: stateDir + "notifications.json"
// One file per on-screen popup, so live toasts survive shell restarts.
// A file exists exactly as long as its popup is showing: written when the
// toast appears, moved into historyDir when it expires, is dismissed, or is
// acted upon.
readonly property string popupStateDir: stateDir + "notifications/"
// The notifications that already left the screen, one file each, trimmed to
// the newest historyLimit. This directory IS the history: `showHistory`
// replays exactly what has been moved in here.
readonly property string historyDir: popupStateDir + "history/"
// Copies of the avatars/images persisted entries reference — the sender's
// originals don't outlive the notification (see persistablePopup). Each
// copy lives and dies with the JSON file whose stem it carries.
readonly property string imagesDir: popupStateDir + "images/"
// Corner radius is shared with the menu and shell panels.
// It mirrors Hyprland's current decoration:rounding value.
readonly property int cornerRadius: Style.cornerRadius
// Toasts are fixed to the top-right corner. They only clear the omarchy bar
// when the bar occupies the top or right edge, so left/bottom bars do not
// pull notification popups away from the expected top-right location.
// Falls back to the bar's default size (26 horizontal / 28 vertical) when
// shell.bar isn't reachable so the popup never lands on top of the bar.
readonly property string barPosition: shell && shell.barConfig ? String(shell.barConfig.position || "top") : "top"
readonly property bool barVertical: barPosition === "left" || barPosition === "right"
readonly property int defaultBarSize: barVertical ? Style.bar.sizeVertical : Style.bar.sizeHorizontal
readonly property int liveBarSize: shell && shell.bar && !shell.bar.barHidden ? Math.max(0, shell.bar.barSize) : defaultBarSize
readonly property int barClearance: liveBarSize + Style.gapsOut
// Live Notification objects by originalId, kept OUT of the ListModels: a
// QObject stored in a model role becomes a dangling C++ pointer when the
// server destroys the notification (sender close, DND untrack, dismiss),
// and the next read of that role segfaults in QQmlListModel::data. A JS
// map only holds a wrapper, which degrades to a catchable error instead.
property var liveRefs: ({})
// PersistentProperties handles in-process QML reloads. The on-disk
// notifications.json file is the cross-restart backstop — its `dnd` key
// is hydrated into persisted.doNotDisturb on startup and written back via
// a debounced save timer.
PersistentProperties {
id: persisted
reloadableId: "omarchy-notifications"
property bool doNotDisturb: false
onDoNotDisturbChanged: {
// Suppress the write that load-time hydration would otherwise trigger.
if (service._hydrating) return
service.scheduleSettingsSave()
}
}
// Guards onDoNotDisturbChanged while we're hydrating from disk so the
// hydration assignment doesn't immediately schedule a write-back.
property bool _hydrating: false
readonly property alias doNotDisturb: persisted.doNotDisturb
function setDoNotDisturb(value) {
persisted.doNotDisturb = !!value
}
// popupModel feeds the on-screen toast stack — the only model the service
// keeps. Everything a toast leaves behind lives on disk under historyDir.
//
// Aliased as a property so consumers outside this Item's id scope can bind
// to it. QML ids aren't visible to external consumers without the alias.
property alias popupModel: popupModel
ListModel { id: popupModel }
// How many notifications the history directory keeps, and therefore how
// many `showHistory` can replay.
readonly property int historyLimit: 10
readonly property int lowPopupDuration: 5000
readonly property int normalPopupDuration: 8000
readonly property int maxPopupDuration: 30000
function durationFor(urgency, expireTimeout) {
switch (urgency) {
case NotificationUrgency.Critical:
return 0
case NotificationUrgency.Low:
return Math.min(maxPopupDuration, Math.max(lowPopupDuration, requestedDuration(expireTimeout)))
default:
return Math.min(maxPopupDuration, Math.max(normalPopupDuration, requestedDuration(expireTimeout)))
}
}
function requestedDuration(expireTimeout) {
// FreeDesktop notification spec (and Quickshell) report expireTimeout in
// milliseconds, so pass it through directly.
var ms = Number(expireTimeout || 0)
if (!isFinite(ms) || ms <= 0) return 0
return Math.round(ms)
}
// DND bypass: only let through notifications we trust to be intentional
// and rare.
// - omarchy-action: a user-action confirmation toast ("Theme changed",
// "Screenshot saved"). The user JUST did something — their feedback
// should show.
// - urgency=critical AND app_name=notify-send: bare-CLI emergency alerts.
// Trusted because it's almost always omarchy or system shell scripts —
// chat apps set app_name to their brand (Discord/Slack/Vesktop), which
// falls outside this rule.
function shouldBypassDnd(notification) {
return NotificationLogic.shouldBypassDnd(notification, NotificationUrgency.Critical)
}
function snapshotOf(notification) {
return NotificationLogic.snapshotOf(notification, Date.now())
}
// A notification nobody looks back at:
// - the freedesktop `transient` hint is set ("popup only, don't store")
// - app_name is "notify-send" (the CLI default — means the sender
// didn't bother declaring an identity, so it's almost certainly
// ephemeral test/feedback noise)
// - app_name is "omarchy-action" (Omarchy's own user-action toasts —
// the user just triggered them)
// Their toasts still land in history like any other once they've been on
// screen; the distinction only decides whether a DND-silenced one is worth
// recording at all.
function isEphemeral(notification) {
var transient = false
try {
transient = !!(notification.hints && notification.hints["transient"])
} catch (e) { transient = false }
return transient || NotificationLogic.isEphemeralApp(String(notification.appName || ""))
}
function handleNotification(notification) {
// Without `tracked = true` the Notification object is destroyed as soon
// as this signal handler returns, which would null out the `ref` we just
// captured for the popup card.
notification.tracked = true
var snapshot = snapshotOf(notification)
liveRefs[snapshot.originalId] = notification
// Guard the delete: a newer notification may have reused this originalId
// (freedesktop replaces_id) and taken over the map slot.
notification.closed.connect(function() {
if (service.liveRefs[snapshot.originalId] === notification)
delete service.liveRefs[snapshot.originalId]
})
// DND bypass rules: chat apps abuse urgency=critical to force
// visibility, so critical alone isn't enough — we also require the
// sender to be CLI-style. See shouldBypassDnd().
if (service.doNotDisturb && !shouldBypassDnd(notification)) {
// The toast never shows, so the only record a silenced notification
// can leave is a history entry. Write it straight into history —
// "what did I miss while silenced" is exactly what history is for.
if (!isEphemeral(notification)) {
writeSilenced(notification, snapshot)
return
}
delete liveRefs[snapshot.originalId]
notification.tracked = false
return
}
persistPopupFile(snapshot)
watchForUpdates(notification, snapshot)
// Qt.callLater avoids "QV4::Object::insertMember" crashes when a
// Repeater is mid-incubation while we mutate its model.
Qt.callLater(function() {
removePopupsByOriginalId(snapshot.originalId, NotificationLogic.popupFileName(snapshot))
popupModel.insert(0, snapshot)
// An update that arrived while the insert was deferred found no row to
// write to, and a property that already changed will not change again.
// Reading the object once the row exists catches up on it.
service.refreshPopup(notification, snapshot.originalId, snapshot.timestamp)
})
}
// Persist a silenced notification, held tracked until its content is
// stable: untracking tells the sender its notification closed (Chromium
// then deletes its avatar file), and a replaces_id update lands on this
// object without a second onNotification — releasing on a stale snapshot
// would drop it. Each catch-up write reuses the original file identity.
function writeSilenced(notification, written) {
writeHistoryFile(written, function() {
var updated = null
try {
updated = NotificationLogic.replacementSnapshot(notification, written.originalId, written.timestamp)
} catch (e) {
// Torn down by the server while the write was queued.
}
if (updated && NotificationLogic.popupRowChanged(written, updated)) {
service.writeSilenced(notification, updated)
return
}
service.releaseSilenced(notification, written.originalId)
})
}
// Let go of a DND-silenced notification once its history write has run.
// The id may have been reused and the object torn down meanwhile.
function releaseSilenced(notification, originalId) {
if (liveRefs[originalId] === notification) delete liveRefs[originalId]
try {
notification.tracked = false
} catch (e) {
// Object already destroyed by the server — nothing left to release.
}
}
// Everything the card draws. A change to any of these is a client updating
// the notification in place, which is the only kind of update we ever hear
// about after the popup exists.
readonly property var updateSignals: [
"summaryChanged", "bodyChanged", "appNameChanged", "appIconChanged",
"imageChanged", "urgencyChanged", "expireTimeoutChanged", "hintsChanged"
]
// A client that updates a notification through replaces_id does not produce
// a second onNotification: the server writes the new content onto the object
// we are already holding. The card draws a snapshot copied out of that
// object — deliberately, since the object itself must stay out of the model
// — so nothing reaches the screen until we copy it again.
function watchForUpdates(notification, snapshot) {
function refresh() {
service.refreshPopup(notification, snapshot.originalId, snapshot.timestamp)
}
for (var i = 0; i < updateSignals.length; i++) {
var signal = notification[updateSignals[i]]
if (signal && typeof signal.connect === "function") signal.connect(refresh)
}
}
function refreshPopup(notification, originalId, timestamp) {
// A newer notification may have taken this id over, and the object may
// outlive its popup — in both cases there is nothing here to refresh.
if (service.liveRefs[originalId] !== notification) return
var updated
try {
updated = NotificationLogic.replacementSnapshot(notification, originalId, timestamp)
} catch (e) {
// Object torn down by the server while the signal was in flight.
return
}
var roles = NotificationLogic.popupRoles()
for (var i = 0; i < popupModel.count; i++) {
var row = popupModel.get(i)
if (!row || row.originalId !== originalId || row.timestamp !== timestamp) continue
if (!NotificationLogic.popupRowChanged(row, updated)) return
for (var r = 0; r < roles.length; r++) popupModel.setProperty(i, roles[r], updated[roles[r]])
// The file name is the timestamp and id this popup was persisted under,
// so the rewrite lands on the same file: a restart restores the version
// last shown, and so does the copy that ends up in history.
persistPopupFile(updated)
return
}
}
// A restored row carries an id from the previous server generation, and
// the new server hands out ids from 1 again — so a fresh notification
// with the same originalId is a coincidence, not the same notification.
// The timestamp (via the file name) disambiguates: it travels with the
// row through every model and file round-trip.
function isRestoredRow(row) {
return !!row && !!restoredPopups[NotificationLogic.popupFileName(row)]
}
// A notification arriving under an originalId a popup on screen already
// holds supersedes it, so that row leaves the screen. Its file is deleted
// rather than archived: the row taking its place archives itself when it
// goes, and history would otherwise hold two entries for what the sender
// means as one notification.
// keepFileName is the replacement's own file: a same-millisecond
// replacement shares the replaced row's filename, and the new write is
// already queued — deleting that path here would erase the replacement's
// only file.
function removePopupsByOriginalId(originalId, keepFileName) {
for (var i = popupModel.count - 1; i >= 0; i--) {
var row = popupModel.get(i)
if (!row || row.originalId !== originalId) continue
// Not a replaces_id match — see isRestoredRow. Removing it here
// would silently kill a restored critical alert on an unrelated ping.
if (isRestoredRow(row)) continue
if (NotificationLogic.popupFileName(row) !== keepFileName) deletePopupFileFor(row)
popupModel.remove(i)
}
}
function dismissPopup(index) {
removePopup(index, "dismiss")
}
function expirePopup(index) {
removePopup(index, "expire")
}
function removePopup(index, reason) {
if (index < 0 || index >= popupModel.count) return
var entry = popupModel.get(index)
var originalId = entry ? entry.originalId : -1
// A restored row has no live server object, and its old-generation id
// may meanwhile belong to a fresh notification — resolving liveRefs by
// id would dismiss that unrelated notification at the server.
var restored = isRestoredRow(entry)
var ref = !restored && originalId >= 0 ? liveRefs[originalId] : null
// The popup is leaving the screen — for any reason — so its file must not
// survive to the next shell restart. It becomes the newest history entry
// instead. Rows that never had a file (a history replay, the empty-history
// placeholder) archive to nothing, which the move tolerates.
if (entry) {
archivePopupFileFor(entry)
if (restored) delete restoredPopups[NotificationLogic.popupFileName(entry)]
}
popupModel.remove(index)
if (ref) {
try {
if (ref.tracked) {
if (reason === "expire" && typeof ref.expire === "function") ref.expire()
else ref.dismiss()
}
} catch (e) {
// Object already torn down by the server — nothing to dismiss.
}
}
}
function clearPopups() {
while (popupModel.count > 0) dismissPopup(0)
}
// Run the popup's click action, then dismiss. Omarchy's own toasts carry the
// action as an argv vector in the `execArgv` role (see execArgvFromHints),
// which the persistence files preserve, so restored toasts stay clickable.
// Third-party clients register a libnotify action under the canonical
// identifier "default" instead; that one only works while the sender is live.
function invokePopupDefault(index) {
if (index < 0 || index >= popupModel.count) return
var entry = popupModel.get(index)
// Run the argv (via Util.execArgv, no shell interpretation). Detached so it
// outlives the shell, which installer toasts depend on: they restart it.
var argv = NotificationLogic.parseExecArgv(entry ? entry.execArgv : "")
if (argv) {
Util.execArgv(argv)
dismissPopup(index)
return
}
// Restored rows have no live actions, and looking up liveRefs by their
// old-generation id could fire an unrelated fresh notification's action.
var ref = entry && !isRestoredRow(entry) ? liveRefs[entry.originalId] : null
var invoked = false
try {
if (ref && ref.actions) {
for (var i = 0; i < ref.actions.length; i++) {
var action = ref.actions[i]
if (action && action.identifier === "default") {
action.invoke()
invoked = true
break
}
}
}
} catch (e) {
// Notification already torn down by the server — fall through to focus.
console.warn("invoke default failed:", e)
}
// Chat apps (Slack, Discord, Vesktop, etc.) rarely register a "default"
// libnotify action — they just expect clicking the notification to
// focus their window. Fall back to focusing the sending app by class so
// that click-to-jump actually works.
if (!invoked) focusApp(entry)
dismissPopup(index)
}
// Try to focus an existing Hyprland window matching the notification's
// sender. The helper handles case-insensitive class matching.
function focusApp(entry) {
if (!entry || !entry.app) return
focusAppProc.command = [
service.omarchyPath + "/bin/omarchy-hyprland-focus-app",
String(entry.app)
]
focusAppProc.running = true
}
Process { id: focusAppProc; running: false }
Process {
id: ensureDirsProc
command: ["mkdir", "-p", service.stateDir, service.popupStateDir, service.historyDir, service.imagesDir]
running: false
}
// ---------------------------------------------------- popup persistence
//
// Mirror every on-screen popup to its own file under popupStateDir so
// toasts survive shell restarts (notably the restart `omarchy-update`
// performs). Writes, moves and deletes go through one serialized queue: a
// burst of replaces_id updates must not race a single reused Process, and
// ordering guarantees a delete issued after a write wins.
// Popups restored from a previous shell process, keyed by their file
// name (timestamp-originalId) since ids alone repeat across server
// generations. The replaces_id handling and liveRefs lookups must not
// match these rows against fresh notifications.
property var restoredPopups: ({})
// Entries are either { command, done } for a file job or { read: true } for
// a replay's directory read. Queueing the read rather than running it beside
// the queue is what makes it a barrier: it takes its place in line, so the
// history it sees is the one that existed when the replay was asked for.
// Everything queued after it — a clear, an archive, a silenced write — waits
// for it, and no amount of later traffic can push it back.
property var popupFileQueue: []
// Done callback of the job popupFileProc is currently running.
property var runningPopupFileJobDone: null
function enqueuePopupFileJob(command, done) {
popupFileQueue = popupFileQueue.concat([{ command: command, done: done || null }])
runNextPopupFileJob()
}
function enqueueHistoryRead() {
popupFileQueue = popupFileQueue.concat([{ read: true }])
runNextPopupFileJob()
}
function runNextPopupFileJob() {
if (readHistoryProc.running || popupFileProc.running) return
if (popupFileQueue.length === 0) return
var job = popupFileQueue[0]
popupFileQueue = popupFileQueue.slice(1)
if (job.read) {
startHistoryRead()
return
}
popupFileProc.command = job.command
service.runningPopupFileJobDone = job.done || null
popupFileProc.running = true
}
Process {
id: popupFileProc
running: false
onExited: {
var done = service.runningPopupFileJobDone
service.runningPopupFileJobDone = null
if (done) {
try {
done()
} catch (e) {
console.warn("notifications: file job callback failed:", e)
}
}
service.runNextPopupFileJob()
}
}
// Consumes the remaining args as from/to pairs. Bounded read into a temp
// file, validated, then renamed into place: the source path is
// sender-controlled and may grow, block, or become a FIFO mid-copy, and
// must neither hang the serialized queue nor fill the state dir.
readonly property string copyImagesScript:
"while (( $# >= 2 )); do\n" +
" if [[ -f $1 ]] && timeout 5 head -c 5242881 -- \"$1\" > \"$2.tmp\" 2>/dev/null &&\n" +
" (( $(stat -c%s -- \"$2.tmp\") <= 5242880 )); then mv -f -- \"$2.tmp\" \"$2\"; else rm -f -- \"$2.tmp\"; fi\n" +
" shift 2\n" +
"done\n"
function persistPopupFile(snapshot) {
// The JSON travels as an argument, not through shell interpolation, so
// summaries/bodies with quotes or backticks can't break the command. The
// mkdir guards notifications that arrive before ensureDirsProc has run.
// Copies run before the JSON referencing them, while the source exists.
var persistable = NotificationLogic.persistablePopup(snapshot, imagesDir)
var command = ["bash", "-c",
"mkdir -p \"$1\" \"$2\" || exit 0\n" +
"dir=\"$1\" json=\"$3\" name=\"$4\"\n" +
"shift 4\n" +
copyImagesScript +
"printf '%s\\n' \"$json\" > \"$dir/$name\"", "--",
popupStateDir,
imagesDir,
NotificationLogic.serializePopup(persistable.entry, NotificationUrgency.Normal),
NotificationLogic.popupFileName(snapshot)]
for (var i = 0; i < persistable.copies.length; i++)
command.push(persistable.copies[i].from, persistable.copies[i].to)
enqueuePopupFileJob(command)
}
function deletePopupFileFor(row) {
if (!row) return
// History replays and the "no recent notifications" placeholder never
// had a file — rm -f on the computed paths is a harmless no-op there.
enqueuePopupFileJob(["bash", "-c",
"rm -f \"$1/$2.json\" \"$3/$2\"-*", "--",
popupStateDir, NotificationLogic.imageStem(row), imagesDir])
}
// ---------------------------------------------------- history
//
// A popup that leaves the screen keeps its file — it just moves one level
// down, into historyDir. Trimming happens right there in the same shell
// job: the names sort numerically by their leading millisecond timestamp,
// so everything but the newest historyLimit files is the tail to drop,
// image copies included. Callers set $hist, $limit and $imgs first.
readonly property string trimHistoryScript:
"ls -1 \"$hist\" 2>/dev/null | sort -n | head -n \"-$limit\" | while IFS= read -r stale; do rm -f \"$hist/$stale\" \"$imgs/${stale%.json}\"-*; done"
function archivePopupFileFor(row) {
if (!row) return
// A history replay or the empty-history placeholder has no file to move;
// the failed mv leaves the history untouched, trimming included. Image
// copies stay put — live and archived entries share imagesDir.
enqueuePopupFileJob(["bash", "-c",
"mkdir -p \"$1\" || exit 0\n" +
"hist=\"$1\" limit=\"$2\" imgs=\"$5\"\n" +
"mv -f \"$4/$3\" \"$1/$3\" 2>/dev/null || exit 0\n" +
trimHistoryScript, "--",
historyDir,
String(historyLimit),
NotificationLogic.popupFileName(row),
popupStateDir,
imagesDir])
}
// Record a notification that never made it to the screen (DND silenced it),
// straight into history. Same file format as an archived popup, so the
// replay can't tell the two apart.
//
// A silenced notification is untracked the moment it arrives, so the server
// has nothing left for a later replaces_id to replace and hands the sender a
// fresh id instead. Every update from a chatty thread is therefore its own
// notification here, and several can sit in the ten slots together — there
// is no id to recognize them by, and guessing from app and summary would
// merge genuinely separate messages.
function writeHistoryFile(entry, done) {
if (!entry) {
if (done) done()
return
}
var persistable = NotificationLogic.persistablePopup(entry, imagesDir)
var command = ["bash", "-c",
"mkdir -p \"$1\" \"$5\" || exit 0\n" +
"hist=\"$1\" limit=\"$2\" name=\"$3\" json=\"$4\" imgs=\"$5\"\n" +
"shift 5\n" +
copyImagesScript +
"printf '%s\\n' \"$json\" > \"$hist/$name\" || exit 0\n" +
trimHistoryScript, "--",
historyDir,
String(historyLimit),
NotificationLogic.popupFileName(entry),
NotificationLogic.serializePopup(persistable.entry, NotificationUrgency.Normal),
imagesDir]
for (var i = 0; i < persistable.copies.length; i++)
command.push(persistable.copies[i].from, persistable.copies[i].to)
enqueuePopupFileJob(command, done)
}
function clearHistory() {
enqueuePopupFileJob(["bash", "-c",
"for f in \"$1\"/*.json; do\n" +
" [[ -e $f ]] || continue\n" +
" stale=\"${f##*/}\"\n" +
" rm -f \"$f\" \"$2/${stale%.json}\"-*\n" +
"done", "--", historyDir, imagesDir])
}
// A restart can kill a queued job between its cp and its JSON write,
// leaving copies no JSON-derived cleanup can name. Swept at startup,
// through the queue so in-flight copies aren't mistaken for orphans.
function sweepOrphanImages() {
enqueuePopupFileJob(["bash", "-c",
"for img in \"$3\"/*; do\n" +
" [[ -e $img ]] || continue\n" +
" [[ $img == *.tmp ]] && { rm -f -- \"$img\"; continue; }\n" +
" stem=\"${img##*/}\"\n" +
" stem=\"${stem%-*}\"\n" +
" [[ -e $1/$stem.json || -e $2/$stem.json ]] || rm -f \"$img\"\n" +
"done", "--", popupStateDir, historyDir, imagesDir])
}
Process {
id: readHistoryProc
running: false
// Let the file queue go again, whatever the read did — a failed or empty
// read must not leave archives and clears parked behind it forever.
onExited: service.runNextPopupFileJob()
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: service.replayHistory(text)
}
}
// Toasts that were on screen when the replay was asked for. The clear in
// replayHistory archives them, but the directory read is already in flight
// by then, so they're handed over in memory instead of being waited for.
property var replayCarryOver: []
// Set from the moment a read is queued until it starts, so a second
// showHistory while one is still waiting its turn doesn't queue another.
property bool historyReadQueued: false
// Re-show what's in historyDir as toasts. The read goes through the file
// queue and its own subprocess, so the replay lands in replayHistory once
// the work queued ahead of it has finished.
function showRecentHistory() {
if (readHistoryProc.running || service.historyReadQueued) return "ok"
service.replayCarryOver = liveRowsForReplay()
service.historyReadQueued = true
enqueueHistoryRead()
return "ok"
}
function startHistoryRead() {
service.historyReadQueued = false
readHistoryProc.command = ["bash", "-c",
"awk 1 \"$1\"/*.json 2>/dev/null || true", "--", historyDir]
readHistoryProc.running = true
}
// Copy the on-screen rows out of the model. The placeholder from an earlier
// empty replay carries originalId -1 and is not a notification, so it is
// left behind rather than replayed as one. The replay dismisses these
// notifications, and senders delete their images on close — so the carried
// rows point at the persisted copies, like the archived files they join.
function liveRowsForReplay() {
var rows = []
for (var i = 0; i < popupModel.count; i++) {
var row = popupModel.get(i)
if (!row || row.originalId < 0) continue
rows.push(NotificationLogic.persistablePopup({
id: row.id,
originalId: row.originalId,
app: row.app,
appIcon: row.appIcon,
summary: row.summary,
body: row.body,
image: row.image,
glyph: row.glyph || "",
execArgv: row.execArgv || "",
urgency: row.urgency,
timestamp: row.timestamp
}, imagesDir).entry)
}
return rows
}
function replayHistory(raw) {
var rows = NotificationLogic.historyRows(
raw, service.replayCarryOver, NotificationUrgency.Normal, service.historyLimit)
service.replayCarryOver = []
// Replaying nothing at all looks like a dead keybinding, so say so.
if (rows.length === 0) {
popupModel.insert(0, {
id: -1,
originalId: -1,
app: "omarchy-action",
appIcon: "",
summary: "No recent notifications",
body: "",
image: "",
glyph: "",
execArgv: "",
urgency: NotificationUrgency.Low,
expireTimeout: 0,
timestamp: Date.now()
})
return
}
clearPopups()
// Rows arrive newest-first, and index 0 is the top of the toast stack.
for (var i = 0; i < rows.length; i++) {
// Replayed rows are restored rows: their notification died with the
// sender long ago, so they must never resolve to a live server object
// that has since been handed their old id.
service.restoredPopups[NotificationLogic.popupFileName(rows[i])] = true
popupModel.append(rows[i])
}
}
Process {
id: restorePopupsProc
running: false
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: service.restorePopups(text)
}
}
function restorePopups(raw) {
var entries = NotificationLogic.parsePopupFiles(raw, NotificationUrgency.Normal)
var now = Date.now()
var live = []
for (var i = 0; i < entries.length; i++) {
var entry = entries[i]
var duration = durationFor(entry.urgency, entry.expireTimeout)
if (NotificationLogic.popupExpired(entry, duration, now)) {
// It would have expired on screen had the shell kept running, so it
// gets archived exactly like an expiry that happened while it did.
archivePopupFileFor(entry)
continue
}
// Survivors restart with a full lifetime on purpose: shell restarts
// are rare, and a full look after the restart flicker beats resuming
// a toast with a second left on its clock. The reset is persisted as
// an absolute deadline so a second restart while the toast is still
// on screen judges it by the reset clock, not the original timestamp.
if (duration > 0) {
entry.deadline = now + duration
persistPopupFile(entry)
// deadline is persistence metadata, not a model role — fresh rows
// never carry it, and ListModel roles must stay consistent.
delete entry.deadline
}
live.push(entry)
}
if (live.length === 0) return
Qt.callLater(function() {
for (var j = 0; j < live.length; j++) {
var restored = live[j]
// A notification received while the restore was reading the dir can
// already occupy this originalId with the same timestamp — then it
// IS this entry, live with its own file, and must be left alone. A
// different timestamp is indistinguishable between a genuine
// cross-restart replaces_id and a new-generation id coincidence, so
// show both: a briefly duplicated toast beats silently dropping a
// restored critical alert.
var duplicate = false
for (var k = 0; k < popupModel.count; k++) {
var row = popupModel.get(k)
if (row && row.originalId === restored.originalId && row.timestamp === restored.timestamp) {
duplicate = true
break
}
}
if (duplicate) continue
// Append (entries are newest-first) so restored toasts stack in
// their original order below anything that just arrived. Restored
// popups have no liveRefs entry — the server object died with the
// old shell — so dismissal and action fallbacks degrade gracefully.
service.restoredPopups[NotificationLogic.popupFileName(restored)] = true
popupModel.append(restored)
}
})
}
// ---------------------------------------------------- settings persistence
FileView {
id: settingsFile
path: service.settingsPath
watchChanges: false
atomicWrites: true
printErrors: false
onLoaded: service.loadSettings(text())
// First-run: the file doesn't exist yet. Without this branch,
// `settingsLoaded` stays false forever and `scheduleSettingsSave` becomes
// a no-op — so the file is never created and the DND preference vanishes
// on shell restart.
onLoadFailed: service.loadSettings("")
}
Timer {
id: settingsSaveTimer
interval: 200
repeat: false
onTriggered: service.flushSettings()
}
function scheduleSettingsSave() {
if (!service.settingsLoaded) return
settingsSaveTimer.restart()
}
property bool settingsLoaded: false
function loadSettings(raw) {
// FileView can fire onLoaded more than once during startup — the implicit
// preload when `path` resolves, plus the explicit `settingsFile.reload()`
// in Component.onCompleted can both end up calling here.
if (service.settingsLoaded) return
var parsed = NotificationLogic.parseSettings(raw)
if (parsed.error) console.warn("notifications: settings parse failed:", parsed.errorMessage || "")
if (parsed.dnd !== null) {
service._hydrating = true
persisted.doNotDisturb = parsed.dnd
service._hydrating = false
}
service.settingsLoaded = true
// Versions before the history moved into its own directory kept every
// notification in here. Rewrite once so that dead payload doesn't sit in
// the file until the next DND toggle happens to clear it.
if (parsed.legacy) service.scheduleSettingsSave()
}
function flushSettings() {
settingsFile.setText(JSON.stringify({ version: 3, dnd: persisted.doNotDisturb }, null, 2) + "\n")
}
Component.onCompleted: {
ensureDirsProc.running = true
// Once mkdir has had a tick, load the existing settings file. FileView
// surfaces an empty string when the file doesn't exist; loadSettings
// handles that path.
Qt.callLater(function() {
settingsFile.reload()
// Re-show popups that were on screen when the previous shell died.
// The glob-through-bash tolerates a missing/empty dir (first run).
// awk 1 (not cat) so a torn file missing its trailing newline can't
// glue itself onto the next file and take a valid popup down with it.
restorePopupsProc.command = ["bash", "-c",
"awk 1 \"$1\"/*.json 2>/dev/null || true", "--", service.popupStateDir]
restorePopupsProc.running = true
// Safe beside the restore read: it only re-persists entries whose
// JSON exists, exactly the images the sweep keeps.
service.sweepOrphanImages()
})
}
// ---------------------------------------------------- IPC
IpcHandler {
target: "notifications"
function dndState(): string {
return service.doNotDisturb ? "on" : "off"
}
function toggleDnd(): string {
service.setDoNotDisturb(!service.doNotDisturb)
return dndState()
}
function setDnd(value: string): string {
var v = String(value || "").toLowerCase()
var on = v === "true" || v === "1" || v === "on" || v === "yes"
service.setDoNotDisturb(on)
return dndState()
}
function isDnd(): string {
return dndState()
}
// Replay the notifications that have been moved into the history dir.
function showHistory(): string {
return service.showRecentHistory()
}
// `clear` forgets the recorded history; the toasts on screen stay put.
function clear(): string {
service.clearHistory()
return "ok"
}
function dismissAll(): string {
service.clearPopups()
return "ok"
}
// Dismiss the most recent popup.
function dismissOne(): string {
if (popupModel.count === 0) return "none"
service.dismissPopup(0)
return "ok"
}
// Fire the default action on the most recent popup, then dismiss it.
function invokeLast(): string {
if (popupModel.count === 0) return "none"
service.invokePopupDefault(0)
return "ok"
}
// Take a toast off the screen by summary substring, used by the
// first-run notifications once their action has been clicked.
function dismiss(summary: string): string {
var needle = String(summary || "")
if (!needle) return "none"
var hit = false
for (var i = popupModel.count - 1; i >= 0; i--) {
var row = popupModel.get(i)
if (row && String(row.summary || "").indexOf(needle) !== -1) {
service.dismissPopup(i)
hit = true
}
}
return hit ? "ok" : "none"
}
function ping(): string { return "ok" }
}
// ---------------------------------------------------- server
NotificationServer {
id: server
keepOnReload: false
imageSupported: true
actionsSupported: true
bodyMarkupSupported: true
bodyHyperlinksSupported: true
persistenceSupported: true
onNotification: function(notification) {
service.handleNotification(notification)
}
}
// -------------------------------------------------------------- popup UI
//
// One PanelWindow per output (Variants on Quickshell.screens) holding the
// stacked toast cards. Layer is Overlay, exclusionMode Ignore, no
// keyboard focus — popups are passive surfaces and must never steal input
// from the focused application.
Variants {
model: Quickshell.screens
PanelWindow {
id: popupWindow
required property var modelData
screen: modelData
// Only show toasts on the second monitor (DP-2), not the primary (DP-1).
visible: popupModel.count > 0 && modelData.name === "DP-2"
WlrLayershell.namespace: "omarchy-notifications"
WlrLayershell.layer: WlrLayer.Overlay
WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
exclusionMode: ExclusionMode.Ignore
color: "transparent"
readonly property var popupPlacement: NotificationLogic.popupPlacement(
service.barPosition, service.barClearance, Style.gapsOut)
// Full-screen, fixed-size surface (like the OSD overlay). Adding or
// removing a toast changes only the content inside; the Wayland surface
// never resizes, so the compositor can't briefly scale a stale buffer --
// which is what stretched/squished the cards during count changes.
anchors { top: true; bottom: true; left: true; right: true }
// Keep the surface click-through except over the toast column, so the
// rest of the (invisible) full-screen overlay never eats input.
mask: Region { item: popupColumn }
ColumnLayout {
id: popupColumn
anchors.right: parent.right
anchors.top: parent.top
anchors.topMargin: popupWindow.popupPlacement.margins.top
anchors.rightMargin: popupWindow.popupPlacement.margins.right
spacing: Style.space(8)
Repeater {
model: popupModel
// The delegate is a slot Item that owns lifetime timer state. The
// actual visuals live in NotificationCard, which the history panel
// also reuses.
delegate: Item {
id: cardSlot
required property int index
required property string app
required property string appIcon
required property string summary