UI/Components/TabView.cs
using HC3;
using Sandbox.UI;
using System;
[Alias( "tabview", "TabView" )]
public class TabView : Panel
{
/// <summary>
/// A control housing the tabs
/// </summary>
public Panel TabBar { get; protected set; }
/// <summary>
/// Access to the pages on this control
/// </summary>
public IEnumerable<Tab> Tabs => Children.OfType<Tab>();
/// <summary>
/// If a cookie is set then the selected tab will be saved and restored.
/// </summary>
public string TabCookie { get; set; }
string _activeTab;
/// <summary>
/// The tab that is active
/// </summary>
public string ActiveTab
{
get => _activeTab;
set
{
if ( _activeTab == value ) return;
_activeTab = value;
var t = Tabs.FirstOrDefault( x => x.Key == _activeTab );
SwitchTab( t );
}
}
public TabView()
{
TabBar = Add.Panel( "tabs" );
}
public override void SetProperty( string name, string value )
{
if ( name == "cookie" )
{
TabCookie = value;
return;
}
base.SetProperty( name, value );
}
protected override void OnChildAdded( Panel child )
{
base.OnChildAdded( child );
if ( child is not Tab tab )
return;
var index = Tabs.Count() - 1;
var cookieValue = string.IsNullOrWhiteSpace( TabCookie ) ? null : Game.Cookies.GetString( $"dropdown.{TabCookie}", null );
if ( index == 0 || (cookieValue is not null && cookieValue == tab.Key) )
{
SwitchTab( tab, false );
}
else
{
tab.Active = false;
}
}
/// <summary>
/// Switch to a specific tab.
/// </summary>
public void SwitchTab( Tab tab, bool setCookie = true )
{
ActiveTab = tab.Key;
foreach ( var page in Tabs )
{
page.Active = page == tab;
}
if ( setCookie && !string.IsNullOrEmpty( TabCookie ) )
{
Game.Cookies.SetString( $"dropdown.{TabCookie}", tab.Key );
}
}
}
[Alias( "tab", "Tab" )]
public class Tab : Panel
{
[Property]
public string Key { get; set; }
[Property]
public string Title { get; set; }
[Property]
public bool ShowTitle { get; set; }
[Property]
public Texture Texture { get; set; }
[Property]
public string Icon { get; set; }
private TabView Container => Parent as TabView;
public Button Button { get; protected set; }
public Tab()
{
Key = $"{Title}/{Icon}";
}
public override void OnDeleted()
{
base.OnDeleted();
Button?.Delete( true );
}
protected override void OnParametersSet()
{
base.OnParametersSet();
if ( !Button.IsValid() )
{
Button = new Button( "", null, () => Container?.SwitchTab( this, true ) );
Button.AddClass( "tab-button" );
if ( !string.IsNullOrEmpty( Icon ) )
{
Button.AddChild( new Silkicon() { Icon = Icon } );
}
if ( !string.IsNullOrEmpty( Title ) && ShowTitle )
{
Button.AddChild( new Label() { Text = Title } );
}
Container.TabBar.AddChild( Button );
}
//Button.Text = Title;
Button.Tooltip = Title;
Button.Active = active;
}
bool active;
/// <summary>
/// Change appearance based on active status
/// </summary>
public bool Active
{
get => active;
set
{
active = value;
if ( Button.IsValid() )
{
Button.Active = value;
}
SetClass( "active", value );
}
}
}