@@ -96,6 +96,7 @@ struct ImGui_ImplSDL2_Data
96
96
Uint32 MouseWindowID;
97
97
int MouseButtonsDown;
98
98
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
99
+ int PendingMouseLeaveFrame;
99
100
char * ClipboardTextData;
100
101
bool MouseCanUseGlobalState;
101
102
bool UseVulkan;
@@ -315,16 +316,19 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
315
316
}
316
317
case SDL_WINDOWEVENT:
317
318
{
318
- // When capturing mouse, SDL will send a bunch of conflicting LEAVE/ENTER event on every mouse move, but the final ENTER tends to be right.
319
- // However we won't get a correct LEAVE event for a captured window.
319
+ // - When capturing mouse, SDL will send a bunch of conflicting LEAVE/ENTER event on every mouse move, but the final ENTER tends to be right.
320
+ // - However we won't get a correct LEAVE event for a captured window.
321
+ // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
322
+ // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
323
+ // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
320
324
Uint8 window_event = event->window .event ;
321
325
if (window_event == SDL_WINDOWEVENT_ENTER)
322
- bd->MouseWindowID = event->window .windowID ;
323
- if (window_event == SDL_WINDOWEVENT_LEAVE)
324
326
{
325
- bd->MouseWindowID = 0 ;
326
- io. AddMousePosEvent (-FLT_MAX, -FLT_MAX) ;
327
+ bd->MouseWindowID = event-> window . windowID ;
328
+ bd-> PendingMouseLeaveFrame = 0 ;
327
329
}
330
+ if (window_event == SDL_WINDOWEVENT_LEAVE)
331
+ bd->PendingMouseLeaveFrame = ImGui::GetFrameCount () + 1 ;
328
332
if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
329
333
io.AddFocusEvent (true );
330
334
else if (window_event == SDL_WINDOWEVENT_FOCUS_LOST)
@@ -665,6 +669,12 @@ void ImGui_ImplSDL2_NewFrame()
665
669
io.DeltaTime = bd->Time > 0 ? (float )((double )(current_time - bd->Time ) / frequency) : (float )(1 .0f / 60 .0f );
666
670
bd->Time = current_time;
667
671
672
+ if (bd->PendingMouseLeaveFrame && bd->PendingMouseLeaveFrame == ImGui::GetFrameCount ())
673
+ {
674
+ bd->MouseWindowID = 0 ;
675
+ io.AddMousePosEvent (-FLT_MAX, -FLT_MAX);
676
+ }
677
+
668
678
ImGui_ImplSDL2_UpdateMouseData ();
669
679
ImGui_ImplSDL2_UpdateMouseCursor ();
670
680
0 commit comments