Skip to content

Commit

Permalink
fix: things in background properly reflect in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Mar 24, 2020
1 parent 311beef commit fdf1524
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
16 changes: 7 additions & 9 deletions src/logic/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Application: NSObject {
let windows = axWindows.map { Window($0, self) }
Windows.list.insertAndScaleRecycledPool(windows, at: 0)
windows.forEach { _ in Windows.moveFocusedWindowIndexAfterWindowCreatedInBackground() }
(App.shared as! App).refreshOpenUi()
(App.shared as! App).refreshOpenUi(windows)
}

private func observeEvents() {
Expand Down Expand Up @@ -104,15 +104,13 @@ private func eventApplicationActivated(_ app: App, _ element: AXUIElement) {
let appFocusedWindow = element.focusedWindow(),
let existingIndex = Windows.list.firstIndexThatMatches(appFocusedWindow) else { return }
Windows.list.insert(Windows.list.remove(at: existingIndex), at: 0)
app.refreshOpenUi()
app.refreshOpenUi([Windows.list[0]])
}

private func eventApplicationHiddenOrShown(_ app: App, _ element: AXUIElement, _ type: String) {
for window in Windows.list {
guard window.application.axUiElement! == element else { continue }
window.isHidden = type == kAXApplicationHiddenNotification
}
app.refreshOpenUi()
let windows = Windows.list.filter { $0.application.axUiElement! == element }
windows.forEach { $0.isHidden = type == kAXApplicationHiddenNotification }
app.refreshOpenUi(windows)
}

private func eventWindowCreated(_ app: App, _ element: AXUIElement, _ application: Application) {
Expand All @@ -122,12 +120,12 @@ private func eventWindowCreated(_ app: App, _ element: AXUIElement, _ applicatio
let window = Window(element, application)
Windows.list.insertAndScaleRecycledPool([window], at: 0)
Windows.moveFocusedWindowIndexAfterWindowCreatedInBackground()
app.refreshOpenUi()
app.refreshOpenUi([window])
}

private func eventFocusedWindowChanged(_ app: App, _ element: AXUIElement) {
guard !app.appIsBeingUsed,
let existingIndex = Windows.list.firstIndexThatMatches(element) else { return }
Windows.list.insert(Windows.list.remove(at: existingIndex), at: 0)
app.refreshOpenUi()
app.refreshOpenUi([Windows.list[0]])
}
7 changes: 4 additions & 3 deletions src/logic/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private func eventWindowMiniaturizedOrDeminiaturized(_ app: App, _ element: AXUI
guard let index = Windows.list.firstIndexThatMatches(element) else { return }
let window = Windows.list[index]
window.isMinimized = type == kAXWindowMiniaturizedNotification
app.refreshOpenUi()
app.refreshOpenUi([window])
}

private func eventWindowTitleChanged(_ app: App, _ element: AXUIElement) {
Expand All @@ -148,10 +148,11 @@ private func eventWindowTitleChanged(_ app: App, _ element: AXUIElement) {
guard let newTitle = window.axUiElement.title(),
newTitle != window.title else { return }
window.title = newTitle
app.refreshOpenUi()
app.refreshOpenUi([window])
}

private func eventWindowResized(_ app: App, _ element: AXUIElement) {
guard let index = Windows.list.firstIndexThatMatches(element) else { return }
app.refreshOpenUi()
let window = Windows.list[index]
app.refreshOpenUi([window])
}
6 changes: 4 additions & 2 deletions src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@ class App: NSApplication, NSApplicationDelegate {
}

func reopenUi() {
// TODO: retest this
thumbnailsPanel!.orderOut(nil)
Windows.refreshAllThumbnails()
refreshOpenUi()
thumbnailsPanel!.show()
}

func refreshOpenUi() {
func refreshOpenUi(_ windowsToRefresh: [Window]? = nil) {
guard appIsBeingUsed else { return }
if let windowsToRefresh = windowsToRefresh {
windowsToRefresh.map { $0.refreshThumbnail() }
}
let currentScreen = Screen.preferred() // fix screen between steps since it could change (e.g. mouse moved to another screen)
guard uiWorkShouldBeDone else { return }
thumbnailsPanel!.thumbnailsView.updateItems(currentScreen)
Expand Down

0 comments on commit fdf1524

Please sign in to comment.