Files
plezy/lib/widgets/video_controls/parts/markers.dart
T
edde746 e3703892b3 fix(player): keep a keyboard Enter out of focus navigation
Pressing Enter over the player put the whole app into keyboard mode and
dropped focus onto Play/Pause, even with Video Player Navigation off. Two
independent paths did it. InputModeTracker promoted on any key satisfying
isNavigationKey, a set that unioned activation, dismissal and the menu key
with the arrows and consulted no setting at all; separately the surface's
Select handler always asked the chrome for focus. Escape had the same effect,
which on desktop reads as the mouse cursor vanishing mid-playback.

Both now ask one predicate. eventRequestsFocusNavigation decides whether the
app switches to keyboard mode and whether a key may hand focus to the chrome,
so the two cannot disagree and focus can never land on a control while focus
chrome is still suppressed. Activation and dismissal act on what already has
focus, so they answer no; Tab, the menu key, a remote's OK or BACK, and an
arrow that will really traverse answer yes. The one input the predicate cannot
read off the event, whether the focused feature owns arrow keys, rides on the
node as DirectionalShortcutFocusNode instead of on a subtree, so every sheet,
prompt and OSD button stays an ordinary traversal target with nothing to
re-enable.

playerDirectionalNavigationEnabled and videoPlayerNavigationPreference replace
five hand-copied pref-or-isTV expressions and a screen-level cache that
disagreed with the live getter after a toggle. Services whose input is
synthesized past HardwareKeyboard announce themselves through
InputModeTracker.reportNonPointerInput rather than two static callbacks and
three copies of a highlight-strategy write. That registration is now
identity-guarded: the bootstrap-to-app tree swap disposed the outgoing tracker
after the incoming one initialised and cleared both callbacks, so gamepad and
companion remote input had stopped switching to keyboard mode entirely.

Falling out of the same rule: a companion heartbeat no longer flips an idle
desktop host into keyboard mode, analog-stick drift promotes only past the
deadzone that actually navigates, Enter keeps toggling playback once the
chrome is up, Tab both reaches and traverses the OSD, and the player surface
claims the remote from mount rather than only when the chrome starts hidden,
so the first key on a desktop route is a playback shortcut instead of the
screen node's chrome-raising self-heal.

isNavigationKey becomes isReservedControlKey, since its real meaning is a
shell key rather than a text character and the old name is what invited the
conflation. The unreachable PlayerChromeFocusTarget.timeline goes with it.
2026-08-07 13:23:53 +02:00

236 lines
7.0 KiB
Dart

part of '../video_controls.dart';
extension _PlexVideoControlsMarkerMethods on _PlexVideoControlsState {
void _listenToPosition() {
_positionSubscription = widget.player.streams.position.listen((position) {
_syncCurrentMarkerForPosition(position);
});
}
void _syncCurrentMarkerForCurrentPosition() {
_syncCurrentMarkerForPosition(widget.player.state.position);
}
void _syncCurrentMarkerForPosition(Duration position) {
if (!_hasRenderedFirstFrame || _markers.isEmpty || !_markersLoaded) {
_clearCurrentMarker();
return;
}
MediaMarker? foundMarker;
for (final marker in _markers) {
if (marker.containsPosition(position)) {
foundMarker = marker;
break;
}
}
if (foundMarker != _currentMarker && mounted) {
_updateCurrentMarker(foundMarker);
}
}
void _clearCurrentMarker() {
final hasMarkerState =
_currentMarker != null ||
_skipButtonDismissed ||
_autoSkipTimer != null ||
_autoSkipProgress != 0.0 ||
_skipButtonDismissTimer != null;
if (!hasMarkerState) return;
if (_currentMarker != null || _skipButtonDismissed) {
_setControlsState(() {
_currentMarker = null;
_skipButtonDismissed = false;
});
}
if (_skipMarkerFocusNode.hasFocus) _skipMarkerFocusNode.unfocus();
_cancelAutoSkipTimer();
_cancelSkipButtonDismissTimer();
}
/// Updates the current marker and manages auto-skip/focus behavior.
void _updateCurrentMarker(MediaMarker? foundMarker) {
if (!_hasRenderedFirstFrame) {
_clearCurrentMarker();
return;
}
if (foundMarker == null) {
_clearCurrentMarker();
return;
}
_setControlsState(() {
_currentMarker = foundMarker;
_skipButtonDismissed = false;
});
_startAutoSkipTimer(foundMarker);
// Auto-skip OFF: dismiss button after 7s if no interaction
// Auto-skip ON: button stays until controls hide
if (!_shouldAutoSkipForMarker(foundMarker)) {
_startSkipButtonDismissTimer();
}
// Auto-focus skip button on TV when marker appears (only in keyboard/TV mode)
if (PlatformDetector.isTV() && InputModeTracker.isKeyboardMode(context, listen: false)) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
_skipMarkerFocusNode.requestFocus();
}
});
}
}
Future<void> _skipMarker({bool skipAutoPlayCountdown = false}) async {
if (!widget.canControl) return;
if (_currentMarker == null || !_hasRenderedFirstFrame) return;
final marker = _currentMarker!;
final endTime = marker.endTime;
final duration = widget.player.state.duration;
final isAtEnd = duration > Duration.zero && (duration - endTime).inMilliseconds <= 1000;
if (marker.isCredits && isAtEnd) {
if (!skipAutoPlayCountdown && widget.onNext != null) {
_abandoningBurst(widget.onNext)!.call();
} else {
// Seeking to EOF is unreliable due to position stream throttling,
// so pause and defer to the parent's completion flow.
await widget.player.pause();
widget.onReachedEnd?.call(skipAutoPlayCountdown: skipAutoPlayCountdown);
}
} else {
await _seekToPosition(endTime);
}
if (!mounted) return;
_setControlsState(() {
_currentMarker = null;
});
_cancelAutoSkipTimer();
_cancelSkipButtonDismissTimer();
}
void _startAutoSkipTimer(MediaMarker marker) {
_cancelAutoSkipTimer();
if (!_hasRenderedFirstFrame) return;
final shouldAutoSkip = (marker.isCredits && _autoSkipCredits) || (!marker.isCredits && _autoSkipIntro);
if (!shouldAutoSkip || _autoSkipDelay <= 0) return;
_autoSkipProgress = 0.0;
const tickDuration = Duration(milliseconds: 200);
final totalTicks = (_autoSkipDelay * 1000) / tickDuration.inMilliseconds;
if (totalTicks <= 0) return;
_autoSkipTimer = Timer.periodic(tickDuration, (timer) {
if (!mounted || _currentMarker != marker) {
timer.cancel();
return;
}
_setControlsState(() {
_autoSkipProgress = (timer.tick / totalTicks).clamp(0.0, 1.0);
});
if (timer.tick >= totalTicks) {
timer.cancel();
_performAutoSkip(skipAutoPlayCountdown: true);
}
});
}
void _cancelAutoSkipTimer() {
final hadTimer = _autoSkipTimer != null;
_autoSkipTimer?.cancel();
_autoSkipTimer = null;
if (mounted && (hadTimer || _autoSkipProgress != 0.0)) {
_setControlsState(() {
_autoSkipProgress = 0.0;
});
}
}
bool _cancelAutoSkipFromUserInteraction() {
final hadActiveTimer = _autoSkipTimer?.isActive ?? false;
if (!hadActiveTimer) return false;
_cancelAutoSkipTimer();
if (_currentMarker != null && !_skipButtonDismissed) {
_startSkipButtonDismissTimer();
}
return true;
}
/// Starts/restarts the skip button dismiss timer. When it fires, hides the
/// button and cancels any active auto-skip countdown.
void _startSkipButtonDismissTimer() {
_skipButtonDismissTimer?.cancel();
if (!_hasRenderedFirstFrame) return;
_skipButtonDismissTimer = Timer(const Duration(seconds: 7), () {
if (!mounted || _currentMarker == null) return;
_setControlsState(() {
_skipButtonDismissed = true;
});
_cancelAutoSkipTimer();
});
}
void _cancelSkipButtonDismissTimer() {
_skipButtonDismissTimer?.cancel();
_skipButtonDismissTimer = null;
}
/// Perform the appropriate skip action based on marker type and next episode availability
void _performAutoSkip({bool skipAutoPlayCountdown = false}) {
if (!widget.canControl) return;
if (_currentMarker == null || !_hasRenderedFirstFrame) return;
unawaited(_skipMarker(skipAutoPlayCountdown: skipAutoPlayCountdown));
}
bool _shouldAutoSkipForMarker(MediaMarker marker) {
return (marker.isCredits && _autoSkipCredits) || (!marker.isCredits && _autoSkipIntro);
}
bool _shouldShowAutoSkip() {
if (_currentMarker == null) return false;
return _shouldAutoSkipForMarker(_currentMarker!);
}
bool get _isSkipMarkerButtonVisible => shouldShowSkipMarkerButton(
hasFirstFrame: _hasRenderedFirstFrame,
hasMarker: _currentMarker != null,
hasPlayNextPrompt: widget.playNextFocusNode != null,
skipButtonDismissed: _skipButtonDismissed,
controlsVisible: _showControls,
);
void _activateSkipMarker() {
if (!_isSkipMarkerButtonVisible) return;
_cancelAutoSkipTimer();
_performAutoSkip();
}
Widget _buildSkipMarkerButton() {
final isAutoSkipActive = _autoSkipTimer?.isActive ?? false;
return SkipMarkerButton(
marker: _currentMarker!,
playerDuration: widget.player.state.duration,
hasNextEpisode: widget.onNext != null,
isAutoSkipActive: isAutoSkipActive,
shouldShowAutoSkip: _shouldShowAutoSkip(),
autoSkipDelay: _autoSkipDelay,
autoSkipProgress: _autoSkipProgress,
focusNode: _skipMarkerFocusNode,
onActivate: _activateSkipMarker,
onFocusDown: () => _desktopControlsKey.currentState?.requestPlayPauseFocus(),
);
}
}