// Status notifications — the macOS shape of `Common.ShowToolTip`. // // Windows pops a balloon from its tray icon for clipboard activity and network // events, gated on the `ShowClipboardAndNetworkStatusMessages` setting // (`Core/Clipboard.cs:451, 880, 916`, all passing // `Setting.Values.ShowClipNetStatus`; `MouseWithoutBorders_ShowClipboardAndNetwork // StatusMessages.Description`: "Show clipboard activities and network status in // system tray notifications"). `UNUserNotificationCenter` is the macOS // equivalent, with two macOS-only rules: // // 1. It needs a bundle identifier. Unbundled — `swift run` — every call throws // or traps, so the whole class turns into a no-op instead. // 2. Authorization is asked for once, when the user turns the setting on, and // never on our own initiative. import Foundation import UserNotifications /// Posts the "connected / lost / wrong key" notifications, when the user has /// asked for them. @MainActor public final class Notifier { private var enabled = false private var authorizationAsked = false /// Creates a notifier. Nothing is posted until `enabledChanged(true)`. public init() {} /// True when this process can post at all: an app bundle with an identifier. public var isAvailable: Bool { Bundle.main.bundleIdentifier != nil } /// Follows the `ShowClipboardAndNetworkStatusMessages` setting. Turning it on /// asks macOS for permission the first time, which is a prompt the user has /// just invited by ticking the box. public func enabledChanged(_ isOn: Bool) { guard isAvailable else { return } enabled = isOn guard isOn, !authorizationAsked else { return } authorizationAsked = true UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) { granted, error in if let error { Log.app.error("notification authorization failed: \(error.localizedDescription)") } else if !granted { Log.app.info("notifications not allowed; status messages will not be shown") } } } /// Posts one notification, if they are switched on. public func post(title: String, body: String) { guard enabled, isAvailable else { return } let content = UNMutableNotificationContent() content.title = title content.body = body let request = UNNotificationRequest( identifier: UUID().uuidString, content: content, trigger: nil) UNUserNotificationCenter.current().add(request) { error in if let error { Log.app.error("could not post notification: \(error.localizedDescription)") } } } /// "Connecting to X" is what Windows says as it dials (`Clipboard.cs:451`); /// we say it once the peer is trusted, which is the moment worth telling the /// user about. public func connected(to machine: String) { post(title: "Mouse Without Borders", body: "Connected to \(machine).") } /// A peer that was connected went away, for any reason (contracts C2). public func lost(_ machine: String) { post(title: "Mouse Without Borders", body: "Lost the connection to \(machine).") } /// The peer does not share our key, so nothing it sends is acted on /// (CLAUDE.md invariant 2). public func invalidKey(for machine: String) { post( title: "Mouse Without Borders", body: "\(machine) is using a different security key. Set the same key on both machines.") } }