Getting index of the TabItem #5997

Closed
opened 2022-12-14 16:45:17 +00:00 by romansvozil · 5 comments
romansvozil commented 2022-12-14 16:45:17 +00:00 (Migrated from github.com)

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.

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: ```cpp 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 commented 2022-12-14 20:04:50 +00:00 (Migrated from github.com)

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

Also see #5853 It seems odd to me that you would want this index. For which purpose would you use it ?
romansvozil commented 2022-12-14 20:24:25 +00:00 (Migrated from github.com)

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.

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 commented 2022-12-15 16:05:25 +00:00 (Migrated from github.com)

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!

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 commented 2022-12-15 18:55:20 +00:00 (Migrated from github.com)

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();
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. ```cpp 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 commented 2023-01-24 18:44:48 +00:00 (Migrated from github.com)

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);
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: ```cpp 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 in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#5997
No description provided.