diff --git a/src/logic/Application.swift b/src/logic/Application.swift index f601c6cbc..2b3fa3b83 100644 --- a/src/logic/Application.swift +++ b/src/logic/Application.swift @@ -14,6 +14,7 @@ class Application: NSObject { var dockLabel: String? var pid: pid_t { runningApplication.processIdentifier } var wasLaunchedBeforeAltTab = false + var focusedWindow: Window? = nil static func notifications(_ app: NSRunningApplication) -> [String] { let n = [ diff --git a/src/logic/Windows.swift b/src/logic/Windows.swift index 68069357e..a8c0ef5dd 100644 --- a/src/logic/Windows.swift +++ b/src/logic/Windows.swift @@ -24,6 +24,29 @@ class Windows { } } + static func setInitialFocusedWindowIndex() { + if let app = Applications.list.first { $0.pid == NSWorkspace.shared.frontmostApplication?.processIdentifier }, + app.focusedWindow == nil, + let lastFocusedWindowIndex = getLastFocusedWindowIndex() { + updateFocusedWindowIndex(lastFocusedWindowIndex) + } else { + updateFocusedWindowIndex(0) + cycleFocusedWindowIndex(1) + } + } + + static func getLastFocusedWindowIndex() -> Int? { + var index: Int? = nil + var lastFocusOrderMin = Int.max + Windows.list.enumerated().forEach { + if !$0.element.isWindowlessApp && $0.element.lastFocusOrder < lastFocusOrderMin { + lastFocusOrderMin = $0.element.lastFocusOrder + index = $0.offset + } + } + return index + } + static func appendAndUpdateFocus(_ window: Window) { list.forEach { $0.lastFocusOrder += 1 diff --git a/src/logic/events/AccessibilityEvents.swift b/src/logic/events/AccessibilityEvents.swift index e79116fbb..fe2377f86 100644 --- a/src/logic/events/AccessibilityEvents.swift +++ b/src/logic/events/AccessibilityEvents.swift @@ -44,6 +44,7 @@ fileprivate func applicationActivated(_ element: AXUIElement, _ pid: pid_t) thro app.hasBeenActiveOnce = true } let window = (appFocusedWindow != nil && wid != nil) ? Windows.updateLastFocus(appFocusedWindow!, wid!)?.first : nil + app.focusedWindow = window App.app.checkIfShortcutsShouldBeDisabled(window, app.runningApplication) App.app.refreshOpenUi(window != nil ? [window!] : nil) } @@ -123,6 +124,13 @@ fileprivate func focusedWindowChanged(_ element: AXUIElement, _ pid: pid_t) thro } } } + DispatchQueue.main.async { + Applications.list.first { $0.pid == pid }?.focusedWindow = Windows.list.first { $0.isEqualRobust(element, wid) } + } + } else { + DispatchQueue.main.async { + Applications.list.first { $0.pid == pid }?.focusedWindow = nil + } } } diff --git a/src/ui/App.swift b/src/ui/App.swift index 754b60608..9c2944e31 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -252,8 +252,7 @@ class App: AppCenterApplication, NSApplicationDelegate { Windows.refreshWhichWindowsToShowTheUser(screen) Windows.reorderList() if (!Windows.list.contains { $0.shouldShowTheUser }) { hideUi(); return } - Windows.updateFocusedWindowIndex(0) - Windows.cycleFocusedWindowIndex(1) + Windows.setInitialFocusedWindowIndex() delayedDisplayScheduled += 1 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Preferences.windowDisplayDelay) { () -> () in if self.delayedDisplayScheduled == 1 {