Browse Source

Rework preedit handling

pull/10508/head
Benedikt Stebner 3 years ago
parent
commit
cb1d306482
  1. 33
      src/Avalonia.Controls/Presenters/TextPresenter.cs
  2. 6
      src/Avalonia.Controls/TextBoxTextInputMethodClient.cs

33
src/Avalonia.Controls/Presenters/TextPresenter.cs

@ -564,7 +564,7 @@ namespace Avalonia.Controls.Presenters
var foreground = Foreground;
if(_compositionRegion != null)
if (_compositionRegion != null)
{
var preeditHighlight = new ValueSpan<TextRunProperties>(_compositionRegion?.Start ?? 0, _compositionRegion?.Length ?? 0,
new GenericTextRunProperties(typeface, FontSize,
@ -851,7 +851,7 @@ namespace Avalonia.Controls.Presenters
CaretChanged();
}
private void UpdateCaret(CharacterHit characterHit, bool updateCaretIndex = true)
private void UpdateCaret(CharacterHit characterHit, bool notify = true)
{
_lastCharacterHit = characterHit;
@ -879,10 +879,14 @@ namespace Avalonia.Controls.Presenters
CaretBoundsChanged?.Invoke(this, EventArgs.Empty);
}
if (updateCaretIndex)
if (notify)
{
SetAndRaise(CaretIndexProperty, ref _caretIndex, caretIndex);
}
else
{
_caretIndex = caretIndex;
}
}
internal Rect GetCursorRectangle()
@ -899,24 +903,6 @@ namespace Avalonia.Controls.Presenters
_caretTimer.Tick -= CaretTimerTick;
}
protected void OnPreeditTextChanged(string? oldValue, string? newValue)
{
InvalidateTextLayout();
if (string.IsNullOrEmpty(newValue))
{
UpdateCaret(_lastCharacterHit);
}
else
{
var textPosition = _caretIndex + newValue?.Length ?? 0;
var characterHit = GetCharacterHitFromTextPosition(textPosition);
UpdateCaret(characterHit, false);
}
}
private CharacterHit GetCharacterHitFromTextPosition(int textPosition)
{
var lineIndex = TextLayout.GetLineIndexFromCharacterIndex(textPosition, true);
@ -935,11 +921,6 @@ namespace Avalonia.Controls.Presenters
switch (change.Property.Name)
{
case nameof(PreeditText):
{
OnPreeditTextChanged(change.OldValue as string, change.NewValue as string);
break;
}
case nameof(CompositionRegion):
case nameof(Foreground):
case nameof(FontSize):

6
src/Avalonia.Controls/TextBoxTextInputMethodClient.cs

@ -4,8 +4,6 @@ using Avalonia.Input.TextInput;
using Avalonia.Media.TextFormatting;
using Avalonia.Threading;
using Avalonia.Utilities;
using Avalonia.VisualTree;
using static System.Net.Mime.MediaTypeNames;
namespace Avalonia.Controls
{
@ -161,11 +159,13 @@ namespace Avalonia.Controls
public void SetPreeditText(string? text)
{
if (_presenter == null)
if (_presenter == null || _parent == null)
{
return;
}
_presenter.CaretIndex = _parent.CaretIndex;
_presenter.PreeditText = text;
}

Loading…
Cancel
Save