Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308import Foundationimport ParcelConfigimport ParcelUI
@MainActorprotocol AppConfigurationSource: AnyObject { func start() async throws -> ConfigSnapshot func updates() async -> AsyncStream<ConfigSnapshot> func reload() async -> ConfigSnapshot func stop() async}
@MainActorfinal class AppConfigStore: AppConfigurationSource { let store: ConfigStore init(store: ConfigStore) { self.store = store } func start() async throws -> ConfigSnapshot { try await store.createDefaultFileIfMissing() let snapshot = await store.reload() try await store.startWatching() return snapshot } func updates() async -> AsyncStream<ConfigSnapshot> { await store.updates() } func reload() async -> ConfigSnapshot { await store.reload() } func stop() async { await store.stopWatching() }}
@MainActorprotocol AppRuntime: AnyObject { func claimInstance() throws -> Bool func releaseInstance() func resetBrightness() async throws func permissionGranted() async -> Bool func permissionUpdates() -> AsyncStream<PermissionStatus> func startEngine(configuration: ParcelConfiguration) async throws func applyConfiguration(_ configuration: ParcelConfiguration) throws func startIPC() async throws func registerHotkeys(_ configuration: ParcelConfiguration) throws -> [ConfigCommand: KeyChord] func startDragMonitor() throws func startShell() async throws func showOnboarding() func showSettings() func reopen() func showNotice(_ notice: String?) func showProblem(_ problem: MenuProblem?) func showNotice(_ notice: String?, actions: [NoticeAction]) func showNotice(_ report: AppNotices.Report) func clearNotice(_ kind: AppNotices.Kind) func setLaunchAtLogin(_ enabled: Bool) throws func flushActivityOnQuit() func stopInput() throws func restoreAndStopEngine() async throws func stopIPC() async throws func stopShell() async}
extension AppRuntime { func reopen() { showSettings() } func flushActivityOnQuit() {} func showNotice(_ report: AppNotices.Report) { showNotice(report.message, actions: report.actions) } func clearNotice(_ kind: AppNotices.Kind) { showNotice(nil, actions: []) }}
@MainActorfinal class AppComposition { enum Phase: Equatable { case idle, starting, waitingForPermission, running, stopping, stopped, duplicate } private(set) var phase = Phase.idle private(set) var configuration = ParcelConfiguration.defaults private(set) var loadState = ConfigLoadState.notLoaded private(set) var diagnostics: [ConfigDiagnostic] = [] private let source: any AppConfigurationSource private let runtime: any AppRuntime private var updatesTask: Task<Void, Never>? private var permissionTask: Task<Void, Never>? private var ownsInstance = false private var loginSetting: Bool? private var registeredBindings: [ConfigCommand: KeyChord]? private var desiredConfiguration = ParcelConfiguration.defaults private var engineConfiguration: ParcelConfiguration? private var configNotice: String? let notices = AppNotices() private var renderedProblem: MenuProblem?
init(source: any AppConfigurationSource, runtime: any AppRuntime) { self.source = source self.runtime = runtime }
@discardableResult func start() async throws -> Bool { guard phase == .idle else { return phase != .duplicate } guard try runtime.claimInstance() else { phase = .duplicate return false } ownsInstance = true phase = .starting do { accept(try await source.start()) let updates = await source.updates() updatesTask = Task { @MainActor [weak self] in for await snapshot in updates { guard !Task.isCancelled, let self else { return } self.accept(snapshot) } } do { try await runtime.resetBrightness() clearNotice(.brightness) } catch { report(error, kind: .brightness) } try Task.checkCancellation() // Diagnostics must not wait for window discovery or setup services. try await runtime.startIPC() if await runtime.permissionGranted() { try await startTiling() try applyDesiredConfiguration() do { try runtime.startDragMonitor() } catch { report(error, kind: .input) } try await runtime.startShell() } else { phase = .waitingForPermission try await runtime.startShell() runtime.showOnboarding() } reportConfigurationWarning() let permissions = runtime.permissionUpdates() permissionTask = Task { @MainActor [weak self] in for await status in permissions { guard !Task.isCancelled, let self else { return } guard status != .checking else { continue } await self.permissionChanged(status == .granted) } } renderProblem() return true } catch { report(error, kind: .startup) await stop() throw error } }
func permissionChanged(_ granted: Bool) async { guard phase == .running || phase == .waitingForPermission else { return } if granted, phase == .waitingForPermission { phase = .starting do { try await startTiling() clearNotice(.input) try applyDesiredConfiguration() do { try runtime.startDragMonitor() } catch { report(error, kind: .input) } clearNotice(.permission) clearNotice(.startup) } catch { guard phase != .stopping, phase != .stopped else { return } report(error, kind: .startup) do { try runtime.stopInput() } catch { report(error, kind: .input) } do { try await runtime.restoreAndStopEngine() } catch { report(error, kind: .restoration) } engineConfiguration = nil registeredBindings = nil guard phase != .stopping, phase != .stopped else { return } phase = .waitingForPermission } } else if !granted, phase == .running { phase = .waitingForPermission report( AppText.failure("Accessibility access was lost. Enable Parcel in System Settings."), kind: .permission) do { try runtime.stopInput() } catch { report(error, kind: .input) } do { try await runtime.restoreAndStopEngine() } catch { report(error, kind: .restoration) } engineConfiguration = nil registeredBindings = nil guard phase == .waitingForPermission, !Task.isCancelled else { return } runtime.showOnboarding() } }
func retryStartup() async { guard phase == .waitingForPermission else { return } guard await runtime.permissionGranted(), phase == .waitingForPermission else { return } await permissionChanged(true) }
@discardableResult func reload() async -> ConfigSnapshot { let snapshot = await source.reload() accept(snapshot) return snapshot }
func showSettings() { runtime.showSettings() } func reopen() { runtime.reopen() }
func report(_ error: any Error, kind: AppNotices.Kind = .internalFailure) { guard !notices.recordDiagnosticIfNeeded(error, kind: kind) else { return } runtime.showNotice(notices.report(error, kind: kind)) renderProblem() }
func clearNotice(_ kind: AppNotices.Kind) { let wasReported = notices.counts[kind] != nil notices.clear(kind) if wasReported { runtime.clearNotice(kind) } renderProblem() }
func stop() async { guard phase != .stopped, phase != .stopping, phase != .duplicate else { return } phase = .stopping runtime.flushActivityOnQuit() let permissionTask = permissionTask self.permissionTask = nil permissionTask?.cancel() await permissionTask?.value let task = updatesTask updatesTask = nil task?.cancel() await task?.value do { try runtime.stopInput() } catch { report(error, kind: .input) } do { try await runtime.restoreAndStopEngine() } catch { report(error, kind: .restoration) } do { try await runtime.resetBrightness() } catch { report(error, kind: .brightness) } do { try await runtime.stopIPC() } catch { report(error, kind: .ipc) } await runtime.stopShell() await source.stop() if ownsInstance { runtime.releaseInstance() } ownsInstance = false engineConfiguration = nil registeredBindings = nil phase = .stopped }
private func startTiling() async throws { let startingConfiguration = desiredConfiguration engineConfiguration = startingConfiguration try await runtime.startEngine(configuration: startingConfiguration) try Task.checkCancellation() configuration = startingConfiguration phase = .running }
private func accept(_ snapshot: ConfigSnapshot) { guard phase != .stopping, phase != .stopped else { return } loadState = snapshot.loadState diagnostics = snapshot.diagnostics configNotice = AppText.configurationNotice(snapshot.diagnostics) desiredConfiguration = snapshot.activeConfiguration if phase == .running { do { try applyDesiredConfiguration() } catch { report(error, kind: .configuration) } } else if engineConfiguration == nil { configuration = desiredConfiguration } if phase == .running || phase == .waitingForPermission { reportConfigurationWarning() } if snapshot.diagnostics.isEmpty, phase != .running { clearNotice(.configuration) } if loginSetting != desiredConfiguration.general.launchAtLogin { do { try runtime.setLaunchAtLogin(desiredConfiguration.general.launchAtLogin) loginSetting = desiredConfiguration.general.launchAtLogin clearNotice(.login) } catch { report(error, kind: .login) } } renderProblem() }
private func applyDesiredConfiguration() throws { let updated = desiredConfiguration let previous = engineConfiguration if updated != engineConfiguration { try runtime.applyConfiguration(updated) engineConfiguration = updated } do { if updated.keybindings != registeredBindings || previous?.general.shortcutsEnabled != updated.general.shortcutsEnabled { registeredBindings = try runtime.registerHotkeys(updated) } } catch { if let previous, previous != engineConfiguration { do { try runtime.applyConfiguration(previous) engineConfiguration = previous } catch { report(error, kind: .configuration) } } throw error } configuration = updated clearNotice(.configuration) }
private func reportConfigurationWarning() { guard let warning = diagnostics.first(where: { $0.severity == .warning }) else { return } runtime.showNotice(notices.report(warning)) }
private func renderProblem() { let reason = notices.configurationReason ?? configNotice let problem = reason.map { MenuProblem.configuration(reason: $0) } guard problem != renderedProblem else { return } renderedProblem = problem runtime.showProblem(problem) }
deinit { updatesTask?.cancel() permissionTask?.cancel() }}