import AppKit import ParcelCore import ParcelIPC import ParcelPlatform import ParcelUI @MainActor enum AppFixtureHoverInput { static func post( arguments: [String], state: State, controller: HoverBarController, environment: [String: String] = ProcessInfo.processInfo.environment ) async throws { guard environment["PARCEL_E2E"] == "1", (2...3).contains(arguments.count), let number = UInt32(arguments[0]), number > 0, let window = state.windows[WindowID(rawValue: number)], NSRunningApplication(processIdentifier: window.app.pid)?.executableURL?.lastPathComponent == "FixtureApp", let target = HoverBarModel.target(for: window.id, state: state) else { throw IPCError( code: "invalid_command", message: AppText.string("Fixture hover input requires a visible managed fixture window in e2e mode.")) } if arguments[1] == "grip-drag" || arguments[1] == "grip-cancel-drag" { guard arguments.count == 3, let destination = UInt32(arguments[2]).map(WindowID.init(rawValue:)), let managed = state.windows[destination], managed.app.pid == window.app.pid, state.visibleWindows.contains(destination), !target.stackWindows.contains(destination), let bounds = state.observedFrames[destination] else { throw AppFixtureDrag.rejected("The fixture grip drag needs another visible fixture tile") } let frame = CGRect(x: bounds.minX, y: bounds.minY, width: bounds.size.width, height: bounds.size.height) try await dragGrip( window: window, destination: destination, frame: frame, cancelled: arguments[1] == "grip-cancel-drag", controller: controller) return } if arguments[1] == "inspect-popover" || arguments[1] == "tab" || arguments[1] == "grip-tab" { let selected = arguments.count == 3 ? UInt32(arguments[2]).map(WindowID.init(rawValue:)) : nil guard arguments[1] == "inspect-popover" ? arguments.count == 2 : selected != nil, selected.map({ target.stackWindows.contains($0) }) ?? true else { throw AppFixtureDrag.rejected("The fixture hover tab is unavailable") } try await inspectOrSelect( window: window, target: target, selected: selected, grip: arguments[1] == "grip-tab", controller: controller) return } let action: HoverAction switch (arguments[1], arguments.count) { case ("primary", 2): action = .makePrimary case ("focus", 2): action = .toggleFocus case ("float", 2): action = .toggleFloat case ("always-float", 2): action = .alwaysFloat case ("move-to", 3): action = .moveToDisplay(DisplayID(rawValue: arguments[2])) default: throw IPCError(code: "invalid_command", message: AppText.string("The fixture hover action is invalid.")) } _ = try HoverBarModel.request(action, target: target, state: state) if case .moveToDisplay = action, HoverBarModel.destinations(for: target, state: state).count > 1 { try await controller.fixtureInput(action: action, window: window.id) return } let control: HoverControlID switch action { case .makePrimary: control = .primary case .toggleFocus: control = .focus case .moveToDisplay: control = .display case .toggleFloat, .alwaysFloat: control = .floating } guard NSWorkspace.shared.frontmostApplication?.processIdentifier == window.app.pid, CGPreflightPostEventAccess(), !CGEventSource.buttonState(.combinedSessionState, button: .left), !CGEventSource.buttonState(.combinedSessionState, button: .right), !CGEventSource.buttonState(.combinedSessionState, button: .center), CGEventSource.flagsState(.combinedSessionState) .intersection([.maskCommand, .maskAlternate, .maskControl, .maskShift, .maskSecondaryFn]).isEmpty, let source = CGEventSource(stateID: .privateState), let cursor = CGEvent(source: nil) else { throw AppFixtureDrag.rejected("Hover clicks require the frontmost fixture and released input") } let original = cursor.location guard let titlePoint = controller.passiveTriggerPoint(for: window.id) else { throw AppFixtureDrag.rejected("No uncovered hover margin is available for the fixture") } var heldRelease: CGEvent? func restore() throws { if let release = heldRelease { AppFixtureEventPosting.post(release) heldRelease = nil } let result = CGWarpMouseCursorPosition(original) guard result == .success else { throw AppFixtureDrag.rejected("Could not restore the hover-input cursor: \(result)") } } do { let hit = try frontmostWindow(at: titlePoint) let gap = !target.frame.intersects(target.windowFrame) let desktop = hit.map { $0.layer == CGWindowLevelForKey(.desktopWindow) || $0.layer == CGWindowLevelForKey(.desktopIconWindow) } ?? true let nativePadding = hit.map { $0.layer == CGWindowLevelForKey(.normalWindow) && !$0.bounds.contains(titlePoint) } ?? false let ownGrip = controller.gripTabFrame(for: window.id, tab: window.id)?.contains(titlePoint) == true && hit?.pid == ProcessInfo.processInfo.processIdentifier let backdrop = gap && WindowHitTest.nativeHoverBackdropWindowNumber( at: titlePoint, owner: window.id.rawValue, ignoring: []) == 0 guard hit?.number == window.id.rawValue || ownGrip || backdrop || (gap && (desktop || nativePadding)) else { let actual = try WindowServer().windows(options: .optionOnScreenOnly).first { $0.number == window.id.rawValue } throw AppFixtureDrag.rejected( "An unrelated window covers the fixture title strip: expected=\(window.id.rawValue), point=\(titlePoint), actual=\(String(describing: actual?.bounds)), hit=\(hit?.number ?? 0), owner=\(hit?.pid ?? 0), layer=\(hit?.layer ?? 0), bounds=\(hit?.bounds ?? .zero)" ) } AppFixtureEventPosting.post(try mouseEvent(.mouseMoved, source: source, at: titlePoint)) let deadline = ContinuousClock.now.advanced(by: .seconds(3)) while controller.controlFrame(for: window.id, control: control) == nil, ContinuousClock.now < deadline { try await Task.sleep(for: .milliseconds(20)) } guard NSWorkspace.shared.frontmostApplication?.processIdentifier == window.app.pid, let frame = controller.controlFrame(for: window.id, control: control), controller.model.target?.controlsFrame.contains(frame) == true else { throw AppFixtureDrag.rejected("The real hover button did not appear over the fixture title strip") } let point = CGPoint(x: frame.midX, y: frame.midY) func ownsButton() throws -> Bool { guard controller.controlFrame(for: window.id, control: control) == frame, let hit = try frontmostWindow(at: point), hit.pid == ProcessInfo.processInfo.processIdentifier else { return false } return NSApp.windows.first(where: { $0.windowNumber == Int(hit.number) })?.contentView? .accessibilityIdentifier() == "hover.controls" } let presentedDeadline = ContinuousClock.now.advanced(by: .seconds(1)) while try !ownsButton(), ContinuousClock.now < presentedDeadline { try await Task.sleep(for: .milliseconds(10)) } guard try ownsButton() else { throw AppFixtureDrag.rejected("An unrelated window covers the real hover button") } let flags: CGEventFlags = action == .alwaysFloat ? .maskAlternate : [] let release = try mouseEvent(.leftMouseUp, source: source, at: point, flags: flags) let down = try mouseEvent(.leftMouseDown, source: source, at: point, flags: flags) AppFixtureEventPosting.post(try mouseEvent(.mouseMoved, source: source, at: point, flags: flags)) try await Task.sleep(for: .milliseconds(40)) heldRelease = release AppFixtureEventPosting.post(down) try await Task.sleep(for: .milliseconds(60)) AppFixtureEventPosting.post(release) heldRelease = nil try await Task.sleep(for: .milliseconds(60)) try restore() } catch { let failure = error do { try restore() } catch { throw AppFixtureDrag.rejected("Hover cleanup failed: \(error); input error: \(failure)") } throw failure } } private static func dragGrip( window: ManagedWindow, destination: WindowID, frame: CGRect, cancelled: Bool, controller: HoverBarController ) async throws { try requireReleasedInput() guard !CGEventSource.keyState(.combinedSessionState, key: 53) else { throw AppFixtureDrag.rejected("Release Escape before native grip dragging") } try await waitForReadiness(window: window, controller: controller, trigger: false) guard 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 AppFixtureDrag.rejected("Could not create native grip input events") } guard let grip = controller.gripTabFrame(for: window.id, tab: window.id) else { throw AppFixtureDrag.rejected("The fixture grip disappeared after native readiness") } let start = CGPoint(x: grip.midX, y: grip.midY) let end = CGPoint(x: frame.midX, y: frame.midY) let hit = try frontmostWindow(at: start) guard hit?.pid == ProcessInfo.processInfo.processIdentifier, let panel = NSApp.windows.first(where: { UInt32(exactly: $0.windowNumber) == hit?.number }), panel.contentView?.accessibilityRole() == .tabGroup, !panel.isKeyWindow, try frontmostWindow(at: end)?.number == destination.rawValue else { throw AppFixtureDrag.rejected("An unrelated surface covers the fixture grip or destination") } let original = cursor.location let release = try mouseEvent(.leftMouseUp, source: source, at: end) var buttonHeld = false var escapeHeld = false func restore() throws { if escapeHeld { AppFixtureEventPosting.post(escapeUp) } if buttonHeld { controller.cancelTabDrag() AppFixtureEventPosting.post(release) } escapeHeld = false buttonHeld = false guard CGWarpMouseCursorPosition(original) == .success else { throw AppFixtureDrag.rejected("Could not restore the grip-drag cursor") } } do { AppFixtureEventPosting.post(try mouseEvent(.mouseMoved, source: source, at: start)) try await Task.sleep(for: .milliseconds(30)) AppFixtureEventPosting.post(try mouseEvent(.leftMouseDown, source: source, at: start)) buttonHeld = true try await Task.sleep(for: .milliseconds(40)) for step in 1...8 { let fraction = CGFloat(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 AppFixtureDrag.rejected("Could not create native grip drag movement") } AppFixtureEventPosting.post(event) try await Task.sleep(for: .milliseconds(40)) } guard !panel.isKeyWindow, NSWorkspace.shared.frontmostApplication?.processIdentifier == window.app.pid else { throw AppFixtureDrag.rejected("The native grip drag activated Parcel") } 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)) } AppFixtureEventPosting.post(release) buttonHeld = false try await Task.sleep(for: .milliseconds(100)) try restore() } catch { let failure = error do { try restore() } catch { throw AppFixtureDrag.rejected("Grip drag cleanup failed: \(error); input error: \(failure)") } throw failure } } private static func inspectOrSelect( window: ManagedWindow, target: HoverTarget, selected: WindowID?, grip: Bool, controller: HoverBarController ) async throws { try requireReleasedInput() try await waitForReadiness(window: window, controller: controller, trigger: !grip) guard let source = CGEventSource(stateID: .privateState), let cursor = CGEvent(source: nil) else { throw AppFixtureDrag.rejected("Could not create native hover inspection events") } guard let trigger = controller.passiveTriggerPoint(for: window.id) else { throw AppFixtureDrag.rejected("The fixture trigger disappeared after native readiness") } let original = cursor.location var release: CGEvent? func move(_ point: CGPoint) throws { AppFixtureEventPosting.post(try mouseEvent(.mouseMoved, source: source, at: point)) } func restore() throws { if let release { AppFixtureEventPosting.post(release) } guard CGWarpMouseCursorPosition(original) == .success else { throw AppFixtureDrag.rejected("Could not restore the hover inspection cursor") } } func owns(_ point: CGPoint) throws -> Bool { try frontmostWindow(at: point)?.pid == ProcessInfo.processInfo.processIdentifier } do { let frame: CGRect if grip, let selected { guard let actual = controller.gripTabFrame(for: window.id, tab: selected), try owns(CGPoint(x: actual.midX, y: actual.midY)) else { throw AppFixtureDrag.rejected("The native fixture grip tab is covered or missing") } frame = actual } else { let hit = try frontmostWindow(at: trigger) let ownGrip = controller.gripTabFrame(for: window.id, tab: window.id)?.contains(trigger) == true && hit?.pid == ProcessInfo.processInfo.processIdentifier guard hit?.number == window.id.rawValue || ownGrip || WindowHitTest.nativeHoverWindowNumber(at: trigger) == 0 else { throw AppFixtureDrag.rejected("An unrelated surface covers the fixture hover trigger") } try move(trigger) let deadline = ContinuousClock.now.advanced(by: .seconds(3)) while controller.model.phase != .visible, ContinuousClock.now < deadline { try await Task.sleep(for: .milliseconds(20)) } guard controller.model.target?.window == window.id, controller.model.phase == .visible, let popup = controller.model.target?.controlsFrame, popup.contains(trigger), try owns(trigger), NSWorkspace.shared.frontmostApplication?.processIdentifier == window.app.pid else { throw AppFixtureDrag.rejected("The native popup did not open under the pointer without activation") } if let selected { guard let actual = controller.tabFrame(for: window.id, tab: selected) else { throw AppFixtureDrag.rejected("The requested native popup tab is missing") } frame = actual } else { let away = CGPoint(x: target.windowFrame.midX, y: target.windowFrame.maxY - 30) guard !popup.contains(away), try frontmostWindow(at: away)?.number == window.id.rawValue else { throw AppFixtureDrag.rejected("No safe fixture content point is available for popup departure") } try move(away) try await Task.sleep(for: .milliseconds(250)) guard controller.model.phase == .visible else { throw AppFixtureDrag.rejected("The popup closed before its pointer departure grace period") } try move(trigger) try await Task.sleep(for: .milliseconds(60)) guard controller.model.target?.controlsFrame == popup else { throw AppFixtureDrag.rejected("The popup moved while reentering its controls") } try move(away) try await Task.sleep(for: .milliseconds(1100)) guard controller.model.phase == .visible else { throw AppFixtureDrag.rejected("The popup did not retain its two-second departure grace period") } let hiddenDeadline = ContinuousClock.now.advanced(by: .seconds(2)) while controller.model.phase == .visible, ContinuousClock.now < hiddenDeadline { try await Task.sleep(for: .milliseconds(20)) } guard controller.model.phase == .hidden, NSWorkspace.shared.frontmostApplication?.processIdentifier == window.app.pid else { throw AppFixtureDrag.rejected("The popup failed to dismiss after departure without activation") } try restore() return } } let point = CGPoint(x: frame.midX, y: frame.midY) guard try owns(point) else { throw AppFixtureDrag.rejected("An unrelated surface covers the fixture tab") } try move(point) try await Task.sleep(for: .milliseconds(40)) release = try mouseEvent(.leftMouseUp, source: source, at: point) AppFixtureEventPosting.post(try mouseEvent(.leftMouseDown, source: source, at: point)) try await Task.sleep(for: .milliseconds(60)) if let release { AppFixtureEventPosting.post(release) } release = nil try await Task.sleep(for: .milliseconds(80)) try restore() } catch { let failure = error do { try restore() } catch { throw AppFixtureDrag.rejected("Hover inspection cleanup failed: \(error); input error: \(failure)") } throw failure } } private static func requireReleasedInput() throws { guard CGPreflightPostEventAccess() else { throw AppFixtureDrag.rejected("Native hover fixture input lacks event-posting access") } let buttons = NSEvent.pressedMouseButtons let flags = CGEventSource.flagsState(.combinedSessionState) .intersection([.maskCommand, .maskAlternate, .maskControl, .maskShift, .maskSecondaryFn]) guard buttons == 0, flags.isEmpty else { throw AppFixtureDrag.rejected( "Release native input before hover testing: buttons=\(buttons), modifiers=\(flags.rawValue)") } } private static func waitForReadiness( window: ManagedWindow, controller: HoverBarController, trigger: Bool ) async throws { let deadline = ContinuousClock.now.advanced(by: .seconds(3)) while true { try requireReleasedInput() let frontmost = NSWorkspace.shared.frontmostApplication?.processIdentifier let gripReady = controller.gripTabFrame(for: window.id, tab: window.id) != nil let triggerReady = controller.passiveTriggerPoint(for: window.id) != nil if frontmost == window.app.pid, gripReady, !trigger || triggerReady { return } guard ContinuousClock.now < deadline else { throw AppFixtureDrag.rejected( "Native fixture hover readiness timed out: window=\(window.id.rawValue), expectedPID=\(window.app.pid), frontmostPID=\(frontmost ?? 0), grip=\(gripReady), trigger=\(triggerReady), popup=\(controller.model.phase)" ) } try await Task.sleep(for: .milliseconds(20)) } } static func frontmostWindow(at point: CGPoint) throws -> WindowServer.Window? { guard let number = WindowHitTest.nativeWindowNumber(at: point) else { throw InputError.windowListUnavailable } if number == kCGNullWindowID { return nil } guard let window = try WindowServer().windows(options: .optionOnScreenOnly).first(where: { $0.isOnscreen && $0.number == number }) else { throw InputError.windowListUnavailable } return window } static func mouseEvent( _ type: CGEventType, source: CGEventSource, at point: CGPoint, flags: CGEventFlags = [] ) throws -> CGEvent { guard [.mouseMoved, .leftMouseDown, .leftMouseUp].contains(type), point.x.isFinite, point.y.isFinite, let event = CGEvent( mouseEventSource: source, mouseType: type, mouseCursorPosition: point, mouseButton: .left) else { throw AppFixtureDrag.rejected("Could not create a hover mouse event") } event.flags = flags event.setIntegerValueField(.mouseEventClickState, value: type == .mouseMoved ? 0 : 1) return event } }