Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200public import AppKitimport Observationpublic import ParcelConfigpublic import ParcelCoreimport SwiftUI
@MainActor@Observablepublic final class WindowControlsController { public var hoverPreferences: HoverBarConfiguration { settingsModel?.configuration.hoverBar ?? storedHoverPreferences } public var onHoverPreferencesChanged: (@MainActor (HoverBarConfiguration) -> Void)? public var onChromeChanged: (@MainActor () -> Void)? public var onStackedWindows: (@MainActor () -> Void)? private let settingsModel: SettingsModel? private var storedHoverPreferences: HoverBarConfiguration @ObservationIgnored public let chips: StackChipController @ObservationIgnored public let hover: HoverBarController @ObservationIgnored private var panel: NSWindow? private let defaults: UserDefaults private let failure: @MainActor (any Error) -> Void private let hoverFocusToggle: (@MainActor () -> Void)? private var state = ParcelCore.State() private var configuration = ParcelConfiguration.defaults private var active = false private weak var hoverFocusMenuItem: WindowControlsMenuItem?
public init( defaults: UserDefaults = .standard, settingsModel: SettingsModel? = nil, focus: @escaping @MainActor (WindowID) -> Void, close: @escaping @MainActor (WindowID) -> Void, cycle: (@MainActor (WindowID, CycleDirection) -> Void)? = nil, move: (@MainActor (WindowID, WindowID) -> Void)? = nil, hoverAction: (@MainActor (HoverActionRequest) async throws -> Void)? = nil, hoverFocusToggle: (@MainActor () -> Void)? = nil, onInput: @escaping @MainActor (TourInput) -> Void = { _ in }, onFailure: @escaping @MainActor (any Error) -> Void ) throws { self.defaults = defaults self.settingsModel = settingsModel failure = onFailure self.hoverFocusToggle = hoverFocusToggle chips = StackChipController( focus: { window in onInput(.chip(window)) focus(window) }, close: close, cycle: cycle, move: move) hover = HoverBarController { onInput(.mouse) } hover.onAction = hoverAction hover.onDismiss = focus hover.onFailure = onFailure chips.usesGrip = true if settingsModel == nil, let stored = defaults.object(forKey: "hoverBar.v1") { guard let data = stored as? Data else { throw HoverPreferenceError.invalid } storedHoverPreferences = try JSONDecoder().decode(HoverBarConfiguration.self, from: data) guard storedHoverPreferences.isValid else { throw HoverPreferenceError.invalid } } else { storedHoverPreferences = HoverBarConfiguration() } hover.onSelectTab = { window in onInput(.chip(window)) focus(window) } hover.onTabDragBegan = { [weak chips] source, point in chips?.beginDrag(source, at: point) } hover.onTabDragChanged = { [weak chips] point in chips?.updateDrag(at: point) } hover.onTabDragEnded = { [weak chips] point in chips?.endDrag(at: point) } hover.onTabDragCancelled = { [weak chips] in chips?.cancelDrag() } hover.onPresentationChanged = { [weak self] _ in self?.onChromeChanged?() } }
public func start() throws { try hover.start() }
public func update(state: ParcelCore.State, configuration: ParcelConfiguration, active: Bool) { self.state = state self.configuration = configuration self.active = active hoverFocusMenuItem?.state = state.hoverFocusEnabled ? .on : .off hoverFocusMenuItem?.isEnabled = active if active { if configuration.appearance.stackControls == .persistent { chips.update(state: state, configuration: configuration.motion) } else { chips.updateDragContext(state: state, configuration: configuration.motion) } hover.update(state: state, configuration: configuration, preferences: hoverPreferences) } else { chips.stop() hover.update(state: ParcelCore.State(), configuration: configuration, preferences: hoverPreferences) hover.hide(includingGrips: true) } }
public func setHoverPreferences(_ preferences: HoverBarConfiguration) { guard preferences.isValid else { failure(HoverPreferenceError.invalidUpdate) return } if settingsModel != nil { guard let onHoverPreferencesChanged else { failure(HoverPreferenceError.writerUnavailable) return } onHoverPreferencesChanged(preferences) return } do { defaults.set(try JSONEncoder().encode(preferences), forKey: "hoverBar.v1") storedHoverPreferences = preferences if active { hover.update(state: state, configuration: configuration, preferences: preferences) } } catch { failure(error) } }
public func presentPreferences() { if panel == nil { let window = NSWindow( contentRect: CGRect(x: 0, y: 0, width: 440, height: 220), styleMask: [.titled, .closable], backing: .buffered, defer: false) window.title = UIStrings.text("Shortcut hints") window.isReleasedWhenClosed = false AppWindowPresentation.configure(window) window.contentView = NSHostingView(rootView: HoverPreferencesView(controller: self)) window.center() panel = window } NSApplication.shared.activate() panel?.makeKeyAndOrderFront(nil) }
public func stop() { hover.stop() chips.stop() panel?.close() panel = nil state = ParcelCore.State() active = false hoverFocusMenuItem?.state = .off hoverFocusMenuItem?.isEnabled = false }}
private enum HoverPreferenceError: LocalizedError { case invalid, invalidUpdate, writerUnavailable var errorDescription: String? { switch self { case .invalid: UIStrings.text( "Your hover delay couldn't be read. Reset hoverBar.v1 in Parcel's preferences to start fresh.") case .invalidUpdate: String(localized: "Hover delay must be between 0.1 and 3 seconds.", table: "Settings", bundle: .module) case .writerUnavailable: String( localized: "Hover bar settings could not be saved. Open Settings and try again.", table: "Settings", bundle: .module) } }}
@MainActorfinal class WindowControlsMenuItem: NSMenuItem { private let invoke: @MainActor () -> Void init(title: String, invoke: @escaping @MainActor () -> Void) { self.invoke = invoke super.init(title: title, action: #selector(openPreferences), keyEquivalent: "") target = self setAccessibilityLabel(title) } required init(coder: NSCoder) { fatalError("init(coder:) is unavailable") } @objc private func openPreferences() { invoke() }}
extension WindowControlsController { public func installMenuItem(in menu: NSMenu, at index: Int) { precondition((0...menu.numberOfItems).contains(index)) let preceding = menu.items.prefix(index).filter { $0 is WindowControlsMenuItem }.count for item in menu.items where item is WindowControlsMenuItem { menu.removeItem(item) } let insertion = index - preceding menu.insertItem( WindowControlsMenuItem(title: UIStrings.text("Stacked Windows…")) { [weak self] in self?.onStackedWindows?() }, at: insertion) menu.insertItem( WindowControlsMenuItem(title: UIStrings.text("Shortcut Hints…")) { [weak self] in self?.presentPreferences() }, at: insertion + 1) menu.insertItem( WindowControlsMenuItem(title: UIStrings.text("Window Controls…")) { [weak self] in guard let self else { return } if !self.hover.focusControls() { self.failure(HoverActionError.unavailableWindow) } }, at: insertion + 2) if let hoverFocusToggle { let item = WindowControlsMenuItem(title: UIStrings.text("Hover focus"), invoke: hoverFocusToggle) item.state = state.hoverFocusEnabled ? .on : .off item.isEnabled = active hoverFocusMenuItem = item menu.insertItem(item, at: insertion + 3) } }}