Skip to content

Commit

Permalink
fix: intellij fullscreen windows sometimes not showing (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Mar 31, 2021
1 parent 9b56bf0 commit f97717a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 66 deletions.
35 changes: 35 additions & 0 deletions src/api-wrappers/HelperExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,41 @@ extension pid_t {
}
return kinfo.kp_proc.p_stat == SZOMB
}

// 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
func retryToRefreshTabsUntilScreenIsNotAnimating(_ fn: @escaping ([Window]) -> Void) {
if NSRunningApplication(processIdentifier: self) != nil,
let mainScreen = NSScreen.main,
let uuid = mainScreen.uuid() {
retryAxCallUntilTimeout {
if SLSManagedDisplayIsAnimating(cgsMainConnectionId, uuid) {
throw AxError.runtimeError
}
let currentWindows = try AXUIElementCreateApplication(self).windows()
DispatchQueue.main.async {
fn(updateTabs(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
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 {
let oldIsTabbed = w.isTabbed
w.isTabbed = (currentWindows?.first { $0 == w.axUiElement } == nil)
return oldIsTabbed != w.isTabbed
}
return false
}
return windows
}
}

extension String {
Expand Down
2 changes: 2 additions & 0 deletions src/logic/Spaces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Spaces {
debugPrint("OS event", "activeSpaceDidChangeNotification")
refreshAllIdsAndIndexes()
updateCurrentSpace()
// if UI was kept open during Space transition, the Spaces may be obsolete; we refresh them
Windows.list.forEachAsync { $0.updatesWindowSpace() }
})
NSWorkspace.shared.notificationCenter.addObserver(forName: NSApplication.didChangeScreenParametersNotification, object: nil, queue: nil, using: { _ in
debugPrint("OS event", "didChangeScreenParametersNotification")
Expand Down
15 changes: 15 additions & 0 deletions src/logic/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,20 @@ class Window {
}
return application.runningApplication.localizedName ?? ""
}

func updatesWindowSpace() {
// macOS bug: if you tab a window, then move the tab group to another space, other tabs from the tab group will stay on the current space
// you can use the Dock to focus one of the other tabs and it will teleport that tab in the current space, proving that it's a macOS bug
// note: for some reason, it behaves differently if you minimize the tab group after moving it to another space
let spaceIds = cgWindowId.spaces()
if spaceIds.count == 1 {
spaceId = spaceIds.first!
spaceIndex = Spaces.idsAndIndexes.first { $0.0 == spaceIds.first! }!.1
} else if spaceIds.count > 1 {
spaceId = Spaces.currentSpaceId
spaceIndex = Spaces.currentSpaceIndex
isOnAllSpaces = true
}
}
}

19 changes: 1 addition & 18 deletions src/logic/Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,7 @@ class Windows {
// 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()
list.forEachAsync { (window: Window) in
updatesWindowSpace(window)
}
}

static func updatesWindowSpace(_ window: Window) {
// macOS bug: if you tab a window, then move the tab group to another space, other tabs from the tab group will stay on the current space
// you can use the Dock to focus one of the other tabs and it will teleport that tab in the current space, proving that it's a macOS bug
// note: for some reason, it behaves differently if you minimize the tab group after moving it to another space
let spaceIds = window.cgWindowId.spaces()
if spaceIds.count == 1 {
window.spaceId = spaceIds.first!
window.spaceIndex = Spaces.idsAndIndexes.first { $0.0 == spaceIds.first! }!.1
} else if spaceIds.count > 1 {
window.spaceId = Spaces.currentSpaceId
window.spaceIndex = Spaces.currentSpaceIndex
window.isOnAllSpaces = true
}
list.forEachAsync { $0.updatesWindowSpace() }
}

static func sortByLevel() {
Expand Down
60 changes: 12 additions & 48 deletions src/logic/events/AccessibilityEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,24 @@ fileprivate func handleEvent(_ type: String, _ element: AXUIElement) throws {
switch type {
case kAXApplicationActivatedNotification: try applicationActivated(element, pid)
case kAXApplicationHiddenNotification,
kAXApplicationShownNotification: try applicationHiddenOrShown(element, pid, type)
kAXApplicationShownNotification: try applicationHiddenOrShown(pid, type)
case kAXWindowCreatedNotification: try windowCreated(element, pid)
case kAXMainWindowChangedNotification,
kAXFocusedWindowChangedNotification: try focusedWindowChanged(element, pid)
case kAXUIElementDestroyedNotification: try windowDestroyed(element, pid)
case kAXWindowMiniaturizedNotification,
kAXWindowDeminiaturizedNotification: try windowMiniaturizedOrDeminiaturized(element, type)
case kAXTitleChangedNotification: try windowTitleChanged(element)
case kAXTitleChangedNotification: try windowTitleChanged(element, pid)
case kAXWindowResizedNotification: try windowResized(element)
case kAXWindowMovedNotification: try windowMoved(element)
case kAXFocusedUIElementChangedNotification: try focusedUiElementChanged(element, pid)
case kAXFocusedUIElementChangedNotification: try focusedUiElementChanged(pid)
default: return
}
}
}

fileprivate func focusedUiElementChanged(_ element: AXUIElement, _ pid: pid_t) throws {
if NSRunningApplication(processIdentifier: pid) != nil {
let currentWindows = try AXUIElementCreateApplication(pid).windows()
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 &&
w.spaceId == Spaces.currentSpaceId {
let oldIsTabbed = w.isTabbed
w.isTabbed = (currentWindows?.first { $0 == w.axUiElement } == nil)
return oldIsTabbed != w.isTabbed
}
return false
}
return windows
fileprivate func focusedUiElementChanged(_ pid: pid_t) throws {
pid.retryToRefreshTabsUntilScreenIsNotAnimating { App.app.refreshOpenUi($0) }
}

fileprivate func applicationActivated(_ element: AXUIElement, _ pid: pid_t) throws {
Expand All @@ -89,7 +52,7 @@ fileprivate func applicationActivated(_ element: AXUIElement, _ pid: pid_t) thro
}
}

fileprivate func applicationHiddenOrShown(_ element: AXUIElement, _ pid: pid_t, _ type: String) throws {
fileprivate func applicationHiddenOrShown(_ pid: pid_t, _ type: String) throws {
DispatchQueue.main.async {
if let app = (Applications.list.first { $0.pid == pid }) {
app.isHidden = type == kAXApplicationHiddenNotification
Expand Down Expand Up @@ -167,15 +130,13 @@ fileprivate func focusedWindowChanged(_ element: AXUIElement, _ pid: pid_t) thro

fileprivate func windowDestroyed(_ element: AXUIElement, _ pid: pid_t) throws {
let wid = try element.cgWindowId()
let appIsStillRunning = NSRunningApplication(processIdentifier: pid) != nil
let currentWindows = appIsStillRunning ? try AXUIElementCreateApplication(pid).windows() : []
DispatchQueue.main.async {
if let window = (Windows.list.first { $0.isEqualRobust(element, wid) }) {
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
retryToRefreshTabsUntilScreenIsNotAnimating(pid, currentWindows) { windows in
pid.retryToRefreshTabsUntilScreenIsNotAnimating { windows in
Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(window)
if let windowlessApp = windowlessApp {
App.app.refreshOpenUi(windows + windowlessApp)
Expand All @@ -201,14 +162,17 @@ fileprivate func windowMiniaturizedOrDeminiaturized(_ element: AXUIElement, _ ty
}
}

fileprivate func windowTitleChanged(_ element: AXUIElement) throws {
fileprivate func windowTitleChanged(_ element: AXUIElement, _ pid: pid_t) throws {
if let wid = try element.cgWindowId() {
let newTitle = try element.title()
DispatchQueue.main.async {
if let window = (Windows.list.first { $0.isEqualRobust(element, wid) }),
newTitle != nil && newTitle != window.title {
window.title = newTitle!
App.app.refreshOpenUi([window])
// refreshing tabs during title change helps mitigate some false positives of tab detection
pid.retryToRefreshTabsUntilScreenIsNotAnimating {
App.app.refreshOpenUi($0)
}
}
}
}
Expand Down

0 comments on commit f97717a

Please sign in to comment.