diff --git a/lib/focus/dpad_select_long_press_controller.dart b/lib/focus/dpad_select_long_press_controller.dart new file mode 100644 index 00000000..4ac7d036 --- /dev/null +++ b/lib/focus/dpad_select_long_press_controller.dart @@ -0,0 +1,63 @@ +import 'dart:async'; + +import 'package:flutter/services.dart'; +import 'package:flutter/widgets.dart' show KeyEventResult, VoidCallback; + +import 'dpad_navigator.dart'; + +/// Tracks the timer and physical key state for a D-pad SELECT long press. +/// +/// Focus loss, context-menu dispatch, and transferred/touch gesture suppression +/// stay with the caller because their behavior differs between widgets. The TV +/// guide program selector also stays local: it captures a program at key-down +/// and resets its state before opening program details. +class DpadSelectLongPressController { + static const defaultDuration = Duration(milliseconds: 500); + + Timer? _timer; + bool _isKeyDown = false; + + KeyEventResult handleKeyEvent( + KeyEvent event, { + Duration duration = defaultDuration, + required bool Function() isOwnerActive, + required VoidCallback onShortPress, + required VoidCallback onLongPress, + }) { + if (!event.logicalKey.isSelectKey) return KeyEventResult.ignored; + + if (event is KeyDownEvent) { + // Some platforms report another down instead of a repeat. Only the + // transition from up to down may start (or restart) the timer. + if (!_isKeyDown) { + _isKeyDown = true; + _timer?.cancel(); + _timer = Timer(duration, () { + if (!isOwnerActive() || !_isKeyDown) return; + SelectKeyUpSuppressor.suppressSelectUntilKeyUp(); + onLongPress(); + }); + } + return KeyEventResult.handled; + } + + if (event is KeyRepeatEvent) return KeyEventResult.handled; + + if (event is KeyUpEvent) { + final timerWasActive = _timer?.isActive ?? false; + _timer?.cancel(); + if (timerWasActive && _isKeyDown) onShortPress(); + _isKeyDown = false; + return KeyEventResult.handled; + } + + return KeyEventResult.ignored; + } + + void reset() { + _timer?.cancel(); + _isKeyDown = false; + } + + void dispose() => reset(); +} diff --git a/lib/focus/focusable_chip_mixin.dart b/lib/focus/focusable_chip_mixin.dart index 2f9937d0..e2fe987d 100644 --- a/lib/focus/focusable_chip_mixin.dart +++ b/lib/focus/focusable_chip_mixin.dart @@ -1,11 +1,10 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../utils/scroll_utils.dart'; import 'owned_focus_node_binding.dart'; import 'dpad_navigator.dart'; +import 'dpad_select_long_press_controller.dart'; import 'key_event_utils.dart'; class ChipKeyCallbacks { @@ -48,8 +47,7 @@ class ChipKeyCallbacks { mixin FocusableChipStateMixin on State { final _focusNodeBinding = OwnedFocusNodeBinding(); bool _isFocused = false; - Timer? _longPressTimer; - bool _isSelectKeyDown = false; + final _selectLongPress = DpadSelectLongPressController(); /// Override to return the widget's optional external focus node. FocusNode? get widgetFocusNode; @@ -78,7 +76,7 @@ mixin FocusableChipStateMixin on State { /// Call this in your `dispose` to clean up the focus listener. void disposeFocusNode() { _focusNodeBinding.dispose(); - _longPressTimer?.cancel(); + _selectLongPress.dispose(); } void _onFocusChange() { @@ -86,8 +84,7 @@ mixin FocusableChipStateMixin on State { final hasFocus = focusNode.hasFocus; setState(() => _isFocused = hasFocus); if (!hasFocus) { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; + _selectLongPress.reset(); } // Same convention as FocusableTileStateMixin: a chip inside a // scrollable strip (TabChipStrip, filter bars) reveals itself on @@ -117,8 +114,7 @@ mixin FocusableChipStateMixin on State { if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) { if (event is KeyUpEvent && key.isSelectKey) { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; + _selectLongPress.reset(); } return KeyEventResult.handled; } @@ -126,29 +122,12 @@ mixin FocusableChipStateMixin on State { // SELECT key with long press support if (key.isSelectKey) { if (callbacks.onLongPress != null) { - if (event is KeyDownEvent) { - if (!_isSelectKeyDown) { - _isSelectKeyDown = true; - _longPressTimer?.cancel(); - _longPressTimer = Timer(const Duration(milliseconds: 500), () { - if (mounted && _isSelectKeyDown) { - SelectKeyUpSuppressor.suppressSelectUntilKeyUp(); - callbacks.onLongPress?.call(); - } - }); - } - return KeyEventResult.handled; - } else if (event is KeyRepeatEvent) { - return KeyEventResult.handled; - } else if (event is KeyUpEvent) { - final timerWasActive = _longPressTimer?.isActive ?? false; - _longPressTimer?.cancel(); - if (timerWasActive && _isSelectKeyDown) { - callbacks.onSelect?.call(); - } - _isSelectKeyDown = false; - return KeyEventResult.handled; - } + return _selectLongPress.handleKeyEvent( + event, + isOwnerActive: () => mounted, + onShortPress: () => callbacks.onSelect?.call(), + onLongPress: callbacks.onLongPress!, + ); } else if (callbacks.onSelect != null) { return handleOneShotSelect(event, callbacks.onSelect!); } @@ -156,8 +135,7 @@ mixin FocusableChipStateMixin on State { // Context menu key triggers long press directly if (event.isActionable && key.isContextMenuKey && callbacks.onLongPress != null) { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; + _selectLongPress.reset(); callbacks.onLongPress!(); return KeyEventResult.handled; } diff --git a/lib/focus/focusable_wrapper.dart b/lib/focus/focusable_wrapper.dart index 15e30fd3..c5acc3d8 100644 --- a/lib/focus/focusable_wrapper.dart +++ b/lib/focus/focusable_wrapper.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -7,6 +5,7 @@ import '../widgets/clickable_cursor.dart'; import '../utils/text_input_diagnostics.dart'; import 'card_focus_scope.dart'; import 'dpad_navigator.dart'; +import 'dpad_select_long_press_controller.dart'; import 'focus_glow_overlay.dart'; import 'focus_theme.dart'; import 'input_mode_tracker.dart'; @@ -174,9 +173,7 @@ class _FocusableWrapperState extends State with SingleTickerPr AnimationController? _animationController; Animation? _scaleAnimation; - // Long-press detection for SELECT key - Timer? _longPressTimer; - bool _isSelectKeyDown = false; + final _selectLongPress = DpadSelectLongPressController(); @override void initState() { @@ -240,7 +237,7 @@ class _FocusableWrapperState extends State with SingleTickerPr @override void dispose() { - _longPressTimer?.cancel(); + _selectLongPress.dispose(); _animationController?.dispose(); if (_ownsNode) { _focusNode.dispose(); @@ -256,8 +253,7 @@ class _FocusableWrapperState extends State with SingleTickerPr // Reset long press state when focus is lost if (!hasFocus) { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; + _selectLongPress.reset(); } // Animate scale @@ -377,8 +373,7 @@ class _FocusableWrapperState extends State with SingleTickerPr if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) { if (event is KeyUpEvent && key.isSelectKey) { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; + _selectLongPress.reset(); } return finish(KeyEventResult.handled, 'select-key-up-suppressed'); } @@ -401,33 +396,15 @@ class _FocusableWrapperState extends State with SingleTickerPr // Handle SELECT key with optional long-press detection if (key.isSelectKey) { if (widget.enableLongPress) { - if (event is KeyDownEvent) { - // Only start timer on initial press, not repeats - if (!_isSelectKeyDown) { - _isSelectKeyDown = true; - _longPressTimer?.cancel(); - _longPressTimer = Timer(widget.longPressDuration, () { - // Long press detected - if (mounted && _isSelectKeyDown) { - SelectKeyUpSuppressor.suppressSelectUntilKeyUp(); - widget.onLongPress?.call(); - } - }); - } - return finish(KeyEventResult.handled, 'select-long-press-down'); - } else if (event is KeyRepeatEvent) { - // Consume repeat events to prevent system sounds - return finish(KeyEventResult.handled, 'select-long-press-repeat'); - } else if (event is KeyUpEvent) { - final timerWasActive = _longPressTimer?.isActive ?? false; - _longPressTimer?.cancel(); - if (timerWasActive && _isSelectKeyDown) { - // Timer still active - short press - widget.onSelect?.call(); - } - // If timer already fired, long press was triggered - do nothing on key up - _isSelectKeyDown = false; - return finish(KeyEventResult.handled, 'select-long-press-up'); + final result = _selectLongPress.handleKeyEvent( + event, + duration: widget.longPressDuration, + isOwnerActive: () => mounted, + onShortPress: () => widget.onSelect?.call(), + onLongPress: () => widget.onLongPress?.call(), + ); + if (result != KeyEventResult.ignored) { + return finish(result, 'select-long-press'); } } else if (widget.onSelect != null) { return finish(handleOneShotSelect(event, widget.onSelect!), 'one-shot-select'); @@ -441,8 +418,7 @@ class _FocusableWrapperState extends State with SingleTickerPr // Context menu key if (key.isContextMenuKey) { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; + _selectLongPress.reset(); widget.onLongPress?.call(); return finish(KeyEventResult.handled, 'context-menu'); } diff --git a/lib/screens/livetv/tabs/whats_on_tab.dart b/lib/screens/livetv/tabs/whats_on_tab.dart index 8c3439d9..bb8d0be1 100644 --- a/lib/screens/livetv/tabs/whats_on_tab.dart +++ b/lib/screens/livetv/tabs/whats_on_tab.dart @@ -2,11 +2,11 @@ import 'dart:async'; import '../../../media/ids.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:material_symbols_icons/symbols.dart'; import 'package:provider/provider.dart'; import '../../../focus/dpad_navigator.dart'; +import '../../../focus/dpad_select_long_press_controller.dart'; import '../../../focus/key_event_utils.dart'; import '../../../focus/locked_hub_controller.dart'; import '../../../i18n/strings.g.dart'; @@ -226,8 +226,6 @@ class _LiveTvHubSection extends StatefulWidget { } class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetStateMixin { - static const _longPressDuration = Duration(milliseconds: 500); - late FocusNode _hubFocusNode; final ScrollController _scrollController = ScrollController(); @@ -235,9 +233,7 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta double _itemExtent = 0; static const double _leadingPadding = 12.0; - Timer? _longPressTimer; - bool _isSelectKeyDown = false; - bool _longPressTriggered = false; + final _selectLongPress = DpadSelectLongPressController(); @override void initState() { @@ -259,7 +255,7 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta @override void dispose() { - _longPressTimer?.cancel(); + _selectLongPress.dispose(); _hubFocusNode.removeListener(_onFocusChange); _hubFocusNode.dispose(); _scrollController.dispose(); @@ -268,9 +264,7 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta void _onFocusChange() { if (!_hubFocusNode.hasFocus) { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; - _longPressTriggered = false; + _selectLongPress.reset(); } // ignore: no-empty-block - setState triggers rebuild to update focus styling setStateIfMounted(() {}); @@ -319,35 +313,13 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta KeyEventResult _handleKeyEvent(FocusNode _, KeyEvent event) { final key = event.logicalKey; - if (key.isSelectKey) { - if (event is KeyDownEvent) { - if (!_isSelectKeyDown) { - _isSelectKeyDown = true; - _longPressTriggered = false; - _longPressTimer?.cancel(); - _longPressTimer = Timer(_longPressDuration, () { - if (!mounted) return; - if (_isSelectKeyDown) { - _longPressTriggered = true; - SelectKeyUpSuppressor.suppressSelectUntilKeyUp(); - _activateLongPress(); - } - }); - } - return KeyEventResult.handled; - } else if (event is KeyRepeatEvent) { - return KeyEventResult.handled; - } else if (event is KeyUpEvent) { - final timerWasActive = _longPressTimer?.isActive ?? false; - _longPressTimer?.cancel(); - if (!_longPressTriggered && timerWasActive && _isSelectKeyDown) { - _activateCurrentItem(); - } - _isSelectKeyDown = false; - _longPressTriggered = false; - return KeyEventResult.handled; - } - } + final selectResult = _selectLongPress.handleKeyEvent( + event, + isOwnerActive: () => mounted, + onShortPress: _activateCurrentItem, + onLongPress: _activateLongPress, + ); + if (selectResult != KeyEventResult.ignored) return selectResult; if (widget.onBack != null) { final backResult = handleBackKeyAction(event, widget.onBack!); diff --git a/lib/screens/media_detail_screen.dart b/lib/screens/media_detail_screen.dart index 92d158fe..9e6c1da0 100644 --- a/lib/screens/media_detail_screen.dart +++ b/lib/screens/media_detail_screen.dart @@ -17,6 +17,7 @@ import '../widgets/collapsible_text.dart'; import '../widgets/rating_bottom_sheet.dart'; import '../focus/dpad_navigator.dart'; +import '../focus/dpad_select_long_press_controller.dart'; import '../focus/focusable_action_bar.dart'; import '../focus/focusable_wrapper.dart'; import '../focus/key_event_utils.dart'; @@ -349,10 +350,7 @@ class _MediaDetailScreenState extends State late final FocusNode _playButtonFocusNode; late final FocusNode _ratingChipFocusNode; - Timer? _selectKeyTimer; - bool _isSelectKeyDown = false; - bool _longPressTriggered = false; - static const _longPressDuration = Duration(milliseconds: 500); + final _extrasSelectLongPress = DpadSelectLongPressController(); // Context menu key for the three-dots button final _contextMenuKey = GlobalKey(); @@ -860,7 +858,7 @@ class _MediaDetailScreenState extends State _castFocusNode.dispose(); _infoRowsFocusNode.dispose(); _castScrollController.dispose(); - _selectKeyTimer?.cancel(); + _extrasSelectLongPress.dispose(); for (final node in _seasonTabFocusNodes) { node.dispose(); } @@ -2410,48 +2408,34 @@ class _MediaDetailScreenState extends State if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) { if (event is KeyUpEvent && key.isSelectKey) { - _selectKeyTimer?.cancel(); - _isSelectKeyDown = false; - _longPressTriggered = false; + _extrasSelectLongPress.reset(); } return KeyEventResult.handled; } - // Handle SELECT with long-press detection - if (key.isSelectKey) { - if (event is KeyDownEvent) { - if (!_isSelectKeyDown) { - _selectKeyTimer?.cancel(); - _isSelectKeyDown = true; - _longPressTriggered = false; - _selectKeyTimer = Timer(_longPressDuration, () { - if (!mounted) return; - if (_isSelectKeyDown) { - _longPressTriggered = true; - SelectKeyUpSuppressor.suppressSelectUntilKeyUp(); - _extraCardKeys[_focusedExtraIndex]?.currentState?.showContextMenu(); - } - }); - } - return KeyEventResult.handled; - } else if (event is KeyRepeatEvent) { - return KeyEventResult.handled; - } else if (event is KeyUpEvent) { - final timerWasActive = _selectKeyTimer?.isActive ?? false; - _selectKeyTimer?.cancel(); - if (!_longPressTriggered && timerWasActive && _isSelectKeyDown) { - if (_focusedExtraIndex < _extras!.length) { - navigateToVideoPlayer(context, metadata: _extras![_focusedExtraIndex]); - } - } - _isSelectKeyDown = false; - _longPressTriggered = false; - return KeyEventResult.handled; - } + final extras = _extras; + if (extras == null || extras.isEmpty) { + _extrasSelectLongPress.reset(); + return KeyEventResult.ignored; } + final selectResult = _extrasSelectLongPress.handleKeyEvent( + event, + isOwnerActive: () => mounted, + onShortPress: () { + if (_focusedExtraIndex < extras.length) { + navigateToVideoPlayer(context, metadata: extras[_focusedExtraIndex]); + } + }, + onLongPress: () { + if (_focusedExtraIndex < _extraCardKeys.length) { + _extraCardKeys[_focusedExtraIndex]?.currentState?.showContextMenu(); + } + }, + ); + if (selectResult != KeyEventResult.ignored) return selectResult; + if (!event.isActionable) return KeyEventResult.ignored; - if (_extras == null || _extras!.isEmpty) return KeyEventResult.ignored; // LEFT: previous extra if (key.isLeftKey) { @@ -2504,9 +2488,7 @@ class _MediaDetailScreenState extends State } void _resetExtrasLongPressState() { - _selectKeyTimer?.cancel(); - _isSelectKeyDown = false; - _longPressTriggered = false; + _extrasSelectLongPress.reset(); } /// Handle key events for the cast row (locked focus pattern) diff --git a/lib/widgets/hub_section.dart b/lib/widgets/hub_section.dart index 24348f5e..11b1e57f 100644 --- a/lib/widgets/hub_section.dart +++ b/lib/widgets/hub_section.dart @@ -1,11 +1,9 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart' show ScrollCacheExtent; -import 'package:flutter/services.dart'; import 'package:plezy/widgets/app_icon.dart'; import 'package:material_symbols_icons/symbols.dart'; import '../focus/dpad_navigator.dart'; +import '../focus/dpad_select_long_press_controller.dart'; import '../focus/focus_theme.dart'; import '../focus/input_mode_tracker.dart'; import '../focus/key_event_utils.dart'; @@ -97,8 +95,6 @@ class HubSection extends StatefulWidget { } class HubSectionState extends State with MountedSetStateMixin, SkeletonUpgradeScheduler { - static const _longPressDuration = Duration(milliseconds: 500); - late FocusNode _hubFocusNode; final ScrollController _scrollController = ScrollController(); @@ -117,9 +113,7 @@ class HubSectionState extends State with MountedSetStateMixin, Skele : 12.0; double get _leadingPadding => _leadingPaddingFor(PlatformDetector.isTV()); - Timer? _longPressTimer; - bool _isSelectKeyDown = false; - bool _longPressTriggered = false; + final _selectLongPress = DpadSelectLongPressController(); @override void initState() { @@ -152,7 +146,7 @@ class HubSectionState extends State with MountedSetStateMixin, Skele @override void dispose() { - _longPressTimer?.cancel(); + _selectLongPress.dispose(); _hubFocusNode.removeListener(_onFocusChange); _hubFocusNode.dispose(); _scrollController.dispose(); @@ -162,9 +156,7 @@ class HubSectionState extends State with MountedSetStateMixin, Skele void _onFocusChange() { // Reset long press state when focus is lost if (!_hubFocusNode.hasFocus) { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; - _longPressTriggered = false; + _selectLongPress.reset(); } else { _notifyFocusedItemChanged(); } @@ -237,35 +229,13 @@ class HubSectionState extends State with MountedSetStateMixin, Skele KeyEventResult _handleKeyEvent(FocusNode _, KeyEvent event) { final key = event.logicalKey; - if (key.isSelectKey) { - if (event is KeyDownEvent) { - if (!_isSelectKeyDown) { - _isSelectKeyDown = true; - _longPressTriggered = false; - _longPressTimer?.cancel(); - _longPressTimer = Timer(_longPressDuration, () { - if (!mounted) return; - if (_isSelectKeyDown) { - _longPressTriggered = true; - SelectKeyUpSuppressor.suppressSelectUntilKeyUp(); - _showContextMenuForCurrentItem(); - } - }); - } - return KeyEventResult.handled; - } else if (event is KeyRepeatEvent) { - return KeyEventResult.handled; - } else if (event is KeyUpEvent) { - final timerWasActive = _longPressTimer?.isActive ?? false; - _longPressTimer?.cancel(); - if (!_longPressTriggered && timerWasActive && _isSelectKeyDown) { - _activateCurrentItem(); - } - _isSelectKeyDown = false; - _longPressTriggered = false; - return KeyEventResult.handled; - } - } + final selectResult = _selectLongPress.handleKeyEvent( + event, + isOwnerActive: () => mounted, + onShortPress: _activateCurrentItem, + onLongPress: _showContextMenuForCurrentItem, + ); + if (selectResult != KeyEventResult.ignored) return selectResult; if (widget.onBack != null) { final backResult = handleBackKeyAction(event, widget.onBack!); diff --git a/lib/widgets/tv_browse_rail.dart b/lib/widgets/tv_browse_rail.dart index 22c1ba0f..97fd596c 100644 --- a/lib/widgets/tv_browse_rail.dart +++ b/lib/widgets/tv_browse_rail.dart @@ -9,6 +9,7 @@ import 'package:material_symbols_icons/symbols.dart'; import '../focus/card_focus_scope.dart'; import '../focus/dpad_navigator.dart'; +import '../focus/dpad_select_long_press_controller.dart'; import '../focus/focus_theme.dart'; import '../focus/key_event_utils.dart'; import '../focus/locked_hub_controller.dart'; @@ -385,7 +386,6 @@ class TvBrowseRail extends StatefulWidget { } class TvBrowseRailState extends State { - static const _longPressDuration = Duration(milliseconds: 500); // No-touch fallback only: clear suppression even if no select key-up is seen // (e.g. a held-key carry-over on a non-touch remote). Touch-driven clicks use // the gesture path instead, which is bounded by the physical touch. @@ -421,12 +421,10 @@ class TvBrowseRailState extends State { final _RailFocusModel _focusModel = _RailFocusModel(); List _sectionOffsets = const []; double _sectionMaxScrollExtent = 0; - Timer? _longPressTimer; + final _selectLongPress = DpadSelectLongPressController(); Timer? _selectSuppressionTimer; Timer? _selectSuppressionMaxTimer; VoidCallback? _gestureSignalListener; - bool _isSelectKeyDown = false; - bool _longPressTriggered = false; bool _suppressSelectUntilKeyUp = false; bool _hasUserChangedHub = false; bool _hasUserChangedItem = false; @@ -599,7 +597,7 @@ class TvBrowseRailState extends State { @override void dispose() { - _longPressTimer?.cancel(); + _selectLongPress.dispose(); _selectSuppressionTimer?.cancel(); _selectSuppressionMaxTimer?.cancel(); _detachGestureSignalListener(); @@ -623,9 +621,7 @@ class TvBrowseRailState extends State { } void _resetLongPressState() { - _longPressTimer?.cancel(); - _isSelectKeyDown = false; - _longPressTriggered = false; + _selectLongPress.reset(); } void _clearSelectSuppression() { @@ -698,29 +694,12 @@ class TvBrowseRailState extends State { return KeyEventResult.handled; } - if (event is KeyDownEvent) { - if (!_isSelectKeyDown) { - _isSelectKeyDown = true; - _longPressTriggered = false; - _longPressTimer?.cancel(); - _longPressTimer = Timer(_longPressDuration, () { - if (!mounted || !_isSelectKeyDown) return; - _longPressTriggered = true; - SelectKeyUpSuppressor.suppressSelectUntilKeyUp(); - _showContextMenuForCurrentItem(); - }); - } - return KeyEventResult.handled; - } - if (event is KeyRepeatEvent) return KeyEventResult.handled; - if (event is KeyUpEvent) { - final timerWasActive = _longPressTimer?.isActive ?? false; - _longPressTimer?.cancel(); - if (!_longPressTriggered && timerWasActive && _isSelectKeyDown) _activateCurrentItem(); - _isSelectKeyDown = false; - _longPressTriggered = false; - return KeyEventResult.handled; - } + return _selectLongPress.handleKeyEvent( + event, + isOwnerActive: () => mounted, + onShortPress: _activateCurrentItem, + onLongPress: _showContextMenuForCurrentItem, + ); } if (widget.onBack != null) { diff --git a/test/focus/dpad_select_long_press_controller_test.dart b/test/focus/dpad_select_long_press_controller_test.dart new file mode 100644 index 00000000..384e4921 --- /dev/null +++ b/test/focus/dpad_select_long_press_controller_test.dart @@ -0,0 +1,138 @@ +import 'package:fake_async/fake_async.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter/widgets.dart' show KeyEventResult; +import 'package:plezy/focus/dpad_navigator.dart'; +import 'package:plezy/focus/dpad_select_long_press_controller.dart'; + +const _down = KeyDownEvent( + physicalKey: PhysicalKeyboardKey.enter, + logicalKey: LogicalKeyboardKey.enter, + timeStamp: Duration.zero, +); +const _secondDown = KeyDownEvent( + physicalKey: PhysicalKeyboardKey.enter, + logicalKey: LogicalKeyboardKey.enter, + timeStamp: Duration(milliseconds: 400), +); +const _repeat = KeyRepeatEvent( + physicalKey: PhysicalKeyboardKey.enter, + logicalKey: LogicalKeyboardKey.enter, + timeStamp: Duration(milliseconds: 400), +); +const _up = KeyUpEvent( + physicalKey: PhysicalKeyboardKey.enter, + logicalKey: LogicalKeyboardKey.enter, + timeStamp: Duration(milliseconds: 450), +); + +void main() { + tearDown(SelectKeyUpSuppressor.clearSuppression); + + test('initial down starts once and down/repeat events do not restart it', () { + fakeAsync((async) { + final controller = DpadSelectLongPressController(); + var shortPresses = 0; + var longPresses = 0; + + KeyEventResult handle(KeyEvent event) => controller.handleKeyEvent( + event, + isOwnerActive: () => true, + onShortPress: () => shortPresses++, + onLongPress: () => longPresses++, + ); + + expect(handle(_down), KeyEventResult.handled); + async.elapse(const Duration(milliseconds: 400)); + expect(handle(_secondDown), KeyEventResult.handled); + expect(handle(_repeat), KeyEventResult.handled); + expect(shortPresses, 0); + expect(longPresses, 0); + + async.elapse(const Duration(milliseconds: 100)); + expect(longPresses, 1); + expect(shortPresses, 0); + expect(handle(_up), KeyEventResult.handled); + expect(shortPresses, 0); + }); + }); + + test('key up before the deadline fires one short press and cancels long press', () { + fakeAsync((async) { + final controller = DpadSelectLongPressController(); + var shortPresses = 0; + var longPresses = 0; + + controller.handleKeyEvent( + _down, + isOwnerActive: () => true, + onShortPress: () => shortPresses++, + onLongPress: () => longPresses++, + ); + async.elapse(const Duration(milliseconds: 450)); + expect( + controller.handleKeyEvent( + _up, + isOwnerActive: () => true, + onShortPress: () => shortPresses++, + onLongPress: () => longPresses++, + ), + KeyEventResult.handled, + ); + async.elapse(const Duration(seconds: 1)); + + expect(shortPresses, 1); + expect(longPresses, 0); + }); + }); + + test('focus-loss reset cancels a pending press and clears key-down state', () { + fakeAsync((async) { + final controller = DpadSelectLongPressController(); + var shortPresses = 0; + var longPresses = 0; + + void handle(KeyEvent event) => controller.handleKeyEvent( + event, + isOwnerActive: () => true, + onShortPress: () => shortPresses++, + onLongPress: () => longPresses++, + ); + + handle(_down); + controller.reset(); + async.elapse(const Duration(seconds: 1)); + handle(_up); + + expect(shortPresses, 0); + expect(longPresses, 0); + + handle(_down); + async.elapse(DpadSelectLongPressController.defaultDuration); + expect(longPresses, 1); + }); + }); + + test('disposal cancels the timer and prevents later key-up activation', () { + fakeAsync((async) { + final controller = DpadSelectLongPressController(); + var shortPresses = 0; + var longPresses = 0; + + void handle(KeyEvent event) => controller.handleKeyEvent( + event, + isOwnerActive: () => true, + onShortPress: () => shortPresses++, + onLongPress: () => longPresses++, + ); + + handle(_down); + controller.dispose(); + async.elapse(const Duration(seconds: 1)); + handle(_up); + + expect(shortPresses, 0); + expect(longPresses, 0); + }); + }); +}