Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206import Foundation
struct RealityFrameWriteReceipt: Decodable { struct Write: Decodable { let schemaVersion: Int let setterCount: Int let setterLimit: Int let limitReached: Bool } let frameWrite: Write? enum CodingKeys: String, CodingKey { case frameWrite = "frame_write" }
func requireBounded() throws { guard let frameWrite, frameWrite.schemaVersion == 1, frameWrite.setterLimit == 8, (0...frameWrite.setterLimit).contains(frameWrite.setterCount) else { throw RealityError("Frame fitting lacks a versioned, bounded AX setter receipt") } }}
@MainActorfunc retileWithoutNativeCollapse(_ session: RealitySession, window: OwnedWindow, stage: String) async throws { func sample() async throws -> [Observation] { var observations: [Observation] = [] let start = ContinuousClock.now repeat { let rows = try FrameOracle.read() let frame = try FrameOracle.frames(rows, ids: [window.id])[0] guard frame.width > 1, frame.height > 1 else { throw RealityError("Native fit transiently collapsed an owned window to one point") } try session.requireProtectedFrames() let duration = start.duration(to: .now).components observations.append( Observation( elapsedSeconds: Double(duration.seconds) + Double(duration.attoseconds) / 1e18, windows: rows)) try await Task.sleep(for: .milliseconds(10)) } while start.duration(to: .now) < .seconds(1) return observations } async let observations = sample() _ = try await session.command("retile") try await encodeJSON(observations, to: session.directory.appendingPathComponent("\(stage)-no-collapse.json")) let result = try await session.cli(["why", "--json", String(window.id)]) guard result.status == 0 else { throw RealityError("Frame-write why failed: \(result.errors)") } let receipt = try JSONDecoder().decode(RealityFrameWriteReceipt.self, from: Data(result.output.utf8)) try receipt.requireBounded() try encodeJSON(result, to: session.directory.appendingPathComponent("\(stage)-frame-write.json"))}
@MainActorfunc checkFloatWhenFull(_ session: RealitySession) async throws { try session.configure(cap: 2, whenFull: .float) let tiled = try await session.startingWindows([.textEdit, .textEdit]) try await session.start() try await session.tiles(tiled) let before = try session.readFrames(tiled) let gaps = try session.configuredLayout().gaps let overflow = try await session.apps.open(.textEdit) let all = tiled + [overflow] try await session.expect("Full display centers the new floating document without moving either existing tile") { let frames = try FrameOracle.frames($0, ids: all.map(\.id)) return CoverageOracle.unchanged(Array(frames.prefix(2)), baseline: before) && CoverageOracle.centeredFloat(frames[2], tiles: before, visible: session.visible, gaps: gaps) } let floating = try session.readFrames(all) try await session.command("retile") try await requireCoverageFramesStay(session, owned: all, baseline: floating, evidence: "full-float-retile") try await session.apps.close(tiled[1]) try await session.expect("Closing a tile expands its neighbor but never adopts the floating overflow") { let frames = try FrameOracle.frames($0, ids: [tiled[0].id, overflow.id]) return !$0.contains { $0.id == tiled[1].id } && frames[0].matches(session.visible.inset(gaps.outer), tolerance: 1) && frames[1].matches(floating[2], tolerance: 1) } try await session.apps.focus(overflow) try await session.command("float-toggle") try await session.tiles([tiled[0], overflow])}
@MainActorfunc checkAppMinimumStack(_ session: RealitySession) async throws { try session.configure(minSize: true) let owned = try await session.startingWindows([.safari, .notes, .systemSettings]) let initial = try session.readFrames(owned) var widths: [Double] = [] var maximumWidths: [Double?] = [] for (window, frame) in zip(owned, initial) { let requested = ScreenRect(x: frame.x, y: frame.y, width: 1, height: frame.height) try await session.apps.arrange(window, frame: requested, exactSize: false) let first = try session.readFrames([window])[0] try await session.apps.arrange(window, frame: requested, exactSize: false) let second = try session.readFrames([window])[0] guard first.matches(second, tolerance: 1), second.width > 1 else { throw RealityError("\(window.app.rawValue) did not expose a stable native minimum-width refusal") } widths.append(second.width) let available = session.visible.inset(try session.configuredLayout().gaps.outer) let growth = ScreenRect(x: available.x, y: frame.y, width: available.width, height: frame.height) try await session.apps.arrange(window, frame: growth, exactSize: false) let firstGrowth = try session.readFrames([window])[0] try await session.apps.arrange(window, frame: growth, exactSize: false) let secondGrowth = try session.readFrames([window])[0] guard firstGrowth.matches(secondGrowth, tolerance: 1), secondGrowth.width >= second.width - 1 else { throw RealityError("\(window.app.rawValue) did not expose a stable native growth limit") } maximumWidths.append(secondGrowth.width < growth.width - 1 ? secondGrowth.width : nil) try encodeJSON( [first, second, firstGrowth, secondGrowth], to: session.directory.appendingPathComponent("native-size-bounds-\(window.id).json")) try await session.apps.arrange(window, frame: frame, exactSize: false) } let geometry = try MinimumStackGeometry( minimumWidths: widths, maximumWidths: maximumWidths, screen: session.visible, gaps: session.configuredLayout().gaps) try encodeJSON(geometry, to: session.directory.appendingPathComponent("minimum-stack-geometry.json")) try session.configure( minSize: true, reservedLeft: geometry.reservedLeft, reservedRight: geometry.reservedRight) try await session.start() try await session.expect( "Three native app minimums reduce tile capacity to two; every app keeps its measured width" ) { CoverageOracle.minimumStack(try FrameOracle.frames($0, ids: owned.map(\.id)), geometry: geometry) } let stacked = try session.readFrames(owned) let members = CoverageOracle.stackMembers(stacked) guard members.count == 2 else { throw RealityError("Minimum-capacity geometry has no two-window native stack") } for index in members { try await session.apps.focus(owned[index]) try await session.expect("Focusing each native stack member preserves two fitting real tile frames") { CoverageOracle.minimumStack(try FrameOracle.frames($0, ids: owned.map(\.id)), geometry: geometry) } } try await checkMinimumStackProductFocus( session, owned: owned, minimumWidths: widths, maximumWidths: maximumWidths) try await session.command("retile") try await session.expect("Retile retains app-aware stacking rather than reopening an undersized third tile") { CoverageOracle.minimumStack(try FrameOracle.frames($0, ids: owned.map(\.id)), geometry: geometry) } try encodeJSON(stacked, to: session.directory.appendingPathComponent("minimum-stack-frames.json"))}
@MainActorprivate func checkMinimumStackProductFocus( _ session: RealitySession, owned: [OwnedWindow], minimumWidths: [Double], maximumWidths: [Double?]) async throws { guard let settings = owned.first(where: { $0.app == .systemSettings }) else { throw RealityError("Native minimum stack focus proof requires its owned System Settings window") } try await session.apps.focus(settings) let original = try session.readFrames(owned) try await cycleMinimumStackFocus( session, owned: owned, starting: settings.id, command: "window-next", frames: original) try await session.command("set-window-cap", "1") let workspaces = try await session.cli(["query", "--json", "workspaces"]) guard workspaces.status == 0, try JSONDecoder().decode([DesktopWorkspace].self, from: Data(workspaces.output.utf8)).contains(where: { $0.active && $0.tiles?.tileCount == 1 && Set($0.tiles?.windows.map(\.rawValue) ?? []) == Set(owned.map(\.id)) }) else { throw RealityError("Product cap command did not place all three native apps into one stack") } let area = session.visible.inset(try session.configuredLayout().gaps.outer) try await session.expect("One-stack native frames fit their independently measured size limits") { rows in let frames = try FrameOracle.frames(rows, ids: owned.map(\.id)) return frames.enumerated().allSatisfy { index, frame in area.contains(frame, tolerance: 1) && frame.width >= minimumWidths[index] - 1 && abs(frame.width - min(maximumWidths[index] ?? area.width, area.width)) <= 3 } } let stacked = try session.readFrames(owned) try await cycleMinimumStackFocus( session, owned: owned, starting: settings.id, command: "stack-next", frames: stacked) try encodeJSON(stacked, to: session.directory.appendingPathComponent("minimum-product-focus-stack-frames.json")) try await session.command("set-window-cap", "0")}
@MainActorprivate func cycleMinimumStackFocus( _ session: RealitySession, owned: [OwnedWindow], starting: UInt32, command: String, frames: [ScreenRect]) async throws { let ids = Set(owned.map(\.id)) var previous = starting var visited: Set<UInt32> = [starting] for turn in owned.indices { try await session.command(command) var selected: UInt32? try await session.expect("Product \(command) changes native focus without changing any owned app frame") { rows in guard let current = RealApps.focusedID(rows), current != previous, ids.contains(current), zip(try FrameOracle.frames(rows, ids: owned.map(\.id)), frames) .allSatisfy({ $0.matches($1, tolerance: 1) }) else { return false } selected = current return true } guard let selected else { throw RealityError("Native product focus proof omitted the selected CG window") } try encodeJSON( ["window": selected], to: session.directory.appendingPathComponent("minimum-\(command)-focus-\(turn).json")) previous = selected visited.insert(selected) } guard visited == ids, previous == starting else { throw RealityError("Product \(command) did not visit every native app and return to System Settings") }}