Backends: GLFW: Handle spurious leave/enter sequences apparently submitted by X11/GLFW combo. (#4984)

This commit is contained in:
ocornut 2022-02-07 15:59:28 +01:00
parent 40fd163a3b
commit 732bc2058e

View File

@ -91,6 +91,7 @@ struct ImGui_ImplGlfw_Data
double Time; double Time;
GLFWwindow* MouseWindow; GLFWwindow* MouseWindow;
GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT]; GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT];
ImVec2 LastMousePos;
bool InstalledCallbacks; bool InstalledCallbacks;
// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. // 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(); ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent((float)x, (float)y); 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) void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered)
{ {
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
@ -348,9 +352,13 @@ void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered)
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
if (entered) 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; bd->MouseWindow = NULL;
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
} }