Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoFocusOnAppearing on a popup makes it appear behind the parent window after clicking on a Selectable. #4461

Open
quyykk opened this issue Aug 22, 2021 · 3 comments

Comments

@quyykk
Copy link

quyykk commented Aug 22, 2021

Information:

Version: 1.84.1 (master) with 1 modification (see below)
Backend: SDL + OpenGL 3
Compiler: clang on Linux

My Issue/Question:

Using the ImGuiWindowFlags_NoFocusOnAppearing flag on a (popup) window makes it appear behind the parent window after clicking a Selectable inside that popup. I don't know whether this is a bug, or if I am missing something and I don't know enough about ImGui to tell. But since it works correctly until I click a Selectable, I believe this is a bug.


Background: Like many others, I'm trying to create a combo box under an InputText. To do that, I took some parts of BeginCombo and then used BeginComboPopup to create the actual popup. It works great!

Since popups take focus by default, I had to add a parameter to BeginComboPopup so that I can pass ImGuiWindowFlags_NoFocusOnAppearing to the Begin call in BeginComboPopup. And this works, until I click on a Selectable. After that, the popup renders behind the main window.

I tried to look at the code about what happens differently when passing NoFocusOnAppearing and I looked at Selectable, but I didn't see anything stand out (but I'm also a complete newcomer to ImGui so that doesn't mean anything).

Screenshot

imgui

Standalone, minimal, complete and verifiable example:

#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui_internal.h"
// Don't forget to add a ImGuiWindowFlags parameter to BeginComboPopup
// and OR it with the rest to pass them to Begin
// Or add NoFocusOnAppearing directly inside BeginComboPopup

// Add this to the example window in the main loop inside the sdl_opengl3 example.
const char *label = "test input";
static char buffer[100];
ImGuiWindow *window = ImGui::GetCurrentWindow();
ImGui::InputText(label, buffer, sizeof(buffer));
            
const auto id = ImHashStr("##combo/popup", 0, window->GetID(label));
bool isOpen = ImGui::IsPopupOpen(id, ImGuiPopupFlags_None);
if(ImGui::IsItemActive() && !isOpen)
{
    ImGui::OpenPopupEx(id, ImGuiPopupFlags_None);
    isOpen = true;
}
            
if(isOpen)
{
    const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);
    const float w = ImGui::CalcItemWidth();
    const ImRect bb(window->DC.CursorPos,
            window->DC.CursorPos + ImVec2(w, label_size.y + GImGui->Style.FramePadding.y * 2.0f));
           
    if(ImGui::BeginComboPopup(id, bb, ImGuiComboFlags_None, ImGuiWindowFlags_NoFocusOnAppearing))
    {
        static const char *list[] = {"one", "two", "three"};
        for(int i = 0; i < 3; ++i)
                ImGui::Selectable(list[i]);
        ImGui::EndCombo();
     }
 }
@quyykk
Copy link
Author

quyykk commented Aug 23, 2021

I fixed my issue by calling BringWindowToDisplayFront(GetCurrentWindow()); inside the BeginComboPopup if statement.

Though I still think this is a bug.

@ocornut
Copy link
Owner

ocornut commented Sep 16, 2022

Hello and sorry for my later answer.

ImGuiWindowFlags_NoFocusOnAppearing is a little bit ill-defined. It disable the call to FocusWindow() which does both bringing to display front and and bringing to focus font. I could be that current behavior is not as useful as it should be. Case in point, we use this flag once internally and we still need the window to be moved to display front.

Using the workaround is reasonable until we decide if we change that definition, but it is best to use with a IsWindowAppearing() check:

if (ImGui::IsWindowAppearing())
    ImGui::BringWindowToDisplayFront(ImGui::GetCurrentWindow());

I am currently investigating another related issue and it might contribute to evolving the design of the ImGuiWindowFlags_NoFocusOnAppearing and ImGuiWindowFlags_NoBringToFrontOnFocus flag.

@ocornut
Copy link
Owner

ocornut commented Sep 16, 2022

Linking to #718 which aims for the same end result and has lots of ideas.

ocornut added a commit that referenced this issue Sep 16, 2022
…ed. (useful for e.g. #718, #4461 and probably other things)
ocornut added a commit that referenced this issue Sep 16, 2022
ocornut added a commit that referenced this issue Sep 16, 2022
…ior (useful for e.g. #718, #4461 and probably other things)

(broken by 1c4066c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants