Merge branch 'ItemSearchContextMenuSearchImprovements' of https://github.com/Chikinsupu/FlaxEngine into Chikinsupu-ItemSearchContextMenuSearchImprovements

This commit is contained in:
Wojtek Figat
2024-06-08 11:59:10 +02:00

View File

@@ -23,6 +23,9 @@ namespace FlaxEditor.GUI
[HideInEditor] [HideInEditor]
public class Item : Control public class Item : Control
{ {
private bool _isStartsWithMatch;
private bool _isFullMatch;
/// <summary> /// <summary>
/// The is mouse down flag. /// The is mouse down flag.
/// </summary> /// </summary>
@@ -43,6 +46,11 @@ namespace FlaxEditor.GUI
/// </summary> /// </summary>
public string Category; public string Category;
/// <summary>
/// A computed score for the context menu order
/// </summary>
public float SortScore;
/// <summary> /// <summary>
/// Occurs when items gets clicked by the user. /// Occurs when items gets clicked by the user.
/// </summary> /// </summary>
@@ -61,44 +69,69 @@ namespace FlaxEditor.GUI
{ {
} }
/// <summary>
/// Updates the <see cref="SortScore"/>
/// </summary>
public void UpdateScore()
{
SortScore = 0;
if (!Visible)
return;
if (_highlights is { Count: > 0 })
SortScore += 1;
if (_isStartsWithMatch)
SortScore += 2;
if (_isFullMatch)
SortScore += 5;
}
/// <summary> /// <summary>
/// Updates the filter. /// Updates the filter.
/// </summary> /// </summary>
/// <param name="filterText">The filter text.</param> /// <param name="filterText">The filter text.</param>
public void UpdateFilter(string filterText) public void UpdateFilter(string filterText)
{ {
_isStartsWithMatch = _isFullMatch = false;
if (string.IsNullOrWhiteSpace(filterText)) if (string.IsNullOrWhiteSpace(filterText))
{ {
// Clear filter // Clear filter
_highlights?.Clear(); _highlights?.Clear();
Visible = true; 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)
// Update highlights _highlights = new List<Rectangle>(ranges.Length);
if (_highlights == null)
_highlights = new List<Rectangle>(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;
}
else else
_highlights.Clear();
var style = Style.Current;
var font = style.FontSmall;
for (int i = 0; i < ranges.Length; i++)
{ {
// Hide var start = font.GetCharPosition(Name, ranges[i].StartIndex);
_highlights?.Clear(); var end = font.GetCharPosition(Name, ranges[i].EndIndex);
Visible = false; _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;
} }
/// <summary> /// <summary>
@@ -178,7 +211,14 @@ namespace FlaxEditor.GUI
public override int Compare(Control other) public override int Compare(Control other)
{ {
if (other is Item otherItem) 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); return base.Compare(other);
} }
} }
@@ -249,7 +289,10 @@ namespace FlaxEditor.GUI
for (int i = 0; i < items.Count; i++) for (int i = 0; i < items.Count; i++)
{ {
if (items[i] is Item item) if (items[i] is Item item)
{
item.UpdateFilter(_searchBox.Text); item.UpdateFilter(_searchBox.Text);
item.UpdateScore();
}
} }
if (_categoryPanels != null) if (_categoryPanels != null)
{ {
@@ -262,6 +305,7 @@ namespace FlaxEditor.GUI
if (category.Children[j] is Item item2) if (category.Children[j] is Item item2)
{ {
item2.UpdateFilter(_searchBox.Text); item2.UpdateFilter(_searchBox.Text);
item2.UpdateScore();
anyVisible |= item2.Visible; anyVisible |= item2.Visible;
} }
} }
@@ -273,6 +317,8 @@ namespace FlaxEditor.GUI
} }
} }
SortItems();
UnlockChildrenRecursive(); UnlockChildrenRecursive();
PerformLayout(true); PerformLayout(true);
_searchBox.Focus(); _searchBox.Focus();