Skip to content

Commit 48fce05

Browse files
committed
feat: Added DropdownWindow popup option
Now DropdownWindow can be drawn as a popup as opposed to dropdown
1 parent 03ab613 commit 48fce05

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

Editor/Drawers/TypeDropdownDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void Draw(Action<Type> onTypeSelected)
3434
if (_attribute.ExpandAllFolders)
3535
selectionTree.ExpandAllFolders();
3636

37-
DropdownWindow.Create(selectionTree, _attribute.DropdownHeight, GUIUtility.GUIToScreenPoint(Event.current.mousePosition));
37+
DropdownWindow.Create(selectionTree, _attribute.DropdownHeight, GUIUtility.GUIToScreenPoint(Event.current.mousePosition), DropdownWindowType.Dropdown);
3838
}
3939

4040
public TypeItem[] GetDropdownItems()

Editor/TypeDropdown/DropdownWindow.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using UnityEngine;
99
using Util;
1010

11+
internal enum DropdownWindowType { Dropdown, Popup }
12+
1113
/// <summary>Creates a dropdown window that shows the <see cref="SelectionTree"/> elements.</summary>
1214
internal class DropdownWindow : EditorWindow
1315
{
@@ -19,12 +21,10 @@ internal class DropdownWindow : EditorWindow
1921
private Rect _positionOnCreation;
2022
private bool _positionWasSetAfterCreation;
2123

22-
public event Action OnClose;
23-
24-
public static DropdownWindow Create(SelectionTree selectionTree, int windowHeight, Vector2 windowPosition)
24+
public static DropdownWindow Create(SelectionTree selectionTree, int windowHeight, Vector2 windowPosition, DropdownWindowType windowType)
2525
{
2626
var window = CreateInstance<DropdownWindow>();
27-
window.OnCreate(selectionTree, windowHeight, windowPosition);
27+
window.OnCreate(selectionTree, windowHeight, windowPosition, windowType);
2828
return window;
2929
}
3030

@@ -35,7 +35,7 @@ public static DropdownWindow Create(SelectionTree selectionTree, int windowHeigh
3535
/// <param name="selectionTree">Tree that contains the dropdown items to show.</param>
3636
/// <param name="windowHeight">Height of the window. If set to 0, it will be auto-adjusted.</param>
3737
/// <param name="windowPosition">Position of the window to set.</param>
38-
private void OnCreate(SelectionTree selectionTree, float windowHeight, Vector2 windowPosition)
38+
private void OnCreate(SelectionTree selectionTree, float windowHeight, Vector2 windowPosition, DropdownWindowType windowType)
3939
{
4040
ResetControl();
4141
wantsMouseMove = true;
@@ -46,11 +46,22 @@ private void OnCreate(SelectionTree selectionTree, float windowHeight, Vector2 w
4646

4747
_positionOnCreation = GetWindowRect(windowPosition, windowHeight);
4848

49-
// ShowAsDropDown usually shows the window under a button, but since we don't need to align the window to
50-
// any button, we set buttonRect.height to 0f.
51-
Rect buttonRect = new Rect(_positionOnCreation) { height = 0f };
52-
53-
ShowAsDropDown(buttonRect, _positionOnCreation.size);
49+
if (windowType == DropdownWindowType.Dropdown)
50+
{
51+
// ShowAsDropDown usually shows the window under a button, but since we don't need to align the window to
52+
// any button, we set buttonRect.height to 0f.
53+
Rect buttonRect = new Rect(_positionOnCreation) { height = 0f };
54+
ShowAsDropDown(buttonRect, _positionOnCreation.size);
55+
}
56+
else if (windowType == DropdownWindowType.Popup)
57+
{
58+
position = _positionOnCreation;
59+
ShowPopup();
60+
}
61+
else
62+
{
63+
throw new Exception("Unknown window type");
64+
}
5465
}
5566

5667
public static float CalculateOptimalWidth(string[] selectionPaths)
@@ -123,7 +134,7 @@ private void Update()
123134
AdjustSizeIfNeeded();
124135
}
125136

126-
private void OnDestroy() => OnClose?.Invoke();
137+
private void OnLostFocus() => Close();
127138

128139
private void AdjustSizeIfNeeded()
129140
{

0 commit comments

Comments
 (0)