Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204import AppKitimport Foundation
@MainActorfunc checkWindowModeStability(_ session: RealitySession) async throws { guard NSRunningApplication.runningApplications(withBundleIdentifier: RealApp.fixture.bundleID).isEmpty else { throw RealityError("Safety stop: mode popup classification requires exclusively owned fixture processes") } try session.configure(layout: "columns", cap: 2, defaultClassifiedBundles: [RealApp.fixture.bundleID]) let owned = try await session.startingWindows([.fixture, .fixture]) try await session.start() try await session.tiles(owned) let source = owned[0] let fixture = try session.apps.fixture(for: source) for command in ["focus-toggle", "float-toggle"] { try await session.apps.focus(source) try await session.command(command) try await fixture.setTitle("\(session.apps.token) immediate page \(command)") if command == "focus-toggle" { try await session.expect("Focus expands the owned document before browser-style activity") { try FrameOracle.frames($0, ids: [source.id])[0].matches(session.visible.inset(16)) } } else { try await session.tiles([owned[1]]) var previous: ScreenRect? try await session.expect("Float settles at a centered native frame") { rows in let frame = try FrameOracle.frames(rows, ids: [source.id])[0] let stable = previous?.matches(frame, tolerance: 1) == true previous = frame let centerX = frame.x + frame.width / 2 let centerY = frame.y + frame.height / 2 let screenCenterX = session.visible.x + session.visible.width / 2 let screenCenterY = session.visible.y + session.visible.height / 2 return stable && abs(centerX - screenCenterX) <= 2 && abs(centerY - screenCenterY) <= 2 } } let baseline = try session.readFrames(owned) for index in 1...3 { try await fixture.setTitle("\(session.apps.token) page \(command) \(index)") try await session.apps.focus(source) try await requireCoverageFramesStay( session, owned: owned, baseline: baseline, evidence: "\(command)-activity-\(index)") } try await checkModeNativePicker(session, fixture: fixture, owned: owned, baseline: baseline, mode: command) try await session.apps.focus(source) try await session.command(command) try await session.tiles(owned) }}
@MainActorprivate func checkModeNativePicker( _ session: RealitySession, fixture: RealityFixture, owned: [OwnedWindow], baseline: [ScreenRect], mode: String) async throws { try await observeModeDocumentFrames(session, owned: owned, baseline: baseline, mode: mode) { try await performModeNativePicker(session, fixture: fixture, owned: owned, baseline: baseline, mode: mode) }}
@MainActorprivate func performModeNativePicker( _ session: RealitySession, fixture: RealityFixture, owned: [OwnedWindow], baseline: [ScreenRect], mode: String) async throws { let parent = owned[0] try await requireModePickerProcessed(session, parent: parent, picker: 0, stage: "before", mode: mode) let opened = try await fixture.control("open-file-picker") try encodeJSON(opened, to: session.directory.appendingPathComponent("\(mode)-picker-open-receipt.json")) guard let auxiliaries = opened.auxiliaryWindows, auxiliaries.count == 1, let picker = auxiliaries.first, picker.kind == "open-panel", picker.number > 0, picker.parentNumber == parent.id else { throw RealityError("Mode stability requires the exact owned NSOpenPanel sheet identity") } var observations: [[RealWindow]] = [] func readPicker() throws -> [RealWindow] { let rows = try FrameOracle.read(allLayers: true).filter { $0.id == picker.number && $0.pid == parent.pid } observations.append(rows) try encodeJSON(observations, to: session.directory.appendingPathComponent("\(mode)-picker-cg-frames.json")) return rows } var original: ScreenRect? try await session.expect("\(mode): opening the owned native picker keeps parent and neighbor frames unchanged") { rows in guard let current = try readPicker().first, current.onscreen, current.alpha > 0, current.frame.isValid else { return false } let stable = original?.matches(current.frame, tolerance: 1) == true original = current.frame return try stable && CoverageOracle.unchanged(FrameOracle.frames(rows, ids: owned.map(\.id)), baseline: baseline) } guard let original else { throw RealityError("Mode picker omitted its independent CG frame") } for stage in ["open", "focus"] { if stage == "focus" { let focused = try await fixture.control("focus-dialog") try encodeJSON(focused, to: session.directory.appendingPathComponent("\(mode)-picker-focus-receipt.json")) guard focused.keyWindowNumber == picker.number else { throw RealityError("Native focus did not select the exact owned picker") } } try await requireModePickerProcessed( session, parent: parent, picker: picker.number, stage: stage, mode: mode) let result = try await session.cli(["why", "--json", String(picker.number)]) try encodeJSON(result, to: session.directory.appendingPathComponent("\(mode)-picker-why-\(stage).json")) guard result.status == 0 else { throw RealityError("Mode picker classification receipt was unavailable") } let policy = try JSONDecoder().decode(DesktopInspection.self, from: Data(result.output.utf8)) guard policy.disposition == .float || policy.disposition == .ignore else { throw RealityError("Mode popup entered the tile tree") } if let reason = policy.classification_reason, case .rule = reason { throw RealityError("Mode popup classification used a rule override") } try await requireCoverageFramesStay( session, owned: owned, baseline: baseline, evidence: "\(mode)-picker-documents-\(stage)") try await session.expect("\(mode): picker keeps its own native geometry through \(stage)") { _ in guard let current = try readPicker().first else { return false } return current.onscreen && current.frame.matches(original, tolerance: 1) } let snapshot = try await fixture.control("snapshot") try encodeJSON(snapshot, to: session.directory.appendingPathComponent("\(mode)-picker-\(stage)-receipt.json")) guard let after = snapshot.auxiliaryWindows, after.count == 1, let current = after.first, current.number == picker.number, current.parentNumber == parent.id, current.frameWrites == picker.frameWrites, current.resizeWrites == picker.resizeWrites else { throw RealityError("Mode popup was written, replaced or detached during \(stage)") } } let closed = try await fixture.control("close-dialogs") try encodeJSON(closed, to: session.directory.appendingPathComponent("\(mode)-picker-close-receipt.json")) guard closed.auxiliaryWindows?.isEmpty == true else { throw RealityError("Mode picker Cancel was not acknowledged") } try await session.expect("\(mode): Cancel removes the exact picker without changing either document") { rows in try readPicker().isEmpty && CoverageOracle.unchanged(FrameOracle.frames(rows, ids: owned.map(\.id)), baseline: baseline) } try await requireModePickerProcessed(session, parent: parent, picker: picker.number, stage: "closed", mode: mode) try await requireCoverageFramesStay( session, owned: owned, baseline: baseline, evidence: "\(mode)-picker-documents-closed")}
@MainActorprivate func requireModePickerProcessed( _ session: RealitySession, parent: OwnedWindow, picker: UInt32, stage: String, mode: String) async throws { struct Window: Codable { let id: UInt32 let pid: Int32 let floating: Bool let focused: Bool } var receipts: [[Window]] = [] let deadline = ContinuousClock.now.advanced(by: .seconds(4)) while ContinuousClock.now < deadline { let result = try await session.cli(["query", "--json", "windows"], timeout: .seconds(2)) guard result.status == 0 else { throw RealityError("Mode popup processing query failed") } let windows = try JSONDecoder().decode([Window].self, from: Data(result.output.utf8)) receipts.append(windows) try encodeJSON(receipts, to: session.directory.appendingPathComponent("\(mode)-picker-processed-\(stage).json")) let processed: Bool if stage == "closed" || stage == "before" { processed = !windows.contains { $0.id == picker } && windows.contains { $0.id == parent.id && $0.pid == parent.pid && $0.focused } } else { let admitted = windows.contains { $0.id == picker && $0.pid == parent.pid && $0.floating } let focused = windows.first(where: \.focused) // AppKit can key a sheet while AXFocusedWindow reports no value. let sheetOwnsFocus = focused?.id == picker || focused == nil let parentLostFocus = windows.contains { $0.id == parent.id && $0.pid == parent.pid && !$0.focused } processed = admitted && (stage != "focus" || (sheetOwnsFocus && parentLostFocus)) } if processed { return } try await Task.sleep(for: .milliseconds(20)) } throw RealityError("\(mode): Parcel did not consume the exact picker \(stage) lifecycle within four seconds")}
@MainActorprivate func observeModeDocumentFrames( _ session: RealitySession, owned: [OwnedWindow], baseline: [ScreenRect], mode: String, operation: @MainActor () async throws -> Void) async throws { var samples: [Observation] = [] let start = ContinuousClock.now let sampler = Task { @MainActor in while !Task.isCancelled { let rows = try FrameOracle.read() let elapsed = start.duration(to: .now).components samples.append( Observation( elapsedSeconds: Double(elapsed.seconds) + Double(elapsed.attoseconds) / 1e18, windows: rows)) guard CoverageOracle.unchanged(try FrameOracle.frames(rows, ids: owned.map(\.id)), baseline: baseline) else { throw RealityError("\(mode): native popup lifecycle moved a parent or neighbor CG frame") } try session.requireProtectedFrames() do { try await Task.sleep(for: .milliseconds(10)) } catch is CancellationError { return } } } let outcome: Result<Void, any Error> do { try await operation(); outcome = .success(()) } catch { outcome = .failure(error) } sampler.cancel() let sampling = await sampler.result try encodeJSON(samples, to: session.directory.appendingPathComponent("\(mode)-picker-lifecycle-cg-frames.json")) try sampling.get() try outcome.get()}