- MWBMac/Awake.swift: DisplayAwake renews an IOPMAssertion (PreventUserIdleDisplaySleep, 90 s timeout) on every Awake (21), the macOS answer to Common.HumanBeingDetected/PokeMyself (Receiver.cs:244-249, Common.cs:561-591) without posting input. - MWBMac/Fullscreen.swift: the frontmost app's excluded-apps test and a covers-a-whole-display window check, cached 250 ms; gated in the Engine by IsEasyMouseSwitchAllowed's two early outs (Common.cs:1638-1650), so it is asked only on the edge path. - MWBMac/FakeCursor.swift: CursorOverlay draws NSCursor.arrow at the last injected position (Receiver.cs:186-188) and hides on park, local input and shutdown (Common.cs:1495, InputHook.cs:256-264); FakeCursor coalesces the main-thread hops. - Settings.drawMouseCursor accessor + the Behaviour-pane toggle, answering app-ui's open request. - UIBridge.TransferNotices wires ClipboardAction.note to the Notifier, answering clipboard's open request (Clipboard.cs:610-618, 869-881, 916). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
241 lines
10 KiB
Swift
241 lines
10 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
|
|
|
|
/// Clipboard and file transfers, shown to the user.
|
|
///
|
|
/// Windows pops one tray balloon per transfer — "Receiving X from Y…"
|
|
/// (`Clipboard.cs:610-618`), "X from Y is in Clipboard." (`:869-881`) and the
|
|
/// failure message (`:916`) — each passing `Setting.Values.ShowClipNetStatus` to
|
|
/// `ShowToolTip`, so the setting is the gate. `Notifier` already holds that gate
|
|
/// (`enabledChanged(_:)` follows
|
|
/// `ShowClipboardAndNetworkStatusMessages`), so all that was missing was the
|
|
/// wire: the clipboard lane's `ClipboardAction.note` carries the text and the
|
|
/// Engine only logged it.
|
|
///
|
|
/// A static rendezvous rather than a property on the Engine because the Engine
|
|
/// is an actor, the `Notifier` is `@MainActor`, and there is exactly one of each
|
|
/// for the life of the process.
|
|
@MainActor enum TransferNotices {
|
|
private static weak var notifier: Notifier?
|
|
|
|
/// UIBridge hands its notifier over when the menu bar appears, and takes it
|
|
/// back on the way out.
|
|
static func install(_ notifier: Notifier?) {
|
|
TransferNotices.notifier = notifier
|
|
}
|
|
|
|
/// Shows one transfer message, if the user asked for them.
|
|
static func post(_ text: String) {
|
|
notifier?.post(title: "Mouse Without Borders", body: text)
|
|
}
|
|
}
|
|
|
|
/// 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())
|
|
TransferNotices.install(notifier)
|
|
render(EngineSnapshot())
|
|
}
|
|
|
|
/// Takes the item away again, on the way out.
|
|
func stop() {
|
|
TransferNotices.install(nil)
|
|
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) })
|
|
}
|
|
}
|