From 732bc2058edc847059aaac73001ddd5c85b793d7 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 7 Feb 2022 15:59:28 +0100 Subject: [PATCH] Backends: GLFW: Handle spurious leave/enter sequences apparently submitted by X11/GLFW combo. (#4984) --- backends/imgui_impl_glfw.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backends/imgui_impl_glfw.cpp b/backends/imgui_impl_glfw.cpp index 3fd2819d..57012eba 100644 --- a/backends/imgui_impl_glfw.cpp +++ b/backends/imgui_impl_glfw.cpp @@ -91,6 +91,7 @@ struct ImGui_ImplGlfw_Data double Time; GLFWwindow* MouseWindow; GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT]; + ImVec2 LastMousePos; bool InstalledCallbacks; // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. @@ -338,8 +339,11 @@ void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y) ImGuiIO& io = ImGui::GetIO(); io.AddMousePosEvent((float)x, (float)y); + bd->LastMousePos = ImVec2((float)x, (float)y); } +// Workaround: X11 seems to send spurious Leave/Enter events which would make us lose our position, +// so we back it up and restore on Leave/Enter (see https://github.com/ocornut/imgui/issues/4984) void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); @@ -348,9 +352,13 @@ void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered) ImGuiIO& io = ImGui::GetIO(); if (entered) - bd->MouseWindow = window; - if (!entered && bd->MouseWindow == window) { + bd->MouseWindow = window; + io.AddMousePosEvent(bd->LastMousePos.x, bd->LastMousePos.y); + } + else if (!entered && bd->MouseWindow == window) + { + bd->LastMousePos = io.MousePos; bd->MouseWindow = NULL; io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); }