Files
mac_without_borders/Sources/MWBMac/SettingsWindow/SettingsWindow.swift
T
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

74 lines
2.7 KiB
Swift

// The window the settings panes live in.
//
// One window for the life of the process: `show(pane:)` brings the existing one
// to the front rather than opening a second, and closing it hides it. An
// `LSUIElement` app has no Dock icon to reopen from, so a window that terminated
// the app on close — or that could be opened twice — would be a dead end
// (`frmMatrix` on Windows is likewise a single form the tray menu re-shows).
import AppKit
import Foundation
import MWBCore
import SwiftUI
/// Shows and hides the settings window.
@MainActor public final class SettingsWindowController: NSObject, NSWindowDelegate {
private let model: SettingsViewModel
private var window: NSWindow?
private let pane = PaneSelection()
/// Binds the window to a view model. Nothing is created until `show`.
public init(model: SettingsViewModel) {
self.model = model
super.init()
}
/// Brings the window up, on `pane` when one is named.
public func show(pane: SettingsPane? = nil) {
if let pane { self.pane.selected = pane }
model.storeChanged()
model.refreshDrafts()
let window = window ?? makeWindow()
self.window = window
NSApp.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(nil)
}
/// True while the window is on screen; the bridge uses it to keep the
/// permission state in the pane fresh without asking TCC itself.
public var isVisible: Bool { window?.isVisible ?? false }
private func makeWindow() -> NSWindow {
let root = SettingsView(model: model, pane: pane.binding)
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 560, height: 520),
styleMask: [.titled, .closable, .miniaturizable],
backing: .buffered, defer: false)
window.title = "Mouse Without Borders"
window.contentView = NSHostingView(rootView: root)
window.isReleasedWhenClosed = false
window.center()
window.delegate = self
return window
}
/// Hide, never terminate: this is the only window an accessory app has.
public func windowShouldClose(_ sender: NSWindow) -> Bool {
sender.orderOut(nil)
return false
}
}
/// The selected tab, shared between the controller and the view.
///
/// A tiny observable box rather than `@State` inside `SettingsView`, because the
/// menu bar has to be able to open a particular pane on a window that already
/// exists.
@MainActor final class PaneSelection: ObservableObject {
@Published var selected: SettingsPane = .machines
var binding: Binding<SettingsPane> {
Binding(get: { self.selected }, set: { self.selected = $0 })
}
}