Browse Source

cocoa: release any mouse buttons not pressed when gaining focus

Fixes https://github.com/libsdl-org/SDL/issues/13134
Sam Lantinga 3 weeks ago
parent
commit
92eaa34277
1 changed files with 17 additions and 0 deletions
  1. 17 0
      src/video/cocoa/SDL_cocoamouse.m

+ 17 - 0
src/video/cocoa/SDL_cocoamouse.m

@@ -491,6 +491,22 @@ NSWindow *Cocoa_GetMouseFocus()
     return Cocoa_MouseFocus;
 }
 
+static void Cocoa_ReconcileButtonState(NSEvent *event)
+{
+    // Send mouse up events for any buttons that are no longer pressed
+    Uint32 buttons = SDL_GetMouseState(NULL, NULL);
+    if (buttons && ![NSEvent pressedMouseButtons]) {
+        Uint8 button = SDL_BUTTON_LEFT;
+        while (buttons) {
+            if (buttons & 0x01) {
+                SDL_SendMouseButton(Cocoa_GetEventTimestamp([event timestamp]), SDL_GetMouseFocus(), SDL_GLOBAL_MOUSE_ID, button, false);
+            }
+            ++button;
+            buttons >>= 1;
+        }
+    }
+}
+
 void Cocoa_HandleMouseEvent(SDL_VideoDevice *_this, NSEvent *event)
 {
     SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
@@ -515,6 +531,7 @@ void Cocoa_HandleMouseEvent(SDL_VideoDevice *_this, NSEvent *event)
     } else {
         if ([event window] != NULL) {
             Cocoa_MouseFocus = [event window];
+            Cocoa_ReconcileButtonState(event);
         }
     }