electron / electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS

Home Page:https://electronjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Couldn't get focused element's cursor position of and Electron app.

ArulOSlash opened this issue · comments

Preflight Checklist

Electron Version

17

What operating system are you using?

macOS

Operating System Version

macOS 12.3.1

What arch are you using?

arm64 (including Apple Silicon)

Last Known Working Electron version

No response

Expected Behavior

I'm working on a mac native app that has similar functionalities to the Rocket app.. Whenever a user types : outside of my app I'll show an NSWindow near the cursor. To get the cursor position I'm using Accessibility APIs.

I checked my window positioning in native apps like XCode, Notes, and Messages and tried on web apps as well everything is working fine But I'm facing an issue with electron apps that I'm getting the XOrigin always as 0 and YOrigin as 982 (That is equal to my screen's size: NSScreen.main?.frame.height).

  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
      }
  }

Actual Behavior

The method that I mentioned is supposed to return an actual cursor position like Native and web app does.

Testcase Gist URL

No response

Additional Information

I'm not developing my application using the Electron framework but I'm facing the above-mentioned issue with the apps that were developed using the Electron framework.

Thanks for reaching out!

Because we treat our issues list as the team's backlog, we close issues that are questions since they don't represent a task needing to be completed. For most questions about Electron there are a lot of options.
Check out the Electron community. There are also a bunch of helpful people in this Discord that should be willing to point you in the right direction.