From a89a3cd2f15b09a396428d7fcba433e0acc6880e Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Fri, 23 Aug 2019 16:12:06 +0300 Subject: [PATCH] Viewports, GLFW: Fix window having incorrect size after uncollapse. Issue manifests on Linux when window is in it's own viewport. (#2756, #2117) --- examples/imgui_impl_glfw.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/examples/imgui_impl_glfw.cpp b/examples/imgui_impl_glfw.cpp index dc2d9819..59bd16d0 100644 --- a/examples/imgui_impl_glfw.cpp +++ b/examples/imgui_impl_glfw.cpp @@ -406,8 +406,9 @@ struct ImGuiViewportDataGlfw { GLFWwindow* Window; bool WindowOwned; + bool IgnoreSetWindowSizeEvent; - ImGuiViewportDataGlfw() { Window = NULL; WindowOwned = false; } + ImGuiViewportDataGlfw() { Window = NULL; WindowOwned = false; IgnoreSetWindowSizeEvent = false; } ~ImGuiViewportDataGlfw() { IM_ASSERT(Window == NULL); } }; @@ -426,7 +427,21 @@ static void ImGui_ImplGlfw_WindowPosCallback(GLFWwindow* window, int, int) static void ImGui_ImplGlfw_WindowSizeCallback(GLFWwindow* window, int, int) { if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window)) + { + if (ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData) + { + if (data->IgnoreSetWindowSizeEvent) + { + // GLFW will dispatch window size event. ImGui expects no such event sent when library explicitly requests setting + // window size. Depending on the platform this callback may be invoked during glfwSetWindowSize() call or queued + // for the next frame and invoked during glfwPollEvents() call. When latter happens - restoring collapsed window + // would have incorrect size. + data->IgnoreSetWindowSizeEvent = false; + return; + } + } viewport->PlatformRequestResize = true; + } } static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport) @@ -569,6 +584,8 @@ static ImVec2 ImGui_ImplGlfw_GetWindowSize(ImGuiViewport* viewport) static void ImGui_ImplGlfw_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) { + if (ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData) + data->IgnoreSetWindowSizeEvent = true; ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData; #if __APPLE__ // Native OS windows are positioned from the bottom-left corner on macOS, whereas on other platforms they are @@ -630,7 +647,7 @@ static void ImGui_ImplGlfw_RenderWindow(ImGuiViewport* viewport, void*) static void ImGui_ImplGlfw_SwapBuffers(ImGuiViewport* viewport, void*) { ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData; - if (g_ClientApi == GlfwClientApi_OpenGL) + if (g_ClientApi == GlfwClientApi_OpenGL) { glfwMakeContextCurrent(data->Window); glfwSwapBuffers(data->Window);