- Resources/Info.plist: NSSupportsSuddenTermination = false, so nothing can skip applicationWillTerminate (invariant 3, contracts C2). That is the only bundle change notifications and login items turned out to need; the work log says what else was checked. - docs/features/app-ui/README.md: Assumptions brought in line with what shipped, Handshakes replaced with the real API (five deviations, all reasoned), one open request to app-engine for a DrawMouseCursor accessor, Status/TODO ticked, work log. - TODO.md §7: three lines ticked; CP1 left for the lead. - UIBridge: dropped an unused openSettings entry point. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
210 lines
9.0 KiB
Swift
210 lines
9.0 KiB
Swift
// The one place the Engine and the user interface meet.
|
|
//
|
|
// Two doors, both of them here (app-engine Review): snapshots come in through
|
|
// `Engine.observe` and are turned into the value the menu bar draws; the user's
|
|
// clicks go out as `Engine` calls and as `SettingsStore.update` + `Engine.apply`.
|
|
// `MWBMac` may not import `MWBNet` or the executable target (README D2), so the
|
|
// mapping from `EngineSnapshot`/`PeerStatus` onto `StatusModel`/`MachineRow`
|
|
// lives on this side of the boundary rather than in the status item.
|
|
//
|
|
// Everything here is `@MainActor`: `observe`'s handler arrives on the Engine's
|
|
// executor and hops once, in `AppLifecycle.swift`.
|
|
|
|
import AppKit
|
|
import Foundation
|
|
import MWBCore
|
|
import MWBMac
|
|
import MWBNet
|
|
|
|
/// Owns the menu bar, the settings window and the notifier, and keeps all three
|
|
/// in step with the Engine.
|
|
@MainActor final class UIBridge {
|
|
private let engine: Engine
|
|
private let store: SettingsStore
|
|
private let viewModel: SettingsViewModel
|
|
private let settingsWindow: SettingsWindowController
|
|
private let notifier = Notifier()
|
|
private var statusItem: StatusItemController?
|
|
|
|
private var lastStatuses: [String: SocketStatus] = [:]
|
|
private var lastNames: [String: String] = [:]
|
|
private var notificationsEnabled: Bool?
|
|
|
|
init(engine: Engine, store: SettingsStore, localName: String) {
|
|
self.engine = engine
|
|
self.store = store
|
|
viewModel = SettingsViewModel(
|
|
store: store, localName: localName,
|
|
apply: { [engine] settings in await engine.apply(settings) })
|
|
settingsWindow = SettingsWindowController(model: viewModel)
|
|
}
|
|
|
|
/// Puts the item in the menu bar. Called once, before the Engine starts, so
|
|
/// the first snapshot has something to draw into.
|
|
func start() {
|
|
statusItem = StatusItemController(actions: makeActions())
|
|
render(EngineSnapshot())
|
|
}
|
|
|
|
/// Takes the item away again, on the way out.
|
|
func stop() {
|
|
statusItem?.remove()
|
|
statusItem = nil
|
|
}
|
|
|
|
// MARK: - In: snapshot → UI
|
|
|
|
/// Draws one snapshot. Cheap to call often: `StatusItemController` compares
|
|
/// the model it is given and rebuilds nothing when it has not changed.
|
|
func render(_ snapshot: EngineSnapshot) {
|
|
let settings = store.current
|
|
statusItem?.update(model(from: snapshot, settings: settings))
|
|
viewModel.permissions = snapshot.permissions
|
|
viewModel.storeChanged()
|
|
updateNotifications(snapshot, settings: settings)
|
|
}
|
|
|
|
private func model(from snapshot: EngineSnapshot, settings: Settings) -> StatusModel {
|
|
var model = StatusModel()
|
|
model.rows = rows(from: snapshot, settings: settings)
|
|
model.controlled = snapshot.controlled
|
|
model.broadcasting = snapshot.broadcasting
|
|
model.easyMouse = snapshot.easyMouse
|
|
model.missingPermissions = UIBridge.missing(snapshot.permissions)
|
|
model.canInject = snapshot.canInject
|
|
model.tapRunning = snapshot.tapRunning
|
|
model.hostnameProblem = snapshot.hostnameProblem
|
|
model.lastRecovery = snapshot.lastRecovery.map { $0.reason.text }
|
|
model.lockHotkey = settings.lockMachineShortcut
|
|
model.reconnectHotkey = settings.reconnectShortcut
|
|
model.switchMachineVK = settings.hotKeySwitchMachine
|
|
model.version = MWBVersion.string
|
|
return model
|
|
}
|
|
|
|
/// One row per non-empty matrix slot, then any peer that talked to us
|
|
/// without being in the matrix — Windows shows those in the machine pool
|
|
/// rather than the matrix, and dropping them would hide a machine that is
|
|
/// connected right now.
|
|
private func rows(from snapshot: EngineSnapshot, settings: Settings) -> [MachineRow] {
|
|
let best = UIBridge.bestStatuses(snapshot.peers)
|
|
var rows: [MachineRow] = []
|
|
var listed: Set<String> = []
|
|
for (slot, raw) in settings.matrix.names.enumerated() {
|
|
let name = raw.trimmingCharacters(in: .whitespaces)
|
|
guard !name.isEmpty else { continue }
|
|
listed.insert(name.lowercased())
|
|
let isLocal = name.caseInsensitiveCompare(snapshot.localName) == .orderedSame
|
|
rows.append(
|
|
MachineRow(
|
|
name: name, status: isLocal ? .connected : (best[name.lowercased()] ?? .na),
|
|
isLocal: isLocal, isControlled: isControlled(name, snapshot), slot: slot))
|
|
}
|
|
for (key, status) in best.sorted(by: { $0.key < $1.key }) where !listed.contains(key) {
|
|
guard let name = snapshot.peers.first(where: { $0.name.lowercased() == key })?.name
|
|
else { continue }
|
|
rows.append(
|
|
MachineRow(
|
|
name: name, status: status, isControlled: isControlled(name, snapshot),
|
|
slot: nil))
|
|
}
|
|
return rows
|
|
}
|
|
|
|
private func isControlled(_ name: String, _ snapshot: EngineSnapshot) -> Bool {
|
|
guard !snapshot.broadcasting, let controlled = snapshot.controlled else { return false }
|
|
return controlled.caseInsensitiveCompare(name) == .orderedSame
|
|
}
|
|
|
|
/// A peer pair can have two connections (each side dialled the other), so a
|
|
/// name can appear twice with different statuses; the healthier one is what
|
|
/// the user cares about.
|
|
static func bestStatuses(_ peers: [PeerStatus]) -> [String: SocketStatus] {
|
|
var best: [String: SocketStatus] = [:]
|
|
for peer in peers {
|
|
let key = peer.name.lowercased()
|
|
guard !key.isEmpty else { continue }
|
|
if let existing = best[key], rank(existing) >= rank(peer.status) { continue }
|
|
best[key] = peer.status
|
|
}
|
|
return best
|
|
}
|
|
|
|
private static func rank(_ status: SocketStatus) -> Int {
|
|
switch status {
|
|
case .connected: return 5
|
|
case .handshaking: return 4
|
|
case .connecting: return 3
|
|
case .resolving: return 2
|
|
case .na: return 1
|
|
default: return 0
|
|
}
|
|
}
|
|
|
|
static func missing(_ permissions: PermissionState) -> [String] {
|
|
var missing: [String] = []
|
|
if !permissions.listen { missing.append("Input Monitoring") }
|
|
if !permissions.post { missing.append("Accessibility") }
|
|
return missing
|
|
}
|
|
|
|
// MARK: - Notifications (`Common.ShowToolTip`, gated on ShowClipNetStatus)
|
|
|
|
private func updateNotifications(_ snapshot: EngineSnapshot, settings: Settings) {
|
|
let wanted = settings.showClipboardAndNetworkStatusMessages
|
|
if notificationsEnabled != wanted {
|
|
notificationsEnabled = wanted
|
|
notifier.enabledChanged(wanted)
|
|
}
|
|
let current = UIBridge.bestStatuses(snapshot.peers)
|
|
for (key, status) in current {
|
|
let previous = lastStatuses[key]
|
|
guard previous != status, let name = displayName(key, snapshot) else { continue }
|
|
if status == .connected {
|
|
notifier.connected(to: name)
|
|
} else if previous == .connected {
|
|
notifier.lost(name)
|
|
} else if status == .invalidKey {
|
|
notifier.invalidKey(for: name)
|
|
}
|
|
}
|
|
// A row can vanish outright — the channel drops it when the connection is
|
|
// gone and the name is not in the matrix to be re-dialled.
|
|
for (key, previous) in lastStatuses where current[key] == nil && previous == .connected {
|
|
notifier.lost(lastNames[key] ?? key)
|
|
}
|
|
lastStatuses = current
|
|
// Remember the spelling, not just the key: a peer that vanished cannot be
|
|
// looked up in the snapshot any more. Two connections can carry the same
|
|
// name, so this is a loop rather than `Dictionary(uniqueKeysWithValues:)`,
|
|
// which traps on a duplicate.
|
|
for peer in snapshot.peers where !peer.name.isEmpty {
|
|
lastNames[peer.name.lowercased()] = peer.name
|
|
}
|
|
}
|
|
|
|
private func displayName(_ key: String, _ snapshot: EngineSnapshot) -> String? {
|
|
snapshot.peers.first { $0.name.lowercased() == key }?.name
|
|
}
|
|
|
|
// MARK: - Out: clicks → Engine
|
|
|
|
private func makeActions() -> StatusActions {
|
|
StatusActions(
|
|
switchTo: { [engine] slot in Task { await engine.switchTo(slot: slot) } },
|
|
// Only reached by a local row that has no matrix slot to name; with a
|
|
// slot the ordinary switch path is used. `.escapeHotkey` is the right
|
|
// reason: a menu click asking for control back is a manual escape
|
|
// (contracts C2).
|
|
switchHome: { [engine] in
|
|
Task { await engine.recoverLocal(reason: .escapeHotkey) }
|
|
},
|
|
switchToAll: { [engine] in Task { await engine.switchToAll() } },
|
|
toggleEasyMouse: { [engine] in Task { await engine.toggleEasyMouse() } },
|
|
reconnect: { [engine] in Task { await engine.reconnect() } },
|
|
lockAll: { [engine] in Task { await engine.lockAll() } },
|
|
openSettings: { [weak self] pane in self?.settingsWindow.show(pane: pane) },
|
|
quit: { NSApp.terminate(nil) })
|
|
}
|
|
}
|