From b9a0152f856ece0069386bc6e8bce808e3a1e2c1 Mon Sep 17 00:00:00 2001 From: "louis.pontoise" Date: Fri, 10 Jan 2020 11:01:56 +0900 Subject: [PATCH] fix: don't trigger ui refreshes if the app is not active --- alt-tab-macos/logic/Application.swift | 9 +++++---- alt-tab-macos/logic/Window.swift | 9 +++++---- alt-tab-macos/ui/App.swift | 1 + 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/alt-tab-macos/logic/Application.swift b/alt-tab-macos/logic/Application.swift index 751beff9..9a5a670f 100644 --- a/alt-tab-macos/logic/Application.swift +++ b/alt-tab-macos/logic/Application.swift @@ -66,11 +66,12 @@ class Application: NSObject { func axObserverApplicationCallback(observer: AXObserver, element: AXUIElement, notificationName: CFString, applicationPointer: UnsafeMutableRawPointer?) -> Void { let application = Unmanaged.fromOpaque(applicationPointer!).takeUnretainedValue() + let app = App.shared as! App let type = notificationName as String debugPrint("OS event: " + type, element.title()) switch type { case kAXApplicationActivatedNotification: - guard !(App.shared as! App).appIsBeingUsed, + guard !app.appIsBeingUsed, let appFocusedWindow = element.focusedWindow(), let existingIndex = Windows.listRecentlyUsedFirst.firstIndexThatMatches(appFocusedWindow) else { return } Windows.listRecentlyUsedFirst.insert(Windows.listRecentlyUsedFirst.remove(at: existingIndex), at: 0) @@ -79,7 +80,7 @@ func axObserverApplicationCallback(observer: AXObserver, element: AXUIElement, n guard window.application.axUiElement!.pid() == element.pid() else { continue } window.isHidden = type == kAXApplicationHiddenNotification } - (App.shared as! App).refreshOpenUi() + app.refreshOpenUi() case kAXWindowCreatedNotification: guard element.isActualWindow() else { return } // a window being un-minimized can trigger kAXWindowCreatedNotification @@ -89,9 +90,9 @@ func axObserverApplicationCallback(observer: AXObserver, element: AXUIElement, n Windows.moveFocusedWindowIndexAfterWindowCreatedInBackground() // TODO: find a better way to get thumbnail of the new window window.refreshThumbnail() - (App.shared as! App).refreshOpenUi() + app.refreshOpenUi() case kAXFocusedWindowChangedNotification: - guard !(App.shared as! App).appIsBeingUsed, + guard !app.appIsBeingUsed, element.isActualWindow(), let existingIndex = Windows.listRecentlyUsedFirst.firstIndexThatMatches(element) else { return } Windows.listRecentlyUsedFirst.insert(Windows.listRecentlyUsedFirst.remove(at: existingIndex), at: 0) diff --git a/alt-tab-macos/logic/Window.swift b/alt-tab-macos/logic/Window.swift index aa0a841b..8968be9b 100644 --- a/alt-tab-macos/logic/Window.swift +++ b/alt-tab-macos/logic/Window.swift @@ -100,27 +100,28 @@ class Window { func axObserverWindowCallback(observer: AXObserver, element: AXUIElement, notificationName: CFString, _: UnsafeMutableRawPointer?) -> Void { let type = notificationName as String + let app = App.shared as! App debugPrint("OS event: " + type, element.title()) switch type { case kAXUIElementDestroyedNotification: guard let existingIndex = Windows.listRecentlyUsedFirst.firstIndexThatMatches(element) else { return } Windows.listRecentlyUsedFirst.remove(at: existingIndex) - guard Windows.listRecentlyUsedFirst.count > 0 else { (App.shared as! App).hideUi(); return } + guard Windows.listRecentlyUsedFirst.count > 0 else { app.hideUi(); return } Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(existingIndex) - (App.shared as! App).refreshOpenUi() + app.refreshOpenUi() case kAXWindowMiniaturizedNotification, kAXWindowDeminiaturizedNotification: guard let window = Windows.listRecentlyUsedFirst.firstWindowThatMatches(element) else { return } window.isMinimized = type == kAXWindowMiniaturizedNotification // TODO: find a better way to get thumbnail of the new window (when AltTab is triggered min/demin animation) window.refreshThumbnail() - (App.shared as! App).refreshOpenUi() + app.refreshOpenUi() case kAXTitleChangedNotification: guard element.isActualWindow(), let window = Windows.listRecentlyUsedFirst.firstWindowThatMatches(element), let newTitle = window.axUiElement.title(), newTitle != window.title else { return } window.title = newTitle - (App.shared as! App).refreshOpenUi() + app.refreshOpenUi() default: return } } diff --git a/alt-tab-macos/ui/App.swift b/alt-tab-macos/ui/App.swift index fdbc9acf..65c774ed 100644 --- a/alt-tab-macos/ui/App.swift +++ b/alt-tab-macos/ui/App.swift @@ -104,6 +104,7 @@ class App: NSApplication, NSApplicationDelegate, NSWindowDelegate { } func refreshOpenUi() { + guard appIsBeingUsed else { return } let currentScreen = Screen.preferred() // fix screen between steps since it could change (e.g. mouse moved to another screen) if uiWorkShouldBeDone { thumbnailsPanel!.refreshCollectionView(currentScreen); debugPrint("refreshCollectionView") } if uiWorkShouldBeDone { thumbnailsPanel!.highlightCell(); debugPrint("highlightCellAt") }