Skip to content

Commit

Permalink
add back removing the focused window through AX but with an option now
Browse files Browse the repository at this point in the history
  • Loading branch information
godbout committed Aug 12, 2023
1 parent 8d98a9b commit 3dfe20c
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions Sources/WooshyWindowToTheForeground/Menus/Entrance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ class Entrance {
)
}
} else {
let visibleWindowsExcludingTheFocusedWindow = removeCurrentlyFocusedWindow(from: visibleWindows)
let filterOutFocusedWindow = (ProcessInfo.processInfo.environment["filter_out_focused_window"] == "1" ? true : false)
let visibleWindowsToShowInAlfredResults = filterOutFocusedWindow ? removeCurrentlyFocusedWindow(from: visibleWindows) : visibleWindows

if visibleWindowsExcludingTheFocusedWindow.isEmpty {
if visibleWindowsToShowInAlfredResults.isEmpty {
ScriptFilter.add(
Item(title: "The only visible window is the one you're already on!")
.subtitle("Press ↵ to go back to it")
.arg("do")
.variable(Variable(name: "action", value: "nothing"))
)
} else {
for window in visibleWindowsExcludingTheFocusedWindow {
for window in visibleWindowsToShowInAlfredResults {
ScriptFilter.add(
Item(title: window.title)
.subtitle(window.appName ?? "")
Expand Down Expand Up @@ -176,19 +177,29 @@ extension Entrance {
layer != 25 || height > 37
}

// the previous version of this function was "safer" in the sense that it captured the exact Focused Window
// while now the func removes the most top window that **should** be the Focused Window.
// the change was made because the call to AXUIElementCopyAttributeValue was taking 40ms, which was almost doubling
// the time it takes for the Workflow to grab the windows and render the Alfred Results. and that would make the
// Workflow feel slow (to my taste). so yeah, priorities.
private static func removeCurrentlyFocusedWindow(from visibleWindows: [Window]) -> [Window] {
var visibleWindowsExcludingTheFocusedWindow = visibleWindows

if visibleWindowsExcludingTheFocusedWindow.isEmpty == false {
visibleWindowsExcludingTheFocusedWindow.removeFirst()

if let axFocusedWindowNumber = axFocusedWindowNumber() {
visibleWindowsExcludingTheFocusedWindow.removeAll { cgWindow in
cgWindow.number == axFocusedWindowNumber
}
}

return visibleWindowsExcludingTheFocusedWindow
}

private static func axFocusedWindowNumber() -> CGWindowID? {
guard let pid = NSWorkspace.shared.frontmostApplication?.processIdentifier else { return nil }
let axApplication = AXUIElementCreateApplication(pid)

var axWindow: AnyObject?
guard AXUIElementCopyAttributeValue(axApplication, kAXFocusedWindowAttribute as CFString, &axWindow) == .success else { return nil }

var axWindowNumber: CGWindowID = 0
guard _AXUIElementGetWindow((axWindow as! AXUIElement), &axWindowNumber) == .success else { return nil }

return axWindowNumber
}

}

0 comments on commit 3dfe20c

Please sign in to comment.