Keep keys typed in a terminal out of app shortcuts
Terminals are marked as typing surfaces. While one has focus, the chat's "/ jumps to the composer", the Search tab's Escape, Ctrl+/, Ctrl+1…9 and Ctrl+Shift+arrow pane moves leave the key to the terminal, like they do for text fields.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// Marks a widget that takes raw typing without being an [EditableText], such
|
||||
/// as a terminal emulator. While focus is inside it, app-wide shortcuts that
|
||||
/// would otherwise act on plain keys (`/` to the composer, Escape, Ctrl+/,
|
||||
/// Ctrl+1…9, Ctrl+Shift+arrows) leave the keys to it.
|
||||
class TypingSurface extends StatelessWidget {
|
||||
const TypingSurface({super.key, required this.child});
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => child;
|
||||
}
|
||||
|
||||
/// Whether keyboard focus is inside a [TypingSurface].
|
||||
bool primaryFocusIsTypingSurface() {
|
||||
final context = FocusManager.instance.primaryFocus?.context;
|
||||
if (context == null) return false;
|
||||
if (context.widget is TypingSurface) return true;
|
||||
return context.findAncestorWidgetOfExactType<TypingSurface>() != null;
|
||||
}
|
||||
|
||||
/// Whether keyboard focus is in something that takes typing: an editable
|
||||
/// text field or a [TypingSurface].
|
||||
bool primaryFocusTakesTyping() {
|
||||
final context = FocusManager.instance.primaryFocus?.context;
|
||||
if (context == null) return false;
|
||||
if (primaryFocusIsTypingSurface()) return true;
|
||||
if (context.widget case final EditableText field) return !field.readOnly;
|
||||
final editable = context.findAncestorWidgetOfExactType<EditableText>();
|
||||
return editable != null && !editable.readOnly;
|
||||
}
|
||||
Reference in New Issue
Block a user