Skip to content

Commit

Permalink
fix: don't trigger ui refreshes if the app is not active
Browse files Browse the repository at this point in the history
  • Loading branch information
louis.pontoise authored and lwouis committed Mar 10, 2020
1 parent b03b0aa commit b9a0152
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 5 additions & 4 deletions alt-tab-macos/logic/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ class Application: NSObject {

func axObserverApplicationCallback(observer: AXObserver, element: AXUIElement, notificationName: CFString, applicationPointer: UnsafeMutableRawPointer?) -> Void {
let application = Unmanaged<Application>.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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions alt-tab-macos/logic/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions alt-tab-macos/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down

0 comments on commit b9a0152

Please # to comment.