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 { abstract class AppFlowyKeyboardService {
/// Enables shortcuts service. /// Enables IME shortcuts service.
void enable(); void enable();
/// Disables shortcuts service. /// Disables IME shortcuts service.
/// ///
/// In some cases, if your custom component needs to monitor /// In some cases, if your custom component needs to monitor
/// keyboard events separately, /// keyboard events separately,
@ -33,6 +33,12 @@ abstract class AppFlowyKeyboardService {
UnfocusDisposition disposition = UnfocusDisposition.scope, UnfocusDisposition disposition = UnfocusDisposition.scope,
}); });
/// Enable the keyboard shortcuts
void enableShortcuts();
/// Disable the keyboard shortcuts
void disableShortcuts();
/// Closes the keyboard /// Closes the keyboard
/// ///
/// Used in mobile /// Used in mobile

View file

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