diff --git a/lib/screens/libraries/tabs/base_library_tab.dart b/lib/screens/libraries/tabs/base_library_tab.dart index 2c78a32390..f461f12887 100644 --- a/lib/screens/libraries/tabs/base_library_tab.dart +++ b/lib/screens/libraries/tabs/base_library_tab.dart @@ -224,10 +224,24 @@ abstract class BaseLibraryTabState> extends State /// snapshot keeps the epoch from reading stale here. void _onSurfaceVisibilityChanged() { if (!mounted || !_isLiveSurfaceVisible) return; + onSurfaceShown(); if (!_isLibraryContentStale) return; _livePacer.schedule(); } + /// Whether this tab is the active library tab *and* actually on screen + /// (no opaque route such as the video player covering it). + @protected + bool get isSurfaceVisible => mounted && _isLiveSurfaceVisible; + + /// Called whenever this tab becomes visible again: a covering route (the + /// video player, a detail screen) popped, its main tab was re-shown, or it + /// became the active library tab. Subclasses consume their own staleness + /// here; the default does nothing. + @protected + // ignore: no-empty-block - default no-op hook for subclasses + void onSurfaceShown() {} + @override void didUpdateWidget(W oldWidget) { super.didUpdateWidget(oldWidget); @@ -250,6 +264,7 @@ abstract class BaseLibraryTabState> extends State // Became the visible tab again: consume push-marked staleness (#1646). // The library-changed branch above already reloads unconditionally. refreshIfLibraryContentStale(); + if (_isLiveSurfaceVisible) onSurfaceShown(); } // Check if we should focus (became active after data loaded) diff --git a/lib/screens/libraries/tabs/library_recommended_tab.dart b/lib/screens/libraries/tabs/library_recommended_tab.dart index 9b0f439789..b00544f868 100644 --- a/lib/screens/libraries/tabs/library_recommended_tab.dart +++ b/lib/screens/libraries/tabs/library_recommended_tab.dart @@ -26,6 +26,7 @@ import '../../../widgets/settings_builder.dart'; import '../../../widgets/tv_browse_rail.dart'; import '../../../widgets/tv_spotlight_scaffold.dart'; import '../../main_screen.dart'; +import '../../video_player_screen.dart'; import 'base_library_tab.dart'; /// Recommended tab for library screen @@ -66,12 +67,73 @@ class _LibraryRecommendedTabState extends BaseLibraryTabState? _libraryWatchSubscription; + + /// Short debounce so the burst at player exit (route pop, final stopped + /// report, scrobble) collapses into one refetch, and the refetch runs after + /// the server has accepted the stopped report. + static const _watchStateReloadDebounce = Duration(milliseconds: 600); + static const _watchStateReloadPlayerRetry = Duration(milliseconds: 750); + + @override + void initState() { + super.initState(); + // [WatchStateAware] only delivers events for items already on screen. + // Playing something from this library that is not in Continue Watching + // yet must still bring it in, so listen library-wide just to mark stale. + _libraryWatchSubscription = WatchStateNotifier().stream.listen((event) { + if (!mounted) return; + if (event.serverId.value != widget.library.serverId) return; + if (event.librarySectionID != widget.library.id) return; + _markWatchStateStale(); + }); + } + @override void dispose() { + _libraryWatchSubscription?.cancel(); + _watchStateReloadTimer?.cancel(); _spotlight.dispose(); super.dispose(); } + void _markWatchStateStale() { + _watchStateStale = true; + if (isSurfaceVisible) _scheduleWatchStateReload(); + } + + void _scheduleWatchStateReload([Duration delay = _watchStateReloadDebounce]) { + _watchStateReloadTimer?.cancel(); + _watchStateReloadTimer = Timer(delay, _runWatchStateReload); + } + + void _runWatchStateReload() { + _watchStateReloadTimer = null; + if (!mounted || !_watchStateStale) return; + // Hidden again: the next [onSurfaceShown] picks it up. + if (!isSurfaceVisible) return; + // The player route is still tearing down (pop transition): try again + // shortly rather than racing its final stopped report. + if (VideoPlayerScreenState.activeGlobalKey != null) { + _scheduleWatchStateReload(_watchStateReloadPlayerRetry); + return; + } + _watchStateStale = false; + unawaited(performLiveLibraryRefresh()); + } + + @override + void onSurfaceShown() { + if (_watchStateStale) _scheduleWatchStateReload(); + } + @override String? get watchStateServerId => widget.library.serverId; @@ -108,7 +170,14 @@ class _LibraryRecommendedTabState extends BaseLibraryTabState[]; + for (final hub in items) { + final index = hub.items.indexWhere((item) => item.globalKey == event.globalKey); + if (index == -1 || hub.items[index].viewOffsetMs == viewOffset) { + patched.add(hub); + continue; + } + final newItems = List.from(hub.items); + newItems[index] = newItems[index].copyWith(viewOffsetMs: viewOffset); + patched.add(hub.copyWith(items: newItems)); + changed = true; + } + if (!changed || !mounted) return; + setState(() { + for (var i = 0; i < patched.length; i++) { + items[i] = patched[i]; + } + }); } void _removeContinueWatchingItem(String itemId) { diff --git a/lib/widgets/video_controls/widgets/content_strip.dart b/lib/widgets/video_controls/widgets/content_strip.dart index 040495fbb1..b7cb6188cd 100644 --- a/lib/widgets/video_controls/widgets/content_strip.dart +++ b/lib/widgets/video_controls/widgets/content_strip.dart @@ -7,7 +7,6 @@ import 'package:material_symbols_icons/symbols.dart'; import 'package:provider/provider.dart'; import '../../../focus/dpad_navigator.dart'; -import '../../../focus/focus_theme.dart'; import '../../../focus/focusable_wrapper.dart'; import '../../../i18n/strings.g.dart'; import '../../../media/media_item.dart'; @@ -29,6 +28,10 @@ import '../../optimized_media_image.dart'; import '../../settings_builder.dart'; import 'media_selector_thumbnail.dart'; +/// Focused chapter/episode thumbnails grow a little more than library cards +/// so the selection reads at TV distance over video. +const double _focusedStripItemScale = 1.05; + /// Horizontal scrollable strip of chapter/queue items shown on swipe-up. class ContentStrip extends StatefulWidget { final Player player; @@ -441,8 +444,10 @@ class ContentStripState extends State { }, borderRadius: 6, autoScroll: false, - useBackgroundFocus: true, - focusColor: FocusTheme.onMediaFocusFill, + // A solid white outline over video: the translucent fill sat + // behind the thumbnail and was barely visible on dark frames. + focusColor: Colors.white, + focusScale: _focusedStripItemScale, child: item, ), ); diff --git a/lib/widgets/video_controls/widgets/player_focus_disc.dart b/lib/widgets/video_controls/widgets/player_focus_disc.dart index 107a30627b..bfa5b5ac47 100644 --- a/lib/widgets/video_controls/widgets/player_focus_disc.dart +++ b/lib/widgets/video_controls/widgets/player_focus_disc.dart @@ -63,17 +63,35 @@ class PlayerFocusDisc extends StatelessWidget { const BoxShadow playerFocusShadow = BoxShadow(color: Color(0x59000000), blurRadius: 14, offset: Offset(0, 4)); /// Radius of the knob a focused timeline shows in place of its handle. -const double playerFocusKnobRadius = 9; +const double playerFocusKnobRadius = 11; + +/// Gap between the knob and its focus halo ring, and the ring's stroke. +const double _playerFocusHaloGap = 4; +const double _playerFocusHaloWidth = 2.5; /// Paints the focused-timeline knob: a white disc with the same shadow as /// [PlayerFocusDisc], shared by the VOD slider thumb and the live-TV painter. +/// +/// The played part of the track is white too, so a plain white disc blended +/// into it and the timeline barely looked selected. A dark edge separates the +/// knob from the track and a white halo ring marks it as the focused control, +/// readable on dark (OLED) and bright frames alike. void paintPlayerFocusKnob(Canvas canvas, Offset center) { canvas.drawCircle( center + playerFocusShadow.offset / 2, - playerFocusKnobRadius, + playerFocusKnobRadius + _playerFocusHaloGap, Paint() ..color = playerFocusShadow.color ..maskFilter = MaskFilter.blur(BlurStyle.normal, playerFocusShadow.blurSigma), ); + canvas.drawCircle( + center, + playerFocusKnobRadius + _playerFocusHaloGap + _playerFocusHaloWidth / 2, + Paint() + ..style = PaintingStyle.stroke + ..strokeWidth = _playerFocusHaloWidth + ..color = Colors.white.withValues(alpha: 0.9), + ); + canvas.drawCircle(center, playerFocusKnobRadius + 1.5, Paint()..color = Colors.black.withValues(alpha: 0.6)); canvas.drawCircle(center, playerFocusKnobRadius, Paint()..color = Colors.white); } diff --git a/lib/widgets/video_controls/widgets/skip_marker_button.dart b/lib/widgets/video_controls/widgets/skip_marker_button.dart index 529302912f..caf132b877 100644 --- a/lib/widgets/video_controls/widgets/skip_marker_button.dart +++ b/lib/widgets/video_controls/widgets/skip_marker_button.dart @@ -2,12 +2,14 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart' show KeyDownEvent, LogicalKeyboardKey; import 'package:material_symbols_icons/symbols.dart'; +import '../../../focus/card_focus_scope.dart'; import '../../../focus/focus_theme.dart'; import '../../../focus/focusable_wrapper.dart'; import '../../../i18n/strings.g.dart'; import '../../../media/media_source_info.dart'; import '../../../theme/mono_tokens.dart'; import '../../app_icon.dart'; +import 'player_focus_disc.dart'; class SkipMarkerButton extends StatelessWidget { final MediaMarker marker; @@ -59,13 +61,17 @@ class SkipMarkerButton extends StatelessWidget { ? '$baseButtonText ($remainingSeconds)' : baseButtonText; final buttonIcon = showNextEpisode ? Symbols.skip_next_rounded : Symbols.fast_forward_rounded; + final radius = tokens(context).radiusSm; return FocusableWrapper( focusNode: focusNode, onSelect: _activate, - borderRadius: tokens(context).radiusSm, - useBackgroundFocus: true, - focusColor: FocusTheme.onMediaFocusFill, + borderRadius: radius, + // The button paints its own focus state: a translucent fill behind an + // already-white pill was invisible, so D-pad users could not tell the + // button from the timeline or the transport row as the selection. + delegateFocusBorder: true, + focusScale: _focusScale, autoScroll: false, onKeyEvent: (node, event) { if (event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.arrowDown) { @@ -74,59 +80,124 @@ class SkipMarkerButton extends StatelessWidget { } return KeyEventResult.ignored; }, - child: Material( - color: Colors.transparent, - child: InkWell( - onTap: _activate, - borderRadius: BorderRadius.circular(tokens(context).radiusSm), - child: Stack( - children: [ - Container( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), - decoration: BoxDecoration( - color: Colors.white.withValues(alpha: 0.9), - borderRadius: BorderRadius.circular(tokens(context).radiusSm), - boxShadow: [ - BoxShadow(color: Colors.black.withValues(alpha: 0.3), blurRadius: 8, offset: const Offset(0, 2)), - ], - ), - child: Row( - mainAxisSize: .min, - children: [ - Text( - buttonText, - style: const TextStyle(color: Colors.black, fontSize: 16, fontWeight: .w600), - ), - const SizedBox(width: 8), - AppIcon(buttonIcon, fill: 1, color: Colors.black, size: 20), - ], + child: Builder( + builder: (context) { + // Null outside keyboard/D-pad mode: pointer users keep the solid + // white pill. In D-pad mode the pill is solid white with a ring only + // while focused, and a dark outlined pill otherwise. + final keyboardFocus = CardFocusScope.maybeOf(context); + final isDpad = keyboardFocus != null; + final isFocused = keyboardFocus ?? false; + final isSolid = !isDpad || isFocused; + final foreground = isSolid ? Colors.black : Colors.white; + final duration = FocusTheme.getAnimationDuration(context); + + return AnimatedContainer( + duration: duration, + curve: Curves.easeOutCubic, + padding: const EdgeInsets.all(_ringGap), + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(radius + _ringGap + _ringWidth), + border: Border.all( + color: isFocused ? Colors.white : Colors.transparent, + width: _ringWidth, + strokeAlign: BorderSide.strokeAlignOutside, + ), + ), + child: _buildPill( + context, + radius: radius, + duration: duration, + isSolid: isSolid, + isFocused: isFocused, + foreground: foreground, + buttonText: buttonText, + buttonIcon: buttonIcon, + ), + ); + }, + ), + ); + } + + static const double _focusScale = 1.08; + static const double _ringWidth = 3; + static const double _ringGap = 3; + + Widget _buildPill( + BuildContext context, { + required double radius, + required Duration duration, + required bool isSolid, + required bool isFocused, + required Color foreground, + required String buttonText, + required IconData buttonIcon, + }) { + return Material( + color: Colors.transparent, + child: InkWell( + onTap: _activate, + borderRadius: BorderRadius.circular(radius), + child: Stack( + children: [ + AnimatedContainer( + duration: duration, + curve: Curves.easeOutCubic, + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), + decoration: BoxDecoration( + color: isSolid ? Colors.white : Colors.black.withValues(alpha: 0.6), + borderRadius: BorderRadius.circular(radius), + border: Border.all( + color: isSolid ? Colors.transparent : Colors.white.withValues(alpha: 0.55), + width: 1.5, ), + boxShadow: [ + isFocused + ? playerFocusShadow + : BoxShadow( + color: Colors.black.withValues(alpha: 0.3), + blurRadius: 8, + offset: const Offset(0, 2), + ), + ], + ), + child: Row( + mainAxisSize: .min, + children: [ + Text( + buttonText, + style: TextStyle(color: foreground, fontSize: 16, fontWeight: .w600), + ), + const SizedBox(width: 8), + AppIcon(buttonIcon, fill: 1, color: foreground, size: 20), + ], ), - if (isAutoSkipActive && shouldShowAutoSkip) - Positioned.fill( - child: ClipRRect( - borderRadius: BorderRadius.circular(tokens(context).radiusSm), - child: Row( - children: [ - Expanded( - flex: (autoSkipProgress * 100).round(), - child: Container( - decoration: BoxDecoration( - color: Theme.of(context).colorScheme.primary.withValues(alpha: 0.2), - borderRadius: BorderRadius.circular(tokens(context).radiusSm), - ), + ), + if (isAutoSkipActive && shouldShowAutoSkip) + Positioned.fill( + child: ClipRRect( + borderRadius: BorderRadius.circular(tokens(context).radiusSm), + child: Row( + children: [ + Expanded( + flex: (autoSkipProgress * 100).round(), + child: Container( + decoration: BoxDecoration( + color: Theme.of(context).colorScheme.primary.withValues(alpha: 0.2), + borderRadius: BorderRadius.circular(tokens(context).radiusSm), ), ), - Expanded( - flex: ((1.0 - autoSkipProgress) * 100).round(), - child: Container(decoration: const BoxDecoration(color: Colors.transparent)), - ), - ], - ), + ), + Expanded( + flex: ((1.0 - autoSkipProgress) * 100).round(), + child: Container(decoration: const BoxDecoration(color: Colors.transparent)), + ), + ], ), ), - ], - ), + ), + ], ), ), ); diff --git a/test/screens/libraries/libraries_screen_test.dart b/test/screens/libraries/libraries_screen_test.dart index 17843ee295..dfe3c87209 100644 --- a/test/screens/libraries/libraries_screen_test.dart +++ b/test/screens/libraries/libraries_screen_test.dart @@ -43,6 +43,7 @@ import 'package:plezy/theme/mono_theme.dart'; import 'package:plezy/utils/desktop_window_padding.dart'; import 'package:plezy/utils/media_server_http_client.dart'; import 'package:plezy/utils/library_content_notifier.dart'; +import 'package:plezy/utils/watch_state_notifier.dart'; import 'package:plezy/media/library_change_event.dart'; import 'package:plezy/widgets/backend_badge.dart'; import 'package:plezy/widgets/focusable_tab_chip.dart'; @@ -496,6 +497,37 @@ void main() { expect(_focusedHub(), 'B'); }); + testWidgets('Recommended refetches once playback progress landed while a route covered it', (tester) async { + final client = _HubClient(); + final harness = await _Harness.create( + _GatedPreferences({'selected_library_key': _libraryA.globalKey}), + clients: [client], + libraryOrder: const [_libraryA], + ); + addTearDown(harness.dispose); + await harness.pump(tester); + final callsBefore = client.libraryHubCalls; + final played = client.hubs.first.items.first; + + // Stand-in for the video player: an opaque route over the library. + final navigator = Navigator.of(tester.element(find.byType(LibraryRecommendedTab))); + unawaited(navigator.push(MaterialPageRoute(builder: (_) => const Scaffold(body: Text('Player'))))); + await tester.pumpAndSettle(); + + WatchStateNotifier().notifyProgress(item: played, viewOffset: 60000, duration: 600000); + await tester.pump(const Duration(seconds: 2)); + expect(client.libraryHubCalls, callsBefore, reason: 'a covered tab must not fetch'); + + navigator.pop(); + await tester.pumpAndSettle(); + await tester.pump(const Duration(seconds: 1)); + await tester.pumpAndSettle(); + expect(client.libraryHubCalls, callsBefore + 1, reason: 'returning from playback refreshes Continue Watching'); + + await tester.pump(const Duration(seconds: 2)); + expect(client.libraryHubCalls, callsBefore + 1, reason: 'the staleness is consumed by one refetch'); + }); + testWidgets('entering the screen while its tab loads focuses the content once it lands', (tester) async { final client = _HubClient(); final harness = await _Harness.create(