A bit of a refactor, attempt to be nicer to servers and still load quickly.

This commit is contained in:
Storm Dragon
2025-12-16 02:38:05 -05:00
parent 5507aa8faa
commit 1658321a4b
3 changed files with 609 additions and 232 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -18,6 +18,7 @@ from PySide6.QtWidgets import (
QPushButton,
QTreeWidgetItem,
QVBoxLayout,
QAbstractItemView,
)
from src.api.client import SubsonicClient
@@ -75,6 +76,12 @@ class SearchDialog(QDialog):
self.resultsTree.setHeaderHidden(True)
self.resultsTree.setRootIsDecorated(True)
self.resultsTree.setContextMenuPolicy(Qt.CustomContextMenu)
self.resultsTree.setUniformRowHeights(True)
self.resultsTree.setItemsExpandable(True)
self.resultsTree.setExpandsOnDoubleClick(True)
self.resultsTree.setSelectionMode(QAbstractItemView.SingleSelection)
self.resultsTree.setAllColumnsShowFocus(True)
self.resultsTree.setFocusPolicy(Qt.StrongFocus)
self.resultsTree.itemActivated.connect(self.handleActivate)
self.resultsTree.itemExpanded.connect(self.onItemExpanded)
self.resultsTree.customContextMenuRequested.connect(self.showContextMenu)
@@ -112,10 +119,7 @@ class SearchDialog(QDialog):
self.populateResults(results)
if self._resultsLoaded:
self.resultsTree.setFocus()
first = self.resultsTree.topLevelItem(0)
if first:
self.resultsTree.setCurrentItem(first)
self._focusFirstResult()
self.statusLabel.setText("Search complete. Use the tree to play or queue items.")
else:
self.statusLabel.setText("No results found.")
@@ -123,6 +127,15 @@ class SearchDialog(QDialog):
AccessibleTextDialog.showError("Search Error", str(e), parent=self)
self.statusLabel.setText("Search failed.")
def _focusFirstResult(self):
"""Move focus to the first result item for immediate keyboard navigation."""
self.resultsTree.setFocus(Qt.OtherFocusReason)
first = self.resultsTree.topLevelItem(0)
if not first:
return
target = first.child(0) if first.childCount() else first
self.resultsTree.setCurrentItem(target)
def populateResults(self, results: Dict[str, List]):
"""Populate the results tree from search3 data."""
artists: List[Artist] = results.get("artists", [])
@@ -131,8 +144,7 @@ class SearchDialog(QDialog):
if artists:
root = QTreeWidgetItem([f"Artists ({len(artists)})"])
root.setData(0, Qt.UserRole, {"type": "artists_root"})
root.setExpanded(True)
root.setData(0, Qt.UserRole, {"type": "artists_root", "loaded": True})
for artist in artists:
item = QTreeWidgetItem([artist.name])
item.setData(0, Qt.UserRole, {"type": "artist", "id": artist.id, "artist": artist, "loaded": False})
@@ -140,12 +152,12 @@ class SearchDialog(QDialog):
item.addChild(QTreeWidgetItem(["Loading..."]))
root.addChild(item)
self.resultsTree.addTopLevelItem(root)
self.resultsTree.expandItem(root)
self._resultsLoaded = True
if albums:
root = QTreeWidgetItem([f"Albums ({len(albums)})"])
root.setData(0, Qt.UserRole, {"type": "albums_root"})
root.setExpanded(True)
root.setData(0, Qt.UserRole, {"type": "albums_root", "loaded": True})
for album in albums:
label = f"{album.name}{album.artist} ({album.songCount} tracks)"
item = QTreeWidgetItem([label])
@@ -154,6 +166,7 @@ class SearchDialog(QDialog):
item.addChild(QTreeWidgetItem(["Loading..."]))
root.addChild(item)
self.resultsTree.addTopLevelItem(root)
self.resultsTree.expandItem(root)
self._resultsLoaded = True
if songs:
@@ -164,8 +177,7 @@ class SearchDialog(QDialog):
if not songs:
return
root = QTreeWidgetItem([rootLabel])
root.setData(0, Qt.UserRole, {"type": "songs_root"})
root.setExpanded(True)
root.setData(0, Qt.UserRole, {"type": "songs_root", "loaded": True})
for song in songs:
text = f"{song.title}{song.artist} ({song.album}, {song.durationFormatted})"
item = QTreeWidgetItem([text])
@@ -174,6 +186,7 @@ class SearchDialog(QDialog):
root.addChild(item)
self.resultsTree.addTopLevelItem(root)
self._resultsLoaded = True
self.resultsTree.expandItem(root)
def handleActivate(self, item: QTreeWidgetItem):
"""Activate the selected item similar to the main tree."""
@@ -230,6 +243,8 @@ class SearchDialog(QDialog):
item.addChild(child)
data["loaded"] = True
item.setData(0, Qt.UserRole, data)
# Update accessibility for newly added children
self.resultsTree.updateChildAccessibility(item, True)
except Exception as e:
AccessibleTextDialog.showError("Search Error", str(e), parent=self)