TextEditBuffer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using Android.Text;
  3. using Android.Views;
  4. using Android.Views.InputMethods;
  5. using Avalonia.Android.Platform.SkiaPlatform;
  6. using Avalonia.Input.TextInput;
  7. namespace Avalonia.Android.Platform.Input
  8. {
  9. internal class TextEditBuffer
  10. {
  11. private readonly IAndroidInputMethod _textInputMethod;
  12. private readonly TopLevelImpl _topLevel;
  13. private TextSelection? _composition;
  14. public TextEditBuffer(IAndroidInputMethod textInputMethod, TopLevelImpl topLevel)
  15. {
  16. _textInputMethod = textInputMethod;
  17. _topLevel = topLevel;
  18. }
  19. public bool HasComposition => Composition is { } composition && composition.Start != composition.End;
  20. public TextSelection Selection
  21. {
  22. get
  23. {
  24. var selection = _textInputMethod.Client?.Selection ?? default;
  25. return new TextSelection(Math.Min(selection.Start, selection.End), Math.Max(selection.Start, selection.End));
  26. }
  27. set
  28. {
  29. if (_textInputMethod.Client is { } client)
  30. client.Selection = value;
  31. }
  32. }
  33. public TextSelection? Composition
  34. {
  35. get => _composition; set
  36. {
  37. if (value is { } v)
  38. {
  39. var text = Text;
  40. var start = Math.Clamp(v.Start, 0, text.Length);
  41. var end = Math.Clamp(v.End, 0, text.Length);
  42. _composition = new TextSelection(Math.Min(start, end), Math.Max(start, end));
  43. }
  44. else
  45. _composition = null;
  46. }
  47. }
  48. public string? SelectedText
  49. {
  50. get
  51. {
  52. if(_textInputMethod.Client is not { } client || Selection.Start < 0 || Selection.End >= client.SurroundingText.Length)
  53. {
  54. return "";
  55. }
  56. var selection = Selection;
  57. return client.SurroundingText.Substring(selection.Start, selection.End - selection.Start);
  58. }
  59. }
  60. public string? ComposingText
  61. {
  62. get => !HasComposition ? null : Text?.Substring(Composition!.Value.Start, Composition!.Value.End - Composition!.Value.Start); set
  63. {
  64. if (HasComposition)
  65. {
  66. var start = Composition!.Value.Start;
  67. Replace(Composition!.Value.Start, Composition!.Value.End, value ?? "");
  68. Composition = new TextSelection(start, start + (value?.Length ?? 0));
  69. }
  70. else
  71. {
  72. var selection = Selection;
  73. Replace(selection.Start, selection.End, value ?? "");
  74. Composition = new TextSelection(selection.Start, selection.Start + (value?.Length ?? 0));
  75. }
  76. }
  77. }
  78. public string Text => _textInputMethod.Client?.SurroundingText ?? "";
  79. public ExtractedText? ExtractedText => new ExtractedText
  80. {
  81. Flags = Text.Contains('\n') ? 0 : ExtractedTextFlags.SingleLine,
  82. PartialStartOffset = -1,
  83. PartialEndOffset = Text.Length,
  84. SelectionStart = Selection.Start,
  85. SelectionEnd = Selection.End,
  86. StartOffset = 0,
  87. Text = new Java.Lang.String(Text)
  88. };
  89. internal void Remove(int index, int length)
  90. {
  91. if (_textInputMethod.Client is { } client)
  92. {
  93. client.Selection = new TextSelection(index, index + length);
  94. if (length > 0)
  95. _textInputMethod?.View.DispatchKeyEvent(new KeyEvent(KeyEventActions.Down, Keycode.ForwardDel));
  96. }
  97. }
  98. internal void Replace(int start, int end, string text)
  99. {
  100. if (_textInputMethod.Client is { } client)
  101. {
  102. var realStart = Math.Min(start, end);
  103. var realEnd = Math.Max(start, end);
  104. if (realEnd > realStart)
  105. {
  106. client.Selection = new TextSelection(realStart, realEnd);
  107. _textInputMethod?.View.DispatchKeyEvent(new KeyEvent(KeyEventActions.Down, Keycode.ForwardDel));
  108. }
  109. _topLevel.TextInput(text);
  110. var index = realStart + text.Length;
  111. client.Selection = new TextSelection(index, index);
  112. Composition = null;
  113. }
  114. }
  115. }
  116. }