@@ -150,19 +150,34 @@ def open_file_source_extension(self, file_extension, filepath, file):
150150
151151 def search_files (self , event = None ):
152152 """
153- Search for files in the Treeview based on the entry text.
153+ Display only files in the Treeview that contain the search text.
154154 """
155155 search_text = self .search_entry .get ().lower ()
156156 if search_text :
157- for item in self .tree .get_children ():
158- self .tree .item (item , open = True ) # Expand all nodes to ensure search accuracy
159- if search_text in self .tree .item (item , "text" ).lower ():
160- self .tree .selection_set (item )
161- self .tree .focus (item )
162- self .tree .see (item )
163- else :
164- self .tree .selection_remove (item )
157+ self .clear_selections () # Clear previous selections
158+ self .search_tree (self .tree .get_children (), search_text )
165159 else :
166- for item in self .tree .get_children ():
167- self .tree .selection_remove (item )
160+ self .clear_selections () # Clear selections if search text is empty
161+
162+ def search_tree (self , items , search_text ):
163+ """
164+ Recursively search through the tree and select items containing the search text.
168165
166+ Args:
167+ items (list): A list of items in the tree.
168+ search_text (str): The text to search for.
169+ """
170+ for item in items :
171+ item_text = self .tree .item (item , "text" ).lower ()
172+ if search_text in item_text :
173+ self .tree .selection_add (item )
174+ self .tree .focus (item )
175+ self .tree .see (item )
176+ self .search_tree (self .tree .get_children (item ), search_text ) # Recursively search subitems
177+
178+ def clear_selections (self ):
179+ """
180+ Clear all selections in the Treeview.
181+ """
182+ for item in self .tree .selection ():
183+ self .tree .selection_remove (item )
0 commit comments