Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pressable supports keyboard events #346

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 67 additions & 54 deletions packages/mix/lib/src/widgets/widget_state/pressable_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ abstract class _MixStateWidgetBuilderState<T extends _MixStateWidgetBuilder>
);
int _pressCount = 0;

late final Map<Type, Action<Intent>> _actionMap = <Type, Action<Intent>>{
ActivateIntent: CallbackAction<ActivateIntent>(onInvoke: activateOnIntent),
ButtonActivateIntent:
CallbackAction<ButtonActivateIntent>(onInvoke: activateOnIntent),
};

void _handleFocusUpdate(bool hasFocus) {
updateState(() => _isFocused = hasFocus);
}
Expand Down Expand Up @@ -235,6 +241,31 @@ abstract class _MixStateWidgetBuilderState<T extends _MixStateWidgetBuilder>
}
}

void _onTapUp(TapUpDetails details) {
if (_preventEvent) return;
_handlePressUpdate(false);
}

void _onTapCancel() {
if (_preventEvent) return;
_handlePressUpdate(false);
}

void _onLongPressStart(LongPressStartDetails details) {
if (_preventEvent) return;
handleLongPressUpdate(true);
}

void _onLongPressEnd(LongPressEndDetails details) {
if (_preventEvent) return;
handleLongPressUpdate(false);
}

void _onLongPressCancel() {
if (_preventEvent) return;
handleLongPressUpdate(false);
}

void handleLongPressUpdate(bool isLongPressed) {
if (isLongPressed == _isLongPressed) return;

Expand Down Expand Up @@ -265,70 +296,52 @@ abstract class _MixStateWidgetBuilderState<T extends _MixStateWidgetBuilder>
if (widget.enableFeedback) Feedback.forTap(context);
}

void _onTapUp(TapUpDetails details) {
if (_preventEvent) return;
_handlePressUpdate(false);
}

void _onTapCancel() {
if (_preventEvent) return;
_handlePressUpdate(false);
}

void _onLongPressStart(LongPressStartDetails details) {
if (_preventEvent) return;
handleLongPressUpdate(true);
}

void _onLongPressEnd(LongPressEndDetails details) {
if (_preventEvent) return;
handleLongPressUpdate(false);
}

void _onLongPressCancel() {
if (_preventEvent) return;
handleLongPressUpdate(false);
}

void handleOnLongPress() {
if (_preventEvent) return;

widget.onLongPress?.call();
if (widget.enableFeedback) Feedback.forLongPress(context);
}

void activateOnIntent(Intent? intent) {
handleOnPress();
}

@override
Widget build(BuildContext context) {
return GestureDetector(
onTapUp: _onTapUp,
onTap: handleOnPress,
onTapCancel: _onTapCancel,
onLongPressCancel: _onLongPressCancel,
onLongPress: handleOnLongPress,
onLongPressStart: _onLongPressStart,
onLongPressEnd: _onLongPressEnd,
onPanDown: _handlePanDown,
onPanUpdate: _handlePanUpdate,
onPanEnd: _handlePanUp,
behavior: widget.hitTestBehavior,
child: CustomFocusableActionDetector(
enabled: widget.enabled,
focusNode: widget.focusNode,
autofocus: widget.autofocus,
onShowFocusHighlight: _handleFocusUpdate,
onShowHoverHighlight: _handleHoverUpdate,
onFocusChange: widget.onFocusChange,
onMouseEnter: _handleOnMouseEnter,
onMouseExit: _handleOnMouseExit,
onMouseHover: _handleMouseHover,
child: WidgetStateModel(
return Actions(
actions: _actionMap,
child: GestureDetector(
onTapUp: _onTapUp,
onTap: handleOnPress,
onTapCancel: _onTapCancel,
onLongPressCancel: _onLongPressCancel,
onLongPress: handleOnLongPress,
onLongPressStart: _onLongPressStart,
onLongPressEnd: _onLongPressEnd,
onPanDown: _handlePanDown,
onPanUpdate: _handlePanUpdate,
onPanEnd: _handlePanUp,
behavior: widget.hitTestBehavior,
child: CustomFocusableActionDetector(
enabled: widget.enabled,
hovered: _isHovered,
focused: _isFocused,
pressed: _isPressed,
longPressed: _isLongPressed,
pointerPosition: _pointerPosition,
child: widget.child,
focusNode: widget.focusNode,
autofocus: widget.autofocus,
onShowFocusHighlight: _handleFocusUpdate,
onShowHoverHighlight: _handleHoverUpdate,
onFocusChange: widget.onFocusChange,
onMouseEnter: _handleOnMouseEnter,
onMouseExit: _handleOnMouseExit,
onMouseHover: _handleMouseHover,
child: WidgetStateModel(
enabled: widget.enabled,
hovered: _isHovered,
focused: _isFocused,
pressed: _isPressed,
longPressed: _isLongPressed,
pointerPosition: _pointerPosition,
child: widget.child,
),
),
),
);
Expand Down
49 changes: 49 additions & 0 deletions packages/mix/test/src/widgets/pressable/pressable_widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mix/mix.dart';

Expand Down Expand Up @@ -104,6 +105,30 @@ void main() {
expect(counter, 0);
},
);

testWidgets('Pressable responds to keyboard events',
(WidgetTester tester) async {
var wasPressed = false;

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Pressable(
autofocus: true,
unpressDelay: Duration.zero,
onPress: () {
wasPressed = true;
},
child: const Text('Tap me'),
),
),
),
);

await tester.sendKeyEvent(LogicalKeyboardKey.enter);

expect(wasPressed, isTrue);
});
});

group('PressableBox', () {
Expand Down Expand Up @@ -159,6 +184,30 @@ void main() {
},
);

testWidgets('PressableBox responds to keyboard events',
(WidgetTester tester) async {
var wasPressed = false;

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: PressableBox(
autofocus: true,
unpressDelay: Duration.zero,
onPress: () {
wasPressed = true;
},
child: const Text('Tap me'),
),
),
),
);

await tester.sendKeyEvent(LogicalKeyboardKey.enter);

expect(wasPressed, isTrue);
});

testWidgets(r'must change to attributes in $on.hover variant when hovered',
(WidgetTester tester) async {
await pumpTestCase(
Expand Down
Loading