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

Overlapped InputText doesn't take the focus since 1.89.7 #6690

Closed
cpichard opened this issue Aug 5, 2023 · 2 comments
Closed

Overlapped InputText doesn't take the focus since 1.89.7 #6690

cpichard opened this issue Aug 5, 2023 · 2 comments

Comments

@cpichard
Copy link
Contributor

cpichard commented Aug 5, 2023

Version/Branch of Dear ImGui:

Version: 1.89.8
Branch: master and docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp
Operating System: macOS/Windows10

My Issue/Question:
Hi,

To edit a tree node value, I used to draw an input text on top of the tree node after double clicking on it.

Screen.Recording.2023-08-05.at.09.22.17.mov

It has been working fine until 1.89.7.
In 1.89.7 and beyond, the InputText doesn’t show up (just one frame)

Screen.Recording.2023-08-05.at.09.41.59.mov

A code example looks like this (this can be pasted in the imgui_demo.cpp) :

                       auto cursor = ImGui::GetCursorPos();
			ImGui::SetNextItemAllowOverlap() ; // only in 1.89.7
                       ImGui::TreeNodeEx(node->Name, ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanFullWidth);
                       if (!ImGui::IsItemToggledOpen() && ImGui::IsItemClicked()&& ImGui::IsMouseDoubleClicked(0)) {
                           edit = node->Name;
                       }
                       if (edit == node->Name) {
                          char stuff[96];
                          ImGui::SameLine();
                          ImGui::SetCursorPos(cursor);
                          ImGui::InputText("##EditStuff", stuff, 96);
                          if (ImGui::IsItemDeactivatedAfterEdit() || !ImGui::IsItemFocused()) {
                               edit = nullptr;
                           }
                       }   

I traced the difference to the !ImGui::IsItemFocused() above not behaving the same between 1.89.6 and 1.89.7. In 1.89.7. The InputText is not focused anymore when it is created. I was focused before because the click is “propagated” to the input text which normally takes the focus and allows editing.

After a bit of digging, inside the InputText, ItemHoverable returns false on the ActiveIdAllowOverlap test where it wasn’t in the previous versions.

imgui/imgui.cpp

Line 4059 in d4ddc46

if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap)

I am not sure if this is a bug, or if my code is incorrect in the first place.

As a workaround I tried removing ImGui::IsItemFocused() but then users have to click again on the InputText to start editing which is not great, the previous behaviour was really convenient, specially focusing on the InputText and start text editing.

Thanks a lot for any help,

Best

Cyril

@ocornut
Copy link
Owner

ocornut commented Aug 6, 2023

Hello Cyril,

Thanks for posting a detailed and well researched issue.
This is similar to the reason explained in #6610 (comment), it was generally "incorrect" that AllowOverlap would extend to when the item was active, as it made e.g. dragging from a TreeNode with AllowOverlap able to highlight objects in different locations. But this is the second legit use case that my fix is causing an issue with.

The workaround to get to the same logic as the previous SetItemAllowOverlap() would be to call this after the TreeNode():

ImGuiContext& g = *GImGui;
if (g.ActiveId == ImGui::GetItemID())
    g.ActiveIdAllowOverlap = true;

Because you only need that to be set once to allow the InputText() to be activated, you may call it in the same block where you set the edit value.

However for this specific logic you are using, technically you could also just call ImGui::ClearActiveID() when setting edit = ... and this has the benefit that you are relying on less internals stuff details. While this is still technically an internal API i would suggest this is the best solution for you now.

Note that this pattern is analogous to what we are using for CTRL+Clicking into Sliders and Drags.
See our implementation of the TempInputText() function. It may be reasonable that you call TempInputText() but it's a little more likely to break internal api from time to time, so if just setting ClearActiveID() works for you that's enough.


EDIT

A code example looks like this (this can be pasted in the imgui_demo.cpp) :

Almost but not quite ;)

But for future reference here's a version that is pastable directly:

{
    static bool editing = false;
    ImVec2 cursor = ImGui::GetCursorPos();
    ImGui::SetNextItemAllowOverlap(); // only in 1.89.7
    ImGui::TreeNodeEx("Blah", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanFullWidth);
    if (!ImGui::IsItemToggledOpen() && ImGui::IsItemClicked() && ImGui::IsMouseDoubleClicked(0))
    {
        editing = true;
        ImGuiContext& g = *GImGui;
        //if (g.ActiveId == ImGui::GetItemID())
        //    g.ActiveIdAllowOverlap = true;
        ImGui::SetActiveID(0, NULL);// .ActiveId = 0;
    }
    if (editing)
    {
        static char stuff[96] = "Blah";
        ImGui::SameLine();
        ImGui::SetCursorPos(cursor);
        ImGui::InputText("##EditStuff", stuff, 96);
        if (ImGui::IsItemDeactivatedAfterEdit())
            editing = false;
        if (!ImGui::IsItemFocused())
            editing = false;
    }
}

@cpichard
Copy link
Contributor Author

cpichard commented Aug 7, 2023

Hi Omar,
Thanks, adding the ImGui::ClearActiveID(); fixes the issue in my application, this is great ! I can now upgrade to the latest imgui version.
Thanks again for the explanation and the quick support,
Best,
Cyril

@ocornut ocornut closed this as completed Aug 7, 2023
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