Skip to content

Commit

Permalink
Backends: GLFW: Handle spurious leave/enter sequences apparently subm…
Browse files Browse the repository at this point in the history
…itted by X11/GLFW combo. (#4984)
  • Loading branch information
ocornut committed Feb 7, 2022
1 parent 40fd163 commit 732bc20
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion backends/imgui_impl_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand All @@ -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)
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);
}
Expand Down

0 comments on commit 732bc20

Please sign in to comment.