AvaloniaView.Text.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #nullable enable
  2. using Avalonia.Input.TextInput;
  3. using JetBrains.Annotations;
  4. using UIKit;
  5. namespace Avalonia.iOS;
  6. public partial class AvaloniaView
  7. {
  8. private const string ImeLog = "IOSIME";
  9. private Rect _cursorRect;
  10. private TextInputOptions? _options;
  11. private static UIResponder? CurrentAvaloniaResponder { get; set; }
  12. public override bool BecomeFirstResponder()
  13. {
  14. var res = base.BecomeFirstResponder();
  15. if (res)
  16. CurrentAvaloniaResponder = this;
  17. return res;
  18. }
  19. public override bool ResignFirstResponder()
  20. {
  21. var res = base.ResignFirstResponder();
  22. if (res && ReferenceEquals(CurrentAvaloniaResponder, this))
  23. CurrentAvaloniaResponder = null;
  24. return res;
  25. }
  26. private bool IsDrivingText => CurrentAvaloniaResponder is TextInputResponder t && ReferenceEquals(t.NextResponder, this);
  27. void ITextInputMethodImpl.SetClient(ITextInputMethodClient? client)
  28. {
  29. _client = client;
  30. if (_client == null && IsDrivingText)
  31. BecomeFirstResponder();
  32. if (_client is { })
  33. {
  34. new TextInputResponder(this, _client).BecomeFirstResponder();
  35. }
  36. }
  37. void ITextInputMethodImpl.SetCursorRect(Rect rect) => _cursorRect = rect;
  38. void ITextInputMethodImpl.SetOptions(TextInputOptions options) => _options = options;
  39. void ITextInputMethodImpl.Reset()
  40. {
  41. if (IsDrivingText)
  42. BecomeFirstResponder();
  43. }
  44. }