Skip to content

Commit

Permalink
feat: show alttab on display with active menubar when needed (#2107)
Browse files Browse the repository at this point in the history
closes #2107
  • Loading branch information
lwouis committed Nov 14, 2022
1 parent bf905b1 commit 917e661
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/api-wrappers/PrivateApis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func CGSCopyWindowsWithOptionsAndTags(_ cid: CGSConnectionID, _ owner: Int, _ sp
// returns the current space ID on the provided display UUID
// * macOS 10.10+
@_silgen_name("CGSManagedDisplayGetCurrentSpace")
func CGSManagedDisplayGetCurrentSpace(_ cid: CGSConnectionID, _ displayUuid: CFString) -> CGSSpaceID
func CGSManagedDisplayGetCurrentSpace(_ cid: CGSConnectionID, _ displayUuid: ScreenUuid) -> CGSSpaceID

// adds the provided windows to the provided spaces
// * macOS 10.10-12.2
Expand Down Expand Up @@ -251,6 +251,11 @@ func CGSSpaceGetType(_ cid: CGSConnectionID, _ sid: CGSSpaceID) -> CGSSpaceType
@_silgen_name("CGSSpaceAddWindowsAndRemoveFromSpaces")
func CGSSpaceAddWindowsAndRemoveFromSpaces(_ cid: CGSConnectionID, _ sid: CGSSpaceID, _ wid: NSArray, _ notSure: Int) -> Void

// get the display UUID with the active menubar (other menubar are dimmed)
@_silgen_name("CGSCopyActiveMenuBarDisplayIdentifier")
func CGSCopyActiveMenuBarDisplayIdentifier(_ cid: CGSConnectionID) -> ScreenUuid


// ------------------------------------------------------------
// below are some notes on some private APIs I experimented with
// ------------------------------------------------------------
Expand All @@ -263,7 +268,7 @@ func CGSSpaceAddWindowsAndRemoveFromSpaces(_ cid: CGSConnectionID, _ sid: CGSSpa
//// returns true if the current screen is animating
//// useful to detect Spaces transitions, windows going fullscreen, etc
//@_silgen_name("SLSManagedDisplayIsAnimating")
//func SLSManagedDisplayIsAnimating(_ cid: CGSConnectionID, _ displayUuid: CFString) -> Bool
//func SLSManagedDisplayIsAnimating(_ cid: CGSConnectionID, _ displayUuid: ScreenUuid) -> Bool

//@_silgen_name("CGSGetSymbolicHotKeyValue")
//func CGSGetSymbolicHotKeyValue(_ hotKey: Int, _ options: inout UInt32, _ keyCode: inout UInt32, _ modifiers: inout UInt32) -> CGError
Expand Down
14 changes: 11 additions & 3 deletions src/logic/NSScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,27 @@ extension NSScreen {
}

// NSScreen.main docs are incorrect. It stopped returning the screen with the key window in macOS 10.9

// see https://stackoverflow.com/a/56268826/2249756
// There are a few cases where .main doesn't return the screen with the key window:
// * if the active screen shows a fullscreen app, it always returns screens[0]
// * if NSScreen.screensHaveSeparateSpaces == false, and key window is on another screen than screens[0], it still returns screens[0]
// we find the screen with the key window ourselves manually
static func active() -> NSScreen? {
if let app = Applications.find(NSWorkspace.shared.frontmostApplication?.processIdentifier),
let focusedWindow = app.focusedWindow {
return NSScreen.screens.first { focusedWindow.isOnScreen($0) }
if let app = Applications.find(NSWorkspace.shared.frontmostApplication?.processIdentifier) {
if let focusedWindow = app.focusedWindow {
return NSScreen.screens.first { focusedWindow.isOnScreen($0) }
}
return NSScreen.withActiveMenubar()
}
return nil
}

// there is only 1 active menubar. Other screens will show their menubar dimmed
static func withActiveMenubar() -> NSScreen? {
return NSScreen.screens.first { CGSCopyActiveMenuBarDisplayIdentifier(cgsMainConnectionId) == $0.uuid() }
}

static func withMouse() -> NSScreen? {
return NSScreen.screens.first { NSMouseInRect(NSEvent.mouseLocation, $0.frame, false) }
}
Expand Down

0 comments on commit 917e661

Please sign in to comment.