Skip to content

Modal closes when window regains mouse focus #4317

@mattrbeck

Description

@mattrbeck

Version/Branch of Dear ImGui:

Version: 1.84 WIP (18302)
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_sdl.cpp + imgui_impl_opengl3.cpp
Operating System: Ubuntu 20.04

My Issue/Question:

In my example below, I render the main menu bar only when the SDL window has focus. In the menu bar, I have a single menu and menu item to open a modal. This modal opens and behaves as expected until the window loses and regains focus, at which point the modal closes. Ideally, the modal would remain open until I call ImGui::CloseCurrentPopup(). I'm wondering if I'm misusing modals or if this is possible a bug. Is there a preferred way to implement this behavior?

Apologies if this is a redundant question. I see #331, but my question is more of a tangent from the discussion in that issue.

Thank you for taking a look!

Screenshots/Video

2021-07-13.22-44-48.mp4

Standalone, minimal, complete and verifiable example:

bool open_modal = false;
if (SDL_GetMouseFocus()) {
    if (ImGui::BeginMainMenuBar()) {
        if (ImGui::BeginMenu("File")) {
            open_modal = ImGui::MenuItem("Open Modal");
            ImGui::EndMenu();
        }
        ImGui::EndMainMenuBar();
    }
}
if (open_modal) {
    ImGui::OpenPopup("Modal");
}
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Modal", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
    if (ImGui::Button("Close")) {
        ImGui::CloseCurrentPopup();
    }
    ImGui::EndPopup();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions