Browse Source

Fix text selection brush not applying when text selection changes (#21480)

* fix text selection brush not applying when text selection changes

* only invalidate text cache on selection change when selection foreground is set
pull/21493/head
Emmanuel Hansen 4 months ago
committed by GitHub
parent
commit
1476e73a0d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 41
      src/Avalonia.Controls/Presenters/TextPresenter.cs

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

@ -58,7 +58,7 @@ namespace Avalonia.Controls.Presenters
/// </summary> /// </summary>
public static readonly StyledProperty<string?> PreeditTextProperty = public static readonly StyledProperty<string?> PreeditTextProperty =
AvaloniaProperty.Register<TextPresenter, string?>(nameof(PreeditText)); AvaloniaProperty.Register<TextPresenter, string?>(nameof(PreeditText));
/// <summary> /// <summary>
/// Defines the <see cref="PreeditText"/> property. /// Defines the <see cref="PreeditText"/> property.
/// </summary> /// </summary>
@ -149,7 +149,7 @@ namespace Avalonia.Controls.Presenters
get => GetValue(PreeditTextProperty); get => GetValue(PreeditTextProperty);
set => SetValue(PreeditTextProperty, value); set => SetValue(PreeditTextProperty, value);
} }
public int? PreeditTextCursorPosition public int? PreeditTextCursorPosition
{ {
get => GetValue(PreeditTextCursorPositionProperty); get => GetValue(PreeditTextCursorPositionProperty);
@ -427,11 +427,11 @@ namespace Avalonia.Controls.Presenters
} }
} }
if(VisualRoot is Visual root) if (VisualRoot is Visual root)
{ {
var offset = this.TranslatePoint(Bounds.Position, root); var offset = this.TranslatePoint(Bounds.Position, root);
if(_previousOffset != offset) if (_previousOffset != offset)
{ {
_previousOffset = offset; _previousOffset = offset;
} }
@ -778,7 +778,7 @@ namespace Avalonia.Controls.Presenters
CaretChanged(); CaretChanged();
} }
private void EnsureCaretTimer() private void EnsureCaretTimer()
{ {
if (_caretTimer == null) if (_caretTimer == null)
@ -804,7 +804,7 @@ namespace Avalonia.Controls.Presenters
_caretTimer = null; _caretTimer = null;
} }
if (CaretBlinkInterval.TotalMilliseconds > 0) if (CaretBlinkInterval.TotalMilliseconds > 0)
{ {
_caretTimer = new DispatcherTimer { Interval = CaretBlinkInterval }; _caretTimer = new DispatcherTimer { Interval = CaretBlinkInterval };
_caretTimer.Tick += CaretTimerTick; _caretTimer.Tick += CaretTimerTick;
@ -977,7 +977,7 @@ namespace Avalonia.Controls.Presenters
internal void RemoveTextSelectionCanvas() internal void RemoveTextSelectionCanvas()
{ {
if(_layer != null && TextSelectionHandleCanvas is { } canvas) if (_layer != null && TextSelectionHandleCanvas is { } canvas)
{ {
canvas.SetPresenter(null); canvas.SetPresenter(null);
_layer.Remove(canvas); _layer.Remove(canvas);
@ -998,7 +998,7 @@ namespace Avalonia.Controls.Presenters
_caretTimer.Tick -= CaretTimerTick; _caretTimer.Tick -= CaretTimerTick;
} }
} }
private void OnPreeditChanged(string? preeditText, int? cursorPosition) private void OnPreeditChanged(string? preeditText, int? cursorPosition)
{ {
if (string.IsNullOrEmpty(preeditText)) if (string.IsNullOrEmpty(preeditText))
@ -1025,17 +1025,17 @@ namespace Avalonia.Controls.Presenters
MoveCaretToTextPosition(change.GetNewValue<int>()); MoveCaretToTextPosition(change.GetNewValue<int>());
} }
if(change.Property == PreeditTextProperty) if (change.Property == PreeditTextProperty)
{ {
OnPreeditChanged(change.NewValue as string, PreeditTextCursorPosition); OnPreeditChanged(change.NewValue as string, PreeditTextCursorPosition);
} }
if(change.Property == PreeditTextCursorPositionProperty) if (change.Property == PreeditTextCursorPositionProperty)
{ {
OnPreeditChanged(PreeditText, PreeditTextCursorPosition); OnPreeditChanged(PreeditText, PreeditTextCursorPosition);
} }
if(change.Property == TextProperty || change.Property == CaretIndexProperty) if (change.Property == TextProperty || change.Property == CaretIndexProperty)
{ {
if (!string.IsNullOrEmpty(PreeditText)) if (!string.IsNullOrEmpty(PreeditText))
{ {
@ -1063,6 +1063,8 @@ namespace Avalonia.Controls.Presenters
case nameof(PasswordChar): case nameof(PasswordChar):
case nameof(RevealPassword): case nameof(RevealPassword):
case nameof(FlowDirection): case nameof(FlowDirection):
case nameof(SelectionForegroundBrush):
case nameof(ShowSelectionHighlight):
{ {
InvalidateTextLayout(); InvalidateTextLayout();
break; break;
@ -1071,12 +1073,21 @@ namespace Avalonia.Controls.Presenters
case nameof(TextAlignment): case nameof(TextAlignment):
case nameof(TextWrapping): case nameof(TextWrapping):
case nameof(LineHeight): case nameof(LineHeight):
{
InvalidateTextLayoutKeepCache();
break;
}
case nameof(SelectionStart): case nameof(SelectionStart):
case nameof(SelectionEnd): case nameof(SelectionEnd):
case nameof(SelectionForegroundBrush):
case nameof(ShowSelectionHighlight):
{ {
InvalidateTextLayoutKeepCache(); if (SelectionForegroundBrush != null)
{
InvalidateTextLayout();
}
else
{
InvalidateTextLayoutKeepCache();
}
break; break;
} }
} }

Loading…
Cancel
Save