On OSX backend, the mouse cursor has an offset when it first comes to focus #5842
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
On macos (12.6 running on M1),
when the window comes into focus, the reported mouse cursor position has an offset.
Once the user clicks somewhere in the window the offset is gone.
It seems that before the user clicks somewhere in the window, imgui reports the screen coordinate instead of the window coordinate.

How to reproduce the problem
Build and run
example_apple_metal_macos
. Unless the window is placed at the top-left, the highlighted UI element will be offset relative to the cursor initially. Once the user clicks somewhere inside the window, the UI element under the mouse cursor will be selected.Reason
NSEvent's
locationInWindow
property is used to retrieve the window coordinate of the cursor when handling mouse events inImGui_ImplOSX_HandleEvent
ofimgui_impl_osx.mm
backend, but according to the reference, this property may contain the screen coordinate, if the NSEvent's.window
property is nil.Fix
Check the
.window
property of the NSEvent. If null, convert from screen coordinate to the window coordinate associated with the view