Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209import AppKitimport Foundationimport ParcelConfigimport ParcelEngineimport ParcelUIimport Testing
@testable import ParcelApp@testable import ParcelCore
extension AppKitTestSuite { @Suite(.serialized) @MainActor struct LearningRuntimeTests { @Test func chipFocusGoesThroughWorldWhileAutomaticTilingIsOffAndRejectsStaleRequests() async throws { let scene = try scene() let runtime = scene.runtime let world = scene.world defer { scene.defaults.removePersistentDomain(forName: scene.suite) } try await runtime.startEngine(configuration: configuration()) let engine = try #require(runtime.services.engine) _ = try await engine.perform(.retile) world.focused = nil runtime.focusStackWindow(WindowID(rawValue: 99)) #expect(world.focused == nil) runtime.focusStackWindow(WindowID(rawValue: 1)) _ = try await engine.perform(.retile) #expect(world.focused == WindowID(rawValue: 1)) #expect(engine.snapshot.state.focusedWindow == WindowID(rawValue: 1)) world.focused = nil runtime.services.setAutomaticTilingEnabled(false) runtime.focusStackWindow(WindowID(rawValue: 2)) _ = try await engine.perform(.retile) #expect(world.focused == WindowID(rawValue: 2)) #expect(engine.snapshot.state.focusedWindow == WindowID(rawValue: 2)) #expect(!engine.isAutomaticTilingEnabled) world.focused = nil try await runtime.restoreAndStopEngine() runtime.focusStackWindow(WindowID(rawValue: 2)) #expect(world.focused == nil) }
@Test func explicitChipFocusStillRequiresTourConsent() async throws { let scene = try scene() defer { scene.defaults.removePersistentDomain(forName: scene.suite) } let runtime = scene.runtime try await runtime.startEngine(configuration: configuration()) let engine = try #require(runtime.services.engine) _ = try await engine.perform(.retile) runtime.services.holdForTour() scene.world.focused = nil runtime.focusStackWindow(WindowID(rawValue: 2)) #expect(scene.world.focused == nil) #expect(runtime.services.isTourUnresolved) #expect(!engine.isAutomaticTilingEnabled) runtime.services.resolveTour() runtime.services.setAutomaticTilingEnabled(false) runtime.focusStackWindow(WindowID(rawValue: 2)) _ = try await engine.perform(.retile) #expect(scene.world.focused == WindowID(rawValue: 2)) #expect(!engine.isAutomaticTilingEnabled) try await runtime.restoreAndStopEngine() }
@Test func hoverMoveExplainsAutomaticTilingOffWithoutChangingWindows() async throws { let scene = try scene() defer { scene.defaults.removePersistentDomain(forName: scene.suite) } let runtime = scene.runtime try await runtime.startEngine(configuration: configuration()) let engine = try #require(runtime.services.engine) _ = try await engine.perform(.retile) runtime.services.setAutomaticTilingEnabled(false) let state = engine.snapshot.state let window = WindowID(rawValue: 1) let workspace = try #require(state.workspace(containing: window)) do { try await runtime.performHoverAction( HoverActionRequest(window: window, workspace: workspace, action: .moveToDisplay(workspace.display))) Issue.record("Mouse movement was accepted while Automatic tiling was off") } catch let failure as AppFailure { #expect( failure.message == "Automatic tiling is off. Turn it on before moving windows with mouse controls.") } #expect(engine.snapshot.state == state) #expect(!engine.isAutomaticTilingEnabled) try await runtime.restoreAndStopEngine() }
@Test func singletonHasNoChipFocusRoute() async throws { let scene = try scene() let runtime = scene.runtime let world = scene.world defer { scene.defaults.removePersistentDomain(forName: scene.suite) } var config = configuration() config.layout.global.windowCap = 8 try await runtime.startEngine(configuration: config) let engine = try #require(runtime.services.engine) _ = try await engine.perform(.retile) world.focused = nil runtime.focusStackWindow(WindowID(rawValue: 1)) #expect(world.focused == nil) try await runtime.restoreAndStopEngine() }
@Test func focusConfirmationRequiresSuccessAndIgnoresLateReplies() throws { let base = LearningDelayedFocusWorld() var confirmed: [WindowID] = [] let world = RestoringWindowWorld(base: base, confirmedFocus: { confirmed.append($0) }) try world.start { _ in } let first = WindowID(rawValue: 1) let second = WindowID(rawValue: 2) var reported: EnginePlatformFailure? world.focus(first) { reported = $0 } base.completeFocus(.operation(.focusUnchanged(first))) #expect(confirmed.isEmpty) #expect(reported == .operation(.focusUnchanged(first))) base.receive?(.observed(.focusChanged(first))) #expect(confirmed == [first]) world.focus(second) { _ in } #expect(confirmed == [first]) base.completeFocus(nil) #expect(confirmed == [first, second]) world.focus(first) { _ in } world.stop() base.completeFocus(nil) #expect(confirmed == [first, second]) }
private func configuration() -> ParcelConfiguration { var config = ParcelConfiguration.defaults config.layout.global.windowCap = 1 config.motion.enabled = false return config }
private func scene() throws -> LearningRuntimeScene { _ = NSApplication.shared let suite = "learning-runtime-tests.\(UUID().uuidString)" let defaults = try #require(UserDefaults(suiteName: suite)) defaults.set(true, forKey: "guidedTour.v2.completed") let world = RecordingWorld() let display = DisplayID(rawValue: "chip-display") let key = WorkspaceKey(display: display, space: SpaceID(rawValue: 1)) world.startingEvents = [ .observed(.displaySetChanged([Display(id: display, frame: Rect(width: 1200, height: 800))])), .observed(.activeSpaceChanged(display: display, space: key.space)), ] for number: UInt32 in 1...2 { world.startingEvents.append( .observed( .windowAppeared( ManagedWindow(id: WindowID(rawValue: number), app: AppID(pid: 123, bundleID: "fixture")), workspace: key, frame: Rect(width: 400, height: 300)))) } let runtime = NativeAppRuntime( store: ConfigStore(), socketPath: "/tmp/learning-unused.sock", defaults: defaults, makeWindowWorld: { world }, makeDisplayLink: { _ in LearningIdleDisplayLink() }) return LearningRuntimeScene(runtime: runtime, world: world, defaults: defaults, suite: suite) } }}
@MainActorprivate final class LearningDelayedFocusWorld: EngineWindowWorld { let base = RecordingWorld() var receive: (@MainActor (EnginePlatformEvent) -> Void)? private var pendingFocus: (@MainActor (EnginePlatformFailure?) -> Void)?
func start(receive: @escaping @MainActor (EnginePlatformEvent) -> Void) throws { self.receive = receive try base.start(receive: receive) } func stop() { base.stop() } func write( _ frames: [EngineFrameWrite], for app: AppID, completion: @escaping @MainActor (EngineWriteReceipt) -> Void ) { base.write(frames, for: app, completion: completion) } func focus(_ window: WindowID, completion: @escaping @MainActor (EnginePlatformFailure?) -> Void) { pendingFocus = completion } func raise(_ window: WindowID, completion: @escaping @MainActor (EnginePlatformFailure?) -> Void) { base.raise(window, completion: completion) } func close(_ window: WindowID) async throws { try await base.close(window) } func toggleFullscreen(_ window: WindowID) async throws { try await base.toggleFullscreen(window) } func reconcile(_ apps: Set<AppID>) { base.reconcile(apps) } func completeFocus(_ failure: EnginePlatformFailure?) { let completion = pendingFocus pendingFocus = nil completion?(failure) }}
@MainActorprivate struct LearningRuntimeScene { let runtime: NativeAppRuntime let world: RecordingWorld let defaults: UserDefaults let suite: String}
@MainActorprivate final class LearningIdleDisplayLink: EngineDisplayLink { let frameRate: Double = 120 var isRunning = false func start(tick: @escaping @MainActor (Double) -> Void) { isRunning = true } func stop() { isRunning = false }}