From 917e661e8800bbb4a003a16cdfebc2d78794ae70 Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Mon, 14 Nov 2022 18:32:18 +0100 Subject: [PATCH] feat: show alttab on display with active menubar when needed (#2107) closes #2107 --- src/api-wrappers/PrivateApis.swift | 9 +++++++-- src/logic/NSScreen.swift | 14 +++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/api-wrappers/PrivateApis.swift b/src/api-wrappers/PrivateApis.swift index 60aa3fcfc..d756fb21c 100644 --- a/src/api-wrappers/PrivateApis.swift +++ b/src/api-wrappers/PrivateApis.swift @@ -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 @@ -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 // ------------------------------------------------------------ @@ -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 diff --git a/src/logic/NSScreen.swift b/src/logic/NSScreen.swift index 909935da6..9f04d0b07 100644 --- a/src/logic/NSScreen.swift +++ b/src/logic/NSScreen.swift @@ -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) } }