TabView.cs
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Collections.Generic;
using UnityEngine.UIElements;
using Unity.PlasticSCM.Editor;
namespace Unity.PlasticSCM.Editor.UI.UIElements
{
internal class TabView : VisualElement
{
internal TabView()
{
InitializeLayoutAndStyles();
BuildComponents();
}
internal Button AddTab(string name, VisualElement content)
{
mTabs.Add(name, content);
Button newButton = new Button()
{
text = name,
name = name
};
newButton.AddToClassList("tab-button");
mButtons.Add(name, newButton);
newButton.clickable.clickedWithEventInfo += OnClickButton;
mTabArea.Add(newButton);
if (mTabs.Count == 1)
ButtonClicked(newButton);
return newButton;
}
internal void SwitchContent(VisualElement content)
{
mContentArea.Clear();
mContentArea.Add(content);
foreach (Button button in mButtons.Values)
button.RemoveFromClassList("active");
}
void OnClickButton(EventBase eventBase)
{
ButtonClicked((Button)eventBase.target);
}
void ButtonClicked(Button clickedButton)
{
VisualElement content;
mTabs.TryGetValue(clickedButton.text, out content);
mContentArea.Clear();
mContentArea.Add(content);
foreach (Button button in mButtons.Values)
button.RemoveFromClassList("active");
clickedButton.AddToClassList("active");
}
void BuildComponents()
{
mTabArea = this.Query<VisualElement>("TabArea").First();
mContentArea = this.Query<VisualElement>("ContentArea").First();
}
void InitializeLayoutAndStyles()
{
name = "TabView";
this.LoadLayout(typeof(TabView).Name);
this.LoadStyle(typeof(TabView).Name);
}
VisualElement mContentArea;
VisualElement mTabArea;
Dictionary<string, VisualElement> mTabs = new Dictionary<string, VisualElement>();
Dictionary<string, Button> mButtons = new Dictionary<string, Button>();
}
}