refactor(input): share D-pad long press state
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
import '../utils/scroll_utils.dart';
|
import '../utils/scroll_utils.dart';
|
||||||
import 'owned_focus_node_binding.dart';
|
import 'owned_focus_node_binding.dart';
|
||||||
import 'dpad_navigator.dart';
|
import 'dpad_navigator.dart';
|
||||||
|
import 'dpad_select_long_press_controller.dart';
|
||||||
import 'key_event_utils.dart';
|
import 'key_event_utils.dart';
|
||||||
|
|
||||||
class ChipKeyCallbacks {
|
class ChipKeyCallbacks {
|
||||||
@@ -48,8 +47,7 @@ class ChipKeyCallbacks {
|
|||||||
mixin FocusableChipStateMixin<T extends StatefulWidget> on State<T> {
|
mixin FocusableChipStateMixin<T extends StatefulWidget> on State<T> {
|
||||||
final _focusNodeBinding = OwnedFocusNodeBinding();
|
final _focusNodeBinding = OwnedFocusNodeBinding();
|
||||||
bool _isFocused = false;
|
bool _isFocused = false;
|
||||||
Timer? _longPressTimer;
|
final _selectLongPress = DpadSelectLongPressController();
|
||||||
bool _isSelectKeyDown = false;
|
|
||||||
|
|
||||||
/// Override to return the widget's optional external focus node.
|
/// Override to return the widget's optional external focus node.
|
||||||
FocusNode? get widgetFocusNode;
|
FocusNode? get widgetFocusNode;
|
||||||
@@ -78,7 +76,7 @@ mixin FocusableChipStateMixin<T extends StatefulWidget> on State<T> {
|
|||||||
/// Call this in your `dispose` to clean up the focus listener.
|
/// Call this in your `dispose` to clean up the focus listener.
|
||||||
void disposeFocusNode() {
|
void disposeFocusNode() {
|
||||||
_focusNodeBinding.dispose();
|
_focusNodeBinding.dispose();
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onFocusChange() {
|
void _onFocusChange() {
|
||||||
@@ -86,8 +84,7 @@ mixin FocusableChipStateMixin<T extends StatefulWidget> on State<T> {
|
|||||||
final hasFocus = focusNode.hasFocus;
|
final hasFocus = focusNode.hasFocus;
|
||||||
setState(() => _isFocused = hasFocus);
|
setState(() => _isFocused = hasFocus);
|
||||||
if (!hasFocus) {
|
if (!hasFocus) {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
}
|
}
|
||||||
// Same convention as FocusableTileStateMixin: a chip inside a
|
// Same convention as FocusableTileStateMixin: a chip inside a
|
||||||
// scrollable strip (TabChipStrip, filter bars) reveals itself on
|
// scrollable strip (TabChipStrip, filter bars) reveals itself on
|
||||||
@@ -117,8 +114,7 @@ mixin FocusableChipStateMixin<T extends StatefulWidget> on State<T> {
|
|||||||
|
|
||||||
if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) {
|
if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) {
|
||||||
if (event is KeyUpEvent && key.isSelectKey) {
|
if (event is KeyUpEvent && key.isSelectKey) {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
}
|
}
|
||||||
return KeyEventResult.handled;
|
return KeyEventResult.handled;
|
||||||
}
|
}
|
||||||
@@ -126,29 +122,12 @@ mixin FocusableChipStateMixin<T extends StatefulWidget> on State<T> {
|
|||||||
// SELECT key with long press support
|
// SELECT key with long press support
|
||||||
if (key.isSelectKey) {
|
if (key.isSelectKey) {
|
||||||
if (callbacks.onLongPress != null) {
|
if (callbacks.onLongPress != null) {
|
||||||
if (event is KeyDownEvent) {
|
return _selectLongPress.handleKeyEvent(
|
||||||
if (!_isSelectKeyDown) {
|
event,
|
||||||
_isSelectKeyDown = true;
|
isOwnerActive: () => mounted,
|
||||||
_longPressTimer?.cancel();
|
onShortPress: () => callbacks.onSelect?.call(),
|
||||||
_longPressTimer = Timer(const Duration(milliseconds: 500), () {
|
onLongPress: callbacks.onLongPress!,
|
||||||
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;
|
|
||||||
}
|
|
||||||
} else if (callbacks.onSelect != null) {
|
} else if (callbacks.onSelect != null) {
|
||||||
return handleOneShotSelect(event, callbacks.onSelect!);
|
return handleOneShotSelect(event, callbacks.onSelect!);
|
||||||
}
|
}
|
||||||
@@ -156,8 +135,7 @@ mixin FocusableChipStateMixin<T extends StatefulWidget> on State<T> {
|
|||||||
|
|
||||||
// Context menu key triggers long press directly
|
// Context menu key triggers long press directly
|
||||||
if (event.isActionable && key.isContextMenuKey && callbacks.onLongPress != null) {
|
if (event.isActionable && key.isContextMenuKey && callbacks.onLongPress != null) {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
callbacks.onLongPress!();
|
callbacks.onLongPress!();
|
||||||
return KeyEventResult.handled;
|
return KeyEventResult.handled;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
@@ -7,6 +5,7 @@ import '../widgets/clickable_cursor.dart';
|
|||||||
import '../utils/text_input_diagnostics.dart';
|
import '../utils/text_input_diagnostics.dart';
|
||||||
import 'card_focus_scope.dart';
|
import 'card_focus_scope.dart';
|
||||||
import 'dpad_navigator.dart';
|
import 'dpad_navigator.dart';
|
||||||
|
import 'dpad_select_long_press_controller.dart';
|
||||||
import 'focus_glow_overlay.dart';
|
import 'focus_glow_overlay.dart';
|
||||||
import 'focus_theme.dart';
|
import 'focus_theme.dart';
|
||||||
import 'input_mode_tracker.dart';
|
import 'input_mode_tracker.dart';
|
||||||
@@ -174,9 +173,7 @@ class _FocusableWrapperState extends State<FocusableWrapper> with SingleTickerPr
|
|||||||
AnimationController? _animationController;
|
AnimationController? _animationController;
|
||||||
Animation<double>? _scaleAnimation;
|
Animation<double>? _scaleAnimation;
|
||||||
|
|
||||||
// Long-press detection for SELECT key
|
final _selectLongPress = DpadSelectLongPressController();
|
||||||
Timer? _longPressTimer;
|
|
||||||
bool _isSelectKeyDown = false;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -240,7 +237,7 @@ class _FocusableWrapperState extends State<FocusableWrapper> with SingleTickerPr
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.dispose();
|
||||||
_animationController?.dispose();
|
_animationController?.dispose();
|
||||||
if (_ownsNode) {
|
if (_ownsNode) {
|
||||||
_focusNode.dispose();
|
_focusNode.dispose();
|
||||||
@@ -256,8 +253,7 @@ class _FocusableWrapperState extends State<FocusableWrapper> with SingleTickerPr
|
|||||||
|
|
||||||
// Reset long press state when focus is lost
|
// Reset long press state when focus is lost
|
||||||
if (!hasFocus) {
|
if (!hasFocus) {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Animate scale
|
// Animate scale
|
||||||
@@ -377,8 +373,7 @@ class _FocusableWrapperState extends State<FocusableWrapper> with SingleTickerPr
|
|||||||
|
|
||||||
if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) {
|
if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) {
|
||||||
if (event is KeyUpEvent && key.isSelectKey) {
|
if (event is KeyUpEvent && key.isSelectKey) {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
}
|
}
|
||||||
return finish(KeyEventResult.handled, 'select-key-up-suppressed');
|
return finish(KeyEventResult.handled, 'select-key-up-suppressed');
|
||||||
}
|
}
|
||||||
@@ -401,33 +396,15 @@ class _FocusableWrapperState extends State<FocusableWrapper> with SingleTickerPr
|
|||||||
// Handle SELECT key with optional long-press detection
|
// Handle SELECT key with optional long-press detection
|
||||||
if (key.isSelectKey) {
|
if (key.isSelectKey) {
|
||||||
if (widget.enableLongPress) {
|
if (widget.enableLongPress) {
|
||||||
if (event is KeyDownEvent) {
|
final result = _selectLongPress.handleKeyEvent(
|
||||||
// Only start timer on initial press, not repeats
|
event,
|
||||||
if (!_isSelectKeyDown) {
|
duration: widget.longPressDuration,
|
||||||
_isSelectKeyDown = true;
|
isOwnerActive: () => mounted,
|
||||||
_longPressTimer?.cancel();
|
onShortPress: () => widget.onSelect?.call(),
|
||||||
_longPressTimer = Timer(widget.longPressDuration, () {
|
onLongPress: () => widget.onLongPress?.call(),
|
||||||
// Long press detected
|
);
|
||||||
if (mounted && _isSelectKeyDown) {
|
if (result != KeyEventResult.ignored) {
|
||||||
SelectKeyUpSuppressor.suppressSelectUntilKeyUp();
|
return finish(result, 'select-long-press');
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
} else if (widget.onSelect != null) {
|
} else if (widget.onSelect != null) {
|
||||||
return finish(handleOneShotSelect(event, widget.onSelect!), 'one-shot-select');
|
return finish(handleOneShotSelect(event, widget.onSelect!), 'one-shot-select');
|
||||||
@@ -441,8 +418,7 @@ class _FocusableWrapperState extends State<FocusableWrapper> with SingleTickerPr
|
|||||||
|
|
||||||
// Context menu key
|
// Context menu key
|
||||||
if (key.isContextMenuKey) {
|
if (key.isContextMenuKey) {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
widget.onLongPress?.call();
|
widget.onLongPress?.call();
|
||||||
return finish(KeyEventResult.handled, 'context-menu');
|
return finish(KeyEventResult.handled, 'context-menu');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ import 'dart:async';
|
|||||||
import '../../../media/ids.dart';
|
import '../../../media/ids.dart';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:material_symbols_icons/symbols.dart';
|
import 'package:material_symbols_icons/symbols.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import '../../../focus/dpad_navigator.dart';
|
import '../../../focus/dpad_navigator.dart';
|
||||||
|
import '../../../focus/dpad_select_long_press_controller.dart';
|
||||||
import '../../../focus/key_event_utils.dart';
|
import '../../../focus/key_event_utils.dart';
|
||||||
import '../../../focus/locked_hub_controller.dart';
|
import '../../../focus/locked_hub_controller.dart';
|
||||||
import '../../../i18n/strings.g.dart';
|
import '../../../i18n/strings.g.dart';
|
||||||
@@ -226,8 +226,6 @@ class _LiveTvHubSection extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetStateMixin {
|
class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetStateMixin {
|
||||||
static const _longPressDuration = Duration(milliseconds: 500);
|
|
||||||
|
|
||||||
late FocusNode _hubFocusNode;
|
late FocusNode _hubFocusNode;
|
||||||
final ScrollController _scrollController = ScrollController();
|
final ScrollController _scrollController = ScrollController();
|
||||||
|
|
||||||
@@ -235,9 +233,7 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta
|
|||||||
double _itemExtent = 0;
|
double _itemExtent = 0;
|
||||||
static const double _leadingPadding = 12.0;
|
static const double _leadingPadding = 12.0;
|
||||||
|
|
||||||
Timer? _longPressTimer;
|
final _selectLongPress = DpadSelectLongPressController();
|
||||||
bool _isSelectKeyDown = false;
|
|
||||||
bool _longPressTriggered = false;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -259,7 +255,7 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.dispose();
|
||||||
_hubFocusNode.removeListener(_onFocusChange);
|
_hubFocusNode.removeListener(_onFocusChange);
|
||||||
_hubFocusNode.dispose();
|
_hubFocusNode.dispose();
|
||||||
_scrollController.dispose();
|
_scrollController.dispose();
|
||||||
@@ -268,9 +264,7 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta
|
|||||||
|
|
||||||
void _onFocusChange() {
|
void _onFocusChange() {
|
||||||
if (!_hubFocusNode.hasFocus) {
|
if (!_hubFocusNode.hasFocus) {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
_longPressTriggered = false;
|
|
||||||
}
|
}
|
||||||
// ignore: no-empty-block - setState triggers rebuild to update focus styling
|
// ignore: no-empty-block - setState triggers rebuild to update focus styling
|
||||||
setStateIfMounted(() {});
|
setStateIfMounted(() {});
|
||||||
@@ -319,35 +313,13 @@ class _LiveTvHubSectionState extends State<_LiveTvHubSection> with MountedSetSta
|
|||||||
KeyEventResult _handleKeyEvent(FocusNode _, KeyEvent event) {
|
KeyEventResult _handleKeyEvent(FocusNode _, KeyEvent event) {
|
||||||
final key = event.logicalKey;
|
final key = event.logicalKey;
|
||||||
|
|
||||||
if (key.isSelectKey) {
|
final selectResult = _selectLongPress.handleKeyEvent(
|
||||||
if (event is KeyDownEvent) {
|
event,
|
||||||
if (!_isSelectKeyDown) {
|
isOwnerActive: () => mounted,
|
||||||
_isSelectKeyDown = true;
|
onShortPress: _activateCurrentItem,
|
||||||
_longPressTriggered = false;
|
onLongPress: _activateLongPress,
|
||||||
_longPressTimer?.cancel();
|
);
|
||||||
_longPressTimer = Timer(_longPressDuration, () {
|
if (selectResult != KeyEventResult.ignored) return selectResult;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (widget.onBack != null) {
|
if (widget.onBack != null) {
|
||||||
final backResult = handleBackKeyAction(event, widget.onBack!);
|
final backResult = handleBackKeyAction(event, widget.onBack!);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import '../widgets/collapsible_text.dart';
|
|||||||
import '../widgets/rating_bottom_sheet.dart';
|
import '../widgets/rating_bottom_sheet.dart';
|
||||||
|
|
||||||
import '../focus/dpad_navigator.dart';
|
import '../focus/dpad_navigator.dart';
|
||||||
|
import '../focus/dpad_select_long_press_controller.dart';
|
||||||
import '../focus/focusable_action_bar.dart';
|
import '../focus/focusable_action_bar.dart';
|
||||||
import '../focus/focusable_wrapper.dart';
|
import '../focus/focusable_wrapper.dart';
|
||||||
import '../focus/key_event_utils.dart';
|
import '../focus/key_event_utils.dart';
|
||||||
@@ -349,10 +350,7 @@ class _MediaDetailScreenState extends State<MediaDetailScreen>
|
|||||||
|
|
||||||
late final FocusNode _playButtonFocusNode;
|
late final FocusNode _playButtonFocusNode;
|
||||||
late final FocusNode _ratingChipFocusNode;
|
late final FocusNode _ratingChipFocusNode;
|
||||||
Timer? _selectKeyTimer;
|
final _extrasSelectLongPress = DpadSelectLongPressController();
|
||||||
bool _isSelectKeyDown = false;
|
|
||||||
bool _longPressTriggered = false;
|
|
||||||
static const _longPressDuration = Duration(milliseconds: 500);
|
|
||||||
|
|
||||||
// Context menu key for the three-dots button
|
// Context menu key for the three-dots button
|
||||||
final _contextMenuKey = GlobalKey<MediaContextMenuState>();
|
final _contextMenuKey = GlobalKey<MediaContextMenuState>();
|
||||||
@@ -860,7 +858,7 @@ class _MediaDetailScreenState extends State<MediaDetailScreen>
|
|||||||
_castFocusNode.dispose();
|
_castFocusNode.dispose();
|
||||||
_infoRowsFocusNode.dispose();
|
_infoRowsFocusNode.dispose();
|
||||||
_castScrollController.dispose();
|
_castScrollController.dispose();
|
||||||
_selectKeyTimer?.cancel();
|
_extrasSelectLongPress.dispose();
|
||||||
for (final node in _seasonTabFocusNodes) {
|
for (final node in _seasonTabFocusNodes) {
|
||||||
node.dispose();
|
node.dispose();
|
||||||
}
|
}
|
||||||
@@ -2410,48 +2408,34 @@ class _MediaDetailScreenState extends State<MediaDetailScreen>
|
|||||||
|
|
||||||
if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) {
|
if (SelectKeyUpSuppressor.consumeIfSuppressed(event)) {
|
||||||
if (event is KeyUpEvent && key.isSelectKey) {
|
if (event is KeyUpEvent && key.isSelectKey) {
|
||||||
_selectKeyTimer?.cancel();
|
_extrasSelectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
_longPressTriggered = false;
|
|
||||||
}
|
}
|
||||||
return KeyEventResult.handled;
|
return KeyEventResult.handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle SELECT with long-press detection
|
final extras = _extras;
|
||||||
if (key.isSelectKey) {
|
if (extras == null || extras.isEmpty) {
|
||||||
if (event is KeyDownEvent) {
|
_extrasSelectLongPress.reset();
|
||||||
if (!_isSelectKeyDown) {
|
return KeyEventResult.ignored;
|
||||||
_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 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 (!event.isActionable) return KeyEventResult.ignored;
|
||||||
if (_extras == null || _extras!.isEmpty) return KeyEventResult.ignored;
|
|
||||||
|
|
||||||
// LEFT: previous extra
|
// LEFT: previous extra
|
||||||
if (key.isLeftKey) {
|
if (key.isLeftKey) {
|
||||||
@@ -2504,9 +2488,7 @@ class _MediaDetailScreenState extends State<MediaDetailScreen>
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _resetExtrasLongPressState() {
|
void _resetExtrasLongPressState() {
|
||||||
_selectKeyTimer?.cancel();
|
_extrasSelectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
_longPressTriggered = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle key events for the cast row (locked focus pattern)
|
/// Handle key events for the cast row (locked focus pattern)
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/rendering.dart' show ScrollCacheExtent;
|
import 'package:flutter/rendering.dart' show ScrollCacheExtent;
|
||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:plezy/widgets/app_icon.dart';
|
import 'package:plezy/widgets/app_icon.dart';
|
||||||
import 'package:material_symbols_icons/symbols.dart';
|
import 'package:material_symbols_icons/symbols.dart';
|
||||||
import '../focus/dpad_navigator.dart';
|
import '../focus/dpad_navigator.dart';
|
||||||
|
import '../focus/dpad_select_long_press_controller.dart';
|
||||||
import '../focus/focus_theme.dart';
|
import '../focus/focus_theme.dart';
|
||||||
import '../focus/input_mode_tracker.dart';
|
import '../focus/input_mode_tracker.dart';
|
||||||
import '../focus/key_event_utils.dart';
|
import '../focus/key_event_utils.dart';
|
||||||
@@ -97,8 +95,6 @@ class HubSection extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HubSectionState extends State<HubSection> with MountedSetStateMixin, SkeletonUpgradeScheduler {
|
class HubSectionState extends State<HubSection> with MountedSetStateMixin, SkeletonUpgradeScheduler {
|
||||||
static const _longPressDuration = Duration(milliseconds: 500);
|
|
||||||
|
|
||||||
late FocusNode _hubFocusNode;
|
late FocusNode _hubFocusNode;
|
||||||
final ScrollController _scrollController = ScrollController();
|
final ScrollController _scrollController = ScrollController();
|
||||||
|
|
||||||
@@ -117,9 +113,7 @@ class HubSectionState extends State<HubSection> with MountedSetStateMixin, Skele
|
|||||||
: 12.0;
|
: 12.0;
|
||||||
double get _leadingPadding => _leadingPaddingFor(PlatformDetector.isTV());
|
double get _leadingPadding => _leadingPaddingFor(PlatformDetector.isTV());
|
||||||
|
|
||||||
Timer? _longPressTimer;
|
final _selectLongPress = DpadSelectLongPressController();
|
||||||
bool _isSelectKeyDown = false;
|
|
||||||
bool _longPressTriggered = false;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -152,7 +146,7 @@ class HubSectionState extends State<HubSection> with MountedSetStateMixin, Skele
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.dispose();
|
||||||
_hubFocusNode.removeListener(_onFocusChange);
|
_hubFocusNode.removeListener(_onFocusChange);
|
||||||
_hubFocusNode.dispose();
|
_hubFocusNode.dispose();
|
||||||
_scrollController.dispose();
|
_scrollController.dispose();
|
||||||
@@ -162,9 +156,7 @@ class HubSectionState extends State<HubSection> with MountedSetStateMixin, Skele
|
|||||||
void _onFocusChange() {
|
void _onFocusChange() {
|
||||||
// Reset long press state when focus is lost
|
// Reset long press state when focus is lost
|
||||||
if (!_hubFocusNode.hasFocus) {
|
if (!_hubFocusNode.hasFocus) {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
_longPressTriggered = false;
|
|
||||||
} else {
|
} else {
|
||||||
_notifyFocusedItemChanged();
|
_notifyFocusedItemChanged();
|
||||||
}
|
}
|
||||||
@@ -237,35 +229,13 @@ class HubSectionState extends State<HubSection> with MountedSetStateMixin, Skele
|
|||||||
KeyEventResult _handleKeyEvent(FocusNode _, KeyEvent event) {
|
KeyEventResult _handleKeyEvent(FocusNode _, KeyEvent event) {
|
||||||
final key = event.logicalKey;
|
final key = event.logicalKey;
|
||||||
|
|
||||||
if (key.isSelectKey) {
|
final selectResult = _selectLongPress.handleKeyEvent(
|
||||||
if (event is KeyDownEvent) {
|
event,
|
||||||
if (!_isSelectKeyDown) {
|
isOwnerActive: () => mounted,
|
||||||
_isSelectKeyDown = true;
|
onShortPress: _activateCurrentItem,
|
||||||
_longPressTriggered = false;
|
onLongPress: _showContextMenuForCurrentItem,
|
||||||
_longPressTimer?.cancel();
|
);
|
||||||
_longPressTimer = Timer(_longPressDuration, () {
|
if (selectResult != KeyEventResult.ignored) return selectResult;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (widget.onBack != null) {
|
if (widget.onBack != null) {
|
||||||
final backResult = handleBackKeyAction(event, widget.onBack!);
|
final backResult = handleBackKeyAction(event, widget.onBack!);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import 'package:material_symbols_icons/symbols.dart';
|
|||||||
|
|
||||||
import '../focus/card_focus_scope.dart';
|
import '../focus/card_focus_scope.dart';
|
||||||
import '../focus/dpad_navigator.dart';
|
import '../focus/dpad_navigator.dart';
|
||||||
|
import '../focus/dpad_select_long_press_controller.dart';
|
||||||
import '../focus/focus_theme.dart';
|
import '../focus/focus_theme.dart';
|
||||||
import '../focus/key_event_utils.dart';
|
import '../focus/key_event_utils.dart';
|
||||||
import '../focus/locked_hub_controller.dart';
|
import '../focus/locked_hub_controller.dart';
|
||||||
@@ -385,7 +386,6 @@ class TvBrowseRail extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class TvBrowseRailState extends State<TvBrowseRail> {
|
class TvBrowseRailState extends State<TvBrowseRail> {
|
||||||
static const _longPressDuration = Duration(milliseconds: 500);
|
|
||||||
// No-touch fallback only: clear suppression even if no select key-up is seen
|
// 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
|
// (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.
|
// the gesture path instead, which is bounded by the physical touch.
|
||||||
@@ -421,12 +421,10 @@ class TvBrowseRailState extends State<TvBrowseRail> {
|
|||||||
final _RailFocusModel _focusModel = _RailFocusModel();
|
final _RailFocusModel _focusModel = _RailFocusModel();
|
||||||
List<double> _sectionOffsets = const [];
|
List<double> _sectionOffsets = const [];
|
||||||
double _sectionMaxScrollExtent = 0;
|
double _sectionMaxScrollExtent = 0;
|
||||||
Timer? _longPressTimer;
|
final _selectLongPress = DpadSelectLongPressController();
|
||||||
Timer? _selectSuppressionTimer;
|
Timer? _selectSuppressionTimer;
|
||||||
Timer? _selectSuppressionMaxTimer;
|
Timer? _selectSuppressionMaxTimer;
|
||||||
VoidCallback? _gestureSignalListener;
|
VoidCallback? _gestureSignalListener;
|
||||||
bool _isSelectKeyDown = false;
|
|
||||||
bool _longPressTriggered = false;
|
|
||||||
bool _suppressSelectUntilKeyUp = false;
|
bool _suppressSelectUntilKeyUp = false;
|
||||||
bool _hasUserChangedHub = false;
|
bool _hasUserChangedHub = false;
|
||||||
bool _hasUserChangedItem = false;
|
bool _hasUserChangedItem = false;
|
||||||
@@ -599,7 +597,7 @@ class TvBrowseRailState extends State<TvBrowseRail> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.dispose();
|
||||||
_selectSuppressionTimer?.cancel();
|
_selectSuppressionTimer?.cancel();
|
||||||
_selectSuppressionMaxTimer?.cancel();
|
_selectSuppressionMaxTimer?.cancel();
|
||||||
_detachGestureSignalListener();
|
_detachGestureSignalListener();
|
||||||
@@ -623,9 +621,7 @@ class TvBrowseRailState extends State<TvBrowseRail> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _resetLongPressState() {
|
void _resetLongPressState() {
|
||||||
_longPressTimer?.cancel();
|
_selectLongPress.reset();
|
||||||
_isSelectKeyDown = false;
|
|
||||||
_longPressTriggered = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _clearSelectSuppression() {
|
void _clearSelectSuppression() {
|
||||||
@@ -698,29 +694,12 @@ class TvBrowseRailState extends State<TvBrowseRail> {
|
|||||||
return KeyEventResult.handled;
|
return KeyEventResult.handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event is KeyDownEvent) {
|
return _selectLongPress.handleKeyEvent(
|
||||||
if (!_isSelectKeyDown) {
|
event,
|
||||||
_isSelectKeyDown = true;
|
isOwnerActive: () => mounted,
|
||||||
_longPressTriggered = false;
|
onShortPress: _activateCurrentItem,
|
||||||
_longPressTimer?.cancel();
|
onLongPress: _showContextMenuForCurrentItem,
|
||||||
_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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (widget.onBack != null) {
|
if (widget.onBack != null) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user