Created
November 9, 2025 08:49
-
-
Save stormychel/01a26d655955834beac5eb2a890b2c28 to your computer and use it in GitHub Desktop.
Cocoa to Quartz point conversion
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
| /// Converts a Cocoa global point (origin at lower-left) into Quartz display coordinates (origin at upper-left). - #763 | |
| static func quartzPoint(from cocoaPoint: CGPoint) -> CGPoint { | |
| let screens = NSScreen.screens | |
| // Find screen containing the point; fall back to main screen | |
| let targetScreen = screens.first { NSMouseInRect(cocoaPoint, $0.frame, false) } ?? NSScreen.main | |
| guard let screen = targetScreen else { return cocoaPoint } | |
| // Translate into screen-local coordinates | |
| let localY = cocoaPoint.y - screen.frame.origin.y | |
| let flippedLocalY = screen.frame.size.height - localY | |
| let quartzY = screen.frame.origin.y + flippedLocalY | |
| return CGPoint(x: cocoaPoint.x, y: quartzY) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment