cocoa: Position non-left mouse button events in background windows correctly.

Fixes #4828.
This commit is contained in:
Ryan C. Gordon 2021-11-21 00:14:18 -05:00
parent 343fa61215
commit 18715acb55
1 changed files with 34 additions and 12 deletions

View File

@ -1150,16 +1150,44 @@ Cocoa_UpdateClipCursor(SDL_Window * window)
return NO; /* not a special area, carry on. */ return NO; /* not a special area, carry on. */
} }
static int
Cocoa_SendMouseButtonClicks(SDL_Mouse * mouse, NSEvent *theEvent, SDL_Window * window, const Uint8 state, const Uint8 button)
{
const SDL_MouseID mouseID = mouse->mouseID;
const int clicks = (int) [theEvent clickCount];
SDL_Window *focus = SDL_GetKeyboardFocus();
int rc;
// macOS will send non-left clicks to background windows without raising them, so we need to
// temporarily adjust the mouse position when this happens, as `mouse` will be tracking
// the position in the currently-focused window. We don't (currently) send a mousemove
// event for the background window, this just makes sure the button is reported at the
// correct position in its own event.
if ( focus && ([theEvent window] == ((SDL_WindowData *) focus->driverdata)->nswindow) ) {
rc = SDL_SendMouseButtonClicks(window, mouseID, state, button, clicks);
} else {
printf("BACKGROUND CLICK!\n"); fflush(stdout);
const int orig_x = mouse->x;
const int orig_y = mouse->y;
const NSPoint point = [theEvent locationInWindow];
mouse->x = (int) point.x;
mouse->y = (int) (window->h - point.y);
rc = SDL_SendMouseButtonClicks(window, mouseID, state, button, clicks);
mouse->x = orig_x;
mouse->y = orig_y;
}
return rc;
}
- (void)mouseDown:(NSEvent *)theEvent - (void)mouseDown:(NSEvent *)theEvent
{ {
const SDL_Mouse *mouse = SDL_GetMouse(); SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse) { if (!mouse) {
return; return;
} }
const SDL_MouseID mouseID = mouse->mouseID;
int button; int button;
int clicks;
/* Ignore events that aren't inside the client area (i.e. title bar.) */ /* Ignore events that aren't inside the client area (i.e. title bar.) */
if ([theEvent window]) { if ([theEvent window]) {
@ -1196,9 +1224,7 @@ Cocoa_UpdateClipCursor(SDL_Window * window)
break; break;
} }
clicks = (int) [theEvent clickCount]; Cocoa_SendMouseButtonClicks(mouse, theEvent, _data->window, SDL_PRESSED, button);
SDL_SendMouseButtonClicks(_data->window, mouseID, SDL_PRESSED, button, clicks);
} }
- (void)rightMouseDown:(NSEvent *)theEvent - (void)rightMouseDown:(NSEvent *)theEvent
@ -1213,14 +1239,12 @@ Cocoa_UpdateClipCursor(SDL_Window * window)
- (void)mouseUp:(NSEvent *)theEvent - (void)mouseUp:(NSEvent *)theEvent
{ {
const SDL_Mouse *mouse = SDL_GetMouse(); SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse) { if (!mouse) {
return; return;
} }
const SDL_MouseID mouseID = mouse->mouseID;
int button; int button;
int clicks;
if ([self processHitTest:theEvent]) { if ([self processHitTest:theEvent]) {
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0); SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
@ -1247,9 +1271,7 @@ Cocoa_UpdateClipCursor(SDL_Window * window)
break; break;
} }
clicks = (int) [theEvent clickCount]; Cocoa_SendMouseButtonClicks(mouse, theEvent, _data->window, SDL_RELEASED, button);
SDL_SendMouseButtonClicks(_data->window, mouseID, SDL_RELEASED, button, clicks);
} }
- (void)rightMouseUp:(NSEvent *)theEvent - (void)rightMouseUp:(NSEvent *)theEvent