input_behavior_default.dart 2.3 KB

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