Skip to content

Commit 1d4d60c

Browse files
committed
Close nodes we open while searching but that contain no results.
1 parent 5f5fd50 commit 1d4d60c

1 file changed

Lines changed: 41 additions & 17 deletions

File tree

src/FlaUInspect/ViewModels/SearchViewModel.cs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,49 @@ private void NavigateToSearchResult(ElementViewModel element) {
168168
private static IEnumerable<ElementViewModel> EnumerateMatchingElements(ElementViewModel startVm, string searchText, FindByType findBy) {
169169
if (startVm.AutomationElement == null) yield break;
170170

171-
var stack = new Stack<(ElementViewModel vm, bool skipCheck)>();
172-
stack.Push((startVm, true));
171+
// Stack contains: (node, childIndex, wasExpandedByUs, foundMatchInSubtree)
172+
var stack = new Stack<(ElementViewModel vm, int childIdx, bool expandedByUs, bool foundMatch)>();
173173

174-
while (stack.Count > 0) {
175-
var (current, skipCheck) = stack.Pop();
176-
if (current.AutomationElement == null) continue;
177-
178-
if (!skipCheck && MatchesCondition(current.AutomationElement, searchText, findBy)) {
179-
yield return current;
180-
}
181-
182-
if (!current.IsExpanded) {
183-
current.IsExpanded = true;
184-
}
174+
// Start with the root, skip checking it
175+
bool startWasExpanded = startVm.IsExpanded;
176+
if (!startWasExpanded) startVm.IsExpanded = true;
177+
stack.Push((startVm, 0, !startWasExpanded, false));
185178

186-
for (int i = current.Children.Count - 1; i >= 0; i--) {
187-
var child = current.Children[i];
188-
if (child != null) {
189-
stack.Push((child, false));
179+
while (stack.Count > 0) {
180+
var (current, childIdx, expandedByUs, foundMatch) = stack.Pop();
181+
182+
// Process children
183+
if (childIdx < current.Children.Count) {
184+
var child = current.Children[childIdx];
185+
// Push current back with next child index, preserving foundMatch state
186+
stack.Push((current, childIdx + 1, expandedByUs, foundMatch));
187+
188+
if (child?.AutomationElement != null) {
189+
bool isMatch = MatchesCondition(child.AutomationElement, searchText, findBy);
190+
if (isMatch) {
191+
// Update parent's foundMatch flag
192+
if (stack.Count > 0) {
193+
var parent = stack.Pop();
194+
stack.Push((parent.vm, parent.childIdx, parent.expandedByUs, true));
195+
}
196+
yield return child;
197+
}
198+
199+
// Expand child and push it for processing
200+
bool childWasExpanded = child.IsExpanded;
201+
if (!childWasExpanded) child.IsExpanded = true;
202+
stack.Push((child, 0, !childWasExpanded, isMatch));
203+
}
204+
} else {
205+
// Done with all children of current node
206+
// If we expanded this node and found no match in subtree, collapse it
207+
if (expandedByUs && !foundMatch) {
208+
current.IsExpanded = false;
209+
}
210+
// Propagate foundMatch to parent
211+
if (foundMatch && stack.Count > 0) {
212+
var parent = stack.Pop();
213+
stack.Push((parent.vm, parent.childIdx, parent.expandedByUs, true));
190214
}
191215
}
192216
}

0 commit comments

Comments
 (0)