Browse Source

only add the text selection canvas when the textbox is focused (#16643)

pull/16693/head
Emmanuel Hansen 2 years ago
committed by GitHub
parent
commit
335894c503
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 19
      src/Avalonia.Controls/Presenters/TextPresenter.cs
  2. 7
      src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs

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

@ -462,6 +462,7 @@ namespace Avalonia.Controls.Presenters
public void ShowCaret()
{
EnsureCaretTimer();
EnsureTextSelectionLayer();
_caretBlink = true;
_caretTimer?.Start();
InvalidateVisual();
@ -470,10 +471,7 @@ namespace Avalonia.Controls.Presenters
public void HideCaret()
{
_caretBlink = false;
if (TextSelectionHandleCanvas != null)
{
TextSelectionHandleCanvas.ShowHandles = false;
}
RemoveTextSelectionCanvas();
_caretTimer?.Stop();
InvalidateVisual();
}
@ -908,8 +906,6 @@ namespace Avalonia.Controls.Presenters
base.OnAttachedToVisualTree(e);
ResetCaretTimer();
EnsureTextSelectionLayer();
}
private void EnsureTextSelectionLayer()
@ -931,6 +927,17 @@ namespace Avalonia.Controls.Presenters
_layer?.Add(TextSelectionHandleCanvas);
}
private void RemoveTextSelectionCanvas()
{
if(_layer != null && TextSelectionHandleCanvas is { } canvas)
{
canvas.SetPresenter(null);
_layer.Remove(canvas);
}
TextSelectionHandleCanvas = null;
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);

7
src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs

@ -273,7 +273,6 @@ namespace Avalonia.Controls.Primitives
{
_textBox.AddHandler(TextBox.TextChangingEvent, TextChanged, handledEventsToo: true);
_textBox.AddHandler(KeyDownEvent, TextBoxKeyDown, handledEventsToo: true);
_textBox.AddHandler(LostFocusEvent, TextBoxLostFocus, handledEventsToo: true);
_textBox.AddHandler(PointerReleasedEvent, TextBoxPointerReleased, handledEventsToo: true);
_textBox.AddHandler(Gestures.HoldingEvent, TextBoxHolding, handledEventsToo: true);
@ -289,7 +288,6 @@ namespace Avalonia.Controls.Primitives
_textBox.RemoveHandler(TextBox.TextChangingEvent, TextChanged);
_textBox.RemoveHandler(KeyDownEvent, TextBoxKeyDown);
_textBox.RemoveHandler(PointerReleasedEvent, TextBoxPointerReleased);
_textBox.RemoveHandler(LostFocusEvent, TextBoxLostFocus);
_textBox.RemoveHandler(Gestures.HoldingEvent, TextBoxHolding);
_textBox.PropertyChanged -= TextBoxPropertyChanged;
@ -390,11 +388,6 @@ namespace Avalonia.Controls.Primitives
}
}
private void TextBoxLostFocus(object? sender, RoutedEventArgs e)
{
ShowHandles = false;
}
private void TextBoxKeyDown(object? sender, KeyEventArgs e)
{
ShowHandles = false;

Loading…
Cancel
Save