I'm working on a mac app that uses Accessibility APIs to get the cursor position from the focused element (Focused element can be from any app) and shows a window near the cursor position. I checked the window positioning in native apps like XCode, Notes, and Messages and tried on web apps as well everything is working fine.
I checked the same on the slack desktop app where I'm facing an issue. Which is I'm getting the XOrigin always as 0 and YOrigin as 982 (That is equal to my screen's size: NSScreen.main?.frame.height). Seems it is breaking on electron apps. What am I missing here? Do we need to account for anything else to handle electron apps?
Attaching my code for reference.
extension AXUIElement {
func getCursorRect() -> CGRect? {
guard let cursorPosition = cursorPosition else {return nil}
var cfRange: CFRange = .init(location: cursorPosition, length: 1)
let axval: AXValue? = AXValueCreate(.cfRange, &cfRange)
var bounds: CFTypeRef?
guard let axval = axval else {return nil}
AXUIElementCopyParameterizedAttributeValue(self, kAXBoundsForRangeParameterizedAttribute as CFString, axval, &bounds)
var cursorRect: CGRect = .zero
guard let bounds = bounds else {return nil}
AXValueGetValue(bounds as! AXValue, .cgRect, &cursorRect)
return cursorRect
}
}