Skip to content

Commit c736e8d

Browse files
committed
Possibility to use Lists in the SearchablePopups; minor refactor changes
1 parent eff39a3 commit c736e8d

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

Assets/Editor Toolbox/Editor/Internal/SearchablePopup.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ public class SearchablePopup : PopupWindowContent
1717
/// <summary>
1818
/// Creates searchable popup using given properties.
1919
/// </summary>
20-
public static void Show(Rect activatorRect, int current, string[] options, Action<int> onSelect)
20+
public static void Show(Rect activatorRect, int current, IReadOnlyList<string> options, Action<int> onSelect)
2121
{
2222
PopupWindow.Show(activatorRect, new SearchablePopup(activatorRect, current, options, onSelect));
2323
}
2424

25-
2625
private readonly Action<int> onSelect;
2726

2827
private readonly SearchArray searchArray;
@@ -35,12 +34,11 @@ public static void Show(Rect activatorRect, int current, string[] options, Actio
3534
private Rect activatorRect;
3635
private Rect toolbarRect;
3736
private Rect contentRect;
38-
3937

4038
/// <summary>
41-
/// Constructor should be called only internally by the <see cref="Show(Rect, int, string[], Action{int})"/> method.
39+
/// Constructor should be called only internally by the <see cref="Show(Rect, int, IReadOnlyList{String}, Action{int})"/> method.
4240
/// </summary>
43-
private SearchablePopup(Rect activatorRect, int startIndex, string[] options, Action<int> onSelect)
41+
private SearchablePopup(Rect activatorRect, int startIndex, IReadOnlyList<string> options, Action<int> onSelect)
4442
{
4543
this.activatorRect = activatorRect;
4644

@@ -57,7 +55,6 @@ private SearchablePopup(Rect activatorRect, int startIndex, string[] options, Ac
5755
};
5856
}
5957

60-
6158
private void SelectItem(int index)
6259
{
6360
onSelect(index);
@@ -228,7 +225,6 @@ public override void OnGUI(Rect rect)
228225
GUI.enabled = false;
229226
}
230227

231-
232228
private class SearchArray
233229
{
234230
public struct Item
@@ -243,19 +239,16 @@ public Item(int index, string label)
243239
}
244240
}
245241

246-
247242
private readonly List<Item> items;
248-
private readonly string[] options;
243+
private readonly IReadOnlyList<string> options;
249244

250-
251-
public SearchArray(string[] options)
245+
public SearchArray(IReadOnlyList<string> options)
252246
{
253247
this.options = options;
254248
items = new List<Item>();
255249
Search(string.Empty);
256250
}
257251

258-
259252
public bool Search(string filter)
260253
{
261254
if (Filter == filter)
@@ -265,7 +258,8 @@ public bool Search(string filter)
265258

266259
items.Clear();
267260
var simplifiedFilter = filter.ToLower();
268-
for (var i = 0; i < options.Length; i++)
261+
var optionsCount = options.Count;
262+
for (var i = 0; i < optionsCount; i++)
269263
{
270264
var option = options[i];
271265
if (string.IsNullOrEmpty(filter) || option.ToLower().Contains(simplifiedFilter))
@@ -291,7 +285,6 @@ public Item GetItemAt(int index)
291285
return items[index];
292286
}
293287

294-
295288
public int ItemsCount => items.Count;
296289

297290
public string Filter { get; private set; }

Assets/Examples/Editor/SampleToolbar.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using UnityEngine;
2-
using UnityEngine.SceneManagement;
1+
using Toolbox.Editor;
32
using UnityEditor;
43
using UnityEditor.SceneManagement;
5-
6-
using Toolbox.Editor;
4+
using UnityEngine;
5+
using UnityEngine.SceneManagement;
76

87
[InitializeOnLoad]
98
public static class SampleToolbar
@@ -13,7 +12,6 @@ public static class SampleToolbar
1312
/// </summary>
1413
private readonly static string mySampleSceneName = "SampleScene";
1514

16-
1715
static SampleToolbar()
1816
{
1917
EditorSceneManager.sceneOpened -= SceneOpenedCallback;
@@ -23,7 +21,6 @@ static SampleToolbar()
2321
EditorApplication.update += ValidateFirstScene;
2422
}
2523

26-
2724
/// <summary>
2825
/// This method is used to validate first scene after Editor launch.
2926
/// </summary>
@@ -35,8 +32,8 @@ private static void ValidateFirstScene()
3532
}
3633

3734
EditorApplication.update -= ValidateFirstScene;
38-
39-
SceneOpenedCallback(SceneManager.GetActiveScene(), OpenSceneMode.Single);
35+
var activeScene = SceneManager.GetActiveScene();
36+
SceneOpenedCallback(activeScene, OpenSceneMode.Single);
4037
}
4138

4239
/// <summary>
@@ -51,10 +48,10 @@ private static void SceneOpenedCallback(Scene scene, OpenSceneMode mode)
5148
{
5249
return;
5350
}
51+
5452
ToolboxEditorToolbar.OnToolbarGui += OnToolbarGui;
5553
}
5654

57-
5855
/// <summary>
5956
/// Layout-based GUI call.
6057
/// </summary>
@@ -65,21 +62,23 @@ private static void OnToolbarGui()
6562
{
6663
Debug.Log("1");
6764
}
65+
6866
if (GUILayout.Button(Style.sampleContent1, Style.commandMidStyle))
6967
{
7068
Debug.Log("2");
7169
}
70+
7271
if (GUILayout.Button(Style.sampleContent2, Style.commandMidStyle))
7372
{
7473
Debug.Log("3");
7574
}
75+
7676
if (GUILayout.Button("4", Style.commandRightStyle))
7777
{
7878
Debug.Log("4");
7979
}
8080
}
8181

82-
8382
private static class Style
8483
{
8584
internal static readonly GUIStyle commandMidStyle = new GUIStyle("CommandMid")

0 commit comments

Comments
 (0)