Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149public import AppKitimport CoreGraphicsimport os
public struct AssistiveTechnology: Sendable, Equatable { public let voiceOver: Bool public let switchControl: Bool
public init(voiceOver: Bool, switchControl: Bool) { self.voiceOver = voiceOver self.switchControl = switchControl }
@MainActor public static var current: Self { Self(voiceOver: VoiceOverStatus.isRunning, switchControl: NSWorkspace.shared.isSwitchControlEnabled) }}
public enum FrameWriteStep: Sendable, Equatable { case size(CGSize) case position(CGPoint)}
public struct FrameWritePlan: Sendable, Equatable { public let steps: [FrameWriteStep]
public init(current: CGRect, target: CGRect) { self.steps = current == target ? [] : [.size(target.size), .position(target.origin), .size(target.size)] }}
public struct FrameWriteFailure: Error, Sendable { public let writeError: any Error public let restoreError: any Error}
protocol FrameWriteAccess { var frame: CGRect { get throws } var size: CGSize { get throws } var position: CGPoint { get throws } var enhancedUI: Bool { get throws } func setSize(_ value: CGSize) throws func setPosition(_ value: CGPoint) throws func setEnhancedUI(_ value: Bool) throws func recordSizeCorrection(_ requested: CGSize, actual: CGRect, attempt: Int)}
extension FrameWriteAccess { func recordSizeCorrection(_ requested: CGSize, actual: CGRect, attempt: Int) {}}
// Every frame setter, including stale retries and origin repair, spends the same owner-thread budget.final class FrameWriteBudget { let limit: Int private let isCancelled: () -> Bool private(set) var count = 0 private(set) var refused = false var remaining: Int { limit - count } var limitReached: Bool { refused || count == limit }
init(limit: Int = AppThreadFrameWriteDiagnostics.maximumSetters, isCancelled: @escaping () -> Bool = { false }) { precondition((0...AppThreadFrameWriteDiagnostics.maximumSetters).contains(limit)) self.limit = limit self.isCancelled = isCancelled }
func stopFitting() { refused = true }
func spend() throws { if isCancelled() { throw CancellationError() } guard remaining > 0 else { refused = true throw FrameWriteBudgetExhausted() } count += 1 }}
struct FrameWriteBudgetExhausted: Error {}
struct BudgetedFrameWriteAccess<Window: FrameWriteAccess>: FrameWriteAccess { let window: Window let budget: FrameWriteBudget var frame: CGRect { get throws { try window.frame } } var size: CGSize { get throws { try window.size } } var position: CGPoint { get throws { try window.position } } var enhancedUI: Bool { get throws { try window.enhancedUI } } func setSize(_ value: CGSize) throws { try budget.spend() try window.setSize(value) } func setPosition(_ value: CGPoint) throws { try budget.spend() try window.setPosition(value) } func setEnhancedUI(_ value: Bool) throws { try window.setEnhancedUI(value) } func recordSizeCorrection(_ requested: CGSize, actual: CGRect, attempt: Int) { window.recordSizeCorrection(requested, actual: actual, attempt: attempt) }}
extension AXElement: FrameWriteAccess { func recordSizeCorrection(_ requested: CGSize, actual: CGRect, attempt: Int) { guard AppThreadWriteTrace.enabled else { return } do { AppThreadWriteTrace.sizeCorrection( window: try windowID(), requested: requested, actual: actual, attempt: attempt) } catch { Self.reportSizeCorrectionIdentityFailure(error) } }
private static func reportSizeCorrectionIdentityFailure(_ error: any Error) { Logger(subsystem: "com.parcel", category: "ax.sizeCorrection").error( "Correction window identification failed: \(String(describing: error), privacy: .public)") }}
func executeFrameWrite<Window: FrameWriteAccess, Application: FrameWriteAccess>( _ target: CGRect, window: Window, application: Application, assistiveTechnology: AssistiveTechnology) throws -> CGRect { let current = try window.frame guard current != target else { return current } var enhancedUI = EnhancedUIBurstGuard() let actual: CGRect do { try enhancedUI.prepare(application: application, assistiveTechnology: assistiveTechnology) guard let readback = try executeMotionFrameWrite(target, window: window, current: current, final: true) else { preconditionFailure("A final frame write must read back its result") } actual = readback } catch { do { try enhancedUI.restore(application: application) } catch let restoreError { throw FrameWriteFailure(writeError: error, restoreError: restoreError) } throw error } try enhancedUI.restore(application: application) return actual}
extension AXElement { func writeFrame(_ target: CGRect, application: AXElement, assistiveTechnology: AssistiveTechnology) throws -> CGRect { try executeFrameWrite(target, window: self, application: application, assistiveTechnology: assistiveTechnology) }}