From 6885e561db7ef1d8fb75fc9121ca4d2b6b39fd71 Mon Sep 17 00:00:00 2001 From: Nils Hausfeld Date: Thu, 30 May 2024 19:48:34 +0200 Subject: [PATCH] - Improved item search for item list context menu (based on visject CM item search improvements) --- Source/Editor/GUI/ItemsListContextMenu.cs | 90 +++++++++++++++++------ 1 file changed, 68 insertions(+), 22 deletions(-) diff --git a/Source/Editor/GUI/ItemsListContextMenu.cs b/Source/Editor/GUI/ItemsListContextMenu.cs index 78fbcd779..4084715fe 100644 --- a/Source/Editor/GUI/ItemsListContextMenu.cs +++ b/Source/Editor/GUI/ItemsListContextMenu.cs @@ -23,6 +23,9 @@ namespace FlaxEditor.GUI [HideInEditor] public class Item : Control { + private bool _isStartsWithMatch; + private bool _isFullMatch; + /// /// The is mouse down flag. /// @@ -43,6 +46,11 @@ namespace FlaxEditor.GUI /// public string Category; + /// + /// A computed score for the context menu order + /// + public float SortScore; + /// /// Occurs when items gets clicked by the user. /// @@ -61,44 +69,69 @@ namespace FlaxEditor.GUI { } + /// + /// Updates the + /// + public void UpdateScore() + { + SortScore = 0; + + if(!Visible) + return; + + if (_highlights is { Count: > 0 }) + SortScore += 1; + if (_isStartsWithMatch) + SortScore += 2; + if (_isFullMatch) + SortScore += 5; + } + /// /// Updates the filter. /// /// The filter text. public void UpdateFilter(string filterText) { + _isStartsWithMatch = _isFullMatch = false; + if (string.IsNullOrWhiteSpace(filterText)) { // Clear filter _highlights?.Clear(); Visible = true; + return; } - else + + if (QueryFilterHelper.Match(filterText, Name, out var ranges)) { - if (QueryFilterHelper.Match(filterText, Name, out var ranges)) - { - // Update highlights - if (_highlights == null) - _highlights = new List(ranges.Length); - else - _highlights.Clear(); - var style = Style.Current; - var font = style.FontSmall; - for (int i = 0; i < ranges.Length; i++) - { - var start = font.GetCharPosition(Name, ranges[i].StartIndex); - var end = font.GetCharPosition(Name, ranges[i].EndIndex); - _highlights.Add(new Rectangle(start.X + 2, 0, end.X - start.X, Height)); - } - Visible = true; - } + // Update highlights + if (_highlights == null) + _highlights = new List(ranges.Length); else + _highlights.Clear(); + var style = Style.Current; + var font = style.FontSmall; + for (int i = 0; i < ranges.Length; i++) { - // Hide - _highlights?.Clear(); - Visible = false; + var start = font.GetCharPosition(Name, ranges[i].StartIndex); + var end = font.GetCharPosition(Name, ranges[i].EndIndex); + _highlights.Add(new Rectangle(start.X + 2, 0, end.X - start.X, Height)); + + if (ranges[i].StartIndex <= 0) + { + _isStartsWithMatch = true; + if (ranges[i].Length == Name.Length) + _isFullMatch = true; + } } + Visible = true; + return; } + + // Hide + _highlights?.Clear(); + Visible = false; } /// @@ -178,7 +211,14 @@ namespace FlaxEditor.GUI public override int Compare(Control other) { if (other is Item otherItem) - return string.Compare(Name, otherItem.Name, StringComparison.Ordinal); + { + int order = -1 * SortScore.CompareTo(otherItem.SortScore); + if (order == 0) + { + order = string.Compare(Name, otherItem.Name, StringComparison.Ordinal); + } + return order; + } return base.Compare(other); } } @@ -249,7 +289,10 @@ namespace FlaxEditor.GUI for (int i = 0; i < items.Count; i++) { if (items[i] is Item item) + { item.UpdateFilter(_searchBox.Text); + item.UpdateScore(); + } } if (_categoryPanels != null) { @@ -262,6 +305,7 @@ namespace FlaxEditor.GUI if (category.Children[j] is Item item2) { item2.UpdateFilter(_searchBox.Text); + item2.UpdateScore(); anyVisible |= item2.Visible; } } @@ -273,6 +317,8 @@ namespace FlaxEditor.GUI } } + SortItems(); + UnlockChildrenRecursive(); PerformLayout(true); _searchBox.Focus();