Skip to content

Conversation

Cmdv
Copy link

@Cmdv Cmdv commented Oct 6, 2025

Add screen-absolute mouse position and cursor setting support

Motivation

I understand this might not be accepted due to breaking changes but as it worked so well locally I thought I'd pass it upstream just incase 😄

When implementing custom window resize handles (or any drag operations that modify window geometry), window-relative mouse coordinates become unreliable. As the window resizes, the cursor position relative to the window constantly changes even when the mouse hasn't moved, making smooth drag calculations difficult or impossible.

Changes

Mouse Events - Screen Position Support:
Added screen_position: Point field to:

  • MouseEvent::CursorMoved
  • MouseEvent::DragEntered
  • MouseEvent::DragMoved
  • MouseEvent::DragDropped

macOS Cursor Support:

  • Implemented set_mouse_cursor() for macOS (was previously todo!())
  • Added new macos/cursor.rs module with cursor conversions
  • Supports all MouseCursor variants including resize cursors using private NSCursor APIs

Implementation Details

macOS:

  • Uses NSEvent::mouseLocation() for screen coordinates
  • Flips Y-axis from bottom-origin to top-origin for consistency with other platforms
  • Handles both NSView::convertPoint (window-relative) and global mouse location

Windows:

  • Uses GetCursorPos() for screen-absolute coordinates
  • Converts to logical coordinates using DPI scaling
  • Applied to both WM_MOUSEMOVE and drag events

X11:

  • Uses root_x/root_y from X11 events (already in screen space)
  • Converts to logical coordinates consistently with window-relative positions

Breaking Changes

This is a breaking API change. The following enum variants now have an additional screen_position field:

  • MouseEvent::CursorMoved { position, screen_position, modifiers }
  • MouseEvent::DragEntered { position, screen_position, modifiers, data }
  • MouseEvent::DragMoved { position, screen_position, modifiers, data }
  • MouseEvent::DragDropped { position, screen_position, modifiers, data }

Users will need to update pattern matches, either by:

  • Adding the new field: MouseEvent::CursorMoved { position, screen_position, modifiers }
  • Using wildcard: MouseEvent::CursorMoved { position, modifiers, .. }

2025-10-06 14 19 01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant