Skip to content

Commit 2b26e9d

Browse files
committed
Fixed library not being restored on clearing of the searchbar
1 parent 3baec05 commit 2b26e9d

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed

mixtaper.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -569,26 +569,71 @@ def move_selected(self, direction):
569569

570570
def filter_library(self, *args):
571571
search_term = self.search_var.get().lower()
572-
572+
573573
if not search_term:
574-
# If no search term, restore full hierarchy
574+
# Restore full hierarchical structure when search is cleared
575575
self.library_tree.delete(*self.library_tree.get_children())
576-
# We'd need to rebuild from self.library, but for simplicity,
577-
# let's just reload the library when search is cleared
576+
577+
# Rebuild the folder hierarchy from the original library
578578
if hasattr(self, '_original_library'):
579-
self.library_tree.delete(*self.library_tree.get_children())
579+
folder_nodes = {}
580+
580581
for song in self._original_library:
581-
# Rebuild logic would go here
582-
pass
582+
# Get relative path from library root
583+
if self.library_root and 'folder' in song:
584+
full_folder_path = song['folder']
585+
try:
586+
if full_folder_path.startswith(self.library_root):
587+
relative_path = os.path.relpath(full_folder_path, self.library_root)
588+
else:
589+
relative_path = os.path.basename(full_folder_path)
590+
except:
591+
relative_path = os.path.basename(full_folder_path)
592+
else:
593+
relative_path = os.path.basename(os.path.dirname(song['path']))
594+
595+
if relative_path == '.':
596+
relative_path = os.path.basename(self.library_root) if self.library_root else "Music"
597+
598+
folder_parts = relative_path.split(os.sep)
599+
current_parent = ""
600+
601+
# Build folder hierarchy
602+
for i, part in enumerate(folder_parts):
603+
parent_path = os.sep.join(folder_parts[:i])
604+
current_path = os.sep.join(folder_parts[:i+1])
605+
606+
if current_path not in folder_nodes:
607+
if i == 0:
608+
node = self.library_tree.insert("", "end", text=part, values=("", ""))
609+
else:
610+
parent_node = folder_nodes[parent_path]
611+
node = self.library_tree.insert(parent_node, "end", text=part, values=("", ""))
612+
folder_nodes[current_path] = node
613+
614+
# Add song to folder
615+
parent_node = folder_nodes.get(relative_path, "")
616+
if parent_node:
617+
self.library_tree.insert(
618+
parent_node, "end",
619+
text="",
620+
values=(song['name'], self.format_time(song['duration'])),
621+
tags=("song",)
622+
)
623+
624+
self.library_tree.tag_configure("song", foreground="black")
625+
# Expand first level folders
626+
for child in self.library_tree.get_children():
627+
self.library_tree.item(child, open=False)
583628
return
584-
629+
585630
# Store original library if first search
586631
if not hasattr(self, '_original_library'):
587632
self._original_library = self.library.copy()
588-
589-
# Clear and show only matching songs
633+
634+
# Clear and show only matching songs in a flat list during search
590635
self.library_tree.delete(*self.library_tree.get_children())
591-
636+
592637
for song in self._original_library:
593638
if search_term in song['name'].lower():
594639
# Show matching songs in a flat list during search

0 commit comments

Comments
 (0)