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

Interactable hint popup without taking focus from input field? #5513

Closed
squadack opened this issue Jul 28, 2022 · 3 comments
Closed

Interactable hint popup without taking focus from input field? #5513

squadack opened this issue Jul 28, 2022 · 3 comments

Comments

@squadack
Copy link

squadack commented Jul 28, 2022

Version/Branch of Dear ImGui:

Version: 1.83
Branch: docking

My Issue/Question:

Hi!

I need to implement an interactable tooltip/popup containing clickable suggestions on top of InputText(). I want the popup to appear over the window that contains the InputText() without taking keyboard focus from the text field (think code completion). Navigation using arrows and Tab/Enter would be a nice cherry on top (unfortunately, CallbackHistory and multiline input don't go well together :<).

The specific use case for me is that I have several dynamically created input fields (both single and multiline), where the user can put any
text, but there are some special values that will get translated later. If the user writes something that is a prefix of at least one of those special values, I want the popup with possible matches to appear under the field without taking the focus from said field (so the user can continue writing, if they decide to do so).

My current solution is based on your comment, which looks nice, but you cannot interact with it.
I tried other solutions from the same thread and I had the best looking results with solution posted by @rokups. It worked as intended but only for single field forms due to static bool isOpen variable - I couldn't figure out how to get rid of it. The second issue I noticed is that when I tried to open the whole form inside a modal popup, the hint popup stopped being interactable - my guess is that modal blocks all interaction with other windows and the hint popup, even as it is on top of the modal, is treated as separate window.

The question is: how can I implement a solution that will work in the scenario described above? Is it possible at all, using public API, or is it something that is yet to come?

Current solution preview:

//InputText() here

if( ImGui::IsItemActive() || ImGui::IsItemFocused() )
{
    auto matches = std::array<std::string, 2>{"text1", "text2"}; //replaced actual matching with this
    if( matches.empty() ) return;

    ImGui::SetNextWindowPos( ImVec2{ ImGui::GetItemRectMin().x, ImGui::GetItemRectMax().y } );
    const int itemCount = std::min<int>( 4, matches.size() );
    const float popupH = itemCount * ( ImGui::GetFontSize() + ImGui::GetStyle().ItemSpacing.y );
    ImGui::PushStyleVar( ImGuiStyleVar_WindowPadding, ImVec2{ ImGui::GetStyle().WindowPadding.x, 0.0f } );
    ImGui::SetNextWindowSize( ImVec2{ 0, popupH } );
    ImGui::BeginTooltip();
    int count = 0;
    for( const std::string& s : matches )
    {
        if( count++ == itemCount ) break;
        ImGui::Selectable( s.c_str() );
    }
    ImGui::EndTooltip();
    ImGui::PopStyleVar();
}

image

@ocornut
Copy link
Owner

ocornut commented Jul 28, 2022

It would seem appropriate to post this in #718 instead of opening a new thread. AFAIK this is the same topic?

@rokups
Copy link
Contributor

rokups commented Jul 28, 2022

Also relevant: #1658, #3725.

@squadack
Copy link
Author

squadack commented Aug 18, 2022

Yeah, it’s mostly the same topic, I probably should have posted this in previous thread. I’ll continue the discussion there.

@squadack squadack closed this as not planned Won't fix, can't repro, duplicate, stale Aug 18, 2022
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

3 participants