import AppKit import ApplicationServices import CoreGraphics // WindowActivator runs the window actions: activate, minimize, and close. // The class follows spec section 7.6. The class is main-actor isolated. // // NOTE: These AX calls run on the main actor. A user click starts each action, // so the call is short. The spec wants AX calls off the main thread. A later // change can move these reads to the serial AX queue. @MainActor final class WindowActivator { private let store: WindowStore private let cache: AXElementCache init(store: WindowStore, cache: AXElementCache) { self.store = store self.cache = cache } // activate brings the window to the front. The method handles the current // Space, a minimized window, and a window on another Space. func activate(windowID: CGWindowID) { guard let info = findWindow(windowID) else { Log.activation.error("activate: no window info for \(windowID)") return } let app = NSRunningApplication(processIdentifier: info.pid) let element = resolveElement(for: info) // Step 1: restore a minimized window. if info.isMinimized, let element = element { setBool(element, kAXMinimizedAttribute, false) } if info.isOnCurrentSpace { // Step 2: the window is on the current Space. if let element = element { AXUIElementPerformAction(element, kAXRaiseAction as CFString) } app?.activate() if let element = element { setBool(element, kAXMainAttribute, true) } } else { // Step 3: the window is on another Space. if let element = element { // Method A: raise the remote element, then activate the app. AXUIElementPerformAction(element, kAXRaiseAction as CFString) app?.activate() } else { // Method B: activate the app only. Log.activation.warning("activate: no AX element for other-Space window \(windowID); app activate only") app?.activate() } } // Step 4: check the focused window after 300 ms. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in self?.verifyFocused(info) } } // minimize minimizes the window. func minimize(windowID: CGWindowID) { guard let info = findWindow(windowID), let element = resolveElement(for: info) else { Log.activation.error("minimize: no AX element for \(windowID)") return } setBool(element, kAXMinimizedAttribute, true) } // close presses the close button of the window. The application can show a // "save changes?" dialog. This is correct behavior. func close(windowID: CGWindowID) { guard let info = findWindow(windowID), let element = resolveElement(for: info) else { Log.activation.error("close: no AX element for \(windowID)") return } var button: CFTypeRef? let error = AXUIElementCopyAttributeValue(element, kAXCloseButtonAttribute as CFString, &button) guard error == .success, let button = button, CFGetTypeID(button) == AXUIElementGetTypeID() else { Log.activation.error("close: no close button for \(windowID)") return } let buttonElement = button as! AXUIElement AXUIElementPerformAction(buttonElement, kAXPressAction as CFString) } // resolveElement returns the AX element for a window. The method reads the // cache first. For a window on another Space the method uses the private // remote token. private func resolveElement(for info: WindowInfo) -> AXUIElement? { if let cached = cache.element(for: info.id) { return cached } if info.isOnCurrentSpace == false { return PrivateAPI.shared.remoteElement(pid: info.pid, windowID: info.id) } return nil } // verifyFocused reads the focused window of the application. The method // logs a warning if the focused window is not the target window. private func verifyFocused(_ info: WindowInfo) { let appElement = AXUIElementCreateApplication(info.pid) var focused: CFTypeRef? let error = AXUIElementCopyAttributeValue(appElement, kAXFocusedWindowAttribute as CFString, &focused) guard error == .success, let focused = focused, CFGetTypeID(focused) == AXUIElementGetTypeID() else { Log.activation.debug("verify: cannot read focused window for pid \(info.pid)") return } let focusedElement = focused as! AXUIElement let focusedID = PrivateAPI.shared.windowID(for: focusedElement) if focusedID != info.id { let text = focusedID.map { String($0) } ?? "nil" Log.activation.debug("verify: focused window \(text, privacy: .public) is not target \(info.id)") } } private func findWindow(_ id: CGWindowID) -> WindowInfo? { for pid in store.allPIDs() { if let window = store.windows(for: pid).first(where: { $0.id == id }) { return window } } return nil } private func setBool(_ element: AXUIElement, _ attribute: String, _ value: Bool) { let cfValue: CFTypeRef = value ? kCFBooleanTrue : kCFBooleanFalse AXUIElementSetAttributeValue(element, attribute as CFString, cfValue) } }