Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280public import AppKitimport Observationpublic import ParcelConfigpublic import ParcelPlatformimport SwiftUI
@MainActor @Observablepublic final class AlwaysFloatConfirmationModel { public let title: String public let matchesTitle: Bool public let matchesRole: Bool public private(set) var failure: String? public private(set) var isBusy = false private let undo: @MainActor () async throws -> Void public var onDismiss: @MainActor () -> Void = {}
public init( title: String, matchesTitle: Bool = true, matchesRole: Bool = true, undo: @escaping @MainActor () async throws -> Void ) { self.title = title self.matchesTitle = matchesTitle self.matchesRole = matchesRole self.undo = undo }
public func undoRule() async { guard !isBusy else { return } isBusy = true failure = nil defer { isBusy = false } do { try await undo() onDismiss() } catch { failure = error.localizedDescription } }}
enum AlwaysFloatConfirmationTiming: String, CaseIterable { case standard, extended, untilDismissed
static let preferenceKey = "windowRules.confirmationTiming"
var duration: Duration? { switch self { case .standard: .seconds(12) case .extended: .seconds(60) case .untilDismissed: nil } }
var title: String { switch self { case .standard: SettingsText.string("12 seconds") case .extended: SettingsText.string("60 seconds") case .untilDismissed: SettingsText.string("Until dismissed") } }
static func current(defaults: UserDefaults = .standard) -> Self { guard let value = defaults.string(forKey: preferenceKey) else { return .standard } guard let timing = Self(rawValue: value) else { preconditionFailure("Unknown rule confirmation timing") } return timing }}
struct RuleConfirmationCountdown { private(set) var remaining: Duration?
static func isPaused(hovered: Bool, focused: Bool, voiceOver: Bool, busy: Bool, hasFailure: Bool) -> Bool { hovered || focused || voiceOver || busy || hasFailure }
mutating func advance(by elapsed: Duration, paused: Bool) -> Bool { guard !paused, let remaining else { return false } self.remaining = max(.zero, remaining - elapsed) return self.remaining == .zero }}
struct RuleConfirmationScheduler: Sendable { var now: @MainActor () -> ContinuousClock.Instant var sleep: @MainActor (Duration) async throws -> Void
static let continuous = Self(now: { ContinuousClock.now }, sleep: { try await Task.sleep(for: $0) })}
@MainActorpublic final class AlwaysFloatConfirmationController { private var panel: LearningPanel? private var expiration: Task<Void, Never>? private var hovered = false private let sensory: FeedbackSensory private let announce: @MainActor (String) -> Void private let voiceOver: @MainActor () -> Bool private let focused: @MainActor (NSPanel) -> Bool private let duration: @MainActor () -> Duration? private let scheduler: RuleConfirmationScheduler var isVisible: Bool { panel?.isVisible == true }
public convenience init( sensory: FeedbackSensory = FeedbackSensory(), announce: (@MainActor (String) -> Void)? = nil, voiceOver: @escaping @MainActor () -> Bool = { VoiceOverStatus.isRunning } ) { self.init( sensory: sensory, announce: announce ?? Self.announce, voiceOver: voiceOver, focused: { $0.isKeyWindow }, duration: { AlwaysFloatConfirmationTiming.current().duration }, scheduler: .continuous) }
init( sensory: FeedbackSensory, announce: @escaping @MainActor (String) -> Void, voiceOver: @escaping @MainActor () -> Bool, focused: @escaping @MainActor (NSPanel) -> Bool, duration: @escaping @MainActor () -> Duration?, scheduler: RuleConfirmationScheduler ) { self.sensory = sensory self.announce = announce self.voiceOver = voiceOver self.focused = focused self.duration = duration self.scheduler = scheduler }
public func show( model: AlwaysFloatConfirmationModel, configuration: MotionConfiguration, undoShortcut: KeyChord? = nil ) { close() let panel = LearningPanel(title: SettingsText.string("Always float rule saved"), interactive: true) panel.setAccessibilityIdentifier("rules.confirmation") panel.contentView = NSHostingView( rootView: AlwaysFloatConfirmationView(model: model, undoShortcut: undoShortcut) { [weak self] in self?.setHovered($0) }) panel.contentView?.wantsLayer = true if let screen = NSScreen.main ?? NSScreen.screens.first { panel.setFrame( CGRect(x: screen.visibleFrame.midX - 240, y: screen.visibleFrame.maxY - 240, width: 480, height: 220), display: false) } model.onDismiss = { [weak self] in self?.close() } self.panel = panel panel.orderFrontRegardless() sensory.notice( configuration: configuration, reduceMotion: NSWorkspace.shared.accessibilityDisplayShouldReduceMotion) if let layer = panel.contentView?.layer { LearningMotion.show(layer, token: .pop, configuration: configuration) } announce(Self.savedAnnouncement(title: model.title, undoShortcut: undoShortcut)) guard let duration = duration() else { return } let scheduler = scheduler expiration = Task { @MainActor [weak self] in var previous = scheduler.now() var countdown = RuleConfirmationCountdown(remaining: duration) while !Task.isCancelled { guard let paused = self?.isPaused(model) else { return } do { try await scheduler.sleep(.milliseconds(250)) } catch is CancellationError { return } catch { preconditionFailure("Unexpected confirmation timer failure: \(error)") } guard !Task.isCancelled, let self else { return } let now = scheduler.now() let stillPaused = self.isPaused(model) if countdown.advance(by: previous.duration(to: now), paused: paused || stillPaused) { self.close() return } previous = now } } }
public func announceAlreadySaved(_ title: String) { announce( SettingsText.string("This window already has an always-float rule. Review Settings → Apps.") + " " + title) }
public func close() { expiration?.cancel() expiration = nil panel?.close() panel = nil hovered = false }
static func savedAnnouncement(title: String, undoShortcut: KeyChord?) -> String { var announcement = SettingsText.string("Always float rule saved") + ". " + title + ". " + SettingsText.string("Undo available in Settings → Apps or from the Parcel menu.") if let undoShortcut { announcement += " " + SettingsText.string("Undo shortcut: \(undoShortcut.accessibilityLabel).") } return announcement }
func setHovered(_ hovered: Bool) { self.hovered = hovered } func waitForExpiration() async { await expiration?.value }
private func isPaused(_ model: AlwaysFloatConfirmationModel) -> Bool { RuleConfirmationCountdown.isPaused( hovered: hovered, focused: panel.map(focused) == true, voiceOver: voiceOver(), busy: model.isBusy, hasFailure: model.failure != nil) }
private static func announce(_ text: String) { NSAccessibility.post( element: NSApplication.shared, notification: .announcementRequested, userInfo: [.announcement: text, .priority: NSAccessibilityPriorityLevel.medium.rawValue]) }
deinit { expiration?.cancel() }}
private struct AlwaysFloatConfirmationView: View { let model: AlwaysFloatConfirmationModel let undoShortcut: KeyChord? let hover: (Bool) -> Void @Environment(\.accessibilityReduceTransparency) private var reduceTransparency
var body: some View { VStack(alignment: .leading, spacing: 10) { Text(SettingsText.string("Always float rule saved")).font(.headline) Text(model.title).lineLimit(2) if model.matchesTitle { Text( SettingsText.string( "Only this app and matching window title float. Manage the rule in Settings → Apps.") ) .font(.caption) } else if model.matchesRole { Text(SettingsText.string("This rule applies to this app’s windows with the same accessibility role.")) .font(.caption) } else { Text(SettingsText.string("This rule applies to all windows of this app.")) .font(.caption) } Text(SettingsText.string("Undo available in Settings → Apps or from the Parcel menu.")) .font(.caption) if let undoShortcut { Text(SettingsText.string("Undo shortcut: \(undoShortcut.displayString).")) .font(.caption) .accessibilityLabel(SettingsText.string("Undo shortcut: \(undoShortcut.accessibilityLabel).")) } if let failure = model.failure { Text(failure).textSelection(.enabled) } HStack { Button(SettingsText.string("Undo rule")) { Task { await model.undoRule() } } .disabled(model.isBusy) .accessibilityIdentifier("rules.confirmation.undo") Button(SettingsText.string("Dismiss")) { model.onDismiss() } .accessibilityIdentifier("rules.confirmation.dismiss") } } .padding(16) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) .background { if reduceTransparency { RoundedRectangle(cornerRadius: 14).fill(Color(nsColor: .windowBackgroundColor)) } else { RoundedRectangle(cornerRadius: 14).fill(.regularMaterial) } } .onHover(perform: hover) }}
@MainActorpublic final class AppTilingMenuItem: NSMenuItem { private let invoke: @MainActor () -> Void
public init(appName: String, isTiling: Bool, invoke: @escaping @MainActor () -> Void) { self.invoke = invoke let title = SettingsText.string("Tile app") + " — " + appName super.init(title: title, action: #selector(toggle), keyEquivalent: "") target = self state = isTiling ? .on : .off setAccessibilityLabel(title) }
required init(coder: NSCoder) { fatalError("init(coder:) is unavailable") } @objc private func toggle() { invoke() }}