From 4f6fec7e82ca41be7de7a747ee355ea4aefe47da Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Wed, 30 Aug 2023 10:59:11 -0700 Subject: [PATCH] Accelerating search --- .../Library/Widgets/SearchableTreePanel.cs | 49 ++++++++++++++++--- Submodules/agg-sharp | 2 +- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/MatterControlLib/Library/Widgets/SearchableTreePanel.cs b/MatterControlLib/Library/Widgets/SearchableTreePanel.cs index afd2864f5..2093fad21 100644 --- a/MatterControlLib/Library/Widgets/SearchableTreePanel.cs +++ b/MatterControlLib/Library/Widgets/SearchableTreePanel.cs @@ -151,16 +151,35 @@ namespace MatterHackers.MatterControl.Library.Widgets { } + public TreeNode RootNode + { + get + { + return contentPanel.Children.OfType().FirstOrDefault(); + } + } + + // A dictionary to store the initial expanded states of nodes + private Dictionary searchInitialExpandedStates = new Dictionary(); + + public bool SearchRunning => searchInitialExpandedStates.Count > 0; + protected virtual void PerformSearch(string filter) { + // Store the initial expanded state of each node before the search + if (searchInitialExpandedStates.Count == 0) + { + foreach (var node in RootNode.DescendantsAndSelf()) + { + searchInitialExpandedStates[node] = node.Expanded; + } + } + var matches = new List(); Console.WriteLine("Filter for: " + filter); - foreach (var rootNode in contentPanel.Children.OfType()) - { - FilterTree(rootNode, filter, false, matches); - } + FilterTree(RootNode, filter, false, matches); if (matches.Count == 1) { @@ -176,14 +195,30 @@ namespace MatterHackers.MatterControl.Library.Widgets private void ClearSearch() { - foreach (var rootNode in contentPanel.Children.OfType()) + if (searchInitialExpandedStates.Count > 0) { - ResetTree(rootNode); + // Reset the expanded state of each node to its initial state before the search + foreach (var node in RootNode.DescendantsAndSelf()) + { + if (searchInitialExpandedStates.TryGetValue(node, out bool wasExpanded)) + { + node.Expanded = wasExpanded; + node.Visible = true; + } + } + + // Clear the stored initial expanded states + searchInitialExpandedStates.Clear(); } searchBox.Text = ""; searchBox.ResetButton.Visible = false; - TreeView.SelectedNode = null; + var parent = TreeView.SelectedNode; + while(parent != null) + { + parent.Expanded = true; + parent = parent.NodeParent; + } this.OnClearSearch(); } diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index bde388be6..468884f09 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit bde388be61bc0ea262438e850330762f073cfd3f +Subproject commit 468884f09fe630dc7bf1baa10f96340d3ddbbd54