-
Notifications
You must be signed in to change notification settings - Fork 878
Expand file tree
/
Copy pathDropDownButtonBase.cs
More file actions
204 lines (176 loc) · 7.16 KB
/
Copy pathDropDownButtonBase.cs
File metadata and controls
204 lines (176 loc) · 7.16 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.VFX.UI
{
abstract class DropDownButtonBase : VisualElement
{
protected readonly VFXView m_VFXView;
private class NotifyEditorWindow : EditorWindow
{
public event Action Destroyed;
private void OnDestroy()
{
Destroyed?.Invoke();
}
}
readonly bool m_HasLeftSeparator;
readonly Button m_MainButton;
NotifyEditorWindow m_CurrentPopup;
DateTime m_PopupClosedTimestamp;
protected readonly VisualElement m_PopupContent;
protected DropDownButtonBase(
string elementName,
VFXView view,
string uxmlSource,
string mainButtonLabel,
string mainButtonName,
string iconPath,
bool hasSeparatorBefore = false,
bool hasSeparatorAfter = false)
{
name = elementName;
m_VFXView = view;
style.flexDirection = new StyleEnum<FlexDirection>(FlexDirection.Row);
if (hasSeparatorBefore)
{
m_HasLeftSeparator = true;
var divider = new VisualElement();
divider.AddToClassList("separator");
Add(divider);
}
m_MainButton = new Button(OnMainButton) { name = mainButtonName };
m_MainButton.focusable = false;
m_MainButton.AddToClassList("dropdown-button");
m_MainButton.AddToClassList("unity-toolbar-toggle");
if (!string.IsNullOrEmpty(iconPath))
{
var icon = new Image { image = EditorGUIUtility.LoadIcon(iconPath) };
m_MainButton.Add(icon);
m_MainButton.tooltip = mainButtonLabel;
}
else
{
m_MainButton.text = mainButtonLabel;
}
Add(m_MainButton);
var separator = new VisualElement();
separator.AddToClassList("dropdown-separator");
Add(separator);
var dropDownButton = new Button(OnTogglePopup);
dropDownButton.focusable = false;
dropDownButton.AddToClassList("dropdown-arrow");
dropDownButton.AddToClassList("unity-toolbar-toggle");
dropDownButton.Add(new VisualElement());
Add(dropDownButton);
if (hasSeparatorAfter)
{
var divider = new VisualElement();
divider.AddToClassList("separator");
Add(divider);
}
m_PopupContent = new VisualElement();
var tpl = VFXView.LoadUXML(uxmlSource);
tpl.CloneTree(m_PopupContent);
contentContainer.AddStyleSheetPath("VFXToolbar");
}
protected virtual void OnOpenPopup() { }
protected virtual void OnMainButton() { }
protected abstract Vector2 GetPopupSize();
protected void ClosePopup()
{
m_CurrentPopup?.Close();
}
private Vector2 GetPopupPosition() => m_VFXView.ViewToScreenPosition(worldBound.position);
protected void OnTogglePopup()
{
// If the user click on the arrow button while the popup is opened
// the popup is then closed (because clicked outside) and immediately reopened
// To prevent this behavior we allow the popup to reopen only after a very short period of time after being closed
var deltaTime = DateTime.UtcNow - m_PopupClosedTimestamp;
if (m_CurrentPopup == null && deltaTime.TotalMilliseconds > 500)
{
m_CurrentPopup = ScriptableObject.CreateInstance<NotifyEditorWindow>();
m_CurrentPopup.hideFlags = HideFlags.HideAndDontSave;
if (m_PopupContent.parent != null)
{
m_PopupContent.parent.Remove(m_PopupContent);
}
m_CurrentPopup.Destroyed += OnPopupClosed;
m_CurrentPopup.rootVisualElement.AddStyleSheetPath("VFXToolbar");
m_CurrentPopup.rootVisualElement.Add(m_PopupContent);
m_CurrentPopup.rootVisualElement.AddToClassList("popup");
m_CurrentPopup.rootVisualElement.RegisterCallback<KeyUpEvent>(OnKeyUp);
OnOpenPopup();
var bounds = new Rect(GetPopupPosition(), localBound.size);
// Offset the bounds to align the popup with the real dropdown left edge
if (m_HasLeftSeparator)
{
bounds.xMin += 6;
}
m_CurrentPopup.ShowAsDropDown(bounds, GetPopupSize(), new[] { PopupLocation.BelowAlignLeft, PopupLocation.AboveAlignLeft });
m_CurrentPopup.minSize = GetPopupSize();
m_CurrentPopup.maxSize = m_CurrentPopup.minSize;
GetNextFocusable(null, m_PopupContent.Children(), false)?.Focus();
}
else if (m_CurrentPopup != null)
{
ClosePopup();
}
}
private void OnPopupClosed()
{
m_CurrentPopup.Destroyed -= OnPopupClosed;
m_CurrentPopup.rootVisualElement.UnregisterCallback<KeyUpEvent>(OnKeyUp);
m_CurrentPopup = null;
m_PopupClosedTimestamp = DateTime.UtcNow;
}
private void OnKeyUp(KeyUpEvent evt)
{
var focused = m_PopupContent.focusController.focusedElement;
switch (evt.keyCode)
{
case KeyCode.DownArrow:
var next = GetNextFocusable(focused, m_PopupContent.Children(), false);
next?.Focus();
break;
case KeyCode.UpArrow:
var prev = GetNextFocusable(focused, m_PopupContent.Children(), true);
prev?.Focus();
break;
case KeyCode.Escape:
ClosePopup();
break;
}
}
private VisualElement GetNextFocusable(Focusable focused, IEnumerable<VisualElement> elements, bool reverse)
{
var found = focused == null;
return GetNextFocusableRecursive(focused, elements, reverse, ref found);
}
private VisualElement GetNextFocusableRecursive(Focusable focused, IEnumerable<VisualElement> elements, bool reverse, ref bool found)
{
var collection = reverse ? elements.Reverse() : elements;
foreach (var child in collection)
{
if (child == focused)
{
found = true;
continue;
}
if (found && child != focused && child.enabledSelf && child.focusable)
{
return child;
}
var next = GetNextFocusableRecursive(focused, child.Children(), reverse, ref found);
if (next != null)
{
return next;
}
}
return null;
}
}
}