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

Getting index of the TabItem #5997

Closed
romansvozil opened this issue Dec 14, 2022 · 5 comments
Closed

Getting index of the TabItem #5997

romansvozil opened this issue Dec 14, 2022 · 5 comments
Labels
tabs tab bars, tabs

Comments

@romansvozil
Copy link

romansvozil commented Dec 14, 2022

Hey, is there any way how could I get the current index of element in BeginTabBar/EndTabBar while having enabled ImGuiTabBarFlags_Reorderable? Was browsing the demo, but sadly didn't find anything related to that. Thanks for any answers.

Edit:

inline static int GetTabItemIndex()
{
    ImGuiContext& g = *GImGui;
    ImGuiTabBar* tab_bar = g.CurrentTabBar;
    return tab_bar->Tabs[tab_bar->LastTabItemIdx].IndexDuringLayout;
}

After some checking of the implementation of the TabItems, I found out I can write something like this and so far looks like it does what I was looking for. Gonna leave it here in case someone else will need it.

@ocornut ocornut added the tabs tab bars, tabs label Dec 14, 2022
@ocornut
Copy link
Owner

ocornut commented Dec 14, 2022

Also see #5853
It seems odd to me that you would want this index. For which purpose would you use it ?

@romansvozil
Copy link
Author

romansvozil commented Dec 14, 2022

It's for my small VFX editor, where each tab is VFX object (and its properties), so for my usage, it's more convenient to move the order of objects (which matters) using the ImGuiTabBarFlags_Reorderable. I understand it's probably not made for those use cases, but for me, it's good enough atm. Anyway if you have any tips, they're more than welcome.

@ocornut
Copy link
Owner

ocornut commented Dec 15, 2022

I don’t understand your explanation but I am interested to understand this better. Could you clarify?
If there is a use case, I can consider improving the public API. Thank you!

@romansvozil
Copy link
Author

romansvozil commented Dec 15, 2022

Well, I'll give a different example of a use-case. When you have in Chrome opened multiple tabs, you can right-click one of the tabs and click close all the tabs on the right side from the tab you've clicked. In that case you need to somehow keep a track of the order of the windows. Should make more sense as an example.

Here is how it could be handled using the GetTabItemIndex function.

static std::vector<tab> tabs;
static std::vector<int> index_mapping;

ImGui::BeginTabBar("Tabs", ImGuiTabBarFlags_Reorderable)
for (int tab_index = 0; tab_index < tabs.size(); tab_index++)
{
	if (ImGui::BeginTabItem(tabs[tab_index].id))
	{
		if (ImGui::Button("Remove tabs on right"))
		{
			for (usize index = 0; index < tabs.size(); index++)
			{
				if (index_mapping[index] > index_mapping[tab_index])
				{
					/* schedule to be removed */
				}
			}
		}
	
		ImGui::EndTabItem();
	}
	
	index_mapping[tab_index] = ImGui::GetTabItemIndex();
}
ImGui::EndTabBar();

ocornut added a commit that referenced this issue Jan 24, 2023
…bBarGetCurrentTab(), TabBarGetTabOrder(), TabBarGetTabName(), TabBarQueueFocus() + clear LastTabItemIdx on EndTabBar(). (#5853, #5997)

ImGuiTabBar::GetTabOrder() -> TabBarGetTabOrder().
ImGuiTabBar::GetTabName() -> TabBarGetTabName()
@ocornut
Copy link
Owner

ocornut commented Jan 24, 2023

Sorry for my late answer, I have now pushed 3d8885c with a handful of internal/low-level helpers to facilitate this.

It may little more mouthful as I didn't want to add extraneous/unnecessary helpers, but something like this should work:

ImGuiTabBar* tab_bar = GetCurrentTabBar();
int tab_order = TabBarGetTabOrder(tab_bar, TabBarGetCurrentTab(tab_bar));
for (int n = tab_order + 1; n < tab_bar.Tabs.Size; n++)
     if (ImGuiTabItem* tab = TabBarFindTabByOrder(tab_bar, n))
         TabBarCloseTab(tab_bar, tab);

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