Skip to content

Commit

Permalink
fix: Window switcher lag (#280)
Browse files Browse the repository at this point in the history
* fix: lazily update window switcher cache rather than wait for update before displaying windows

* fix valid window logic
  • Loading branch information
ejbills authored Aug 30, 2024
1 parent 24b4a6e commit e3821d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
30 changes: 19 additions & 11 deletions DockDoor/Utilities/KeybindHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,27 @@ class KeybindHelper {
}

private func showHoverWindow() {
Task { [weak self] in
// Show window immediately on the main thread
DispatchQueue.main.async { [weak self] in
guard let self, isModifierKeyPressed else { return }

let windows = WindowUtil.getAllWindowInfosAsList()

SharedPreviewWindowCoordinator.shared.showWindow(
appName: "Alt-Tab",
windows: windows,
overrideDelay: true,
centeredHoverWindowState: .windowSwitcher,
onWindowTap: { SharedPreviewWindowCoordinator.shared.hideWindow() }
)
}

Task(priority: .background) { [weak self] in
guard self != nil else { return }
do {
guard let self else { return }
let windows = try await WindowUtil.activeWindows(for: "")
await MainActor.run { [weak self] in
guard let self else { return }
if isModifierKeyPressed {
SharedPreviewWindowCoordinator.shared.showWindow(appName: "Alt-Tab", windows: windows, overrideDelay: true,
centeredHoverWindowState: .windowSwitcher, onWindowTap: { SharedPreviewWindowCoordinator.shared.hideWindow() })
}
}
_ = try await WindowUtil.activeWindows(for: "")
} catch {
print("Error fetching active windows: \(error)")
print("Failed to update windows from keybind helper.")
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions DockDoor/Utilities/WindowUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,16 @@ enum WindowUtil {
}

static func isValidElement(_ element: AXUIElement) -> Bool {
do {
let _ = try element.role()
return true
} catch {
return false
}
// Check if we can get the window's position
var position: CFTypeRef?
let positionResult = AXUIElementCopyAttributeValue(element, kAXPositionAttribute as CFString, &position)

// Check if we can get the window's size
var size: CFTypeRef?
let sizeResult = AXUIElementCopyAttributeValue(element, kAXSizeAttribute as CFString, &size)

// If we can get both position and size, the window likely still exists
return positionResult == .success && sizeResult == .success
}

static func findWindow(matchingWindow window: SCWindow, in axWindows: [AXUIElement]) -> AXUIElement? {
Expand Down

0 comments on commit e3821d9

Please sign in to comment.