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

Toggling collapsing headers #7349

Closed
Sandwich4780 opened this issue Feb 25, 2024 · 4 comments
Closed

Toggling collapsing headers #7349

Sandwich4780 opened this issue Feb 25, 2024 · 4 comments

Comments

@Sandwich4780
Copy link

Version/Branch of Dear ImGui:

Version 1.90.4 WIP

Back-ends:

imgui_impl_win32.cpp

Compiler, OS:

Windows 10

Full config/build information:

No response

Details:

Is there a way to open/close collapsing headers with other mouse buttons? I'm trying to toggle a collapsing header with right-click but I can't figure out any way to do it. Maybe in the future there could be some sort of field/flag for using other mouse buttons.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

@PathogenDavid
Copy link
Contributor

Could you explain why you want this behavior? It sounds very non-standard.

There is no built-in way to accomplish this, but you could tweak TreeNodeBehavior to pass ImGuiButtonFlags_MouseButtonRight to ButtonBehavior here:

bool pressed = ButtonBehavior(interact_bb, id, &hovered, &held, button_flags);

You'd probably want to add your own flag to ImGuiTreeNodeFlags_ to enable this behavior since TreeNodeBehavior affects more than just ImGui::CollapsingHeader.

@Sandwich4780
Copy link
Author

Could you explain why you want this behavior? It sounds very non-standard.

There is no built-in way to accomplish this, but you could tweak TreeNodeBehavior to pass ImGuiButtonFlags_MouseButtonRight to ButtonBehavior here:

bool pressed = ButtonBehavior(interact_bb, id, &hovered, &held, button_flags);

You'd probably want to add your own flag to ImGuiTreeNodeFlags_ to enable this behavior since TreeNodeBehavior affects more than just ImGui::CollapsingHeader.

I want it to behave like that so I can have both a checkbox and a header in one. I'll try your suggested method but anyways ty for the help

@PathogenDavid
Copy link
Contributor

I want it to behave like that so I can have both a checkbox and a header in one.

In that case you might actually be better served by using ImGuiTreeNodeFlags_AllowOverlap and ImGui::SameLine to put a checkbox on the header. Something like this:

static bool CheckableCollapsingHeader(const char* label, bool* v, ImGuiTreeNodeFlags flags = 0)
{
    ImGui::PushID(label);
    bool is_open = ImGui::CollapsingHeader("##CollapsingHeader", flags | ImGuiTreeNodeFlags_AllowOverlap);
    ImGui::SameLine();
    ImGui::Checkbox("##Checkbox", v);
    ImGui::SameLine();
    ImGui::Text("Header"); // (Label submitted as text so the user can click it to toggle the header instead of the checkbox)
    ImGui::PopID();
    return is_open;
}

static void GH7349()
{
    ImGui::Begin("GH-7349");
    static bool toggle;
    if (CheckableCollapsingHeader("Header", &toggle))
    {
        ImGui::Text("Expanded!");
    }

    ImGui::Text("Header %s checked!", toggle ? "is" : "is not");
    ImGui::End();
}

Here's what that looks like:

Screenshot of the above code with the header expanded and the checkbox checked

(Linking to related issues: #6294 #2396)

@Sandwich4780
Copy link
Author

I want it to behave like that so I can have both a checkbox and a header in one.

In that case you might actually be better served by using ImGuiTreeNodeFlags_AllowOverlap and ImGui::SameLine to put a checkbox on the header. Something like this:

static bool CheckableCollapsingHeader(const char* label, bool* v, ImGuiTreeNodeFlags flags = 0)
{
    ImGui::PushID(label);
    bool is_open = ImGui::CollapsingHeader("##CollapsingHeader", flags | ImGuiTreeNodeFlags_AllowOverlap);
    ImGui::SameLine();
    ImGui::Checkbox("##Checkbox", v);
    ImGui::SameLine();
    ImGui::Text("Header"); // (Label submitted as text so the user can click it to toggle the header instead of the checkbox)
    ImGui::PopID();
    return is_open;
}

static void GH7349()
{
    ImGui::Begin("GH-7349");
    static bool toggle;
    if (CheckableCollapsingHeader("Header", &toggle))
    {
        ImGui::Text("Expanded!");
    }

    ImGui::Text("Header %s checked!", toggle ? "is" : "is not");
    ImGui::End();
}

Here's what that looks like:

Screenshot of the above code with the header expanded and the checkbox checked

(Linking to related issues: #6294 #2396)

Oh this will work pretty good, I’ll do this instead. Thank you for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants