input_behavior_default.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:flutter/services.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:xterm/frontend/input_behavior.dart';
  4. import 'package:xterm/frontend/input_map.dart';
  5. import 'package:xterm/xterm.dart';
  6. class InputBehaviorDefault extends InputBehavior {
  7. InputBehaviorDefault();
  8. @override
  9. bool get acceptKeyStroke => true;
  10. @override
  11. TextEditingValue get initEditingState => TextEditingValue.empty;
  12. @override
  13. void onKeyStroke(RawKeyEvent event, TerminalUiInteraction terminal) {
  14. if (event is! RawKeyDownEvent) {
  15. return;
  16. }
  17. final key = inputMap(event.logicalKey);
  18. if (key != null) {
  19. terminal.keyInput(
  20. key,
  21. ctrl: event.isControlPressed,
  22. alt: event.isAltPressed,
  23. shift: event.isShiftPressed,
  24. mac: terminal.platform.useMacInputBehavior,
  25. character: event.character,
  26. );
  27. }
  28. }
  29. String? _composingString = null;
  30. TextEditingValue? _lastEditingState;
  31. @override
  32. TextEditingValue? onTextEdit(
  33. TextEditingValue value, TerminalUiInteraction terminal) {
  34. var inputText = value.text;
  35. // we just want to detect if a composing is going on and notify the terminal
  36. // about it
  37. if (value.composing.start != value.composing.end) {
  38. _composingString = inputText;
  39. terminal.updateComposingString(_composingString!);
  40. _lastEditingState = value;
  41. return null;
  42. }
  43. //when we reach this point the composing state is over
  44. if (_composingString != null) {
  45. _composingString = null;
  46. terminal.updateComposingString('');
  47. }
  48. //this is a hack to bypass some race condition in the input system
  49. //we just take the last rune if there are more than one as it sometimes
  50. //happens that the last value is still part of the new value
  51. if (_lastEditingState?.text.isNotEmpty == true) {
  52. if (inputText.length > _lastEditingState!.text.length) {
  53. inputText = inputText.substring(_lastEditingState!.text.length);
  54. }
  55. }
  56. if (inputText.isNotEmpty) {
  57. terminal.raiseOnInput(inputText);
  58. }
  59. _lastEditingState = value;
  60. if (value == TextEditingValue.empty || inputText == '') {
  61. return null;
  62. } else {
  63. return TextEditingValue.empty;
  64. }
  65. }
  66. @override
  67. void onAction(TextInputAction action, TerminalUiInteraction terminal) {
  68. //
  69. }
  70. }