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

Adding button in tab bars #3291

Closed
rjx-ray opened this issue Jun 8, 2020 · 13 comments
Closed

Adding button in tab bars #3291

rjx-ray opened this issue Jun 8, 2020 · 13 comments
Labels
tabs tab bars, tabs

Comments

@rjx-ray
Copy link

rjx-ray commented Jun 8, 2020

I am using a "+" button after a reorderable tab bar to add a new tab.
image

if I move the last tab the button's position moves to after the moved tab instead of after the tab bar
image

I'm using ImGui 1.75 included with https://github.com/jvcleave/ofxImGui

Here's the code outline - a tab is class which has a name variable and I keep the tabs in a C++ vector
`

ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable;
if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) {
    for (auto tab : tabs) {
        string name = tab->name;
        if (ImGui::BeginTabItem(name.c_str())) { 
            // switch to the newly selected tab
            ImGui::EndTabItem();
        }
    }
    ImGui::EndTabBar();
}
ImGui::SameLine();
if (ImGui::Button("+")) {
    // create the new tab and push_back
}

`

@ocornut
Copy link
Owner

ocornut commented Jun 8, 2020

Hello Richard,

I thought we could modify EndTabBar() to set the "last item" information for layout, but this usage pattern would break if the tabs shrinking down or scrolling, so I remember why I explicitly didn't make that change in EndTabBar().

I think instead we should add specific ImGuiTabItemFlags flags to:

  • disable reordering on a specific tab
  • treat the tab as a button: it would not get selected and return true on click

This would allow implementing this behavior using a tab (and you can alter tab colors in the style if you want it to be red, etc).

@rjx-ray
Copy link
Author

rjx-ray commented Jun 8, 2020

Hi Omar, thanks for the quick response.
My + button is a separate ImGui button, outside of the tab bar.
It seemed to me that its just the end point of the tab bar that was calculated wrong.
So I do want to be able to re-order the last tab.

By the way, I had originally tried having a "+" tab after all the others.
I had to do special logic as the new tab wasn't selected until the following cycle so I ended up with two new tabs.
Also I had the issue you describe that the new tab was placed after the "+" tab and the "+" tab then couldn't be re-ordered because any click on it creates a new tab.
So it seemed simpler to have a separate button

@rjx-ray
Copy link
Author

rjx-ray commented Jun 16, 2020

I've managed to devise a workaround for this:

If I sort my tabs vector to be in the same order as the ImGui tabs display then the "+" button stays at the end after the last tabItem I call.
That also means that I can save the tab ordering between app invocations which is a requirement for me.

I did the sorting by getting the tabbar tab offsets and names and copying the offset into my tab class. Obviously I'm having to access ImGui internals for this which is not ideal. Also I'm having to sort on every cycle as I don't know when the ordering is changed

But for now its working OK for me.

@ocornut
Copy link
Owner

ocornut commented Jun 16, 2020

Hello Richard,
We are currently working on a proper solution for this.

@rjx-ray
Copy link
Author

rjx-ray commented Jun 16, 2020

Fantastic!
It would be great if you could also include some method of notifying a change of ordering and getting the results
Thanks

@ocornut
Copy link
Owner

ocornut commented Jun 16, 2020 via email

@rjx-ray
Copy link
Author

rjx-ray commented Jun 16, 2020

Could it be preserved between app invocations?

@ocornut
Copy link
Owner

ocornut commented Jun 16, 2020 via email

@rjx-ray
Copy link
Author

rjx-ray commented Jun 16, 2020

OK, I understand, thanks for all the great stuff you do.

@rjx-ray
Copy link
Author

rjx-ray commented Jun 17, 2020

Would it be possible to add a favicon to tab - maybe as a optional parameter to BeginTabItem?

@ocornut
Copy link
Owner

ocornut commented Jun 17, 2020

Use fonts icons as described in the docs. Let’s focus on one issue if you have more questions about this please ask on Discord or open a separate issue, thank you!

ocornut added a commit that referenced this issue Aug 6, 2020
…won't lag by an extra frame.

Vaguely relate to underlying (uncommited) work for #3291
@ocornut ocornut changed the title Reordering tabs changes position of following widget on the same line Adding button in tab bars Aug 10, 2020
ocornut pushed a commit that referenced this issue Sep 22, 2020
…emFlags_Trailing + demo. (#3291)

(squashed various commits by 2 authors)
ocornut added a commit that referenced this issue Sep 22, 2020
…s. + warning fixes + bunch of renaming. (#3291)

Demo tweaks.
ocornut added a commit that referenced this issue Sep 22, 2020
…h in TabBarLayout(). Misc amends, shortening. (#3291)
ocornut added a commit that referenced this issue Sep 22, 2020
… shrinkable (unused yet) + don't unnecessarily move data within ShrinkWidthBuffer. (#3291)
@ocornut
Copy link
Owner

ocornut commented Sep 22, 2020

This now has been implemented (it was surprisingly lots of work to get it right, done mostly by @Xipiryon, lots of corner cases, resizing/scrolling policy issues)

You can now use TabItemButton() + ImGuiTabItemFlags_Leading, ImGuiTabItemFlags_Trailing flags.
See Demo > Layouts > Tabs for details.

Regular tabs can also be trailing/leading.

image

The next thing in line is to allow to appends tab using multiple BeginTabBar()/EndTabBar() which would make it possible to add custom buttons into docking nodes created by the docking system.

@ocornut ocornut added the tabs tab bars, tabs label Sep 30, 2020
ocornut added a commit that referenced this issue Oct 1, 2020
ocornut added a commit that referenced this issue Oct 1, 2020
@ocornut
Copy link
Owner

ocornut commented Dec 25, 2020

Appending to tab bar has been added a bit ago by the way, so closing this as all done.

@ocornut ocornut closed this as completed Dec 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tabs tab bars, tabs
Projects
None yet
Development

No branches or pull requests

2 participants