Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196import AppKitimport Foundation
struct MotionSpeedRuntimeReceipt: Codable { struct Motion: Codable { let enabled: Bool let level: String let speed: Double let transitions: String }
let path: String let running: Bool let motion: Motion
func requireApplied(config: URL, speed: Double) throws { guard path == config.path, running, motion.enabled, motion.level == "playful", motion.speed == speed, motion.transitions == "glide" else { throw RealityError("Running engine did not apply the requested isolated motion configuration") } }}
struct MotionSpeedSample: Codable, Sendable { let elapsedSeconds: Double let frames: [ScreenRect]}
struct MotionSpeedMeasurement: Codable, Sendable { let speed: Double let from: [ScreenRect] let to: [ScreenRect] let samples: [MotionSpeedSample] let observedMovementSeconds: Double}
enum MotionSpeedOracle { static func duration(samples: [MotionSpeedSample], from: [ScreenRect], to: [ScreenRect]) throws -> Double { guard !from.isEmpty, from.count == to.count, zip(from, to).allSatisfy({ !$0.matches($1) }), samples.count >= 4, samples.allSatisfy({ $0.elapsedSeconds.isFinite && $0.elapsedSeconds >= 0 && $0.frames.count == from.count } ), zip(samples, samples.dropFirst()).allSatisfy({ $0.elapsedSeconds < $1.elapsedSeconds }), matches(samples[0].frames, from), samples.suffix(3).allSatisfy({ matches($0.frames, to) }) else { throw RealityError("Motion samples lack a valid baseline or three converged target frames") }
guard let firstMoving = samples.firstIndex(where: { !$0.frames[0].matches(from[0]) && !$0.frames[0].matches(to[0]) }) else { throw RealityError("No intermediate CG frame was observed for the tracked window") } let firstTarget = samples.count - 3 guard firstMoving < firstTarget else { throw RealityError("Intermediate CG frame did not precede convergence") } let span = Array(samples[firstMoving...firstTarget]) guard zip(span, span.dropFirst()).allSatisfy({ $1.elapsedSeconds - $0.elapsedSeconds <= 0.2 }) else { throw RealityError("CG sampling stalled during the measured transition") } return samples[firstTarget].elapsedSeconds - samples[firstMoving].elapsedSeconds }
static func matches(_ frames: [ScreenRect], _ expected: [ScreenRect]) -> Bool { frames.count == expected.count && zip(frames, expected).allSatisfy { $0.matches($1) } }
static func requireSpeedDifference(fast: Double, slow: Double) throws { guard fast.isFinite, slow.isFinite, fast > 0, slow >= 0.5, slow > fast * 2, slow - fast >= 0.3 else { throw RealityError("Slow motion did not outlast fast motion: fast=\(fast)s, slow=\(slow)s") } }}
enum RealityScreenPrecondition { static func requireOwner(_ owner: String?, parentPID: Int32) throws { let fields = owner?.split(whereSeparator: \.isWhitespace) ?? [] guard fields.count == 2, parentPID > 1, Int32(fields[1]) == parentPID else { throw RealitySkip("This scenario requires ownership of Scripts/screen-lock.sh") } }
static func requireAvailableConsole() throws { let environment = ProcessInfo.processInfo.environment let path = environment["PARCEL_SCREEN_LOCK_PATH"] ?? "/tmp/parcel-screen.lock" let owner: String do { owner = try String(contentsOfFile: "\(path)/owner", encoding: .utf8) } catch { throw RealitySkip("Screen-lock ownership could not be verified: \(error)") } try requireOwner(owner, parentPID: getppid()) do { try TabViewConsoleSession.requireUnlockedConsole() } catch { throw RealitySkip("This scenario requires an active, unlocked console: \(error)") } }}
@MainActorfunc checkMotionSpeed(_ session: RealitySession) async throws { try RealityScreenPrecondition.requireAvailableConsole() guard !NSWorkspace.shared.accessibilityDisplayShouldReduceMotion else { throw RealitySkip("Reduce Motion is enabled; window transitions intentionally snap") } try session.configure() let windows = try await session.startingWindows([.textEdit, .textEdit]) try await session.start() try await session.tiles(windows) let initial = try session.readFrames(windows) let ordered = zip(windows, initial).sorted { $0.1.x < $1.1.x }.map(\.0) let baseline = try session.readFrames(ordered) guard !baseline[0].matches(baseline[1]) else { throw RealityError("Swap needs distinct CG tile frames") } let swapped = [baseline[1], baseline[0]]
try await setMotionSpeed(2, session: session) let fast = try await measureMotionSwap( session, windows: ordered, from: baseline, to: swapped, forward: true, speed: 2) try await setMotionSpeed(0.1, session: session) let slow = try await measureMotionSwap( session, windows: ordered, from: swapped, to: baseline, forward: false, speed: 0.1) try MotionSpeedOracle.requireSpeedDifference(fast: fast.observedMovementSeconds, slow: slow.observedMovementSeconds) session.recordExpectation( "CG-observed slow swap (\(slow.observedMovementSeconds)s) outlasts the reverse fast swap (\(fast.observedMovementSeconds)s)" )}
@MainActorprivate func setMotionSpeed(_ speed: Double, session: RealitySession) async throws { try RealityScreenPrecondition.requireAvailableConsole() guard !NSWorkspace.shared.accessibilityDisplayShouldReduceMotion else { throw RealitySkip("Reduce Motion became enabled during the motion speed scenario") } try session.configure(motionEnabled: true, motionSpeed: speed) let result = try await session.cli(["reload", "--json"]) guard result.status == 0 else { throw RealityError("Motion configuration reload failed: \(result.errors)") } struct Reload: Decodable { let reloaded: Bool; let path: String } let receipt = try JSONDecoder().decode(Reload.self, from: Data(result.output.utf8)) guard receipt.reloaded, receipt.path == session.config.path else { throw RealityError("Motion configuration reload did not acknowledge the isolated config") } let query = try await session.cli(["query", "--json", "config"]) guard query.status == 0 else { throw RealityError("Motion configuration query failed: \(query.errors)") } let applied = try JSONDecoder().decode(MotionSpeedRuntimeReceipt.self, from: Data(query.output.utf8)) try encodeJSON(applied, to: session.directory.appendingPathComponent("motion-speed-\(speed)-config.json")) try applied.requireApplied(config: session.config, speed: speed)}
@MainActorprivate func sendMotionSwap(_ session: RealitySession, forward: Bool) async throws { if forward { _ = try await session.command("swap", "right") } else { _ = try await session.command("swap", "left") }}
@MainActorprivate func measureMotionSwap( _ session: RealitySession, windows: [OwnedWindow], from: [ScreenRect], to: [ScreenRect], forward: Bool, speed: Double) async throws -> MotionSpeedMeasurement { try RealityScreenPrecondition.requireAvailableConsole() try await session.apps.focus(windows[0]) try await session.expect("Motion swap starts at its independently observed CG baseline") { MotionSpeedOracle.matches(try FrameOracle.frames($0, ids: windows.map(\.id)), from) } let clock = ContinuousClock() let start = clock.now var samples = [MotionSpeedSample(elapsedSeconds: 0, frames: try session.readFrames(windows))] async let command: Void = sendMotionSwap(session, forward: forward) do { var stable = 0 while start.duration(to: clock.now) < .seconds(8) { try await Task.sleep(for: .milliseconds(10)) let frames = try session.readFrames(windows) let elapsed = start.duration(to: clock.now).components samples.append( MotionSpeedSample( elapsedSeconds: Double(elapsed.seconds) + Double(elapsed.attoseconds) / 1e18, frames: frames)) stable = MotionSpeedOracle.matches(frames, to) ? stable + 1 : 0 if stable >= 3 { break } } try await command try encodeJSON(samples, to: session.directory.appendingPathComponent("motion-speed-\(speed)-samples.json")) let duration = try MotionSpeedOracle.duration(samples: samples, from: from, to: to) let measurement = MotionSpeedMeasurement( speed: speed, from: from, to: to, samples: samples, observedMovementSeconds: duration) try encodeJSON(measurement, to: session.directory.appendingPathComponent("motion-speed-\(speed).json")) try await session.expect("Motion swap converges to both exchanged CG frames") { MotionSpeedOracle.matches(try FrameOracle.frames($0, ids: windows.map(\.id)), to) } return measurement } catch { try encodeJSON(samples, to: session.directory.appendingPathComponent("motion-speed-\(speed)-samples.json")) throw error }}