Connect to several servers, one coloured window each

This commit is contained in:
Ian Gulliver
2026-10-18 20:58:56 -07:00
parent c0b2f38584
commit 4347d4e9e4
15 changed files with 456 additions and 209 deletions
+15 -15
View File
@@ -4,28 +4,26 @@ import UserNotifications
@MainActor
final class Notifier: NSObject, UNUserNotificationCenterDelegate {
static let shared = Notifier()
weak var connection: Connection?
var showWindow: (() -> Void)?
private var pending: String?
private var pending: (origin: String, taskId: String)?
func install() {
UNUserNotificationCenter.current().delegate = self
}
private func open(_ taskId: String) {
private func open(_ origin: String, _ taskId: String) {
NSApp.activate()
showWindow?()
pending = taskId
Servers.shared.openWindow?(origin)
pending = (origin, taskId)
deliver()
}
func deliver() {
guard let taskId = pending, let store = connection?.store else {
guard let p = pending, let store = Servers.shared.connections[p.origin]?.store else {
return
}
pending = nil
if !taskId.isEmpty {
store.focusTask(taskId)
if !p.taskId.isEmpty {
store.focusTask(p.taskId)
}
}
@@ -34,9 +32,11 @@ final class Notifier: NSObject, UNUserNotificationCenterDelegate {
}
nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
let taskId = response.notification.request.content.userInfo["task_id"] as? String ?? ""
let info = response.notification.request.content.userInfo
let origin = info["origin"] as? String ?? ""
let taskId = info["task_id"] as? String ?? ""
await MainActor.run {
open(taskId)
open(origin, taskId)
}
}
}
@@ -60,7 +60,7 @@ private func show(_ store: Store, _ taskId: String, _ tag: String, _ text: Strin
content.title = store.findTask(taskId).map { "byre · \($0.title)" } ?? "byre"
content.body = line.count > 200 ? "\(line.prefix(199))…" : line
content.sound = .default
content.userInfo = ["task_id": taskId]
content.userInfo = ["task_id": taskId, "origin": store.api.origin.absoluteString]
let request = UNNotificationRequest(identifier: tag, content: content, trigger: nil)
enqueue {
let status = await UNUserNotificationCenter.current().notificationSettings().authorizationStatus
@@ -92,7 +92,7 @@ func notifyError(_ store: Store, _ ev: ByreEvent) {
if !(limited ? me.notificationPrefs.rateLimits : me.notificationPrefs.errors) {
return
}
show(store, taskId, "error-\(ev.seq)", limited ? "Rate limited" : "Error: \(str(ev.payload.value, "error") ?? "")")
show(store, taskId, "error-\(ev.seq)@\(store.api.origin.absoluteString)", limited ? "Rate limited" : "Error: \(str(ev.payload.value, "error") ?? "")")
}
@MainActor
@@ -109,7 +109,7 @@ func clearInbox(_ item: InboxItem) {
func clearTurns(_ taskId: String, through seq: Int64) {
enqueue {
let ids = await UNUserNotificationCenter.current().deliveredNotifications().map(\.request).filter { r in
guard r.content.userInfo["task_id"] as? String == taskId, r.identifier.hasPrefix("turn-"), let s = Int64(r.identifier.dropFirst(5)) else {
guard r.content.userInfo["task_id"] as? String == taskId, r.identifier.hasPrefix("turn-"), let s = Int64(r.identifier.dropFirst(5).prefix { $0 != "@" }) else {
return false
}
return s <= seq
@@ -123,5 +123,5 @@ func notifyTurn(_ store: Store, _ ev: ByreEvent) {
guard let me = store.me, me.notificationPrefs.reports, ev.payload.value["summary"] as? Bool == true, let taskId = ev.taskId, !taskId.isEmpty else {
return
}
show(store, taskId, "turn-\(ev.seq)", str(ev.payload.value, "result") ?? "")
show(store, taskId, "turn-\(ev.seq)@\(store.api.origin.absoluteString)", str(ev.payload.value, "result") ?? "")
}