Skip to content

Commit

Permalink
Merge pull request #9 from aymswick/fix/flutter_RawKeyboardEvent_depr…
Browse files Browse the repository at this point in the history
…ecation

Fixes #6 Migrate RawKeyboardEvent and related implications according …
  • Loading branch information
doonfrs authored Feb 18, 2024
2 parents 36ed914 + 08879cb commit 29e2ada
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 40 deletions.
13 changes: 7 additions & 6 deletions lib/src/helper/pluto_key_manager_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/services.dart';

class PlutoKeyManagerEvent {
FocusNode focusNode;
RawKeyEvent event;
KeyEvent event;

PlutoKeyManagerEvent({
required this.focusNode,
Expand All @@ -12,9 +12,9 @@ class PlutoKeyManagerEvent {

bool get needsThrottle => isMoving || isTab || isPageUp || isPageDown;

bool get isKeyDownEvent => event.runtimeType == RawKeyDownEvent;
bool get isKeyDownEvent => event.runtimeType == KeyDownEvent;

bool get isKeyUpEvent => event.runtimeType == RawKeyUpEvent;
bool get isKeyUpEvent => event.runtimeType == KeyUpEvent;

bool get isMoving => isHorizontal || isVertical;

Expand Down Expand Up @@ -94,15 +94,16 @@ class PlutoKeyManagerEvent {
}

bool get isShiftPressed {
return event.isShiftPressed;
return HardwareKeyboard.instance.isShiftPressed;
}

bool get isCtrlPressed {
return event.isMetaPressed || event.isControlPressed;
return HardwareKeyboard.instance.isMetaPressed ||
HardwareKeyboard.instance.isControlPressed;
}

bool get isAltPressed {
return event.isAltPressed;
return HardwareKeyboard.instance.isAltPressed;
}

bool get isModifierPressed {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/manager/pluto_grid_key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class PlutoGridKeyManager {
if (stateManager.configuration.shortcut.handle(
keyEvent: keyEvent,
stateManager: stateManager,
state: RawKeyboard.instance,
state: HardwareKeyboard.instance,
)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/manager/shortcut/pluto_grid_shortcut.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlutoGridShortcut {
bool handle({
required PlutoKeyManagerEvent keyEvent,
required PlutoGridStateManager stateManager,
required RawKeyboard state,
required HardwareKeyboard state,
}) {
for (final action in actions.entries) {
if (action.key.accepts(keyEvent.event, state)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/manager/shortcut/pluto_grid_shortcut_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class PlutoGridActionDefaultTab extends PlutoGridShortcutAction {

final saveIsEditing = stateManager.isEditing;

keyEvent.event.isShiftPressed
keyEvent.isShiftPressed
? _moveCellPrevious(stateManager)
: _moveCellNext(stateManager);

Expand Down Expand Up @@ -408,7 +408,7 @@ class PlutoGridActionDefaultEnterKey extends PlutoGridShortcutAction {
}

if (enterKeyAction.isEditingAndMoveDown) {
if (keyEvent.event.isShiftPressed) {
if (keyEvent.isShiftPressed) {
stateManager.moveCurrentCell(
PlutoMoveDirection.up,
notify: false,
Expand All @@ -420,7 +420,7 @@ class PlutoGridActionDefaultEnterKey extends PlutoGridShortcutAction {
);
}
} else if (enterKeyAction.isEditingAndMoveRight) {
if (keyEvent.event.isShiftPressed) {
if (keyEvent.isShiftPressed) {
stateManager.moveCurrentCell(
PlutoMoveDirection.left,
force: true,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/pluto_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ class PlutoGridState extends PlutoStateWithChange<PlutoGrid> {
}
}

KeyEventResult _handleGridFocusOnKey(FocusNode focusNode, RawKeyEvent event) {
KeyEventResult _handleGridFocusOnKey(FocusNode focusNode, KeyEvent event) {
if (_keyManager.eventResult.isSkip == false) {
_keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: focusNode,
Expand All @@ -608,7 +608,7 @@ class PlutoGridState extends PlutoStateWithChange<PlutoGrid> {
Widget build(BuildContext context) {
return FocusScope(
onFocusChange: _stateManager.setKeepFocus,
onKey: _handleGridFocusOnKey,
onKeyEvent: _handleGridFocusOnKey,
child: _GridContainer(
stateManager: _stateManager,
child: LayoutBuilder(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/ui/cells/popup_cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mixin PopupCellState<T extends PopupCell> on State<T>
..text =
widget.column.formattedValueForDisplayInEditing(widget.cell.value);

textFocus = FocusNode(onKey: _handleKeyboardFocusOnKey);
textFocus = FocusNode(onKeyEvent: _handleKeyboardFocusOnKey);
}

@override
Expand Down Expand Up @@ -175,7 +175,7 @@ mixin PopupCellState<T extends PopupCell> on State<T>
}
}

KeyEventResult _handleKeyboardFocusOnKey(FocusNode node, RawKeyEvent event) {
KeyEventResult _handleKeyboardFocusOnKey(FocusNode node, KeyEvent event) {
var keyManager = PlutoKeyManagerEvent(
focusNode: node,
event: event,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/ui/cells/text_cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mixin TextCellState<T extends TextCell> on State<T> implements TextFieldProps {
void initState() {
super.initState();

cellFocus = FocusNode(onKey: _handleOnKey);
cellFocus = FocusNode(onKeyEvent: _handleOnKey);

widget.stateManager.setTextEditingController(_textController);

Expand Down Expand Up @@ -172,7 +172,7 @@ mixin TextCellState<T extends TextCell> on State<T> implements TextFieldProps {
});
}

KeyEventResult _handleOnKey(FocusNode node, RawKeyEvent event) {
KeyEventResult _handleOnKey(FocusNode node, KeyEvent event) {
var keyManager = PlutoKeyManagerEvent(
focusNode: node,
event: event,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/ui/columns/pluto_column_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class PlutoColumnFilterState extends PlutoStateWithChange<PlutoColumnFilter> {
initState() {
super.initState();

_focusNode = FocusNode(onKey: _handleOnKey);
_focusNode = FocusNode(onKeyEvent: _handleOnKey);

widget.column.setFilterFocusNode(_focusNode);

Expand Down Expand Up @@ -145,7 +145,7 @@ class PlutoColumnFilterState extends PlutoStateWithChange<PlutoColumnFilter> {
stateManager.notifyListeners();
}

KeyEventResult _handleOnKey(FocusNode node, RawKeyEvent event) {
KeyEventResult _handleOnKey(FocusNode node, KeyEvent event) {
var keyManager = PlutoKeyManagerEvent(
focusNode: node,
event: event,
Expand Down
23 changes: 16 additions & 7 deletions test/src/helper/pluto_key_manager_event_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void main() {

late PlutoKeyManagerEvent? keyManagerEvent;

KeyEventResult callback(FocusNode node, RawKeyEvent event) {
KeyEventResult callback(FocusNode node, KeyEvent event) {
keyManagerEvent = PlutoKeyManagerEvent(
focusNode: node,
event: event,
Expand All @@ -23,17 +23,16 @@ void main() {

tearDown(() {
keyManagerEvent = null;
focusNode.dispose();
});

Future<void> buildWidget({
required WidgetTester tester,
required KeyEventResult Function(FocusNode, RawKeyEvent) callback,
required KeyEventResult Function(FocusNode, KeyEvent) callback,
}) async {
await tester.pumpWidget(MaterialApp(
home: FocusScope(
autofocus: true,
onKey: callback,
onKeyEvent: callback,
child: Focus(
focusNode: focusNode,
child: const SizedBox(width: 100, height: 100),
Expand Down Expand Up @@ -62,7 +61,7 @@ void main() {
(tester) async {
late PlutoKeyManagerEvent keyManagerEvent;

KeyEventResult callback(FocusNode node, RawKeyEvent event) {
KeyEventResult callback(FocusNode node, KeyEvent event) {
keyManagerEvent = PlutoKeyManagerEvent(
focusNode: node,
event: event,
Expand Down Expand Up @@ -140,15 +139,25 @@ void main() {
},
);

// While key combos still work in the real world, these 3 tests are failing due to what I suspect is an
// incomplete deprecation/migration from focusNode `onKey` to `onKeyEvent`.
// Flutter 3.19 does not trigger our event for `sendKeyUpEvent` only, and I prefer not
// to switch these tests to `sendKeyDownEvent` as that may cause unexpected behavior
// such as pasting multiple times due to repeating key presses. It might also be fine.

// https://github.com/flutter/flutter/issues/136419
testWidgets(
'Control + C 키를 입력하면 isCtrlC 가 true 여야 한다.',
(tester) async {
await buildWidget(tester: tester, callback: callback);

const key = LogicalKeyboardKey.control;
const key2 = LogicalKeyboardKey.keyC;
await tester.sendKeyDownEvent(key);
await tester.sendKeyUpEvent(LogicalKeyboardKey.keyC);
expect(keyManagerEvent!.isCtrlC, true);
await tester.sendKeyUpEvent(
key2); // sendKeyUpEvent is not sending a keyManagerEvent

expect(keyManagerEvent?.isCtrlC, true);
await tester.sendKeyUpEvent(key);
},
);
Expand Down
28 changes: 14 additions & 14 deletions test/src/manager/pluto_grid_key_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
onKey: (event) {
child: KeyboardListener(
onKeyEvent: (event) {
keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: FocusNode(),
event: event,
Expand Down Expand Up @@ -98,8 +98,8 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
onKey: (event) {
child: KeyboardListener(
onKeyEvent: (event) {
keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: FocusNode(),
event: event,
Expand Down Expand Up @@ -150,8 +150,8 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
onKey: (event) {
child: KeyboardListener(
onKeyEvent: (event) {
keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: FocusNode(),
event: event,
Expand Down Expand Up @@ -204,8 +204,8 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
onKey: (event) {
child: KeyboardListener(
onKeyEvent: (event) {
keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: FocusNode(),
event: event,
Expand Down Expand Up @@ -258,8 +258,8 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
onKey: (event) {
child: KeyboardListener(
onKeyEvent: (event) {
keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: FocusNode(),
event: event,
Expand Down Expand Up @@ -311,8 +311,8 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
onKey: (event) {
child: KeyboardListener(
onKeyEvent: (event) {
keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: FocusNode(),
event: event,
Expand Down Expand Up @@ -440,8 +440,8 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: RawKeyboardListener(
onKey: (event) {
child: KeyboardListener(
onKeyEvent: (event) {
keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: FocusNode(),
event: event,
Expand Down

0 comments on commit 29e2ada

Please sign in to comment.