Skip to content

Commit

Permalink
Add setting to always search the root category - fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Dec 2, 2024
1 parent faeb7d7 commit 728914e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ComponentSelectorAdditions.Events
{
public sealed class EnumerateCategoriesEvent : CancelableSortedItemsEvent<CategoryNode<Type>>
public sealed class EnumerateCategoriesEvent : CancelableSortedItemsEvent<CategoryNode<Type>>, IEnumerateSelectorResultEvent
{
public override IEnumerable<CategoryNode<Type>> Items
=> sortableItems.OrderBy(entry => entry.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace ComponentSelectorAdditions.Events
{
public sealed class EnumerateComponentsEvent : CancelableSortedItemsEvent<ComponentResult>
public sealed class EnumerateComponentsEvent : CancelableSortedItemsEvent<ComponentResult>, IEnumerateSelectorResultEvent
{
public Predicate<Type> ComponentFilter { get; }

Expand Down
14 changes: 14 additions & 0 deletions ComponentSelectorAdditions/Events/IEnumerateSelectorResultEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using FrooxEngine;
using System;
using System.Collections.Generic;
using System.Text;

namespace ComponentSelectorAdditions.Events
{
public interface IEnumerateSelectorResultEvent
{
public SelectorPath Path { get; }
public CategoryNode<Type> RootCategory { get; }
public ComponentSelector Selector { get; }
}
}
10 changes: 5 additions & 5 deletions ComponentSelectorAdditions/Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private static void BuildCategoryUI(ComponentSelector selector, UIBuilder ui, Se

if (rootCategory is null)
{
selector._rootPath.Value = null;
selector._rootPath.Value = null!;
selectorData.CurrentPath = new SelectorPath("/", selectorData.CurrentPath.Search, false, null, true);
rootCategory = WorkerInitializer.ComponentLibrary;
}
Expand Down Expand Up @@ -224,7 +224,7 @@ private static bool BuildUIPrefix(ComponentSelector __instance, string path, boo

__instance._uiRoot.Target.DestroyChildren();
__instance._customGenericArguments.Clear();
__instance._genericType.Value = null;
__instance._genericType.Value = null!;

var ui = SetupStyle(new UIBuilder(__instance._uiRoot.Target));

Expand Down Expand Up @@ -314,8 +314,8 @@ private static BuildCustomGenericBuilder OnBuildCustomGenericBuilder(ComponentSe
if (!eventData.AddsCreateCustomTypeButton)
Logger.Warn(() => "No event handler handled adding a create custom type button!");

selector._customGenericTypeLabel.Target = eventData.CreateCustomTypeButton?.Label.Content;
selector._customGenericTypeColor.Target = eventData.CreateCustomTypeButton?.BaseColor;
selector._customGenericTypeLabel.Target = eventData.CreateCustomTypeButton?.Label.Content!;
selector._customGenericTypeColor.Target = eventData.CreateCustomTypeButton?.BaseColor!;

return eventData;
}
Expand Down Expand Up @@ -465,7 +465,7 @@ private static bool SetupUIPrefix(ComponentSelector __instance, LocaleString tit
if (selectorData.HasSearchBar)
selectorData.SearchBar.Text.Content.OnValueChange += MakeBuildUICall(__instance, selectorData);

__instance.BuildUI(null);
__instance.BuildUI(null!);

return false;
}
Expand Down
31 changes: 24 additions & 7 deletions ComponentSelectorAdditions/SearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace ComponentSelectorAdditions
internal sealed class SearchBar : ConfiguredResoniteMonkey<SearchBar, SearchConfig>, IEventHandler<BuildSelectorHeaderEvent>,
ICancelableEventHandler<EnumerateCategoriesEvent>, ICancelableEventHandler<EnumerateComponentsEvent>
{
private const string ProtoFluxPath = "/ProtoFlux/Runtimes/Execution/Nodes";
private static readonly char[] _searchSplits = new[] { ' ', ',', '+', '|' };
public override bool CanBeDisabled => true;
public int Priority => HarmonyLib.Priority.VeryHigh;
Expand All @@ -39,7 +40,7 @@ public void Handle(BuildSelectorHeaderEvent eventData)
searchLayout.DestroyWhenLocalUserLeaves();

ui.Style.FlexibleWidth = 1;
var textField = ui.TextField(null, parseRTF: false);
var textField = ui.TextField(null!, parseRTF: false);
var details = new SelectorSearchBar(searchLayout, textField.Editor.Target, () => ConfigSection.SearchRefreshDelay);
eventData.SearchBar = details;

Expand All @@ -48,20 +49,23 @@ public void Handle(BuildSelectorHeaderEvent eventData)

ui.Style.FlexibleWidth = -1;
ui.Style.ButtonTextAlignment = Alignment.MiddleCenter;
ui.LocalActionButton("∅", _ => details.Content = null);

var clearButton = ui.Button("∅");
var clearAction = clearButton.Slot.AttachComponent<ButtonValueSet<string>>();
clearAction.TargetValue.Target = details.Text.Content;

ui.PopStyle();
ui.NestOut();
}

public void Handle(EnumerateCategoriesEvent eventData)
{
if (!eventData.Path.HasSearch || (eventData.Path.IsSelectorRoot && eventData.Path.Search.Length < 3))
if (!eventData.Path.HasSearch || ((eventData.Path.IsSelectorRoot || ConfigSection.AlwaysSearchRoot) && eventData.Path.Search.Length < 3))
return;

var search = eventData.Path.Search.Split(_searchSplits, StringSplitOptions.RemoveEmptyEntries);

foreach (var category in SearchCategories(eventData.RootCategory, search))
foreach (var category in SearchCategories(PickSearchCategory(eventData), search))
eventData.AddItem(category);

eventData.Canceled = true;
Expand All @@ -73,10 +77,12 @@ public void Handle(EnumerateComponentsEvent eventData)
return;

var search = eventData.Path.Search.Split(_searchSplits, StringSplitOptions.RemoveEmptyEntries);
var results = eventData.RootCategory.Elements
.Select(type => (Category: eventData.RootCategory, Type: type, Matches: SearchContains(type.Name, search)))
var searchCategory = PickSearchCategory(eventData);

var results = searchCategory.Elements
.Select(type => (Category: searchCategory, Type: type, Matches: SearchContains(type.Name, search)))
.Concat(
SearchCategories(eventData.RootCategory)
SearchCategories(searchCategory)
.SelectMany(category => category.Elements
.Select(type => (Category: category, Type: type, Matches: SearchContains(type.Name, search)))))
.Where(match => match.Matches > 0)
Expand Down Expand Up @@ -141,5 +147,16 @@ private static IEnumerable<CategoryNode<Type>> SearchCategories(CategoryNode<Typ

private static int SearchContains(string haystack, string[] needles)
=> needles.Count(needle => CultureInfo.InvariantCulture.CompareInfo.IndexOf(haystack, needle, CompareOptions.IgnoreCase) >= 0);

private CategoryNode<Type> PickSearchCategory(IEnumerateSelectorResultEvent eventData)
{
var isProtoFlux = eventData.Path.Path.StartsWith(ProtoFluxPath);

return ConfigSection.AlwaysSearchRoot
? (isProtoFlux
? WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxPath)
: WorkerInitializer.ComponentLibrary)
: eventData.RootCategory;
}
}
}
2 changes: 2 additions & 0 deletions ComponentSelectorAdditions/SearchConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace ComponentSelectorAdditions
/// </summary>
public sealed class SearchConfig : ConfigSection
{
private static readonly DefiningConfigKey<bool> _alwaysSearchRoot = new("AlwaysSearchRoot", "Always starts searching from the root category, regardless of the current one.", () => false);
private static readonly Dictionary<string, bool> _excludedCategories = new(StringComparer.OrdinalIgnoreCase);

private static readonly DefiningConfigKey<int> _maxResultCount = new("MaxResultCount", "The maximum number of component / node results to display. 'Better' results are listed first. Categories don't count.", () => 64)
Expand Down Expand Up @@ -49,6 +50,7 @@ public sealed class SearchConfig : ConfigSection
/// <inheritdoc/>
public override Version Version { get; } = new(1, 0, 0);

internal bool AlwaysSearchRoot => _alwaysSearchRoot;
internal int MaxResultCount => _maxResultCount;
internal int SearchRefreshDelay => (int)(1000 * _searchRefreshDelay);

Expand Down
2 changes: 1 addition & 1 deletion ComponentSelectorAdditions/SelectorSearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public bool Active
public string? Content
{
get => Active ? Text.Content.Value : null;
set => Text.Content.Value = value;
set => Text.Content.Value = value!;
}

/// <summary>
Expand Down

0 comments on commit 728914e

Please sign in to comment.