From a1ee38d5ee609141aa23b600a6b8520fbd856539 Mon Sep 17 00:00:00 2001 From: Charley Peng <23526+chid@users.noreply.github.com> Date: Sat, 6 Jun 2026 13:22:25 +1000 Subject: [PATCH 1/2] Fix focus on target window --- Swift Shift/src/Manager/WindowManager.swift | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Swift Shift/src/Manager/WindowManager.swift b/Swift Shift/src/Manager/WindowManager.swift index f450c08..6ec8c67 100644 --- a/Swift Shift/src/Manager/WindowManager.swift +++ b/Swift Shift/src/Manager/WindowManager.swift @@ -73,7 +73,19 @@ class WindowManager { if let parent = p { return getWindow(from: parent as! AXUIElement) } return nil } - static func focus(window: AXUIElement) { AXUIElementPerformAction(window, kAXRaiseAction as CFString); getNSApplication(from: window)?.activate() } + static func focus(window: AXUIElement) { + guard let app = getNSApplication(from: window) else { return } + let axApp = AXUIElementCreateApplication(app.processIdentifier) + let trueValue = kCFBooleanTrue! + + app.activate(options: [.activateIgnoringOtherApps]) + AXUIElementSetAttributeValue(axApp, kAXFrontmostAttribute as CFString, trueValue) + AXUIElementPerformAction(window, kAXRaiseAction as CFString) + AXUIElementSetAttributeValue(axApp, kAXMainWindowAttribute as CFString, window) + AXUIElementSetAttributeValue(axApp, kAXFocusedWindowAttribute as CFString, window) + AXUIElementSetAttributeValue(window, kAXMainAttribute as CFString, trueValue) + AXUIElementSetAttributeValue(window, kAXFocusedAttribute as CFString, trueValue) + } static func getNSApplication(from element: AXUIElement) -> NSRunningApplication? { var pid: pid_t = 0; AXUIElementGetPid(element, &pid); return NSRunningApplication(processIdentifier: pid) } @@ -89,4 +101,3 @@ class WindowManager { return WindowBounds(topLeft: fixed, topRight: NSPoint(x: fixed.x + windowSize.width, y: fixed.y), bottomLeft: NSPoint(x: fixed.x, y: fixed.y - windowSize.height), bottomRight: NSPoint(x: fixed.x + windowSize.width, y: fixed.y - windowSize.height)) } } - From c113118e0f2f2372456807a0cd3de4c145f3b4a8 Mon Sep 17 00:00:00 2001 From: Charley Peng <23526+chid@users.noreply.github.com> Date: Sat, 6 Jun 2026 13:43:51 +1000 Subject: [PATCH 2/2] fix: update ordering as suggested by CodeRabbit --- Swift Shift/src/Manager/WindowManager.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Swift Shift/src/Manager/WindowManager.swift b/Swift Shift/src/Manager/WindowManager.swift index 6ec8c67..eea3dd0 100644 --- a/Swift Shift/src/Manager/WindowManager.swift +++ b/Swift Shift/src/Manager/WindowManager.swift @@ -78,13 +78,15 @@ class WindowManager { let axApp = AXUIElementCreateApplication(app.processIdentifier) let trueValue = kCFBooleanTrue! - app.activate(options: [.activateIgnoringOtherApps]) - AXUIElementSetAttributeValue(axApp, kAXFrontmostAttribute as CFString, trueValue) AXUIElementPerformAction(window, kAXRaiseAction as CFString) AXUIElementSetAttributeValue(axApp, kAXMainWindowAttribute as CFString, window) AXUIElementSetAttributeValue(axApp, kAXFocusedWindowAttribute as CFString, window) AXUIElementSetAttributeValue(window, kAXMainAttribute as CFString, trueValue) AXUIElementSetAttributeValue(window, kAXFocusedAttribute as CFString, trueValue) + + AXUIElementSetAttributeValue(axApp, kAXFrontmostAttribute as CFString, trueValue) + // TODO: evaluate whether this works on sandboxed/Electron apps that ignore AX attributes. + app.activate(options: [.activateIgnoringOtherApps]) } static func getNSApplication(from element: AXUIElement) -> NSRunningApplication? { var pid: pid_t = 0; AXUIElementGetPid(element, &pid); return NSRunningApplication(processIdentifier: pid)