@@ -121,6 +121,49 @@ enum SkyLightToolBelt {
121121 }
122122 }
123123
124+ /// Returns the display ID containing the given point, using the same tie-breaking
125+ /// WindowServer uses at display boundaries.
126+ /// - Parameter cgPoint: The point in the CoreGraphics coordinate system.
127+ /// - Returns: The matching `CGDirectDisplayID`, or `nil` if the point isn't on any managed display.
128+ static func bestManagedDisplayID( forCGPoint cgPoint: CGPoint ) -> CGDirectDisplayID ? {
129+ guard let SLSMainConnectionID = SkyLightSymbolLoader . SLSMainConnectionID,
130+ let SLSCopyBestManagedDisplayForPoint = SkyLightSymbolLoader . SLSCopyBestManagedDisplayForPoint
131+ else {
132+ return nil
133+ }
134+
135+ guard let uuidString = SLSCopyBestManagedDisplayForPoint ( SLSMainConnectionID ( ) , cgPoint) ? . takeRetainedValue ( ) ,
136+ let uuid = CFUUIDCreateFromString ( nil , uuidString)
137+ else {
138+ return nil
139+ }
140+
141+ let displayID = CGDisplayGetDisplayIDFromUUID ( uuid)
142+ return displayID != 0 ? displayID : nil
143+ }
144+
145+ /// Finds the topmost window at a given screen position.
146+ /// - Parameter position: The screen position to check.
147+ /// - Returns: The `CGWindowID` of the window at the position, or `nil` if none found.
148+ static func windowIDAtPosition( _ position: CGPoint ) -> CGWindowID ? {
149+ guard let SLSMainConnectionID = SkyLightSymbolLoader . SLSMainConnectionID,
150+ let SLSFindWindowByGeometry = SkyLightSymbolLoader . SLSFindWindowByGeometry
151+ else {
152+ return nil
153+ }
154+
155+ let cid = SLSMainConnectionID ( )
156+ var screenPoint = position
157+ var windowPoint = CGPoint . zero
158+ var hitWindowID : CGWindowID = 0
159+ var windowCID : Int32 = 0
160+
161+ let status = SLSFindWindowByGeometry ( cid, 0 , 1 , 0 , & screenPoint, & windowPoint, & hitWindowID, & windowCID)
162+ guard status == . success else { return nil }
163+
164+ return hitWindowID != 0 ? hitWindowID : nil
165+ }
166+
124167 /// Captures images for each of the windows that are passed in.
125168 /// - Parameter windowIDs: The `CGWindowID`s for each of the windows to capture.
126169 /// - Returns: An array of `CGImage`s for each window, in the same order as the windows that were passed in.
0 commit comments