Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import AppKitimport SwiftUI
// Find is a standard window-local editing command, not a global product shortcut.enum SettingsSearchDefaults { static let findKey = "f" static let findModifiers: NSEvent.ModifierFlags = .command
static func isFind(_ event: NSEvent) -> Bool { event.charactersIgnoringModifiers?.lowercased() == findKey && event.modifierFlags.intersection([.command, .control, .option, .shift]) == findModifiers }}
struct SettingsSearchField: NSViewRepresentable { let model: SettingsSearchModel let select: (SettingsControl) -> Void
func makeCoordinator() -> Coordinator { Coordinator(model: model, select: select) }
func makeNSView(context: Context) -> SearchField { let field = SearchField() field.delegate = context.coordinator field.sendsSearchStringImmediately = true field.sendsWholeSearchString = false field.setContentHuggingPriority(.defaultLow, for: .horizontal) field.setAccessibilityLabel(SettingsText.string("Search Settings")) field.setAccessibilityIdentifier("settings.search.field") field.onFind = { [weak field] in model.handle(.find) field?.window?.makeFirstResponder(field) } return field }
func updateNSView(_ field: SearchField, context: Context) { field.placeholderString = SettingsText.string("Search Settings") if field.stringValue != model.query { field.stringValue = model.query } context.coordinator.select = select if context.coordinator.focusRequest != model.focusRequest { context.coordinator.focusRequest = model.focusRequest field.window?.makeFirstResponder(field) } }
static func dismantleNSView(_ field: SearchField, coordinator: Coordinator) { field.stopMonitoring() }
final class Coordinator: NSObject, NSSearchFieldDelegate { let model: SettingsSearchModel var select: (SettingsControl) -> Void var focusRequest = 0
init(model: SettingsSearchModel, select: @escaping (SettingsControl) -> Void) { self.model = model self.select = select }
func controlTextDidChange(_ notification: Notification) { guard let field = notification.object as? NSSearchField else { preconditionFailure("Unexpected settings search sender") } model.query = field.stringValue }
func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool { let command: SettingsSearchCommand switch NSStringFromSelector(selector) { case "moveDown:": command = .next case "moveUp:": command = .previous case "insertNewline:": command = .choose case "cancelOperation:": guard !model.query.isEmpty else { return false } command = .cancel default: return false } if let chosen = model.handle(command) { select(chosen) } return true } }
final class SearchField: NSSearchField { var onFind: (() -> Void)? private var removeMonitor: (() -> Void)?
override func viewDidMoveToWindow() { super.viewDidMoveToWindow() stopMonitoring() guard window != nil else { return } guard let monitor = NSEvent.addLocalMonitorForEvents( matching: .keyDown, handler: { [weak self] event in let handled = MainActor.assumeIsolated { self?.handleFind(event) ?? false } return handled ? nil : event }) else { preconditionFailure("Could not install the Settings Find command") } removeMonitor = { NSEvent.removeMonitor(monitor) } }
func handleFind(_ event: NSEvent) -> Bool { guard let window, event.windowNumber == window.windowNumber, SettingsSearchDefaults.isFind(event) else { return false } onFind?() return true }
func stopMonitoring() { removeMonitor?() removeMonitor = nil } }}