Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243import AppKitimport ParcelCoreimport ParcelIPCimport ParcelUI
@MainActorenum AppFixtureStackInput { static func post( arguments: [String], state: State, controller: StackChipController, environment: [String: String] = ProcessInfo.processInfo.environment ) async throws { guard environment["PARCEL_E2E"] == "1", let action = arguments.first, [ "select", "next", "previous", "scroll-next", "inspect-rest", "drag", "cancel-drag", "native-drag", "native-cancel-drag", ].contains( action), arguments.count == (action.hasSuffix("drag") ? 3 : 2), let number = UInt32(arguments[1]), number > 0 else { throw rejected() } let source = WindowID(rawValue: number) if action == "select" { controller.hideChips() } if action != "select" && action != "inspect-rest" { controller.showChips(for: source) } guard let group = controller.groups.first(where: { $0.windows.contains(source) }), let managed = state.windows[source], let application = NSRunningApplication(processIdentifier: managed.app.pid), !application.isTerminated, application.executableURL?.lastPathComponent == "FixtureApp", group.windows.allSatisfy({ state.windows[$0]?.app.pid == managed.app.pid }), let panel = NSApplication.shared.windows.first(where: { $0.isVisible && $0.contentView?.accessibilityRole() == .tabGroup && $0.contentView?.subviews.contains(where: { ($0 as? NSButton)?.tag == Int(number) }) == true }), let view = panel.contentView, let sourceButton = view.subviews.compactMap({ $0 as? NSButton }).first(where: { $0.tag == Int(number) }) else { throw rejected() } if action == "inspect-rest" { guard !group.active, let count = view.subviews.compactMap({ $0 as? NSTextField }).first, !count.isHidden, count.stringValue == group.countText, count.textColor != nil, !count.isAccessibilityElement(), view.bounds.contains(count.frame), view.accessibilityLabel() == group.accessibilityLabel, view.subviews.compactMap({ $0 as? NSButton }).filter({ $0.tag > 0 }).allSatisfy({ $0.frame.width >= 12 && $0.frame.height >= 16 && !$0.frame.intersects(count.frame) && $0.accessibilityLabel()?.isEmpty == false }), !NSWorkspace.shared.accessibilityDisplayShouldReduceMotion || view.layer?.animation(forKey: "learningScale") == nil else { throw rejected() } return } if action == "select" { guard view.subviews.compactMap({ $0 as? NSButton }).allSatisfy({ $0.tag > 0 }) else { throw rejected() } sourceButton.performClick(nil) return } if action == "next" || action == "previous" { let control: StackControl = action == "next" ? .next : .previous guard let button = view.subviews.compactMap({ $0 as? NSButton }).first(where: { $0.tag == control.rawValue }), !button.isHidden, button.isEnabled else { throw rejected() } button.performClick(nil) return } if action == "scroll-next" { guard let cg = CGEvent( scrollWheelEvent2Source: nil, units: .line, wheelCount: 1, wheel1: -1, wheel2: 0, wheel3: 0) else { throw rejected() } cg.timestamp = UInt64(ProcessInfo.processInfo.systemUptime * 1_000_000_000) guard let event = NSEvent(cgEvent: cg) else { throw rejected() } sourceButton.scrollWheel(with: event) return } guard let targetNumber = UInt32(arguments[2]), targetNumber > 0, let target = controller.dropTargets.first(where: { $0.window == WindowID(rawValue: targetNumber) }), !target.windows.contains(source), target.windows.allSatisfy({ state.windows[$0]?.app.pid == managed.app.pid }), let frame = controller.frame(of: source) else { throw rejected() } let start = CGPoint(x: frame.midX, y: frame.midY) let end = CGPoint(x: target.frame.midX, y: target.frame.midY) if action.hasPrefix("native-") { try await nativeDrag( start: start, end: end, cancelled: action == "native-cancel-drag", panel: panel, controller: controller, fixturePID: managed.app.pid, sourceWindow: source) return } let down = try mouseEvent(.leftMouseDown, at: start, panel: panel) let dragged = try mouseEvent(.leftMouseDragged, at: end, panel: panel) let up = try mouseEvent(.leftMouseUp, at: end, panel: panel) let escape: NSEvent? if action == "cancel-drag" { guard let event = NSEvent.keyEvent( with: .keyDown, location: .zero, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: panel.windowNumber, context: nil, characters: "\u{1b}", charactersIgnoringModifiers: "\u{1b}", isARepeat: false, keyCode: 53) else { throw rejected() } escape = event } else { escape = nil } sourceButton.mouseDown(with: down) sourceButton.mouseDragged(with: dragged) if let escape { sourceButton.interpretKeyEvents([escape]) } sourceButton.mouseUp(with: up) }
private static func nativeDrag( start: CGPoint, end: CGPoint, cancelled: Bool, panel: NSWindow, controller: StackChipController, fixturePID: Int32, sourceWindow: WindowID ) async throws { guard CGPreflightPostEventAccess(), !panel.isKeyWindow, NSWorkspace.shared.frontmostApplication?.processIdentifier == fixturePID, NSEvent.pressedMouseButtons == 0, NSEvent.modifierFlags.intersection([.command, .control, .option, .shift, .function]).isEmpty, !CGEventSource.keyState(.combinedSessionState, key: 53), let source = CGEventSource(stateID: .privateState), let cursor = CGEvent(source: nil), let escapeDown = CGEvent(keyboardEventSource: source, virtualKey: 53, keyDown: true), let escapeUp = CGEvent(keyboardEventSource: source, virtualKey: 53, keyDown: false) else { throw rejected() } let original = cursor.location let release = try AppFixtureHoverInput.mouseEvent(.leftMouseUp, source: source, at: end) var buttonHeld = false var escapeHeld = false func restore() throws { if escapeHeld { AppFixtureEventPosting.post(escapeUp) } if buttonHeld { controller.cancelDrag() AppFixtureEventPosting.post(release) } escapeHeld = false buttonHeld = false let result = CGWarpMouseCursorPosition(original) guard result == .success else { throw AppFixtureDrag.rejected("Could not restore the chip-input cursor: \(result)") } } do { controller.hideChips() guard let grip = NSApp.windows.first(where: { window in let buttons = window.contentView?.subviews.compactMap { $0 as? NSButton } ?? [] return window.isVisible && !buttons.isEmpty && buttons.allSatisfy { $0.tag > 0 } && buttons.contains { $0.tag == Int(sourceWindow.rawValue) } }), let view = grip.contentView, let button = view.subviews.compactMap({ $0 as? NSButton }).first(where: { $0.tag == Int(sourceWindow.rawValue) }) else { throw AppFixtureDrag.rejected("The fixture stack has no visible grip") } let rectangle = grip.convertToScreen(button.convert(button.bounds, to: nil)) let hoverPoint = CGPoint( x: rectangle.midX, y: (NSScreen.screens.first?.frame.maxY ?? 0) - rectangle.midY) let gripCovering = try AppFixtureHoverInput.frontmostWindow(at: hoverPoint) guard gripCovering?.number == UInt32(exactly: grip.windowNumber) else { throw AppFixtureDrag.rejected( "An unrelated window covers the fixture grip: expected \(grip.windowNumber), found \(String(describing: gripCovering?.number)), PID \(String(describing: gripCovering?.pid)), layer \(String(describing: gripCovering?.layer))" ) } AppFixtureEventPosting.post( try AppFixtureHoverInput.mouseEvent(.mouseMoved, source: source, at: hoverPoint)) let deadline = ContinuousClock.now.advanced(by: .seconds(4)) while !panel.isVisible { guard ContinuousClock.now < deadline else { throw AppFixtureDrag.rejected("Hover did not reveal the fixture chips") } try await Task.sleep(for: .milliseconds(20)) } try await Task.sleep(for: .milliseconds(40)) let covering = try AppFixtureHoverInput.frontmostWindow(at: start) guard covering?.number == UInt32(exactly: panel.windowNumber) else { throw AppFixtureDrag.rejected( "An unrelated window covers the fixture chip: expected \(panel.windowNumber), found \(String(describing: covering?.number)), PID \(String(describing: covering?.pid)), layer \(String(describing: covering?.layer))" ) } AppFixtureEventPosting.post(try AppFixtureHoverInput.mouseEvent(.mouseMoved, source: source, at: start)) try await Task.sleep(for: .milliseconds(40)) AppFixtureEventPosting.post(try AppFixtureHoverInput.mouseEvent(.leftMouseDown, source: source, at: start)) buttonHeld = true try await Task.sleep(for: .milliseconds(40)) guard !panel.isKeyWindow else { throw AppFixtureDrag.rejected("A chip press stole key focus before dragging") } for step in 1...8 { let fraction = Double(step) / 8 let point = CGPoint( x: start.x + (end.x - start.x) * fraction, y: start.y + (end.y - start.y) * fraction) guard let event = CGEvent( mouseEventSource: source, mouseType: .leftMouseDragged, mouseCursorPosition: point, mouseButton: .left) else { throw rejected() } AppFixtureEventPosting.post(event) try await Task.sleep(for: .milliseconds(40)) } guard controller.isDragging, !panel.isKeyWindow, NSApp.keyWindow !== panel, NSWorkspace.shared.frontmostApplication?.processIdentifier == fixturePID else { throw AppFixtureDrag.rejected("The chip drag was not active without taking key focus") } if cancelled { AppFixtureEventPosting.post(escapeDown) escapeHeld = true try await Task.sleep(for: .milliseconds(60)) AppFixtureEventPosting.post(escapeUp) escapeHeld = false try await Task.sleep(for: .milliseconds(60)) guard !controller.isDragging, !panel.isKeyWindow, !NSApp.windows.contains(where: { $0.isVisible && $0.title == "Move here" }) else { throw AppFixtureDrag.rejected("Escape did not cancel the real chip drag") } } AppFixtureEventPosting.post(release) buttonHeld = false try await Task.sleep(for: .milliseconds(80)) guard !controller.isDragging, NSWorkspace.shared.frontmostApplication?.processIdentifier == fixturePID else { throw AppFixtureDrag.rejected("The chip release did not finish without activating Parcel") } try restore() } catch { let failure = error do { try restore() } catch { throw AppFixtureDrag.rejected("Chip cleanup failed: \(error); input error: \(failure)") } throw failure } }
private static func mouseEvent(_ type: NSEvent.EventType, at point: CGPoint, panel: NSWindow) throws -> NSEvent { let screen = CGPoint(x: point.x, y: (NSScreen.screens.first?.frame.maxY ?? 0) - point.y) guard let event = NSEvent.mouseEvent( with: type, location: panel.convertPoint(fromScreen: screen), modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: panel.windowNumber, context: nil, eventNumber: 0, clickCount: 1, pressure: type == .leftMouseUp ? 0 : 1) else { throw rejected() } return event }
private static func rejected() -> IPCError { IPCError( code: "invalid_command", message: AppText.string("Fixture chip input requires a managed fixture window in e2e mode.")) }}