diff --git a/lib/screens/discover_screen.dart b/lib/screens/discover_screen.dart index 14c096aa..ee31c6be 100644 --- a/lib/screens/discover_screen.dart +++ b/lib/screens/discover_screen.dart @@ -166,17 +166,43 @@ class _DiscoverScreenState extends State return keys; } + bool get _isHeroSectionVisible => _onDeck.isNotEmpty && context.read().showHeroSection; + + void _scrollToTop() { + if (!_scrollController.hasClients) return; + _scrollController.animateTo(0, duration: const Duration(milliseconds: 200), curve: Curves.easeOut); + } + + void _focusTopBoundary() { + if (_isHeroSectionVisible) { + _heroFocusNode.requestFocus(); + } else { + _refreshButtonFocusNode.requestFocus(); + } + _scrollToTop(); + } + + void _focusContentFromAppBar() { + if (_isHeroSectionVisible) { + _heroFocusNode.requestFocus(); + return; + } + + final keys = _allHubKeys; + if (keys.isNotEmpty) { + keys.first.currentState?.requestFocusFromMemory(); + } + } + /// Handle vertical navigation between hubs /// Returns true if the navigation was handled bool _handleVerticalNavigation(int hubIndex, bool isUp) { final keys = _allHubKeys; if (keys.isEmpty) return false; - // UP from first hub: Navigate to hero section + // UP from first hub: navigate to hero when visible, otherwise app bar if (isUp && hubIndex == 0) { - _heroFocusNode.requestFocus(); - // Scroll to top to show hero fully - _scrollController.animateTo(0, duration: const Duration(milliseconds: 200), curve: Curves.easeOut); + _focusTopBoundary(); return true; } @@ -307,9 +333,9 @@ class _DiscoverScreenState extends State final key = event.logicalKey; - // DOWN: Return to hero + // DOWN: Return to hero/content if (key.isDownKey) { - _heroFocusNode.requestFocus(); + _focusContentFromAppBar(); return KeyEventResult.handled; } @@ -347,9 +373,9 @@ class _DiscoverScreenState extends State final key = event.logicalKey; - // DOWN: Return to hero + // DOWN: Return to hero/content if (key.isDownKey) { - _heroFocusNode.requestFocus(); + _focusContentFromAppBar(); return KeyEventResult.handled; } @@ -387,9 +413,9 @@ class _DiscoverScreenState extends State final key = event.logicalKey; - // DOWN: Return to hero + // DOWN: Return to hero/content if (key.isDownKey) { - _heroFocusNode.requestFocus(); + _focusContentFromAppBar(); return KeyEventResult.handled; } @@ -1120,14 +1146,7 @@ class _DiscoverScreenState extends State onRemoveFromContinueWatching: _refreshContinueWatching, isInContinueWatching: true, onVerticalNavigation: (isUp) => _handleVerticalNavigation(0, isUp), - onNavigateUp: () { - _heroFocusNode.requestFocus(); - _scrollController.animateTo( - 0, - duration: const Duration(milliseconds: 200), - curve: Curves.easeOut, - ); - }, + onNavigateUp: _focusTopBoundary, onNavigateToSidebar: _navigateToSidebar, ), ), @@ -1143,16 +1162,7 @@ class _DiscoverScreenState extends State onRefresh: updateItem, // Hub index is i + 1 if continue watching exists, otherwise i onVerticalNavigation: (isUp) => _handleVerticalNavigation(_onDeck.isNotEmpty ? i + 1 : i, isUp), - onNavigateUp: (i == 0 && _onDeck.isEmpty) - ? () { - _heroFocusNode.requestFocus(); - _scrollController.animateTo( - 0, - duration: const Duration(milliseconds: 200), - curve: Curves.easeOut, - ); - } - : null, + onNavigateUp: (i == 0 && _onDeck.isEmpty) ? _focusTopBoundary : null, onNavigateToSidebar: _navigateToSidebar, ), ),