From 8c6f33ea4ce48d0d2975c2ffffcbac4dd95335b0 Mon Sep 17 00:00:00 2001 From: Sebastien Graf <95282692+Seb-GRAF@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:37:19 +0100 Subject: [PATCH] fix: keep the bar running when a notification for a script carries a date --- Sources/StableBar/BarStore.swift | 22 ++++++++++++++++++--- Tests/StableBarTests/ScriptEventTests.swift | 11 +++++++++++ docs/create-a-widget.md | 2 +- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Sources/StableBar/BarStore.swift b/Sources/StableBar/BarStore.swift index 958913d6..e64332f1 100644 --- a/Sources/StableBar/BarStore.swift +++ b/Sources/StableBar/BarStore.swift @@ -811,9 +811,8 @@ final class BarStore: NSObject { eventObservers = events.map { event in center.addObserver(forName: Notification.Name(event), object: nil, queue: .main) { [weak self] notification in var environment = withoutShellVariables(notification.userInfo as? [String: String] ?? [:]) - if environment["INFO"] == nil, let info = notification.userInfo, - let data = try? JSONSerialization.data(withJSONObject: info, options: [.sortedKeys]) { - environment["INFO"] = String(decoding: data, as: UTF8.self) + if environment["INFO"] == nil, let info = notification.userInfo as? [String: Any] { + environment["INFO"] = notificationInfo(info) } MainActor.assumeIsolated { self?.refreshScripts(for: event, environment: environment) } } @@ -1319,6 +1318,23 @@ private func withoutShellVariables(_ environment: [String: String]) -> [String: return environment.filter { shell[$0.key] == nil } } +/// A distributed notification's payload as JSON. `JSONSerialization` raises an exception on values +/// JSON can't hold, so dates become ISO 8601 strings and the rest, such as data, is left out. +func notificationInfo(_ payload: [String: Any]) -> String? { + func jsonValue(_ value: Any) -> Any? { + switch value { + case let date as Date: date.formatted(.iso8601) + case let array as [Any]: array.compactMap(jsonValue) + case let dictionary as [String: Any]: dictionary.compactMapValues(jsonValue) + default: JSONSerialization.isValidJSONObject([value]) ? value : nil + } + } + guard let data = try? JSONSerialization.data(withJSONObject: payload.compactMapValues(jsonValue), options: [.sortedKeys]) else { + return nil + } + return String(decoding: data, as: UTF8.self) +} + /// One widget's resolved style and presence, observed per widget so one widget's update doesn't /// invalidate the views of the others. @MainActor @Observable diff --git a/Tests/StableBarTests/ScriptEventTests.swift b/Tests/StableBarTests/ScriptEventTests.swift index 945a484b..ef1c4433 100644 --- a/Tests/StableBarTests/ScriptEventTests.swift +++ b/Tests/StableBarTests/ScriptEventTests.swift @@ -369,3 +369,14 @@ func widgetActionsRunInOrderAndStoppingCancelsQueuedWork(stop: Bool) async throw } #expect(script.output.text == "frontApp:Safari") } + +@Test func notificationInfoKeepsDatesAndLeavesOutData() { + let payload: [String: Any] = [ + "Name": "Song", "Play Date": Date(timeIntervalSince1970: 0), "Artwork": Data([1, 2, 3]), + "Album": ["Year": 1999, "Added": Date(timeIntervalSince1970: 60), "Tracks": [1, Data(), Date(timeIntervalSince1970: 120)]] + ] + #expect(notificationInfo(payload) == """ + {"Album":{"Added":"1970-01-01T00:01:00Z","Tracks":[1,"1970-01-01T00:02:00Z"],"Year":1999},\ + "Name":"Song","Play Date":"1970-01-01T00:00:00Z"} + """) +} diff --git a/docs/create-a-widget.md b/docs/create-a-widget.md index 29a44426..000802b1 100644 --- a/docs/create-a-widget.md +++ b/docs/create-a-widget.md @@ -83,7 +83,7 @@ Commands inherit your login shell's environment, then top-level `environment`, t | `NAME` | Widget ID; event data cannot replace it | | `CONFIG_DIR` | Directory containing the main configuration; event data cannot replace it | | `SENDER` | `startup`, `routine`, the subscribed event name, `action` after an action completes, or a mouse event below | -| `INFO` | For StableBar's events, as the table above lists; for a trigger, its `INFO` value; otherwise the distributed notification's JSON payload when available | +| `INFO` | For StableBar's events, as the table above lists; for a trigger, its `INFO` value; otherwise the distributed notification's JSON payload when available, with dates as ISO 8601 strings and without values JSON can't hold, such as data | | `SID` | Display number for pointer actions and workspace events | | `BUTTON` | `left`, `right` or `middle` for widget mouse clicks | | `MODIFIER` | Space-separated `shift`, `ctrl`, `alt`, `cmd`, `fn` for pointer actions |