Skip to content

[osx] Use NSTrackingInVisibleRect for mouse movement tracking - #21996

Merged
MrJul merged 4 commits into
mainfrom
dev/timill/mouse-move
Sep 1, 2026
Merged

MrJul merged 4 commits into
mainfrom
dev/timill/mouse-move

Conversation

@drasticactions

Copy link
Copy Markdown
Contributor

I finally got so annoyed with this that I took the time to try to fix it.

When you resize a window on macOS, you sometimes see the traffic-light icons highlight, and the drop-down menu for window positioning appear. This is because we had set up an NSTrackingArea on every setFrameSize, which caused stale locations to be set for the window's mouse position. That would happen to be located in the top-left corner, which would make macOS think the buttons were being highlighted. Instead of recreating it, we can use NSTrackingInVisibleRect once. AppKit follows that; we get the same events, and it doesn't break the mouse positioning.

For tracking enter and exit, we can use the real cursor events with mouseLocationOutsideOfEventStream. and NSEventTypeMouseMoved.

** Copilot stuff below **

This pull request addresses issues with mouse tracking and event handling in the AvnView implementation for macOS. The changes ensure more reliable pointer tracking during and after view resizes, and prevent stale or synthetic mouse events from corrupting the pointer state. The most important changes are grouped below by theme.

Mouse Tracking Improvements:

  • The tracking area _area is now created with the NSTrackingInVisibleRect option during initialization, so it automatically follows the visible bounds of the view and does not need to be recreated on every resize. This prevents AppKit from queuing synthetic enter/exit events with stale locations during live-resize, which previously corrupted pointer positions. (AvnView.mm, native/Avalonia.Native/src/OSX/AvnView.mmL81-R91)
  • The logic to remove and recreate the tracking area in the setFrameSize: method was removed, since the tracking area now automatically adjusts with the view. (AvnView.mm, native/Avalonia.Native/src/OSX/AvnView.mmL129-L147)

Mouse Event Handling Fixes:

@MrJul MrJul added bug os-macos backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Aug 17, 2026
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0068388-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0068402-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0068637-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested, works as expected: no more annoying random popup menu.
LGTM!

@MrJul
MrJul added this pull request to the merge queue Sep 1, 2026
Merged via the queue into main with commit bf44e6a Sep 1, 2026
10 checks passed
@MrJul
MrJul deleted the dev/timill/mouse-move branch September 1, 2026 17:00
MrJul pushed a commit to MrJul/Avalonia that referenced this pull request Sep 2, 2026
…iaUI#21996)

* [osx] Use NSTrackingInVisibleRect for tracking mouse movement

* nit: comment

* Update native/Avalonia.Native/src/OSX/AvnView.mm

Co-authored-by: Max Katz <maxkatz6@outlook.com>

---------

Co-authored-by: Max Katz <maxkatz6@outlook.com>
@MrJul MrJul added backported-12.1.x and removed backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants