Make the player answer the keyboard and the mouse

PlayerPanel.keyPressEvent handled Space, F and Escape and was dead code:
nothing in the player called setFocusPolicy, so focus went to whichever child
button, slider or combo box was first in the tab order, and that child ate the
keys. Fixing focus alone would not have been enough -- clicking play parks
focus on the play button, which then swallows Space -- so the control bar is
explicitly NoFocus and the bindings are QShortcuts with
WidgetWithChildrenShortcut context on the panel. That fires whichever
descendant holds focus, and does not reach the Search box or the URL field on
other tabs the way an application shortcut would.

mpv's own input stays disabled. With vo=libmpv there is no mpv-owned window,
so its input layer receives nothing; enabling default bindings would mean
hand-forwarding events through a Qt-to-mpv key-name table, handing mpv the
OSD and OSC that are switched off here, and letting `q` quit the core out from
under the Qt UI. One declarative action table now drives the shortcuts, the
context menu and the buttons.

There was no mouse handling at all. Click pauses, double-click goes
fullscreen -- via a doubleClickInterval timer, so a double-click does not also
pause on the way -- the wheel changes volume and Ctrl+wheel seeks, with
sub-notch deltas accumulated so a trackpad is not inert.

The control bar gained previous, next and mute. Mute and volume are driven by
observed mpv properties rather than assumed, so the icon follows a change made
by key, menu or mpv itself. Buffering was completely invisible: a stalled
stream showed a frozen frame for 25 seconds before the retry with nothing on
screen, so paused-for-cache now surfaces a label over the video.

WatchPage owns next/previous because it owns the queue; the panel only asks.
Previous restarts the current item when it is more than five seconds in, as
every other player does, and pushes the interrupted item back onto the head of
the queue rather than dropping it.

Verified: all 38 bindings install, every action is a no-op rather than an
exception with nothing loaded, focus policies are as intended, and a
fullscreen round trip drives the player state correctly.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-09 01:04:16 +02:00
parent 3aca43b372
commit 27933dd495
6 changed files with 590 additions and 6 deletions
+44
View File
@@ -34,6 +34,9 @@ class WatchPage(QWidget):
super().__init__(parent)
self._router = router
self._queue: List[Dict[str, Any]] = []
# What has already played this session, so "previous" has somewhere
# to go. Not persisted: it is session history, not the queue.
self._played: List[Dict[str, Any]] = []
layout = QVBoxLayout(self)
layout.setContentsMargins(8, 8, 8, 8)
@@ -81,6 +84,9 @@ class WatchPage(QWidget):
self._position_timer.setInterval(POSITION_SAVE_INTERVAL_MS)
self._position_timer.timeout.connect(self._save_position)
self.player.nowPlayingChanged.connect(self._on_now_playing)
# The panel asks; the queue lives here, so the queue answers.
self.player.nextRequested.connect(self.play_next)
self.player.previousRequested.connect(self.play_previous)
self._restore_queue()
@@ -114,6 +120,39 @@ class WatchPage(QWidget):
def queue_entries(self) -> List[Dict[str, Any]]:
return [self.queue_list.item(i).data(Qt.ItemDataRole.UserRole) for i in range(self.queue_list.count())]
def play_next(self) -> None:
"""Next in the queue. Bound to N and the player's next button."""
self._play_next_from_queue()
def play_previous(self) -> None:
"""
Back to what was playing before.
Restarts the current item first if it is more than a few seconds in,
which is what every other player does with a "previous" press.
"""
if not isinstance(self.player, PlayerPanel):
return
if self.player.current_position() > 5.0:
self.player.seek_absolute(0.0)
return
# _played ends with what is playing now, so going back needs two.
if len(self._played) < 2:
return
current = self._played.pop()
previous = self._played[-1]
# Put the current item back at the head of the queue rather than
# dropping it on the floor.
if current:
item = QListWidgetItem(current.get("title") or current.get("url") or "?")
item.setData(Qt.ItemDataRole.UserRole, dict(current))
self.queue_list.insertItem(0, item)
self._queue.insert(0, dict(current))
self._persist_queue()
# play_entry re-pushes `previous`, so drop it here to avoid a double.
self._played.pop()
self.play_entry(previous)
# --------------------------------------------------------------- internal
def _play_next_from_queue(self) -> None:
@@ -148,6 +187,11 @@ class WatchPage(QWidget):
def _on_now_playing(self, entry: Dict[str, Any]) -> None:
LibraryManager.upsert_watch(entry)
# Record what we were on before this, so "previous" can return to it.
# The retry path replays the same entry; don't stack duplicates.
if self._played[-1:] != [entry] and entry:
self._played.append(dict(entry))
del self._played[:-50]
self._duration = 0.0
self._position_timer.start()