Skip to content

Commit

Permalink
cocoa: Warp mouse to center of window before enabling relative mouse.
Browse files Browse the repository at this point in the history
This prevents the case where the mouse might be at the edge of the
window when enabling relative mode, which confuses macOS, at it
might believe the user is attempting to resize the window.

Fixes libsdl-org#6994.

(cherry picked from commit 2afb49b)
  • Loading branch information
icculus committed Jun 11, 2023
1 parent 4cfacd5 commit 125e742
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/video/cocoa/SDL_cocoamouse.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,16 @@ static int Cocoa_WarpMouse(SDL_Window *window, float x, float y)

static int Cocoa_SetRelativeMouseMode(SDL_bool enabled)
{
SDL_Window *window = SDL_GetKeyboardFocus();
CGError result;
SDL_Window *window;
SDL_CocoaWindowData *data;
if (enabled) {
if (window) {
/* make sure the mouse isn't at the corner of the window, as this can confuse things if macOS thinks a window resize is happening on the first click. */
const CGPoint point = CGPointMake((float)(window->x + (window->w / 2)), (float)(window->y + (window->h / 2)));
Cocoa_HandleMouseWarp(point.x, point.y);
CGWarpMouseCursorPosition(point);
}
DLog("Turning on.");
result = CGAssociateMouseAndMouseCursorPosition(NO);
} else {
Expand All @@ -313,7 +319,6 @@ static int Cocoa_SetRelativeMouseMode(SDL_bool enabled)
/* We will re-apply the non-relative mode when the window gets focus, if it
* doesn't have focus right now.
*/
window = SDL_GetKeyboardFocus();
if (!window) {
return 0;
}
Expand Down

0 comments on commit 125e742

Please sign in to comment.