Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209import AppKitimport Foundationimport ParcelCore
@MainActorfunc checkTourModalDialogs(_ session: RealitySession) async throws { guard NSRunningApplication.runningApplications(withBundleIdentifier: RealApp.fixture.bundleID).isEmpty else { throw RealityError("Safety stop: default modal classification requires an exclusively owned fixture process") } try session.configure(defaultClassifiedBundles: [RealApp.fixture.bundleID]) let owned = try await session.startingWindows([.fixture, .textEdit, .finder]) let parent = owned[0] let fixture = try session.apps.fixture(for: parent) let opened = try await fixture.control("open-dialogs") guard let auxiliaries = opened.auxiliaryWindows, auxiliaries.count == 2, let sheet = auxiliaries.first(where: { $0.kind == "sheet" }), sheet.parentNumber == parent.id, let panel = auxiliaries.first(where: { $0.kind == "panel" }), panel.parentNumber == nil, sheet.number > 0, panel.number > 0, sheet.number != panel.number else { throw RealityError("Owned fixture did not acknowledge an attached sheet and distinct floating panel") } try encodeJSON(opened, to: session.directory.appendingPathComponent("modal-open-receipt.json")) let auxiliaryIDs = auxiliaries.map(\.number) var auxiliaryObservations: [[RealWindow]] = [] func readAuxiliaries() throws -> [RealWindow] { let rows = try FrameOracle.read(allLayers: true) .filter { auxiliaryIDs.contains($0.id) && $0.pid == parent.pid } auxiliaryObservations.append(rows) try encodeJSON( auxiliaryObservations, to: session.directory.appendingPathComponent("modal-auxiliary-cg-frames.json")) return rows } var previous: [ScreenRect]? var originalAuxiliaries: [ScreenRect] = [] try await session.expect("Both exact owned auxiliaries have stable, visible window-server frames") { _ in let rows = try readAuxiliaries() guard auxiliaryIDs.allSatisfy({ id in rows.contains { $0.id == id && $0.onscreen && $0.alpha > 0 } }) else { return false } let current = try FrameOracle.frames(rows, ids: auxiliaryIDs) let stable = previous.map { zip($0, current).allSatisfy { $0.matches($1, tolerance: 1) } } == true previous = current originalAuxiliaries = current return stable } let original = try session.readFrames(owned) let configuration = try Data(contentsOf: session.config) let snapshot = try await fixture.control("snapshot") guard let before = snapshot.auxiliaryWindows, Set(before.map(\.number)) == Set(auxiliaryIDs), NSRunningApplication.runningApplications(withBundleIdentifier: RealApp.fixture.bundleID) .allSatisfy({ $0.processIdentifier == parent.pid }) else { throw RealityError("Safety stop: modal fixture identities changed before Parcel launch") } let hooks = try RealityRecoveryHooks(kind: .tourRollback, token: UUID().uuidString) try await session.start(recoveryHooks: hooks, firstRun: true) try await waitNativeTour(session, step: "welcome") for step in ["allow", "ready"] { try await pressNativeTour(session, step == "allow" ? .automatic : .continue) try await waitNativeTour(session, step: step) try await requireNativeTourHold(session, owned: owned, original: original) try await session.expect("Setup navigation leaves both owned auxiliary frames untouched") { _ in zip(try FrameOracle.frames(readAuxiliaries(), ids: auxiliaryIDs), originalAuxiliaries) .allSatisfy { $0.matches($1, tolerance: 1) } } } try await pressNativeTour(session, .tidy) try await waitNativeTour(session, terminal: "success") let parentPolicy = try await modalInspection(session, id: parent.id, stage: "parent-with-sheet") guard parentPolicy.disposition == .tile else { throw RealityError("The fixture parent stopped tiling while its owned sheet was attached") } for id in auxiliaryIDs { let policy = try await modalInspection(session, id: id, stage: "auxiliary") guard policy.disposition == .float || policy.disposition == .ignore else { throw RealityError("A default-classified auxiliary entered the tile layout") } } try await session.tiles(owned) try await session.expect("The floating panel keeps its frame and the sheet keeps its native size") { rows in let current = try FrameOracle.frames(readAuxiliaries(), ids: auxiliaryIDs) guard let sheetIndex = auxiliaryIDs.firstIndex(of: sheet.number), let panelIndex = auxiliaryIDs.firstIndex(of: panel.number) else { return false } let sheetFrame = current[sheetIndex] let parentFrame = try FrameOracle.frames(rows, ids: [parent.id])[0] return current[panelIndex].matches(originalAuxiliaries[panelIndex], tolerance: 1) && abs(sheetFrame.width - originalAuxiliaries[sheetIndex].width) <= 1 && abs(sheetFrame.height - originalAuxiliaries[sheetIndex].height) <= 1 && abs((sheetFrame.x + sheetFrame.width / 2) - (parentFrame.x + parentFrame.width / 2)) <= 3 && session.visible.contains(sheetFrame) } let settled = try await fixture.control("snapshot") guard let after = settled.auxiliaryWindows, let finalSheet = after.first(where: { $0.number == sheet.number }), finalSheet.parentNumber == parent.id, let finalPanel = after.first(where: { $0.number == panel.number }), let initialSheet = before.first(where: { $0.number == sheet.number }), let initialPanel = before.first(where: { $0.number == panel.number }), finalSheet.resizeWrites == initialSheet.resizeWrites, finalPanel.frameWrites == initialPanel.frameWrites else { throw RealityError("Tidy resized the sheet, wrote the floating panel, or detached the owned sheet") } try encodeJSON(settled, to: session.directory.appendingPathComponent("modal-settled-receipt.json")) let closed = try await fixture.control("close-dialogs") guard closed.auxiliaryWindows?.isEmpty == true else { throw RealityError("Owned auxiliary close was not acknowledged") } try await session.expect("Both exact owned auxiliary IDs disappear after closing the dialogs") { _ in try readAuxiliaries().isEmpty } try await session.command("retile") try await session.tiles(owned) try await requireNativeAutomatic(session, enabled: true) try await checkNativeFilePicker(session, fixture: fixture, owned: owned) guard try Data(contentsOf: session.config) == configuration else { throw RealityError("Modal lifecycle unexpectedly changed isolated configuration") } try encodeJSON( try session.readFrames(owned), to: session.directory.appendingPathComponent("modal-final-cg-frames.json")) try await session.stop() try await session.expect("Quit restores every original document frame after the owned dialogs closed") { try TourFirstRunOracle.unchanged($0, owned: owned, original: original) }}
@MainActorprivate func modalInspection(_ session: RealitySession, id: UInt32, stage: String) async throws -> DesktopInspection { let result = try await session.cli(["why", "--json", String(id)]) try encodeJSON(result, to: session.directory.appendingPathComponent("modal-why-\(stage)-\(id).json")) guard result.status == 0 else { throw RealityError("The live modal classification was unavailable for owned window \(id): \(result.errors)") } let inspection = try JSONDecoder().decode(DesktopInspection.self, from: Data(result.output.utf8)) if let reason = inspection.classification_reason, case .rule = reason { throw RealityError("The modal recipe exercised an overriding rule rather than default classification") } return inspection}
@MainActorprivate func checkNativeFilePicker( _ session: RealitySession, fixture: RealityFixture, owned: [OwnedWindow]) async throws { let parent = owned[0] let documents = try session.readFrames(owned) let opened = try await fixture.control("open-file-picker") try encodeJSON(opened, to: session.directory.appendingPathComponent("file-picker-open-receipt.json")) guard let picker = opened.auxiliaryWindows?.onlyNativeFilePicker, picker.number > 0, picker.parentNumber == parent.id else { throw RealityError("NSOpenPanel did not acknowledge its exact owned native sheet identity") } var original: ScreenRect? var observations: [[RealWindow]] = [] func pickerRows() 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("file-picker-cg-frames.json")) return rows } try await session.expect("The real NSOpenPanel is visible and stable beside unchanged tiled documents") { rows in guard let current = try pickerRows().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: documents) } guard let original else { throw RealityError("NSOpenPanel omitted its native CG frame") } let before = try await fixture.control("snapshot") guard let initial = before.auxiliaryWindows?.onlyNativeFilePicker, initial.number == picker.number, initial.frameWrites == picker.frameWrites, initial.resizeWrites == picker.resizeWrites else { throw RealityError("NSOpenPanel identity or frame writes changed during opening") } for stage in ["open", "retile"] { if stage == "retile" { try await session.command("retile") } let policy = try await modalInspection(session, id: picker.number, stage: "file-picker-\(stage)") guard policy.disposition == .float || policy.disposition == .ignore, try await modalInspection(session, id: parent.id, stage: "file-picker-parent-\(stage)").disposition == .tile else { throw RealityError("Native file-picker lifecycle tiled its temporary dialog or untiled the parent") } try await session.expect("The native picker keeps its CG frame through \(stage), without document reflow") { rows in guard let current = try pickerRows().first else { return false } return try current.frame.matches(original, tolerance: 1) && CoverageOracle.unchanged(FrameOracle.frames(rows, ids: owned.map(\.id)), baseline: documents) } try await requireCoverageFramesStay( session, owned: owned, baseline: documents, evidence: "file-picker-documents-\(stage)") let snapshot = try await fixture.control("snapshot") try encodeJSON(snapshot, to: session.directory.appendingPathComponent("file-picker-\(stage)-receipt.json")) guard let final = snapshot.auxiliaryWindows?.onlyNativeFilePicker, final.number == picker.number, final.parentNumber == parent.id, final.frameWrites == initial.frameWrites, final.resizeWrites == initial.resizeWrites else { throw RealityError("Parcel wrote or detached the real NSOpenPanel during \(stage)") } } let closed = try await fixture.control("close-dialogs") guard closed.auxiliaryWindows?.isEmpty == true else { throw RealityError("NSOpenPanel cancellation was not acknowledged") } try await session.expect("Cancel removes the exact native picker and leaves all three document tiles unchanged") { rows in try pickerRows().isEmpty && CoverageOracle.unchanged(FrameOracle.frames(rows, ids: owned.map(\.id)), baseline: documents) } try await session.tiles(owned)}
private extension Array where Element == RealityFixtureReply.AuxiliaryWindow { var onlyNativeFilePicker: Element? { count == 1 && first?.kind == "open-panel" ? first : nil }}