input_behavior_default.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. );
  26. }
  27. }
  28. String? _composingString = null;
  29. @override
  30. TextEditingValue? onTextEdit(
  31. TextEditingValue value, TerminalUiInteraction terminal) {
  32. var inputText = value.text;
  33. // we just want to detect if a composing is going on and notify the terminal
  34. // about it
  35. if (value.composing.start != value.composing.end) {
  36. _composingString = inputText;
  37. terminal.updateComposingString(_composingString!);
  38. return null;
  39. }
  40. //when we reach this point the composing state is over
  41. if (_composingString != null) {
  42. _composingString = null;
  43. terminal.updateComposingString('');
  44. }
  45. //this is a hack to bypass some race condition in the input system
  46. //we just take the last rune if there are more than one as it sometimes
  47. //happens that the last value is still part of the new value
  48. if(inputText.runes.length > 1) {
  49. inputText = String.fromCharCode(inputText.runes.last);
  50. }
  51. terminal.raiseOnInput(inputText);
  52. if (value == TextEditingValue.empty || inputText == '') {
  53. return null;
  54. } else {
  55. return TextEditingValue.empty;
  56. }
  57. }
  58. @override
  59. void onAction(TextInputAction action, TerminalUiInteraction terminal) {
  60. //
  61. }
  62. }