From 7b3213c0be2eab1fe8bd92dc42e8c4ba8620c47b Mon Sep 17 00:00:00 2001 From: Graham Howden Date: Fri, 3 Feb 2023 19:10:05 +0000 Subject: [PATCH] feat: allow filtering separate words when selecting items --- Glamourer/Gui/ComboWithFilter.cs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/Glamourer/Gui/ComboWithFilter.cs b/Glamourer/Gui/ComboWithFilter.cs index 9a63425..e43a721 100644 --- a/Glamourer/Gui/ComboWithFilter.cs +++ b/Glamourer/Gui/ComboWithFilter.cs @@ -34,6 +34,28 @@ namespace Glamourer.Gui public ImGuiComboFlags Flags { get; set; } = ImGuiComboFlags.None; public int ItemsAtOnce { get; set; } = 12; + private bool TestFilter(string text, string filter) + { + var remainingWords = text.Split(Array.Empty(), StringSplitOptions.RemoveEmptyEntries).AsEnumerable(); + + foreach (var currentFilterWord in filter.Split(Array.Empty(), StringSplitOptions.RemoveEmptyEntries)) + { + // check each word until we find a match or reach the end of the set + remainingWords = remainingWords.SkipWhile(word => !word.Contains(currentFilterWord)); + + // no remaing words means this filter word was not found + if (!remainingWords.Any()) + { + return false; + } + + // remove matched word and check next filter + remainingWords = remainingWords.Skip(1); + } + + return true; + } + private void UpdateFilter(string newFilter) { if (newFilter == _currentFilter) @@ -41,9 +63,9 @@ namespace Glamourer.Gui var lower = newFilter.ToLowerInvariant(); if (_currentFilterLower.Any() && lower.Contains(_currentFilterLower)) - _currentItemNames = _currentItemNames.Where(p => p.Item1.Contains(lower)).ToArray(); + _currentItemNames = _currentItemNames.Where(p => TestFilter(p.Item1, lower)).ToArray(); else if (lower.Any()) - _currentItemNames = _itemNamesLower.Where(p => p.Item1.Contains(lower)).ToArray(); + _currentItemNames = _itemNamesLower.Where(p => TestFilter(p.Item1, lower)).ToArray(); else _currentItemNames = _itemNamesLower; _currentFilter = newFilter;