Skip to content

Commit

Permalink
fix: windows launched already fullscreen sometimes didn't show (#824)
Browse files Browse the repository at this point in the history
closes #824
  • Loading branch information
lwouis committed Mar 13, 2021
1 parent 5971642 commit 62b43f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/api-wrappers/PrivateApis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ enum CGSSpaceType: Int {
@_silgen_name("CGSSpaceGetType")
func CGSSpaceGetType(_ cid: CGSConnectionID, _ sid: CGSSpaceID) -> CGSSpaceType

// returns true if the current screen is animating
// useful to detect Spaces transitions, windows going fullscreen, etc
@_silgen_name("SLSManagedDisplayIsAnimating")
func SLSManagedDisplayIsAnimating(_ cid: CGSConnectionID, _ displayUuid: CFString) -> Bool


// ------------------------------------------------------------
Expand Down
34 changes: 27 additions & 7 deletions src/logic/events/AccessibilityEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,31 @@ fileprivate func handleEvent(_ type: String, _ element: AXUIElement) throws {
fileprivate func focusedUiElementChanged(_ element: AXUIElement, _ pid: pid_t) throws {
if NSRunningApplication(processIdentifier: pid) != nil {
let currentWindows = try AXUIElementCreateApplication(pid).windows()
DispatchQueue.main.async {
let windows = updateTabs(pid, currentWindows)
retryToRefreshTabsUntilScreenIsNotAnimating(pid, currentWindows) { windows in
App.app.refreshOpenUi(windows)
}
}
}

// the algorithm used by updateTabs is incorrect is the screen is in the middle of an animation (e.g. window going fullscreen)
// we retry until there is no animation, then we proceed
fileprivate func retryToRefreshTabsUntilScreenIsNotAnimating(_ pid: pid_t, _ currentWindows: [AXUIElement]?, _ fn: @escaping ([Window]) -> Void) {
if let mainScreen = NSScreen.main,
let uuid = mainScreen.uuid() {
retryAxCallUntilTimeout {
if SLSManagedDisplayIsAnimating(cgsMainConnectionId, uuid) {
throw AxError.runtimeError
}
DispatchQueue.main.async {
fn(updateTabs(pid, currentWindows))
}
}
}
}

// when a window is tabbed, the AX call to get windows doesn't list it
// 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
fileprivate func updateTabs(_ pid: pid_t, _ currentWindows: [AXUIElement]?) -> [Window] {
let windows = Windows.list.filter { w in
if w.application.pid == pid && pid != ProcessInfo.processInfo.processIdentifier &&
Expand Down Expand Up @@ -157,12 +175,14 @@ fileprivate func windowDestroyed(_ element: AXUIElement, _ pid: pid_t) throws {
let windowlessApp = window.application.addWindowslessAppsIfNeeded()
if Windows.list.count > 0 {
// closing a tab may make another tab visible; we refresh tab status
var windows = updateTabs(pid, currentWindows)
Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(window)
if let windowlessApp = windowlessApp {
windows.append(contentsOf: windowlessApp)
retryToRefreshTabsUntilScreenIsNotAnimating(pid, currentWindows) { windows in
Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(window)
if let windowlessApp = windowlessApp {
App.app.refreshOpenUi(windows + windowlessApp)
} else {
App.app.refreshOpenUi(windows)
}
}
App.app.refreshOpenUi(windows)
} else {
App.app.hideUi()
}
Expand Down

0 comments on commit 62b43f2

Please sign in to comment.