1
0

SearchListContainer.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Linq;
  2. using Robust.Client.UserInterface.Controls;
  3. namespace Content.Client.UserInterface.Controls;
  4. public sealed class SearchListContainer : ListContainer
  5. {
  6. private LineEdit? _searchBar;
  7. private List<ListData> _unfilteredData = new();
  8. /// <summary>
  9. /// The <see cref="LineEdit"/> that is used to filter the list data.
  10. /// </summary>
  11. public LineEdit? SearchBar
  12. {
  13. get => _searchBar;
  14. set
  15. {
  16. if (_searchBar is not null)
  17. _searchBar.OnTextChanged -= FilterList;
  18. _searchBar = value;
  19. if (_searchBar is null)
  20. return;
  21. _searchBar.OnTextChanged += FilterList;
  22. }
  23. }
  24. /// <summary>
  25. /// Runs over the ListData to determine if it should pass the filter.
  26. /// </summary>
  27. public Func<string, ListData, bool>? DataFilterCondition = null;
  28. public override void PopulateList(IReadOnlyList<ListData> data)
  29. {
  30. _unfilteredData = data.ToList();
  31. FilterList();
  32. }
  33. private void FilterList(LineEdit.LineEditEventArgs obj)
  34. {
  35. FilterList();
  36. }
  37. private void FilterList()
  38. {
  39. var filterText = SearchBar?.Text;
  40. if (DataFilterCondition is null || string.IsNullOrEmpty(filterText))
  41. {
  42. base.PopulateList(_unfilteredData);
  43. return;
  44. }
  45. var filteredData = new List<ListData>();
  46. foreach (var data in _unfilteredData)
  47. {
  48. if (!DataFilterCondition(filterText, data))
  49. continue;
  50. filteredData.Add(data);
  51. }
  52. base.PopulateList(filteredData);
  53. }
  54. }