Hi everyone, I’m creating a simple TabView with multipe Tabs generated dynamically:
string[] tabNames = { "Tab1", "Tab2", "Tab3" };
TabView tabView = new TabView();
tabView.reorderable = true;
this.rootVisualElement.Add(tabView);
for (int i = 0; i < tabNames.Length; ++i)
{
Tab tab = new Tab();
tab.closeable = true;
tabView.Add(tab);
}
As you can see, I also set the reorderable and closeable properties to get the basic functiontalities for my tabs. The issue comes in with the tab.closed and tab.closing delegates. As far as I know, the tab.closed event is called after the tab is removed from the tabView, meaning there is no way to get the tab’s child index during a callback subscribed to this event. The tab.closing event also doesn’t accept any parameter, it just returns true.
The second issue is it seems there is no way to register a callback when tabs are reordered. This is important for me as I need to get the correct indices for each of the tabs’ content to save them properly. Is there any way to call a method when the tabs are reordered?
To partially answer my question, instead of trying to get the index of the tab In the closed callback to match it with the corresponding element in an array, I simply directly referenced that element with the VisualElement.dataSource, which seems to do the job. I’m still looking for a solution for reordering tabs and gathering their ids to sort the array properly.
Hello!
I agree, that’s a basic callback that should be included, sorry for the oversight. Can you report this as a bug inside Unity via the menu “Help > Report a bug”?
Thank you!
1 Like
Hello, I’ve sent the bug report. Thank you for your attention.
Another weird quirk I found about reordering tabs, is that the moved tab will be selected as active by the TabView, but will not trigger the Tab.selected event. Plus, it will hide the content of the previous tab and show the new one, even if the user simply wants to move a tab and keep the previous content displayed.
I see, I’ll add this feedback to the ticket, thanks so much!
1 Like
To anyone stumbling upon this issue, the functionalities have been added in Unity 6000.0.438f1:
this._tabView = new TabView();
this._tabView.activeTabChanged += (oldTab, newTab) => Debug.Log($"Old : {oldTab?.label} ; New : {newTab?.label}");
this._tabView.tabClosed += (tab, index) => Debug.Log($"Tab : {tab.label} ; Index : {index}"); // Wrong index
this._tabView.tabReordered += (oldIndex, newIndex) => Debug.Log($"Old : {oldIndex} ; New : {newIndex}");
1 Like