Filtering reagents (#18211)

* Making it work

* Refactoring

* Autoformat revert

* Implementing suggestions

* Changed to file scoped namespaces.
This commit is contained in:
Hebi
2023-07-26 10:05:09 +02:00
committed by GitHub
parent 158af403e8
commit 756699ffcc
9 changed files with 171 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ using System.Linq;
using Content.Client.Chemistry.EntitySystems;
using Content.Client.Guidebook.Richtext;
using Content.Client.Message;
using Content.Client.UserInterface.ControlExtensions;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Chemistry.Reagent;
using JetBrains.Annotations;
@@ -20,7 +21,7 @@ namespace Content.Client.Guidebook.Controls;
/// Control for embedding a reagent into a guidebook.
/// </summary>
[UsedImplicitly, GenerateTypedNameReferences]
public sealed partial class GuideReagentEmbed : BoxContainer, IDocumentTag
public sealed partial class GuideReagentEmbed : BoxContainer, IDocumentTag, ISearchableControl
{
[Dependency] private readonly IEntitySystemManager _systemManager = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
@@ -45,6 +46,16 @@ public sealed partial class GuideReagentEmbed : BoxContainer, IDocumentTag
GenerateControl(reagent);
}
public bool CheckMatchesSearch(string query)
{
return this.ChildrenContainText(query);
}
public void SetHiddenState(bool state, string query)
{
this.Visible = CheckMatchesSearch(query) ? state : !state;
}
public bool TryParseTag(Dictionary<string, string> args, [NotNullWhen(true)] out Control? control)
{
control = null;

View File

@@ -12,14 +12,24 @@
<fancyTree:FancyTree Name="Tree" VerticalExpand="True" HorizontalExpand="True" Access="Public"/>
<cc:VSeparator StyleClasses="LowDivider" Margin="0 -2"/>
</BoxContainer>
<ScrollContainer Name="Scroll" HScrollEnabled="False" HorizontalExpand="True" VerticalExpand="True">
<Control>
<BoxContainer Orientation="Vertical" Name="EntryContainer" Margin="5 5 5 5" Visible="False"/>
<BoxContainer Orientation="Vertical" Name="Placeholder" Margin="5 5 5 5">
<Label HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Loc 'guidebook-placeholder-text'}"/>
<Label HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Loc 'guidebook-placeholder-text-2'}"/>
</BoxContainer>
</Control>
</ScrollContainer>
<BoxContainer Orientation="Vertical" HorizontalExpand="True" VerticalExpand="True">
<BoxContainer Name="SearchContainer" Visible="False" HorizontalExpand="True">
<LineEdit
Name="SearchBar"
PlaceHolder="{Loc 'guidebook-filter-placeholder-text'}"
HorizontalExpand="True"
Margin="0 5 10 5">
</LineEdit>
</BoxContainer>
<ScrollContainer Name="Scroll" HScrollEnabled="False" HorizontalExpand="True" VerticalExpand="True">
<Control>
<BoxContainer Orientation="Vertical" Name="EntryContainer" Margin="5 5 5 5" Visible="False"/>
<BoxContainer Orientation="Vertical" Name="Placeholder" Margin="5 5 5 5">
<Label HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Loc 'guidebook-placeholder-text'}"/>
<Label HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Loc 'guidebook-placeholder-text-2'}"/>
</BoxContainer>
</Control>
</ScrollContainer>
</BoxContainer>
</SplitContainer>
</controls:FancyWindow>

View File

@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Client.Guidebook.RichText;
using Content.Client.UserInterface.ControlExtensions;
using Content.Client.UserInterface.Controls;
using Content.Client.UserInterface.Controls.FancyTree;
using JetBrains.Annotations;
@@ -26,6 +27,11 @@ public sealed partial class GuidebookWindow : FancyWindow, ILinkClickHandler
IoCManager.InjectDependencies(this);
Tree.OnSelectedItemChanged += OnSelectionChanged;
SearchBar.OnTextChanged += _ =>
{
HandleFilter();
};
}
private void OnSelectionChanged(TreeItem? item)
@@ -40,6 +46,7 @@ public sealed partial class GuidebookWindow : FancyWindow, ILinkClickHandler
{
Placeholder.Visible = true;
EntryContainer.Visible = false;
SearchContainer.Visible = false;
EntryContainer.RemoveAllChildren();
}
@@ -48,9 +55,12 @@ public sealed partial class GuidebookWindow : FancyWindow, ILinkClickHandler
Scroll.SetScrollValue(default);
Placeholder.Visible = false;
EntryContainer.Visible = true;
SearchBar.Text = "";
EntryContainer.RemoveAllChildren();
using var file = _resourceManager.ContentFileReadText(entry.Text);
SearchContainer.Visible = entry.FilterEnabled;
if (!_parsingMan.TryAddMarkup(EntryContainer, file.ReadToEnd()))
{
EntryContainer.AddChild(new Label() { Text = "ERROR: Failed to parse document." });
@@ -159,4 +169,20 @@ public sealed partial class GuidebookWindow : FancyWindow, ILinkClickHandler
ShowGuide(entry);
}
}
private void HandleFilter()
{
var emptySearch = SearchBar.Text.Trim().Length == 0;
if (Tree.SelectedItem != null && Tree.SelectedItem.Metadata is GuideEntry entry && entry.FilterEnabled)
{
var foundElements = EntryContainer.GetSearchableControls();
foreach (var element in foundElements)
{
element.SetHiddenState(true, SearchBar.Text.Trim());
}
}
}
}

View File

@@ -0,0 +1,9 @@
namespace Content.Client.Guidebook.Controls;
public interface ISearchableControl
{
public bool CheckMatchesSearch(string query);
/// <summary>
/// Sets the hidden state for the control. In simple cases this could just disable/hide it, but you may want more complex behavior for some elements.
/// </summary>
public void SetHiddenState(bool state, string query);
}