Open on the Feed, and refresh it without hammering yt-dlp
The tab order was Watch, Search, Feed, Browse, Downloads, and the app opened on Watch -- which is where the other tabs send you, not somewhere you start. It is now Feed, Search, Browse, Downloads, Watch, with icons. Routing is unaffected: _tab_index_of resolves by widget identity, not position. Refresh-on-open needed care rather than a call in showEvent. A local refresh is one yt-dlp subprocess per subscribed channel, serially, so it is braked four ways: only channels not seen for feed.auto_refresh_on_open_minutes (30, 0 to disable), at most eight per visit, once per interval per session, and after a delay so it does not race the tab transition or first-run setup. The cached feed is already on screen throughout. That is a new config key rather than the dead auto_refresh_minutes, because existing configs store that as 0 and would have read as opting out of a feature that did not exist yet. SmoothTabWidget gained the currentChanged signal, icons and a corner slot it never had. Its set_current_index needed a re-entrancy flag, not just an index check: setting the tab bar's index emits its currentChanged straight back into the same method, and at that point the stack has not moved, so every switch fired the activation hook twice. The grid was cleared and rebuilt after every channel finished -- flicker, lost scroll position and every thumbnail re-read from disk each time. merge_entries keeps existing cards. While there, _relayout was re-adding cards the layout already owned, so layout items accumulated on every Load more, and the resize check compared against columnCount(), which never shrinks, so it relaid out on every resize event. Smaller things this exposed: feed errors were written into the label the next success overwrote, so failures were invisible; switching to account mode left the local videos on screen; cancel() had no callers, so a refresh outlived the tab and the window; Browse's Subscribe never said Unsubscribe though it toggles; "Play all" queued one page while claiming otherwise; and the feed sorted publish times against wall-clock fetch times in one COALESCE, so the last-refreshed channel floated to the top. Feed is the first thing seen now, so an empty one says what to do about it instead of showing a bare grid. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -207,6 +207,9 @@ class VideoCardGrid(QScrollArea):
|
||||
super().__init__(parent)
|
||||
self._router = router
|
||||
self._cards: List[VideoCard] = []
|
||||
#: video id -> card, so merge_entries can tell new from existing
|
||||
self._by_key: Dict[str, "VideoCard"] = {}
|
||||
self._columns_in_use = 0
|
||||
|
||||
self.setWidgetResizable(True)
|
||||
self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
||||
@@ -246,15 +249,56 @@ class VideoCardGrid(QScrollArea):
|
||||
for entry in entries:
|
||||
card = VideoCard(entry, self._router, self._grid_widget)
|
||||
self._cards.append(card)
|
||||
self._by_key[self._key(entry)] = card
|
||||
self._relayout()
|
||||
self.load_more_btn.setVisible(show_load_more)
|
||||
self.empty_label.setVisible(not self._cards)
|
||||
|
||||
@staticmethod
|
||||
def _key(entry: Dict[str, Any]) -> str:
|
||||
return str(entry.get("id") or entry.get("url") or id(entry))
|
||||
|
||||
def merge_entries(self, entries: List[Dict[str, Any]], prune: bool = False) -> None:
|
||||
"""
|
||||
Fold entries in, keeping cards that are already here.
|
||||
|
||||
set_entries() destroys and rebuilds every card, which during a feed
|
||||
refresh happened once per channel: the grid flickered, the scroll
|
||||
position was lost each time, and every thumbnail was re-read from
|
||||
disk. Existing cards are left alone here, so only genuinely new
|
||||
videos cost anything.
|
||||
"""
|
||||
incoming = {self._key(e): e for e in entries}
|
||||
scroll = self.verticalScrollBar().value()
|
||||
self.setUpdatesEnabled(False)
|
||||
try:
|
||||
for key, entry in incoming.items():
|
||||
if key in self._by_key:
|
||||
continue
|
||||
card = VideoCard(entry, self._router, self._grid_widget)
|
||||
self._cards.append(card)
|
||||
self._by_key[key] = card
|
||||
|
||||
if prune:
|
||||
for key in [k for k in self._by_key if k not in incoming]:
|
||||
card = self._by_key.pop(key)
|
||||
if card in self._cards:
|
||||
self._cards.remove(card)
|
||||
self._grid.removeWidget(card)
|
||||
card.deleteLater()
|
||||
|
||||
self._relayout()
|
||||
self.empty_label.setVisible(not self._cards)
|
||||
finally:
|
||||
self.setUpdatesEnabled(True)
|
||||
self.verticalScrollBar().setValue(scroll)
|
||||
|
||||
def clear(self) -> None:
|
||||
for card in self._cards:
|
||||
self._grid.removeWidget(card)
|
||||
card.deleteLater()
|
||||
self._cards = []
|
||||
self._by_key.clear()
|
||||
self.empty_label.setVisible(True)
|
||||
self.load_more_btn.setVisible(False)
|
||||
|
||||
@@ -269,10 +313,19 @@ class VideoCardGrid(QScrollArea):
|
||||
|
||||
def _relayout(self) -> None:
|
||||
cols = self._columns()
|
||||
# Drain first. addWidget on a card the layout already owns adds a
|
||||
# second item for it, so the layout grew an extra entry per card on
|
||||
# every append. takeAt detaches without deleting the widget.
|
||||
while self._grid.count():
|
||||
self._grid.takeAt(0)
|
||||
for i, card in enumerate(self._cards):
|
||||
self._grid.addWidget(card, i // cols, i % cols)
|
||||
self._columns_in_use = cols
|
||||
|
||||
def resizeEvent(self, event) -> None:
|
||||
super().resizeEvent(event)
|
||||
if self._cards and self._columns() != self._grid.columnCount():
|
||||
# Compared against what was actually laid out, not columnCount():
|
||||
# QGridLayout never shrinks its column count, so that comparison
|
||||
# stayed true forever and relaid out on every resize event.
|
||||
if self._cards and self._columns() != self._columns_in_use:
|
||||
self._relayout()
|
||||
|
||||
Reference in New Issue
Block a user