From 1da3f3219c55dcbb36bc0e7dfcdfbbcfef9d7a7c Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Tue, 20 Apr 2021 17:55:58 +0900 Subject: [PATCH] fix: selected thumbnail was sometimes wrong (closes #926) --- src/api-wrappers/HelperExtensions.swift | 1 - src/logic/Applications.swift | 2 +- src/logic/Windows.swift | 16 ++++++++++++---- src/logic/events/AccessibilityEvents.swift | 5 +++-- src/ui/App.swift | 15 +++++++-------- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/api-wrappers/HelperExtensions.swift b/src/api-wrappers/HelperExtensions.swift index 93c4bbc4..80230cdb 100644 --- a/src/api-wrappers/HelperExtensions.swift +++ b/src/api-wrappers/HelperExtensions.swift @@ -192,7 +192,6 @@ extension pid_t { // we compare a fresh call to get the windows (currentWindows) to the windows we have already (Windows.list) // any window not in currentWindows is considered tabbed private func updateTabs(_ currentWindows: [AXUIElement]?) -> [Window] { - debugPrint(try? Windows.list.map { w in (w.cgWindowId, w.title, w.spaceId == Spaces.currentSpaceId, try currentWindows?.map { try $0.cgWindowId() }) }) let windows = Windows.list.filter { w in if w.application.pid == self && self != ProcessInfo.processInfo.processIdentifier && w.spaceId == Spaces.currentSpaceId { diff --git a/src/logic/Applications.swift b/src/logic/Applications.swift index b8da3d27..061c1d66 100644 --- a/src/logic/Applications.swift +++ b/src/logic/Applications.swift @@ -53,7 +53,7 @@ class Applications { // comparing pid here can fail here, as it can be already nil; we use isEqual here to avoid the issue Applications.list.removeAll { $0.runningApplication.isEqual(runningApp) } Windows.list.enumerated().forEach { (index, window) in - if window.application.runningApplication.isEqual(runningApp) && index < Windows.focusedWindowIndex { + if window.application.runningApplication.isEqual(runningApp) && index < Windows.focusedWindowIndex && window.shouldShowTheUser { windowsOnTheLeftOfFocusedWindow += 1 } } diff --git a/src/logic/Windows.swift b/src/logic/Windows.swift index 2e0fec53..5731ee43 100644 --- a/src/logic/Windows.swift +++ b/src/logic/Windows.swift @@ -100,11 +100,19 @@ class Windows { return targetIndex } - static func moveFocusedWindowIndexAfterWindowDestroyedInBackground(_ window: Window) { - if focusedWindowIndex > window.lastFocusOrder { + static func moveFocusedWindowIndexAfterWindowDestroyedInBackground(_ index: Int) { + if index < focusedWindowIndex { + cycleFocusedWindowIndex(-1) + } + } + + static func updateFocusedWindowIndex() { + if let focusedWindow = focusedWindow() { + if !focusedWindow.shouldShowTheUser { + cycleFocusedWindowIndex(windowIndexAfterCycling(1) > focusedWindowIndex ? 1 : -1) + } + } else { cycleFocusedWindowIndex(-1) - } else if focusedWindowIndex == window.lastFocusOrder && !window.shouldShowTheUser { - cycleFocusedWindowIndex(1) } } diff --git a/src/logic/events/AccessibilityEvents.swift b/src/logic/events/AccessibilityEvents.swift index c0eba030..e79116fb 100644 --- a/src/logic/events/AccessibilityEvents.swift +++ b/src/logic/events/AccessibilityEvents.swift @@ -129,13 +129,14 @@ fileprivate func focusedWindowChanged(_ element: AXUIElement, _ pid: pid_t) thro fileprivate func windowDestroyed(_ element: AXUIElement, _ pid: pid_t) throws { let wid = try element.cgWindowId() DispatchQueue.main.async { - if let window = (Windows.list.first { $0.isEqualRobust(element, wid) }) { + if let index = (Windows.list.firstIndex { $0.isEqualRobust(element, wid) }) { + let window = Windows.list[index] Windows.removeAndUpdateFocus(window) let windowlessApp = window.application.addWindowslessAppsIfNeeded() if Windows.list.count > 0 { // closing a tab may make another tab visible; we refresh tab status pid.retryToRefreshTabsUntilScreenIsNotAnimating { windows in - Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(window) + Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(index) if let windowlessApp = windowlessApp { App.app.refreshOpenUi(windows + windowlessApp) } else { diff --git a/src/ui/App.swift b/src/ui/App.swift index b47de7d3..d43295be 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -206,8 +206,13 @@ class App: AppCenterApplication, NSApplicationDelegate { // workaround: when Preferences > Mission Control > "Displays have separate Spaces" is unchecked, // switching between displays doesn't trigger .activeSpaceDidChangeNotification; we get the latest manually Spaces.refreshCurrentSpaceId() - Windows.reorderList() + Spaces.refreshAllIdsAndIndexes() + guard appIsBeingUsed else { return } refreshSpecificWindows(windowsToUpdate, currentScreen) + if (!Windows.list.contains { $0.shouldShowTheUser }) { hideUi(); return } + guard appIsBeingUsed else { return } + Windows.reorderList() + Windows.updateFocusedWindowIndex() guard appIsBeingUsed else { return } thumbnailsPanel.thumbnailsView.updateItemsAndLayout(currentScreen) guard appIsBeingUsed else { return } @@ -222,13 +227,7 @@ class App: AppCenterApplication, NSApplicationDelegate { guard appIsBeingUsed else { return } window.refreshThumbnail() Windows.refreshIfWindowShouldBeShownToTheUser(window, currentScreen) - if !window.shouldShowTheUser && window.cgWindowId == Windows.focusedWindow()!.cgWindowId { - let stepWithClosestWindow = Windows.windowIndexAfterCycling(-1) > Windows.focusedWindowIndex ? 1 : -1 - Windows.cycleFocusedWindowIndex(stepWithClosestWindow) - } else { - Spaces.refreshAllIdsAndIndexes() - window.updatesWindowSpace() - } + window.updatesWindowSpace() } }