Skip to content

Commit

Permalink
feat: try to add ime client.
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitism committed Jan 13, 2025
1 parent c7be42e commit 685996b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IPv4Box>((o, e) => o.OnFormatChange(e));
IPAddressProperty.Changed.AddClassHandler<IPv4Box>((o, e) => o.OnIPChanged(e));
TextInputMethodClientRequestedEvent.AddClassHandler<IPv4Box>((tb, e) =>
{
e.Client = tb._imClient;
});
}

#region Overrides
Expand Down
44 changes: 44 additions & 0 deletions src/Ursa/Controls/IPv4Box/IPv4BoxInputMethodClient.cs
Original file line number Diff line number Diff line change
@@ -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<AvaloniaPropertyChangedEventArgs>(this.OnParentPropertyChanged);
this._parent = parent;
if (this._parent != null)
this._parent.PropertyChanged += new EventHandler<AvaloniaPropertyChangedEventArgs>(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();
}
}

0 comments on commit 685996b

Please sign in to comment.