// The Advanced pane: the three things that are macOS's business rather than the // protocol's — the two TCC grants, the login item, and where the files are. // // Windows needs none of this: a low-level hook needs no permission there, and // the PowerToys page's "Troubleshooting" group offers a firewall rule instead // (`MouseWithoutBordersPage.xaml:481-496`). The equivalent trouble on this side // is Accessibility and Input Monitoring (README D5, Permissions), so that is // what this pane leads with. // // CLAUDE.md, Workflow: `Permissions.request()` is called from exactly one place // in the repository, the button below, and only because the user pressed it. import AppKit import Foundation import MWBCore import SwiftUI /// Permissions, login item, files and versions. struct AdvancedPane: View { @ObservedObject var model: SettingsViewModel @State private var launchAtLogin = LaunchAtLogin.isEnabled @State private var launchAtLoginProblem: String? /// System Settings deep links. `Privacy_ListenEvent` is Input Monitoring and /// `Privacy_Accessibility` is Accessibility; both are read by System /// Settings, not by us, and a wrong one simply opens the Privacy root. private static let accessibilityURL = "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility" private static let inputMonitoringURL = "x-apple.systempreferences:com.apple.preference.security?Privacy_ListenEvent" var body: some View { SettingsPaneLayout { SettingsSection( "Permissions", note: "macOS ties both grants to the app's code signature, so they have to be " + "given again if the app is rebuilt with a different signature." ) { permissionRow( "Input Monitoring", granted: model.permissions.listen, why: "Needed to see the keyboard and mouse, so this Mac can control another.", url: AdvancedPane.inputMonitoringURL) permissionRow( "Accessibility", granted: model.permissions.post, why: "Needed to swallow input while another machine has control, and to type " + "and move the pointer when this Mac is being controlled.", url: AdvancedPane.accessibilityURL) HStack { Button("Request…") { requestPermissions() } .disabled(model.permissions.isComplete) Text("Shows the macOS prompts for whatever is still missing.") .font(.callout).foregroundStyle(.secondary) Spacer() } } SettingsSection("Startup") { SettingsToggle( "Launch at login", help: launchAtLoginHelp, isOn: Binding(get: { launchAtLogin }, set: { setLaunchAtLogin($0) }) ) .disabled(!LaunchAtLogin.isAvailable) } SettingsSection("Files") { SettingsRow("Settings file", help: model.settingsFileURL.path) { Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([model.settingsFileURL]) } } SettingsRow("Log file", help: Log.fileURL().path) { Button("Show in Finder") { NSWorkspace.shared.activateFileViewerSelecting([Log.fileURL()]) } } } SettingsSection("This machine") { labelled("Version", MWBVersion.string) labelled("Host name", model.localName.isEmpty ? "(none)" : model.localName) labelled("Machine ID", String(model.settings.machineID)) labelled("Device ID", model.settings.deviceID) } } .onAppear { launchAtLogin = LaunchAtLogin.isEnabled } } private var launchAtLoginHelp: String { if !LaunchAtLogin.isAvailable { return "Only available when running from the app bundle." } if let launchAtLoginProblem { return launchAtLoginProblem } if LaunchAtLogin.needsApproval { return "Waiting for approval in System Settings > General > Login Items." } return "Starts Mouse Without Borders when you log in." } private func permissionRow( _ title: String, granted: Bool, why: String, url: String ) -> some View { SettingsRow(title, help: why) { HStack(spacing: 8) { Label( granted ? "Granted" : "Missing", systemImage: granted ? "checkmark.circle.fill" : "exclamationmark.triangle.fill" ) .foregroundStyle(granted ? Color.green : Color.red) Button("Open System Settings") { open(url) } } } } private func labelled(_ title: String, _ value: String) -> some View { HStack(alignment: .firstTextBaseline) { Text(title).foregroundStyle(.secondary) Spacer() Text(value.isEmpty ? "—" : value).textSelection(.enabled) .font(.system(.body, design: .monospaced)) } } /// The one call site (CLAUDE.md, Workflow). private func requestPermissions() { model.permissions = Permissions.request() } private func setLaunchAtLogin(_ enabled: Bool) { do { try LaunchAtLogin.set(enabled) launchAtLogin = LaunchAtLogin.isEnabled launchAtLoginProblem = nil } catch { launchAtLogin = LaunchAtLogin.isEnabled launchAtLoginProblem = error.localizedDescription Log.app.error("login item change failed: \(error.localizedDescription)") } } private func open(_ url: String) { guard let target = URL(string: url) else { return } NSWorkspace.shared.open(target) } }