Skip to content

Commit

Permalink
fix: window was not shown after closing tab (closes #696)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Nov 17, 2020
1 parent 4676758 commit a7e96f2
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/logic/events/AccessibilityEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fileprivate func handleEvent(_ type: String, _ element: AXUIElement) throws {
case kAXWindowCreatedNotification: try windowCreated(element, pid)
case kAXMainWindowChangedNotification,
kAXFocusedWindowChangedNotification: try focusedWindowChanged(element, pid)
case kAXUIElementDestroyedNotification: try windowDestroyed(element)
case kAXUIElementDestroyedNotification: try windowDestroyed(element, pid)
case kAXWindowMiniaturizedNotification,
kAXWindowDeminiaturizedNotification: try windowMiniaturizedOrDeminiaturized(element, type)
case kAXTitleChangedNotification: try windowTitleChanged(element)
Expand All @@ -35,20 +35,25 @@ fileprivate func focusedUiElementChanged(_ element: AXUIElement, _ pid: pid_t) t
if NSRunningApplication(processIdentifier: pid) != nil {
let currentWindows = try AXUIElementCreateApplication(pid).windows()
DispatchQueue.main.async {
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
}
let windows = updateTabs(pid, currentWindows)
App.app.refreshOpenUi(windows)
}
}
}

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 applicationActivated(_ element: AXUIElement) throws {
if let appFocusedWindow = try element.focusedWindow(),
let wid = try appFocusedWindow.cgWindowId() {
Expand Down Expand Up @@ -127,15 +132,22 @@ fileprivate func focusedWindowChanged(_ element: AXUIElement, _ pid: pid_t) thro
}
}

fileprivate func windowDestroyed(_ element: AXUIElement) throws {
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
var windows = updateTabs(pid, currentWindows)
Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(window)
App.app.refreshOpenUi(windowlessApp)
if let windowlessApp = windowlessApp {
windows.append(contentsOf: windowlessApp)
}
App.app.refreshOpenUi(windows)
} else {
App.app.hideUi()
}
Expand Down

0 comments on commit a7e96f2

Please sign in to comment.