Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227public import AppKitimport QuartzCoreimport os
// Animation depends only on Core; matching lanes must not pull in Platform.enum MotionSignposts { static let signposter = OSSignposter(subsystem: "com.parcel", category: "Motion")}
@MainActorprotocol DisplayLinkSource: AnyObject { var isRunning: Bool { get } func start(tick: @escaping @MainActor (Double) -> Void) func stop()}
@MainActorpublic protocol ReduceMotionSource: AnyObject { var isEnabled: Bool { get } func start(changed: @escaping @MainActor () -> Void) func stop()}
@MainActorpublic final class DisplayLinkDriver { public private(set) var animator: Animator public var isRunning: Bool { source.isRunning } private let source: any DisplayLinkSource private let reduceMotionSource: (any ReduceMotionSource)? private var requestedPolicy: MotionPolicy private var sourceGeneration: UInt64 = 0 private let clock: @MainActor () -> Double private let onFrames: @MainActor ([MotionFrame]) -> Void private let onFailure: @MainActor (MotionError) -> Void
public convenience init( screen: NSScreen, policy: MotionPolicy = MotionPolicy(), settings: MotionSettings = MotionSettings(), reduceMotionSource: (any ReduceMotionSource)? = nil, onFrames: @escaping @MainActor ([MotionFrame]) -> Void, onFailure: @escaping @MainActor (MotionError) -> Void ) { self.init( source: ScreenDisplayLink(screen: screen), animator: Animator(policy: policy, settings: settings), reduceMotionSource: reduceMotionSource ?? WorkspaceReduceMotionSource(), onFrames: onFrames, onFailure: onFailure ) }
init( source: any DisplayLinkSource, animator: Animator = Animator(), reduceMotionSource: (any ReduceMotionSource)? = nil, clock: @escaping @MainActor () -> Double = { CACurrentMediaTime() }, onFrames: @escaping @MainActor ([MotionFrame]) -> Void, onFailure: @escaping @MainActor (MotionError) -> Void ) { precondition(animator.isIdle) self.source = source self.reduceMotionSource = reduceMotionSource self.requestedPolicy = animator.policy self.clock = clock var policy = animator.policy policy.reduceMotion = policy.reduceMotion || reduceMotionSource?.isEnabled == true self.animator = Animator(policy: policy, settings: animator.settings) self.onFrames = onFrames self.onFailure = onFailure reduceMotionSource?.start { [weak self] in self?.accessibilityChanged() } }
isolated deinit { source.stop() reduceMotionSource?.stop() }
public func schedule(_ requests: [MotionRequest], at timestamp: Double? = nil) throws { try animator.schedule(requests, at: resolvedTime(timestamp)) refreshSource() }
public func updatePolicy(_ policy: MotionPolicy, at timestamp: Double? = nil) throws { var effective = policy effective.reduceMotion = policy.reduceMotion || reduceMotionSource?.isEnabled == true try animator.updatePolicy(effective, at: resolvedTime(timestamp)) requestedPolicy = policy refreshSource() }
public func updateSettings(_ settings: MotionSettings, at timestamp: Double? = nil) throws { try animator.updateSettings(settings, at: resolvedTime(timestamp)) refreshSource() }
public func cancel(_ windowID: MotionWindowID) { animator.cancel(windowID) refreshSource() }
public func stop() { source.stop() animator.cancelAll() }
private func resolvedTime(_ timestamp: Double?) -> Double { if let timestamp { return timestamp } // Display links predict presentation time, which can be ahead of the command clock. return max(clock(), animator.timestamp ?? -.infinity) }
private func accessibilityChanged() { do { try updatePolicy(requestedPolicy) } catch { fail(error) } }
private func refreshSource() { if animator.isIdle { source.stop() } else if !source.isRunning { sourceGeneration &+= 1 let generation = sourceGeneration source.start { [weak self] timestamp in self?.tick(at: timestamp, generation: generation) } } }
private func tick(at timestamp: Double, generation: UInt64) { guard source.isRunning, sourceGeneration == generation else { return } let signposter = MotionSignposts.signposter let windows = animator.activeWindowCount let interval = signposter.isEnabled ? signposter.beginInterval( "Tick", id: signposter.makeSignpostID(), "windows=\(windows, privacy: .public)") : nil defer { if let interval { signposter.endInterval("Tick", interval) } } do { let frames = try animator.tick(at: timestamp) refreshSource() if !frames.isEmpty { onFrames(frames) } } catch { fail(error) } }
private func fail(_ error: any Error) { guard let error = error as? MotionError else { preconditionFailure("Unexpected animation error: \(error)") } stop() onFailure(error) }}
@MainActorprivate final class WorkspaceReduceMotionSource: ReduceMotionSource { var isEnabled: Bool { NSWorkspace.shared.accessibilityDisplayShouldReduceMotion } private var observer: (any NSObjectProtocol)?
func start(changed: @escaping @MainActor () -> Void) { stop() observer = NSWorkspace.shared.notificationCenter.addObserver( forName: NSWorkspace.accessibilityDisplayOptionsDidChangeNotification, object: nil, queue: .main ) { _ in Task { @MainActor in changed() } } }
func stop() { if let observer { NSWorkspace.shared.notificationCenter.removeObserver(observer) } observer = nil }
isolated deinit { stop() }}
@MainActorstruct RegisteredDisplayLinkCallback<Source: AnyObject> { private let source: Source private let callback: @MainActor (Double) -> Void
init(source: Source, callback: @escaping @MainActor (Double) -> Void) { self.source = source self.callback = callback }
func tick(from source: Source, at timestamp: Double) { guard source === self.source else { return } callback(timestamp) }}
@MainActorprivate final class ScreenDisplayLink: DisplayLinkSource { let screen: NSScreen private var link: CADisplayLink? private var callback: RegisteredDisplayLinkCallback<CADisplayLink>? var isRunning: Bool { link != nil }
init(screen: NSScreen) { self.screen = screen }
func start(tick: @escaping @MainActor (Double) -> Void) { guard link == nil else { return } let relay = DisplayLinkRelay(source: self) let link = screen.displayLink(target: relay, selector: #selector(DisplayLinkRelay.tick(_:))) self.link = link callback = RegisteredDisplayLinkCallback(source: link, callback: tick) link.add(to: .main, forMode: .common) }
func stop() { link?.invalidate() link = nil callback = nil }
func tick(_ link: CADisplayLink) { guard link === self.link else { return } let signposter = MotionSignposts.signposter if signposter.isEnabled { signposter.emitEvent( "Display refresh", "interval=\(link.targetTimestamp - link.timestamp, privacy: .public)") } callback?.tick(from: link, at: link.targetTimestamp) }}
@MainActorprivate final class DisplayLinkRelay: NSObject { weak var source: ScreenDisplayLink? init(source: ScreenDisplayLink) { self.source = source } @objc func tick(_ link: CADisplayLink) { source?.tick(link) }}