Files
lucioandClaude Opus 5 6b2b3fc65a app-ui: menu bar, settings window, notifications, login item
The real user interface, replacing app-engine's placeholder status item.

- MWBMac/StatusItem.swift: NSStatusItem with a code-drawn icon, the
  documented status colours (doc/devdocs/modules/mousewithoutborders.md)
  and the menu the Windows tray menu offers (frmScreen.Designer.cs:91-180).
- MWBMac/SettingsWindow/*: one NSWindow hosting a SwiftUI TabView with six
  panes; every property in README §Settings is reachable. Labels and help
  text taken from MouseWithoutBordersPage.xaml + Resources.resw, cited per
  file.
- MWBMac/Notifications.swift, LaunchAgent.swift: UNUserNotificationCenter
  and SMAppService.mainApp, both guarded on a bundle identifier.
- mwb/UIBridge.swift: EngineSnapshot -> StatusModel, clicks -> Engine, and
  the connected/lost/invalid-key notification diff.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-09-16 04:14:11 -07:00

83 lines
3.5 KiB
Swift

// 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.")
}
}