Skip to content

Commit

Permalink
fix: don't show floating windows + efficiencies
Browse files Browse the repository at this point in the history
  • Loading branch information
louis.pontoise authored and lwouis committed Mar 10, 2020
1 parent 141562d commit 3f8e3ea
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
8 changes: 5 additions & 3 deletions alt-tab-macos/api-wrappers/AXUIElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ extension AXUIElement {
}

func isActualWindow(_ isAppHidden: Bool = false) -> Bool {
// TODO: should we displays windows that disappear when invoking Expose? (e.g. Outlook meeting reminder window) (see https://stackoverflow.com/a/49723037/2249756)
// TODO: TotalFinder and XtraFinder double-window hacks (see #84)
// TODO: should we display menubar windows? (e.g. iStats Pro dropdown menu)
// Some non-windows have subrole: nil (e.g. some OS elements), "AXUnknown" (e.g. Bartender), "AXSystemDialog" (e.g. Intellij tooltips)
// Some non-windows have title: nil (e.g. some OS elements)
// Minimized windows or windows of a hidden app have subrole "AXDialog"
return title() != nil && (subrole() == "AXStandardWindow" || isMinimized() || isAppHidden)
return title() != nil && (subrole() == "AXStandardWindow" || isMinimized() || isAppHidden) && isOnNormalLevel()
}

func isOnNormalLevel() -> Bool {
return cgWindowId().level() == CGWindowLevelForKey(.normalWindow)
}

func title() -> String? {
Expand Down
10 changes: 10 additions & 0 deletions alt-tab-macos/api-wrappers/CGWindowID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ extension CGWindowID {
return cgProperty("kCGSWindowTitle", String.self)
}

func level() -> CGWindowLevel {
var level = CGWindowLevel(0)
CGSGetWindowLevel(cgsMainConnectionId, self, &level)
return level
}

func spaces() -> [CGSSpaceID] {
return CGSCopySpacesForWindows(cgsMainConnectionId, CGSSpaceMask.all.rawValue, [self] as CFArray) as! [CGSSpaceID]
}

func screenshot() -> CGImage? {
// CGSHWCaptureWindowList
var windowId_ = self
Expand Down
10 changes: 5 additions & 5 deletions alt-tab-macos/api-wrappers/PrivateApis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ enum CGSSpaceMask: Int {
@_silgen_name("CGSCopySpacesForWindows")
func CGSCopySpacesForWindows(_ cid: CGSConnectionID, _ mask: CGSSpaceMask.RawValue, _ wids: CFArray) -> CFArray

// returns window level (see definition in CGWindowLevel.h) of provided window
// * macOS 10.10+
@_silgen_name("CGSGetWindowLevel") @discardableResult
func CGSGetWindowLevel(_ cid: CGSConnectionID, _ wid: CGWindowID, _ level: inout CGWindowLevel) -> CGError



// ------------------------------------------------------------
Expand Down Expand Up @@ -151,11 +156,6 @@ func CGSCopySpacesForWindows(_ cid: CGSConnectionID, _ mask: CGSSpaceMask.RawVal
//@_silgen_name("CGSGetWindowType") @discardableResult
//func CGSGetWindowType(_ wid: CGWindowID, _ type: inout UInt32) -> CGError
//
//// seems to always return 0, for windows in the same space, other space, minimized, etc
//// * macOS 10.10+
//@_silgen_name("CGSGetWindowLevel") @discardableResult
//func CGSGetWindowLevel(_ cid: CGSConnectionID, _ wid: CGWindowID, _ level: inout UInt32) -> CGError
//
//// * macOS 10.12+
//@_silgen_name("CGSProcessAssignToSpace") @discardableResult
//func CGSProcessAssignToSpace(_ cid: CGSConnectionID, _ pid: pid_t, _ sid: CGSSpaceID) -> CGError
Expand Down
1 change: 0 additions & 1 deletion alt-tab-macos/logic/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ private func eventWindowCreated(_ app: App, _ element: AXUIElement, _ applicatio

private func eventFocusedWindowChanged(_ app: App, _ element: AXUIElement) {
guard !app.appIsBeingUsed,
element.isActualWindow(),
let existingIndex = Windows.list.firstIndexThatMatches(element) else { return }
Windows.list.insert(Windows.list.remove(at: existingIndex), at: 0)
}
3 changes: 1 addition & 2 deletions alt-tab-macos/logic/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ private func eventWindowMiniaturizedOrDeminiaturized(_ app: App, _ element: AXUI
}

private func eventWindowTitleChanged(_ app: App, _ element: AXUIElement) {
guard element.isActualWindow(),
let window = Windows.list.firstWindowThatMatches(element),
guard let window = Windows.list.firstWindowThatMatches(element),
let newTitle = window.axUiElement.title(),
newTitle != window.title else { return }
window.title = newTitle
Expand Down
2 changes: 1 addition & 1 deletion alt-tab-macos/logic/Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Windows {
static func updateSpaces() {
let spacesMap = Spaces.allIdsAndIndexes()
for window in list {
let spaceIds = CGSCopySpacesForWindows(cgsMainConnectionId, CGSSpaceMask.all.rawValue, [window.cgWindowId] as CFArray) as! [CGSSpaceID]
let spaceIds = window.cgWindowId.spaces()
guard spaceIds.count > 0 else { continue }
if spaceIds.count > 1 {
window.spaceId = Spaces.currentSpaceId
Expand Down

0 comments on commit 3f8e3ea

Please sign in to comment.