ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
61.15k stars 10.3k forks source link

Imgui Window doesn't automatically refresh viewport when dropped on window with different resolution #3356

Open ChaseLewis opened 4 years ago

ChaseLewis commented 4 years ago

Version/Branch of Dear ImGui:

Version: v.1.78 Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.cpp + imgui_impl_GLFW.cpp Compiler: MSVC Operating System: Windows 10

My Issue/Question:

If I drag a subwindow from 1 monitor to another with different resolution the viewport of the window gets messed up. Resizing the window fixes the viewport. It does not get fixed until a user manually resizes it though. If you drop the subwindow onto the main window it fixes itself without requiring any viewport changes.

Is there a way to hook into the window drop event and make it auto resize viewport?

Screenshots/Video https://imgur.com/a/CLdfGv4

Standalone, minimal, complete and verifiable example:


// Here's some code anyone can copy and paste to reproduce your issue
This happens with any subwindow you create that has docking enabled.

                //Config
        ImGui::CreateContext();

        ImGuiIO& io = ImGui::GetIO();
        io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
        io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
        io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
        ImGui::StyleColorsDark();

        ImGuiStyle& style = ImGui::GetStyle();
        if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
        {
            style.WindowRounding = 0.0f;
            style.Colors[ImGuiCol_WindowBg].w = 1.0f;
        }

        ImGui_ImplGlfw_InitForOpenGL((GLFWwindow*)Application::Get()->GetWindow()->GetNativeWindow(),true);
        ImGui_ImplOpenGL3_Init("#version 410");

                //Loop
                ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();
        ImGui::Begin("My Window");
        ImGui::Text("How are you?");
        ImGui::End();

        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
        if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
        {
            GLFWwindow* current_window = glfwGetCurrentContext();
            ImGui::UpdatePlatformWindows();
            ImGui::RenderPlatformWindowsDefault();
            glfwMakeContextCurrent(current_window);
        }
ChaseLewis commented 4 years ago

I was able to fix the issue with the below codein imgui.cpp. I'm not familiar enough with ImGui's inner workings to know if this is reliable enough to be merged in or if there are more callback I should listen too since I noticed there is also a Render_SetWindowSize that is listened to in a few places.

static void ImGui::UpdateViewportPlatformMonitor(ImGuiViewportP* viewport)
{
   ImGuiContext& g = *GImGui;
   short newPlatformMonitor = (short)FindPlatformMonitorForRect(viewport->GetMainRect());
   if(viewport->PlatformMonitor != -1 && newPlatformMonitor != viewport->PlatformMonitor && g.PlatformIO.Platform_SetWindowSize) {

       g.PlatformIO.Platform_SetWindowSize(viewport, viewport->Size);
   }
   viewport->PlatformMonitor = newPlatformMonitor;
}