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

Bubble up key events incl. shortcuts that are not handled by PlutoGrid #1070

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion lib/src/manager/pluto_grid_key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class PlutoGridKeyManager {
_subscription = MergeStream([normalStream, movingStream]).listen(_handler);
}

bool isDefaultAction(PlutoKeyManagerEvent keyEvent){
return !keyEvent.isModifierPressed && keyEvent.isCharacter;
}

void _handler(PlutoKeyManagerEvent keyEvent) {
if (keyEvent.isKeyUpEvent) return;

Expand All @@ -88,7 +92,7 @@ class PlutoGridKeyManager {
}

void _handleDefaultActions(PlutoKeyManagerEvent keyEvent) {
if (!keyEvent.isModifierPressed && keyEvent.isCharacter) {
if (isDefaultAction(keyEvent)) {
_handleCharacter(keyEvent);
return;
}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/manager/shortcut/pluto_grid_shortcut.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class PlutoGridShortcut {
return false;
}

bool shortcutHasAction({
required PlutoKeyManagerEvent keyEvent,
required HardwareKeyboard state
}) {
var handledActions = actions.entries.where((action) => action.key.accepts(keyEvent.event, state));
return handledActions.isNotEmpty;
}

static final Map<ShortcutActivator, PlutoGridShortcutAction> defaultActions =
{
// Move cell focus
Expand Down
16 changes: 13 additions & 3 deletions lib/src/pluto_grid.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math';
import 'dart:ui';

import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart' show Intl;
Expand Down Expand Up @@ -595,13 +596,22 @@ class PlutoGridState extends PlutoStateWithChange<PlutoGrid> {

KeyEventResult _handleGridFocusOnKey(FocusNode focusNode, KeyEvent event) {
if (_keyManager.eventResult.isSkip == false) {
_keyManager.subject.add(PlutoKeyManagerEvent(
PlutoKeyManagerEvent plutoKeyEvent = PlutoKeyManagerEvent(
focusNode: focusNode,
event: event,
));
);
_keyManager.subject.add(plutoKeyEvent);

bool isDefaultAction = _keyManager.isDefaultAction(plutoKeyEvent);
bool shortcutHasAction = isDefaultAction ? false : stateManager.configuration.shortcut.shortcutHasAction(
keyEvent: plutoKeyEvent,
state: HardwareKeyboard.instance
);

return _keyManager.eventResult.consume(isDefaultAction || shortcutHasAction ? KeyEventResult.handled : KeyEventResult.ignored);
}

return _keyManager.eventResult.consume(KeyEventResult.handled);
return _keyManager.eventResult.consume(KeyEventResult.ignored);
}

@override
Expand Down