Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import Foundationimport ParcelConfigimport ParcelCoreimport ParcelPlatformimport ParcelUI
func matchesMainRatio( _ frames: [ScreenRect], mainIndex: Int, ratio: Double, visible: ScreenRect, gaps: LayoutGaps = RealityGeometry.defaultGaps) -> Bool { guard frames.count == 2, frames.indices.contains(mainIndex), ratio.isFinite, (0...1).contains(ratio), FrameOracle.tiles(frames, visible: visible, inner: gaps.inner, outer: gaps.outer) else { return false } let otherIndex = 1 - mainIndex return [true, false].contains { vertical in let expected = FrameOracle.stripes(count: 2, visible: visible, vertical: vertical, ratio: ratio, gaps: gaps) return frames[mainIndex].matches(expected[0]) && frames[otherIndex].matches(expected[1]) }}
struct SettingsRealityWorkspace: Decodable { let display: String let space: String
func key() throws -> WorkspaceKey { guard UUID(uuidString: display) != nil, let space = UInt64(space) else { throw RealityError("Settings bridge received invalid native workspace identity") } return WorkspaceKey(display: DisplayID(rawValue: display), space: SpaceID(rawValue: space)) }}
@MainActorfinal class SettingsRealityCommands: UIWorkspaceCommandSink { let session: RealitySession let target: OwnedWindow var failNext = false
init(session: RealitySession, target: OwnedWindow) { self.session = session; self.target = target }
func send(_ command: ConfigCommand) async throws { throw RealityError("Settings preset must never fall back to a global command") }
func currentWorkspace() async throws -> WorkspaceKey { struct Row: Decodable { let id: UInt32 let pid: Int32 let focused: Bool let workspace: SettingsRealityWorkspace? } guard RealApps.focusedID(try FrameOracle.read()) == target.id else { throw RealityError("Settings bridge requires exact owned native foreground") } let result = try await session.cli(["query", "--json", "windows"]) guard result.status == 0 else { throw RealityError("Settings workspace query failed: \(result.errors)") } let rows = try JSONDecoder().decode([Row].self, from: Data(result.output.utf8)) guard let row = rows.first(where: { $0.id == target.id && $0.pid == target.pid && $0.focused }), let workspace = row.workspace, RealApps.focusedID(try FrameOracle.read()) == target.id else { throw RealityError("Settings native foreground and runtime workspace disagree") } return try workspace.key() }
func send(_ command: ConfigCommand, in workspace: WorkspaceKey) async throws { guard try await currentWorkspace() == workspace else { throw RealityError("Settings bridge refuses a changed workspace, without global fallback") } if failNext { failNext = false; throw RealityError("Owned Settings bridge injected failure before dispatch") } _ = try await session.command(command.rawValue) }}
@MainActorfunc checkSettingsMainSize(_ session: RealitySession) async throws { try session.configure(layout: "side", ratio: 0.5) var owned = try await session.startingWindows([.textEdit, .textEdit]) try await session.start() try await session.tiles(owned) owned = zip(owned, try session.readFrames(owned)).sorted { $0.1.x < $1.1.x }.map(\.0) try await session.apps.focus(owned[0]) let sink = SettingsRealityCommands(session: session, target: owned[0]) let model = SettingsModel(store: ConfigStore(fileURL: session.config), commands: sink) await model.start() do { guard model.isStarted, model.error == nil else { throw RealityError("Isolated Settings failed to start") } let workspace = try await sink.currentWorkspace() let gaps = try session.configuredLayout().gaps func publish(_ ratio: Double) { model.updateRunningLayouts( [workspace.display: .side], activeDisplay: workspace.display, mainSizeRatios: [workspace.display: ratio], workspaces: [workspace.display: workspace]) } func expectRatio(_ ratio: Double, stage: String) async throws { try await session.expect("Settings main size \(stage): exact owned CG tiles use ratio \(ratio)") { matchesMainRatio( try FrameOracle.frames($0, ids: owned.map(\.id)), mainIndex: 0, ratio: ratio, visible: session.visible, gaps: gaps) } } for (preset, ratio) in [(ConfigCommand.ratioHalf, 0.5), (.ratioThreeFifths, 0.6)] { publish(ratio) try await model.setMainSizePreset(preset) try await expectRatio(ratio, stage: "preset saved") let bytes = try Data(contentsOf: session.config) let before = try session.readFrames(owned) let contentWidth = session.visible.inset(gaps.outer).width - gaps.inner let delta = (contentWidth * (0.7 - ratio)).rounded() try await coverageDragEdge( session, window: owned[0], frame: before[0], delta: delta, stage: "settings-\(preset.rawValue)-manual-drift") try await expectRatio(0.7, stage: "manual native resize") publish(0.7) if preset == .ratioThreeFifths { sink.failNext = true var rejected = false do { try await model.setMainSizePreset(preset) } catch is RealityError { rejected = true } guard rejected, model.error != nil else { throw RealityError("Settings command failure was hidden") } try await expectRatio(0.7, stage: "failed apply leaves live geometry unchanged") } try await model.setMainSizePreset(preset) guard try Data(contentsOf: session.config) == bytes else { throw RealityError("Reselecting an unchanged Settings preset rewrote configuration bytes") } try await expectRatio(ratio, stage: "unchanged preset reapplied") } publish(0.6) try await model.setMainSizePreset(.ratioHalf) try await expectRatio(0.5, stage: "reverse to half") } catch { await model.stop() throw error } await model.stop()}
@MainActorfunc checkLayoutPreset(_ session: RealitySession, layout: String) async throws { try session.configure(layout: layout, ratio: 0.6) let owned = try await session.startingWindows([.textEdit, .textEdit]) try await session.start() try await session.tiles(owned) let baseline = try LayoutSizeOracle(ids: owned.map(\.id), initial: session.readFrames(owned), layout: layout) try encodeJSON(baseline, to: session.directory.appendingPathComponent("preset-baseline.json")) try await session.expect("\(layout): saved 60% preset applies when windows first appear") { matchesMainRatio( try FrameOracle.frames($0, ids: baseline.ids), mainIndex: baseline.mainIndex, ratio: 0.6, visible: session.visible) }
for ratio in [0.5, 0.6, 2.0 / 3.0, 0.5] { try session.configure(layout: layout, ratio: ratio) let result = try await session.cli(["reload", "--json"]) guard result.status == 0 else { throw RealityError("Preset configuration reload failed: \(result.output) \(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("Preset reload did not acknowledge the isolated configuration") } try await session.expect("\(layout): configured ratio \(ratio) changes real frames") { matchesMainRatio( try FrameOracle.frames($0, ids: baseline.ids), mainIndex: baseline.mainIndex, ratio: ratio, visible: session.visible) } }}