Last active
March 28, 2025 13:09
-
-
Save zats/233d63b8e7845f62c7c200a359b3c076 to your computer and use it in GitHub Desktop.
Detect dock position on the screen for sandboxed macOS apps
This file contains hidden or 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
import AppKit | |
struct DockInfo { | |
let position: DockPosition | |
let frame: CGRect | |
} | |
enum DockPosition { | |
case bottom | |
case left | |
case right | |
case hiddenOrUnknown | |
} | |
func detectDockInfo() -> DockInfo? { | |
for screen in NSScreen.screens { | |
let full = screen.frame | |
let visible = screen.visibleFrame | |
let deltaLeft = visible.minX - full.minX | |
let deltaBottom = visible.minY - full.minY | |
let deltaRight = full.maxX - visible.maxX | |
let deltaTop = full.maxY - visible.maxY // menu bar | |
// Assume menu bar is always at top, subtract that first | |
let menuBarHeight = deltaTop | |
if deltaBottom > 0 { | |
let height = deltaBottom | |
let frame = CGRect(x: full.minX, y: full.minY, width: full.width, height: height) | |
return DockInfo(position: .bottom, frame: frame) | |
} else if deltaLeft > 0 { | |
let width = deltaLeft | |
let frame = CGRect(x: full.minX, y: full.minY, width: width, height: full.height - menuBarHeight) | |
return DockInfo(position: .left, frame: frame) | |
} else if deltaRight > 0 { | |
let width = deltaRight | |
let frame = CGRect(x: full.maxX - width, y: full.minY, width: width, height: full.height - menuBarHeight) | |
return DockInfo(position: .right, frame: frame) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment