feat: add flag to disable keyboard shortcuts (#1049)

This commit is contained in:
Lucas 2025-03-06 14:25:44 +08:00 • committed by GitHub
commit 866aea4dac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 11 deletions

View file

@ -18,10 +18,10 @@ import 'package:flutter/services.dart';
/// ```
///
abstract class AppFlowyKeyboardService {
/// Enables shortcuts service.
/// Enables IME shortcuts service.
void enable();
/// Disables shortcuts service.
/// Disables IME shortcuts service.
///
/// In some cases, if your custom component needs to monitor
/// keyboard events separately,
@ -33,6 +33,12 @@ abstract class AppFlowyKeyboardService {
UnfocusDisposition disposition = UnfocusDisposition.scope,
});
/// Enable the keyboard shortcuts
void enableShortcuts();
/// Disable the keyboard shortcuts
void disableShortcuts();
/// Closes the keyboard
///
/// Used in mobile

View file

@ -43,7 +43,10 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
Selection? previousSelection;
// use for IME only
bool enableShortcuts = true;
bool enableIMEShortcuts = true;
// use for hardware keyboard only
bool enableKeyboardShortcuts = true;
@override
void initState() {
@ -55,7 +58,7 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
interceptor = SelectionGestureInterceptor(
key: 'keyboard',
canTap: (details) {
enableShortcuts = true;
enableIMEShortcuts = true;
focusNode.requestFocus();
textInputService.close();
return true;
@ -90,11 +93,24 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
void disable({
bool showCursor = false,
UnfocusDisposition disposition = UnfocusDisposition.previouslyFocusedChild,
}) =>
focusNode.unfocus(disposition: disposition);
}) {
focusNode.unfocus(disposition: disposition);
}
@override
void enable() => focusNode.requestFocus();
void enable() {
focusNode.requestFocus();
}
@override
void enableShortcuts() {
enableKeyboardShortcuts = true;
}
@override
void disableShortcuts() {
enableKeyboardShortcuts = false;
}
// Used in mobile only
@override
@ -148,8 +164,12 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
/// handle hardware keyboard
KeyEventResult _onKeyEvent(FocusNode node, KeyEvent event) {
if (!enableKeyboardShortcuts) {
return KeyEventResult.ignored;
}
if ((event is! KeyDownEvent && event is! KeyRepeatEvent) ||
!enableShortcuts) {
!enableIMEShortcuts) {
if (textInputService.composingTextRange != TextRange.empty) {
return KeyEventResult.skipRemainingHandlers;
}
@ -193,7 +213,7 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
// return;
// }
enableShortcuts = true;
enableIMEShortcuts = true;
if (selection == null) {
textInputService.close();
@ -234,9 +254,9 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
),
);
// disable shortcuts when the IME active
enableShortcuts = textEditingValue.composing == TextRange.empty;
enableIMEShortcuts = textEditingValue.composing == TextRange.empty;
} else {
enableShortcuts = true;
enableIMEShortcuts = true;
}
}