Skip to content

Commit c4e60db

Browse files
committed
Enhance Cocoa mouse button state synchronization to exclude processed button, preventing duplicate events
1 parent a7a72a3 commit c4e60db

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ - (BOOL)processHitTest:(NSEvent *)theEvent
17031703
return NO; // not a special area, carry on.
17041704
}
17051705

1706-
static void Cocoa_SyncMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID)
1706+
static void Cocoa_SyncMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID, Uint8 excludeButton)
17071707
{
17081708
const NSUInteger actualButtons = [NSEvent pressedMouseButtons];
17091709
SDL_MouseInputSource *source = NULL;
@@ -1740,8 +1740,13 @@ static void Cocoa_SyncMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID)
17401740
actualButtonState |= SDL_BUTTON_X2MASK;
17411741
}
17421742

1743-
// Sync any buttons that are out of sync
1743+
// Sync any buttons that are out of sync (except the one being processed)
17441744
for (Uint8 button = SDL_BUTTON_LEFT; button <= SDL_BUTTON_X2; ++button) {
1745+
// Skip the button being processed to avoid duplicate events
1746+
if (excludeButton != 0 && button == excludeButton) {
1747+
continue;
1748+
}
1749+
17451750
Uint32 buttonMask = SDL_BUTTON_MASK(button);
17461751
bool sdlPressed = (sdlButtonState & buttonMask) != 0;
17471752
bool actuallyPressed = (actualButtonState & buttonMask) != 0;
@@ -1759,8 +1764,8 @@ static void Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL
17591764
//const int clicks = (int)[theEvent clickCount];
17601765
SDL_Window *focus = SDL_GetKeyboardFocus();
17611766

1762-
// Sync button state with hardware before processing event
1763-
Cocoa_SyncMouseButtonState(mouse, mouseID);
1767+
// Sync button state with hardware before processing event (exclude the button being processed to avoid duplicate events)
1768+
Cocoa_SyncMouseButtonState(mouse, mouseID, button);
17641769

17651770
// macOS will send non-left clicks to background windows without raising them, so we need to
17661771
// temporarily adjust the mouse position when this happens, as `mouse` will be tracking

patches/0007-sync-mouse-button-state.patch

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
diff --git a/Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m b/Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m
2-
index 3425737..df6c350 100644
2+
index 3425737..eb0a807 100644
33
--- a/Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m
44
+++ b/Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m
5-
@@ -1703,12 +1703,65 @@ - (BOOL)processHitTest:(NSEvent *)theEvent
5+
@@ -1703,12 +1703,68 @@ - (BOOL)processHitTest:(NSEvent *)theEvent
66
return NO; // not a special area, carry on.
77
}
88

9-
+static void Cocoa_SyncMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID)
9+
+static void Cocoa_SyncMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID, Uint8 excludeButton)
1010
+{
1111
+ const NSUInteger actualButtons = [NSEvent pressedMouseButtons];
1212
+ SDL_MouseInputSource *source = NULL;
@@ -43,8 +43,13 @@ index 3425737..df6c350 100644
4343
+ actualButtonState |= SDL_BUTTON_X2MASK;
4444
+ }
4545
+
46-
+ // Sync any buttons that are out of sync
46+
+ // Sync any buttons that are out of sync (except the one being processed)
4747
+ for (Uint8 button = SDL_BUTTON_LEFT; button <= SDL_BUTTON_X2; ++button) {
48+
+ // Skip the button being processed to avoid duplicate events
49+
+ if (excludeButton != 0 && button == excludeButton) {
50+
+ continue;
51+
+ }
52+
+
4853
+ Uint32 buttonMask = SDL_BUTTON_MASK(button);
4954
+ bool sdlPressed = (sdlButtonState & buttonMask) != 0;
5055
+ bool actuallyPressed = (actualButtonState & buttonMask) != 0;
@@ -62,10 +67,9 @@ index 3425737..df6c350 100644
6267
//const int clicks = (int)[theEvent clickCount];
6368
SDL_Window *focus = SDL_GetKeyboardFocus();
6469

65-
+ // Sync button state with hardware before processing event
66-
+ Cocoa_SyncMouseButtonState(mouse, mouseID);
70+
+ // Sync button state with hardware before processing event (exclude the button being processed to avoid duplicate events)
71+
+ Cocoa_SyncMouseButtonState(mouse, mouseID, button);
6772
+
6873
// macOS will send non-left clicks to background windows without raising them, so we need to
6974
// temporarily adjust the mouse position when this happens, as `mouse` will be tracking
7075
// the position in the currently-focused window. We don't (currently) send a mousemove
71-

0 commit comments

Comments
 (0)