Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/omamobile-shell/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ service has no count to read through the host's proxy, so the bar watches
the directory (`Qt.labs.folderlistmodel`, a file system watcher, not a poll).
The shade records its mark in `~/.local/state/omamobile/notifications-seen`
and calls `notificationsSeen` below. A toast that was acted on also lands in
history, so it counts until the shade has been opened.
history, so it counts until the shade has been opened. Omarchy 4.5 keeps the
history in one file instead, `~/.local/state/omarchy/notifications/history.json`
(a JSON array, each row's `ts` in seconds); while that file exists, the bar
counts its rows newer than the mark (`StatusModel.unreadStored`), watching
the file and re-reading it every 30 s, since the service replaces it with
rename(2).

**Mobile network and calls:** the cellular icon and the call chip come from
`omamobile-cell-watch` and `omamobile-call-watch --observe` (see
Expand Down Expand Up @@ -169,14 +174,20 @@ history as toasts (`showHistory`) but returns none of it, so the shade reads
the files the service keeps, `~/.local/state/omarchy/notifications/*.json`
and `history/*.json` (format and rules in `NotificationsModel.js`, tests in
`test/notifications-model.js`), when the sheet opens and every 3 s while it
is open. A tap on a row puts the sheet away and runs what a tap on the toast
is open. On Omarchy 4.5, which keeps them in `live.json` and `history.json`
in that directory instead (100 rows over 24 hours, `Store.js`), the shade
reads those two files whenever `history.json` exists, whatever the version
says; `test/notifications-store.sh` runs its commands on both layouts. A tap on a row puts the sheet away and runs what a tap on the toast
runs: Omarchy's own action for its toasts (the `omarchy-exec-argv` hint the
file keeps), otherwise `omarchy-hyprland-focus-app <app>`. Other senders'
live actions die with the toast, so they can't be run from history.

**Clear all** (44 px, two taps: the first turns it into "Clear all?" for 4 s)
calls the notification service on this shell instance, `dismissAll` (the
toasts on screen move into history) and then `clear` (history is emptied).
Omarchy 4.5 has no `clear`: there, after `dismissAll`, the shade calls
`forget <key>` for every notification it read (patch 0019 for 4.5, in
omamobile-omarchy's 4.5 series; without it the rows come back).
The card takes the room left under the sheet and scrolls past that; it hides
in edit mode, or when the sheet leaves no room for it.

Expand Down
23 changes: 23 additions & 0 deletions packages/omamobile-shell/plugins/omamobile.bar/Bar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ Item {
// FolderListModel's file system watcher rather than polled. The shade
// writes its mark to notifications-seen and tells this bar by IPC.
readonly property string notificationsHistory: Quickshell.env("HOME") + "/.local/state/omarchy/notifications/history"
// Omarchy 4.5 keeps the history in one file in the directory above,
// history.json (StatusModel.unreadStored); while it exists, it is what
// the count reads. The service replaces it with rename(2), which a watch
// can miss, so it is also re-read now and then.
readonly property string notificationsStore: Quickshell.env("HOME") + "/.local/state/omarchy/notifications/history.json"
property bool storeLayout: false
property double seenStamp: 0
property int unread: 0

Expand All @@ -151,7 +157,23 @@ Item {
function onDataChanged() { Qt.callLater(root.countUnread) }
}

FileView {
id: historyStore
path: root.notificationsStore
printErrors: false
watchChanges: true
onFileChanged: reload()
onLoaded: { root.storeLayout = true; Qt.callLater(root.countUnread) }
onLoadFailed: { root.storeLayout = false; Qt.callLater(root.countUnread) }
}

Timer { interval: 30000; repeat: true; running: root.storeLayout; onTriggered: historyStore.reload() }

function countUnread() {
if (storeLayout) {
unread = StatusModel.unreadStored(historyStore.text(), seenStamp)
return
}
var names = []
for (var i = 0; i < historyFiles.count; i++) names.push(historyFiles.get(i, "fileName"))
unread = StatusModel.unreadCount(names, seenStamp)
Expand All @@ -160,6 +182,7 @@ Item {
// The notification service makes the directory when it starts; if the
// bar got there first, watch it again once it can exist.
function rewatchHistory() {
historyStore.reload()
if (historyFiles.count > 0) return
historyFiles.folder = ""
historyFiles.folder = "file://" + root.notificationsHistory
Expand Down
18 changes: 17 additions & 1 deletion packages/omamobile-shell/plugins/omamobile.bar/StatusModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ function unreadCount(names, seen) {
return count
}

// Omarchy 4.5 keeps its history in one file, history.json: a JSON array of
// rows, each with `ts`, when it arrived, in seconds. The shade's mark is in
// milliseconds, rounded as its NotificationsModel.storeEntry rounds them.
function unreadStored(text, seen) {
var rows = null
try { rows = JSON.parse(String(text === undefined || text === null ? "" : text)) } catch (error) { return 0 }
if (!Array.isArray(rows)) return 0
var since = parseStamp(seen)
var count = 0
for (var i = 0; i < rows.length; i++) {
var ts = rows[i] && typeof rows[i] === "object" ? Number(rows[i].ts) : 0
if (isFinite(ts) && ts > 0 && Math.round(ts * 1000) > since) count++
}
return count
}

function batteryIcon(percent, charging, full) {
if (percent < 0) return ""
if (full) return "󰁹"
Expand All @@ -134,6 +150,6 @@ if (typeof module !== "undefined") {
findDevice: findDevice, connectedNetwork: connectedNetwork, wifiIcon: wifiIcon, networkIcon: networkIcon,
cellularIcon: cellularIcon, cellGlyph: cellGlyph, cellText: cellText, parseCell: parseCell,
callChip: callChip, elapsed: elapsed, batteryIcon: batteryIcon,
stampOf: stampOf, parseStamp: parseStamp, unreadCount: unreadCount
stampOf: stampOf, parseStamp: parseStamp, unreadCount: unreadCount, unreadStored: unreadStored
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
// named <timestamp>-<id>.json, the timestamp in milliseconds since the epoch.
// One notification is taken away by that name through the service's forget
// call (omamobile-omarchy patch 0019), never by deleting the file here.
//
// Omarchy 4.5 keeps the same notifications in two files instead (its
// shell/plugins/notifications/Store.js): live.json, the toasts on screen,
// and history.json, the closed ones, newest first. Each is one line, a JSON
// array of rows named by a `key` ("n" and base-36 digits), with `ts`, when
// the notification arrived, in seconds. Its forget call (patch 0019 for
// 4.5) takes that key. parse() reads either layout: a line holding an array
// is 4.5's, a line holding an object is one 4.0 file.

var LIMIT = 10

Expand Down Expand Up @@ -69,10 +77,34 @@ function entry(value) {
}
}

// One row of Omarchy 4.5's live.json or history.json, in the shape entry()
// gives a 4.0 file: the key is the row's own, and so is the name forget
// takes (`file`); `ts` turns into milliseconds. Null for anything that
// isn't a row with a key 4.5 itself would accept (Store.js KEY).
function storeEntry(value) {
if (!value || typeof value !== "object" || Array.isArray(value)) return null
var key = str(value.key)
if (!/^[A-Za-z0-9_-]{1,120}$/.test(key)) return null
var ts = Number(value.ts)
var timestamp = isFinite(ts) && ts > 0 ? Math.round(ts * 1000) : 0
return {
key: key,
file: key,
app: str(value.app),
summary: str(value.summary).trim(),
body: plainBody(value.body),
glyph: str(value.glyph),
execArgv: str(value.execArgv),
urgency: typeof value.urgency === "number" ? value.urgency : 1,
timestamp: timestamp
}
}

// The concatenation of every history and on-screen file (`awk 1` over both
// directories), newest first, each notification once (a toast moving into
// history can be read in both places), at most `limit`. Torn lines from a
// crash mid-write are skipped, as Omarchy skips them.
// directories on 4.0, live.json and history.json on 4.5), newest first,
// each notification once (a toast moving into history can be read in both
// places), at most `limit`. Torn lines from a crash mid-write are skipped,
// as Omarchy skips them.
function parse(raw, limit) {
var max = typeof limit === "number" ? Math.max(0, limit) : LIMIT
var lines = str(raw).split("\n")
Expand All @@ -83,12 +115,17 @@ function parse(raw, limit) {
if (!line) continue
var value = null
try { value = JSON.parse(line) } catch (error) { continue }
if (!value || typeof value !== "object" || Array.isArray(value)) continue
var e = entry(value)
if (!e.summary && !e.body) continue
if (seen[e.key]) continue
seen[e.key] = true
out.push(e)
var rows
if (Array.isArray(value)) rows = value.map(storeEntry)
else if (value && typeof value === "object") rows = [entry(value)]
else continue
for (var j = 0; j < rows.length; j++) {
var e = rows[j]
if (!e || (!e.summary && !e.body)) continue
if (seen[e.key]) continue
seen[e.key] = true
out.push(e)
}
}
out.sort(function(a, b) { return b.timestamp - a.timestamp })
return out.slice(0, max)
Expand All @@ -100,7 +137,8 @@ function parse(raw, limit) {
// file, the row stays out; a mark older than graceMs (a forget that never
// happened, say on an Omarchy without the forget call) lapses, and the row
// comes back rather than staying hidden for a notification that is still
// there. Returns the rows and the marks still needed.
// there. Returns the rows, the marks still needed and the forget name of
// every notification read, shown or not (Clear all on 4.5 forgets them).
function shown(raw, hidden, now, graceMs) {
var marks = hidden || {}
var grace = typeof graceMs === "number" ? graceMs : 15000
Expand All @@ -113,7 +151,7 @@ function shown(raw, hidden, now, graceMs) {
if (Number(now) - Number(marks[key]) <= grace) kept[key] = marks[key]
}
var out = all.filter(function(e) { return !kept.hasOwnProperty(e.key) })
return { entries: out.slice(0, LIMIT), hidden: kept }
return { entries: out.slice(0, LIMIT), hidden: kept, files: all.map(function(e) { return e.file }) }
}

function without(entries, key) {
Expand Down Expand Up @@ -174,6 +212,7 @@ if (typeof module !== "undefined") {
plainBody: plainBody,
execArgv: execArgv,
entry: entry,
storeEntry: storeEntry,
parse: parse,
shown: shown,
without: without,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,11 +838,17 @@ Item {
// Clear all goes through the service's own IPC, on this shell instance like
// the DND tile: dismissAll moves the toasts on screen into history, then
// clear empties history (both run in order on the service's file queue).
// Omarchy 4.5 keeps them in live.json and history.json in the same
// directory instead, and has no clear: where history.json exists, the
// shade reads those two files, and Clear all forgets every notification
// it read once dismissAll has closed the toasts (they keep their keys).
readonly property string notificationsDir: Quickshell.env("HOME") + "/.local/state/omarchy/notifications"
// Where the bar's unread count starts from: the newest notification the
// shade has shown (omamobile.bar reads it at start and hears it by IPC).
readonly property string stateDir: Quickshell.env("HOME") + "/.local/state/omamobile"
property var notifications: []
// The forget name of every notification the last read found.
property var notificationFiles: []
property double nowMs: Date.now()
property double seenStamp: 0
property double seenWritten: 0
Expand Down Expand Up @@ -897,13 +903,16 @@ Item {

Process {
id: notificationsRead
command: ["sh", "-c", "for f in \"$1\"/*.json \"$1\"/history/*.json; do [ -f \"$f\" ] && awk 1 \"$f\"; done; exit 0",
command: ["sh", "-c",
"if [ -e \"$1/history.json\" ]; then for f in \"$1/live.json\" \"$1/history.json\"; do [ -f \"$f\" ] && awk 1 \"$f\"; done; "
+ "else for f in \"$1\"/*.json \"$1\"/history/*.json; do [ -f \"$f\" ] && awk 1 \"$f\"; done; fi; exit 0",
"sh", root.notificationsDir]
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: {
var view = NotificationsModel.shown(text, root.hiddenNotifications, Date.now())
root.hiddenNotifications = view.hidden
root.notificationFiles = view.files
// Only a change replaces the rows: a new list rebuilds every row, and
// one being swiped would be dropped from under the finger.
if (JSON.stringify(view.entries) !== JSON.stringify(root.notifications))
Expand Down Expand Up @@ -964,8 +973,10 @@ Item {
notifications = []
if (clearProc.running) return
clearProc.command = ["sh", "-c",
"qs ipc -p \"$1\" call notifications dismissAll; qs ipc -p \"$1\" call notifications clear",
"sh", Quickshell.shellDir]
"if [ -e \"$2/history.json\" ]; then shell=$1; shift 2; qs ipc -p \"$shell\" call notifications dismissAll; "
+ "for name; do qs ipc -p \"$shell\" call notifications forget \"$name\"; done; "
+ "else qs ipc -p \"$1\" call notifications dismissAll; qs ipc -p \"$1\" call notifications clear; fi; exit 0",
"sh", Quickshell.shellDir, root.notificationsDir].concat(notificationFiles)
clearProc.running = true
}

Expand Down
7 changes: 6 additions & 1 deletion packages/omamobile-shell/test/all
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,15 @@ pass "touch navigation: back, home pill, drawer pull, undo, fields in view"
# at its root, the shade reads the directories Omarchy's service writes, and
# the bar hears the shade's mark on the IPC target it serves.
qs=plugins/omamobile.quicksettings/QuickSettings.qml
grep -cF '"qs ipc -p \"$1\" call notifications dismissAll; qs ipc -p \"$1\" call notifications clear"' "$qs" >/dev/null || fail "Clear all dismisses the toasts, then clears the history"
grep -cF '"else qs ipc -p \"$1\" call notifications dismissAll; qs ipc -p \"$1\" call notifications clear; fi; exit 0"' "$qs" >/dev/null || fail "Clear all dismisses the toasts, then clears the history"
grep -cF 'Quickshell.execDetached(["omarchy-menu", "summon", "root"])' "$qs" >/dev/null || fail "the gear opens Omarchy's root menu"
grep -cF '"/.local/state/omarchy/notifications"' "$qs" >/dev/null || fail "the shade reads Omarchy's notification files"
grep -cF '"/.local/state/omarchy/notifications/history"' plugins/omamobile.bar/Bar.qml >/dev/null || fail "the bar counts Omarchy's notification history"
grep -cF '"/.local/state/omarchy/notifications/history.json"' plugins/omamobile.bar/Bar.qml >/dev/null || fail "the bar counts Omarchy 4.5's history.json"
# Omarchy 4.0's files and 4.5's live.json and history.json, chosen by what
# exists: test/notifications-store.sh runs the shade's own commands on both.
grep -cF '"sh", Quickshell.shellDir, root.notificationsDir].concat(notificationFiles)' "$qs" >/dev/null || fail "Clear all hands every notification read to the clear script"
bash test/notifications-store.sh >/dev/null || { bash test/notifications-store.sh; fail "the shade reads and clears both notification layouts"; }
grep -cF 'call omamobile.bar notificationsSeen' "$qs" >/dev/null || fail "the shade tells the bar what it has shown"
grep -cF 'function notificationsSeen(stamp: string): string' plugins/omamobile.bar/Bar.qml >/dev/null || fail "the bar serves notificationsSeen"
grep -cF '"/.local/state/omamobile/notifications-seen"' plugins/omamobile.bar/Bar.qml >/dev/null || fail "the bar reads the shade's mark at start"
Expand Down
62 changes: 62 additions & 0 deletions packages/omamobile-shell/test/notifications-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,68 @@ test("swipeOutcome: past 35% or a flick goes that way, else back", () => {
assert.strictEqual(M.swipeOutcome(300, 0, 0, fling), 0, "no width yet")
})

// Omarchy 4.5's files (shell/plugins/notifications/Store.js): live.json and
// history.json, each one line, a JSON array of rows as Store.persistable and
// Store.closedEntry write them. These rows were made by 4.5.0-3's own
// Store.js (snapshot, persistable, closedEntry, serialise); a verification
// code is stored redacted.
const row45 = (o) => Object.assign({
key: "nmgx1a2b3", originalId: 3, senderPid: 0, execArgv: "[\"omarchy-launch-wifi\"]", execActions: "", glyph: "",
app: "Signal", appIcon: "signal-desktop", summary: "Ana", body: "See you <b>soon</b> &amp; bye", rawBody: "",
source: "Signal", groupKey: "app:signal", urgency: 1, expireTimeout: 0, duration: 0, ts: 1760000000.123
}, o)
const closed45 = (o) => Object.assign(row45(o), { closed_ts: 1760000060.5, closed_reason: "dismissed" }, o)
const store45 = (live, history) => JSON.stringify(live) + "\n" + JSON.stringify(history) + "\n"

test("parse reads Omarchy 4.5's live.json and history.json, newest first", () => {
const raw = store45([row45({ key: "nlive", summary: "on screen", ts: 1760000300.5 })],
[closed45({ key: "nnew", summary: "new", ts: 1760000200 }), closed45({ key: "nold", summary: "old", ts: 1760000100 }),
closed45({ key: "ncode", originalId: 9, app: "", appIcon: "", execArgv: "", source: "", groupKey: "",
summary: "Verification notification", body: "[redacted]", ts: 1760000150, closed_reason: "expired" })])
const rows = M.parse(raw)
assert.deepStrictEqual(rows.map((r) => r.summary), ["on screen", "new", "Verification notification", "old"])
assert.deepStrictEqual(rows.map((r) => r.key), ["nlive", "nnew", "ncode", "nold"])
assert.strictEqual(rows[0].timestamp, 1760000300500, "ts in seconds, as milliseconds")
const [one] = M.parse(store45([], [closed45({})]))
assert.deepStrictEqual(one, { key: "nmgx1a2b3", file: "nmgx1a2b3", app: "Signal", summary: "Ana", body: "See you soon & bye",
glyph: "", execArgv: "[\"omarchy-launch-wifi\"]", urgency: 1, timestamp: 1760000000123 })
assert.deepStrictEqual(M.tapArgv(one), ["omarchy-launch-wifi"])
})

test("on 4.5 each row carries its key, which forget takes", () => {
const [row] = M.parse(store45([], [closed45({ key: "nabc_1-Z" })]))
assert.strictEqual(row.file, "nabc_1-Z")
assert.match(row.file, /^[A-Za-z0-9_-]{1,120}$/, "the shape 4.5's Store.KEY accepts")
})

test("4.5: a row in both files shows once; rows without a usable key or text are skipped", () => {
const raw = store45([row45({ key: "nsame", summary: "live copy" })],
[closed45({ key: "nsame", summary: "history copy" }), closed45({ key: "bad key" }), closed45({ key: "" }),
closed45({ key: "x".repeat(121) }), closed45({ key: "nempty", summary: "", body: "" }), 7, null, [1], "n"])
const rows = M.parse(raw)
assert.deepStrictEqual(rows.map((r) => r.summary), ["live copy"])
assert.deepStrictEqual(M.parse("\n\n"), [], "4.5's files as its service creates them: empty")
assert.deepStrictEqual(M.parse("[]\n[]\n"), [])
assert.strictEqual(M.parse(store45([], [closed45({ ts: "soon" })]))[0].timestamp, 0, "no usable ts: no age")
})

test("4.5: shown hides a swiped row until it is gone, and lists every key for Clear all", () => {
const history = Array.from({ length: 12 }, (_, i) => closed45({ key: "n" + i, ts: 1760000000 + i }))
let view = M.shown(store45([], history), { n11: 900 }, 1000)
assert.strictEqual(view.entries.length, M.LIMIT)
assert.strictEqual(view.entries[0].key, "n10")
assert.deepStrictEqual(view.hidden, { n11: 900 })
assert.strictEqual(view.files.length, 12, "every row read, not only the ten shown")
view = M.shown(store45([], history.slice(0, 11)), view.hidden, 1000)
assert.deepStrictEqual(view.hidden, {})
})

test("4.0: shown lists every file name for Clear all too", () => {
const raw = Array.from({ length: 11 }, (_, i) => file({ timestamp: 100 + i, id: i })).join("\n")
assert.strictEqual(M.shown(raw, {}, 0).files.length, 11)
assert.strictEqual(M.shown(raw, {}, 0).files[0], "110-10.json")
})

let failed = 0
for (const [name, fn] of tests) {
try {
Expand Down
Loading