[macOS] Fix confined mouse movement getting out of sync.

This commit is contained in:
Pāvels Nadtočajevs 2026-02-13 10:56:46 +02:00
parent 20d58a38c6
commit d1698255bc
No known key found for this signature in database
GPG key ID: 8413210218EF35D2

View file

@ -1356,17 +1356,24 @@ bool DisplayServerMacOS::update_mouse_wrap(WindowData &p_wd, NSPoint &r_delta, N
// Confine mouse position to the window, and update delta.
NSRect frame = [p_wd.window_view frame];
NSPoint conf_pos = r_mpos;
conf_pos.x = CLAMP(conf_pos.x + r_delta.x, 0.f, frame.size.width);
conf_pos.y = CLAMP(conf_pos.y - r_delta.y, 0.f, frame.size.height);
r_delta.x = conf_pos.x - r_mpos.x;
r_delta.y = r_mpos.y - conf_pos.y;
r_mpos = conf_pos;
NSRect frameOnScreen = [[p_wd.window_view window] convertRectToScreen:frame];
frameOnScreen.origin.y = CGDisplayBounds(CGMainDisplayID()).size.height - frameOnScreen.origin.y;
CGEventRef ourEvent = CGEventCreate(nullptr);
NSPoint conf_pos = CGEventGetLocation(ourEvent);
CFRelease(ourEvent);
NSPoint prev_conf_pos = conf_pos;
conf_pos.x = CLAMP(conf_pos.x + r_delta.x, frameOnScreen.origin.x, frameOnScreen.origin.x + frameOnScreen.size.width);
conf_pos.y = CLAMP(conf_pos.y + r_delta.y, frameOnScreen.origin.y - frameOnScreen.size.height, frameOnScreen.origin.y);
r_delta.x = conf_pos.x - prev_conf_pos.x;
r_delta.y = conf_pos.y - prev_conf_pos.y;
NSPoint wnd_point = NSMakePoint(conf_pos.x, CGDisplayBounds(CGMainDisplayID()).size.height - conf_pos.y);
r_mpos = [[p_wd.window_view window] convertPointFromScreen:wnd_point];
// Move mouse cursor.
NSRect point_in_window_rect = NSMakeRect(conf_pos.x, conf_pos.y, 0, 0);
conf_pos = [[p_wd.window_view window] convertRectToScreen:point_in_window_rect].origin;
conf_pos.y = CGDisplayBounds(CGMainDisplayID()).size.height - conf_pos.y;
CGWarpMouseCursorPosition(conf_pos);
// Save warp data.