Skip to content

Commit

Permalink
fix: highlight right thumbnail when no window is focused (closes #1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Aug 30, 2021
1 parent 4623e5b commit f4d3db7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/logic/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
23 changes: 23 additions & 0 deletions src/logic/Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/logic/events/AccessibilityEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f4d3db7

Please sign in to comment.