Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Swift Shift/src/Manager/MouseTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class MouseTracker {
mouseLocationCoordinateSpace = coordinateSpace
let currentWindow = coordinateSpace == .coreGraphics ? WindowManager.getCurrentWindow(at: mouseLocation) : WindowManager.getCurrentWindow()
guard let currentWindow = currentWindow, !shouldIgnore(window: currentWindow) else { trackedWindow = nil; return }
shouldFocusWindow = PreferencesManager.loadBool(for: .focusOnApp)
shouldFocusWindow = PreferencesManager.loadBool(for: .focusOnApp, defaultValue: true)
shouldUseQuadrants = PreferencesManager.loadBool(for: .useQuadrants)
trackedWindowIsFocused = false; currentAction = action; initialMouseLocation = mouseLocation
trackedWindow = currentWindow; initialWindowLocation = WindowManager.getPosition(window: currentWindow)
Expand Down
26 changes: 25 additions & 1 deletion Swift Shift/src/Manager/WindowManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,31 @@ class WindowManager {
}
return getWindow(from: p as! AXUIElement)
}
static func focus(window: AXUIElement) { AXUIElementPerformAction(window, kAXRaiseAction as CFString); getNSApplication(from: window)?.activate() }
static func focus(window: AXUIElement) {
activateApplicationOfWindow(window)
setFocusedWindowAttributeForApplicationElementToWindow(window)
setMainAndFocusedAttributesOnWindowToTrue(window)
raiseWindowViaRaiseAction(window)
}
private static func applicationElementOfWindow(_ window: AXUIElement) -> AXUIElement? {
var pid: pid_t = 0
guard AXUIElementGetPid(window, &pid) == .success, pid != 0 else { return nil }
return AXUIElementCreateApplication(pid)
}
private static func activateApplicationOfWindow(_ window: AXUIElement) {
_ = getNSApplication(from: window)?.activate(options: .activateIgnoringOtherApps)
}
private static func setFocusedWindowAttributeForApplicationElementToWindow(_ window: AXUIElement) {
guard let app = applicationElementOfWindow(window) else { return }
AXUIElementSetAttributeValue(app, kAXFocusedWindowAttribute as CFString, window)
}
private static func setMainAndFocusedAttributesOnWindowToTrue(_ window: AXUIElement) {
AXUIElementSetAttributeValue(window, kAXMainAttribute as CFString, kCFBooleanTrue)
AXUIElementSetAttributeValue(window, kAXFocusedAttribute as CFString, kCFBooleanTrue)
}
private static func raiseWindowViaRaiseAction(_ window: AXUIElement) {
AXUIElementPerformAction(window, kAXRaiseAction as CFString)
}
static func getNSApplication(from element: AXUIElement) -> NSRunningApplication? {
var pid: pid_t = 0; AXUIElementGetPid(element, &pid); return NSRunningApplication(processIdentifier: pid)
}
Expand Down
Loading