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
22 changes: 19 additions & 3 deletions Sources/StableBar/BarStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions Tests/StableBarTests/ScriptEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
""")
}
2 changes: 1 addition & 1 deletion docs/create-a-widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Loading