#if UNITY_2019_4_OR_NEWER using System; using System.Collections.Generic; using System.Linq; using StansAssets.Foundation.UIElements; using UnityEngine.Assertions; using UnityEngine.UIElements; namespace StansAssets.Plugins.Editor { /// /// Tab controller based on to switch between tabs /// and to display their contents /// public class TabController { readonly Dictionary m_Tabs = new Dictionary(); readonly ButtonStrip m_TabsButtons; readonly ScrollView m_TabsContainer; /// /// Available tabs' labels. /// public IEnumerable Tabs => m_Tabs.Keys; /// /// Active tab label from . /// public string ActiveTab => m_TabsButtons.Value; /// /// This constructor will looking for already existing elements: /// (without name) and named "tabs-container" /// The purpose of this is to support . /// /// Element that contains and named tabs-container public TabController(VisualElement root) { m_TabsButtons = root.Q(); m_TabsContainer = root.Q("tabs-container"); Init(); } /// /// Add tab to the window top bar. /// /// Tab label. /// Tab content. /// Will throw tab with the same label was already added. public void AddTab(string label, VisualElement content) { if (!m_Tabs.ContainsKey(label)) { m_TabsButtons.AddChoice(label, label); m_Tabs.Add(label, content); content.viewDataKey = label; } else { throw new ArgumentException($"Tab '{label}' already added", nameof(label)); } } /// /// Activate tab by label /// /// Early specified tab label public void ActivateTab(string label) { if (!m_Tabs.ContainsKey(label)) { return; } m_TabsButtons.SetValue(label); } /// /// Set the flexible growth property of tabs content container /// /// public void ContentContainerFlexGrow(StyleFloat styleFloat) { m_TabsContainer.contentContainer.style.flexGrow = styleFloat; } /// /// Container flex grow property. /// public StyleFloat ContainerFlexGrow => m_TabsContainer.contentContainer.style.flexGrow; /// /// Refresh current tab /// public void RefreshActiveTab() { if (string.IsNullOrEmpty(ActiveTab)) { return; } foreach (var tab in m_Tabs) { tab.Value.RemoveFromHierarchy(); } var element = m_Tabs.First(i => i.Key.Equals(m_TabsButtons.Value)).Value; m_TabsContainer.Add(element); } void Init() { Assert.IsNotNull(m_TabsButtons); Assert.IsNotNull(m_TabsContainer); m_TabsButtons.CleanUp(); m_TabsButtons.OnButtonClick += RefreshActiveTab; RefreshActiveTab(); } } } #endif