Skip to content

Commit

Permalink
fix: regularly purge invalid cached windows
Browse files Browse the repository at this point in the history
closes #330
  • Loading branch information
ejbills committed Nov 22, 2024
1 parent e0a54ac commit 658a45e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
8 changes: 7 additions & 1 deletion DockDoor/Utilities/WindowManipulationObservers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class WindowManipulationObservers {

// Set up observers for already running applications
for app in NSWorkspace.shared.runningApplications {
if app.activationPolicy == .regular {
if app.activationPolicy == .regular, app.processIdentifier != ProcessInfo.processInfo.processIdentifier {
createObserverForApp(app)
}
}
Expand Down Expand Up @@ -133,6 +133,12 @@ func axObserverCallback(observer: AXObserver, element: AXUIElement, notification
WindowUtil.updateStatusOfWindowCache(pid: pid, isParentAppHidden: true)
case kAXApplicationShownNotification:
WindowUtil.updateStatusOfWindowCache(pid: pid, isParentAppHidden: false)
case kAXWindowCreatedNotification:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
if SharedPreviewWindowCoordinator.shared.isVisible {
SharedPreviewWindowCoordinator.shared.hideWindow()
}
}
default:
break
}
Expand Down
41 changes: 24 additions & 17 deletions DockDoor/Utilities/WindowUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,7 @@ enum WindowUtil {
} else {
windowInfo.app.terminate()
}

removeWindowFromDesktopSpaceCache(with: windowInfo.app.processIdentifier, removeAll: true)
purgeAppCache(with: windowInfo.app.processIdentifier)
}

// MARK: - Active Window Handling
Expand Down Expand Up @@ -453,11 +452,13 @@ enum WindowUtil {
// Wait for all tasks to complete
_ = try await group.waitForAll()

let windows = desktopSpaceWindowCacheManager.readCache(pid: app.processIdentifier)

guard !Defaults[.ignoreAppsWithSingleWindow] || windows.count > 1 else { return [] }
if let purifiedWindows = await WindowUtil.purifyAppCache(with: app.processIdentifier, removeAll: false) {
let windows = purifiedWindows.sorted(by: { $0.date > $1.date })
guard !Defaults[.ignoreAppsWithSingleWindow] || windows.count > 1 else { return [] }
return windows
}

return windows.sorted(by: { $0.date > $1.date })
return []
}

static func updateAllWindowsInCurrentSpace() async {
Expand Down Expand Up @@ -553,22 +554,28 @@ enum WindowUtil {
desktopSpaceWindowCacheManager.removeFromCache(pid: pid, windowId: windowId)
}

static func removeWindowFromDesktopSpaceCache(with pid: pid_t, removeAll: Bool) {
static func purifyAppCache(with pid: pid_t, removeAll: Bool) async -> Set<WindowInfo>? {
if removeAll {
desktopSpaceWindowCacheManager.writeCache(pid: pid, windowSet: [])
return nil
} else {
Task {
let existingWindowsSet = desktopSpaceWindowCacheManager.readCache(pid: pid)
if existingWindowsSet.isEmpty {
return
}
for window in existingWindowsSet {
if !isValidElement(window.axElement) {
desktopSpaceWindowCacheManager.removeFromCache(pid: pid, windowId: window.id)
return
}
let existingWindowsSet = desktopSpaceWindowCacheManager.readCache(pid: pid)
if existingWindowsSet.isEmpty {
return nil
}

var purifiedSet = existingWindowsSet
for window in existingWindowsSet {
if !isValidElement(window.axElement) {
purifiedSet.remove(window)
desktopSpaceWindowCacheManager.removeFromCache(pid: pid, windowId: window.id)
}
}
return purifiedSet
}
}

static func purgeAppCache(with pid: pid_t) {
desktopSpaceWindowCacheManager.writeCache(pid: pid, windowSet: [])
}
}

0 comments on commit 658a45e

Please sign in to comment.