Skip to content

Instantly share code, notes, and snippets.

@stormychel
Last active March 5, 2023 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormychel/a372bc4ea4acf015dad21546829adf18 to your computer and use it in GitHub Desktop.
Save stormychel/a372bc4ea4acf015dad21546829adf18 to your computer and use it in GitHub Desktop.
Check whether active window is in fullscreen mode on macOS / Cocoa
/// 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