Last active
March 5, 2023 01:01
-
-
Save stormychel/a372bc4ea4acf015dad21546829adf18 to your computer and use it in GitHub Desktop.
Check whether active window is in fullscreen mode on macOS / Cocoa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Check whether active window is in fullscreen mode. | |
/// - Parameter processIdentifier: the pid given by "NSWorkspace.shared.frontmostApplication?.processIdentifier" | |
/// - Returns: true if fullscreen, false if windowed | |
func isFullScreen(processIdentifier: pid_t) -> Bool { | |
if let winArray: CFArray = CGWindowListCopyWindowInfo(.excludeDesktopElements, kCGNullWindowID) { | |
for i in 0..<CFArrayGetCount(winArray) { | |
if let window: [String : Any] = unsafeBitCast(CFArrayGetValueAtIndex(winArray, i), to: CFDictionary.self) as? [String : Any] { | |
if let pid = window["kCGWindowOwnerPID"] as? Int32 { | |
if pid == processIdentifier { | |
if let bounds: [String : Any] = window["kCGWindowBounds"] as? [String : Any], let boundsWidth = bounds["Width"] as? CGFloat, let boundsHeight = bounds["Height"] as? CGFloat { | |
if boundsWidth == NSScreen.main?.frame.width && boundsHeight == NSScreen.main?.frame.height { | |
return true | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment