From 685996b56bfe3ddff4b7956ba0e8cbc3eede55a6 Mon Sep 17 00:00:00 2001 From: Dong Bin Date: Mon, 13 Jan 2025 14:02:48 +0800 Subject: [PATCH] feat: try to add ime client. --- src/Ursa/Controls/{ => IPv4Box}/IPv4Box.cs | 7 ++- .../IPv4Box/IPv4BoxInputMethodClient.cs | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) rename src/Ursa/Controls/{ => IPv4Box}/IPv4Box.cs (98%) create mode 100644 src/Ursa/Controls/IPv4Box/IPv4BoxInputMethodClient.cs diff --git a/src/Ursa/Controls/IPv4Box.cs b/src/Ursa/Controls/IPv4Box/IPv4Box.cs similarity index 98% rename from src/Ursa/Controls/IPv4Box.cs rename to src/Ursa/Controls/IPv4Box/IPv4Box.cs index cb1d2e857..4a41b704a 100644 --- a/src/Ursa/Controls/IPv4Box.cs +++ b/src/Ursa/Controls/IPv4Box/IPv4Box.cs @@ -7,6 +7,7 @@ using Avalonia.Data; using Avalonia.Input; using Avalonia.Input.Platform; +using Avalonia.Input.TextInput; using Avalonia.Interactivity; using Avalonia.Media; using Avalonia.Media.TextFormatting; @@ -96,11 +97,15 @@ public IPv4BoxInputMode InputMode get => GetValue(InputModeProperty); set => SetValue(InputModeProperty, value); } - + private readonly IPv4BoxInputMethodClient _imClient = new IPv4BoxInputMethodClient(); static IPv4Box() { ShowLeadingZeroProperty.Changed.AddClassHandler((o, e) => o.OnFormatChange(e)); IPAddressProperty.Changed.AddClassHandler((o, e) => o.OnIPChanged(e)); + TextInputMethodClientRequestedEvent.AddClassHandler((tb, e) => + { + e.Client = tb._imClient; + }); } #region Overrides diff --git a/src/Ursa/Controls/IPv4Box/IPv4BoxInputMethodClient.cs b/src/Ursa/Controls/IPv4Box/IPv4BoxInputMethodClient.cs new file mode 100644 index 000000000..c6812ec3b --- /dev/null +++ b/src/Ursa/Controls/IPv4Box/IPv4BoxInputMethodClient.cs @@ -0,0 +1,44 @@ +using Avalonia; +using Avalonia.Controls.Presenters; +using Avalonia.Input.TextInput; + +namespace Ursa.Controls; + +public class IPv4BoxInputMethodClient:TextInputMethodClient +{ + private TextPresenter? _presenter; + public override Visual TextViewVisual => _presenter; + public override bool SupportsPreedit => false; + public override bool SupportsSurroundingText => true; + + public override string SurroundingText + { + get; + } + public override Rect CursorRectangle { get; } + public override TextSelection Selection { get; set; } + private IPv4Box? _parent; + public void SetPresenter(TextPresenter? presenter, IPv4Box? parent) + { + if (this._parent != null) + this._parent.PropertyChanged -= new EventHandler(this.OnParentPropertyChanged); + this._parent = parent; + if (this._parent != null) + this._parent.PropertyChanged += new EventHandler(this.OnParentPropertyChanged); + TextPresenter presenter1 = this._presenter; + if (presenter1 != null) + { + presenter1.CaretBoundsChanged -= (EventHandler) ((s, e) => this.RaiseCursorRectangleChanged()); + } + this._presenter = presenter; + if (this._presenter != null) + this._presenter.CaretBoundsChanged += (EventHandler) ((s, e) => this.RaiseCursorRectangleChanged()); + this.RaiseTextViewVisualChanged(); + this.RaiseCursorRectangleChanged(); + } + + private void OnParentPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) + { + this.RaiseSelectionChanged(); + } +} \ No newline at end of file