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() public void ShowCaret()
{ {
EnsureCaretTimer(); EnsureCaretTimer();
EnsureTextSelectionLayer();
_caretBlink = true; _caretBlink = true;
_caretTimer?.Start(); _caretTimer?.Start();
InvalidateVisual(); InvalidateVisual();
@ -470,10 +471,7 @@ namespace Avalonia.Controls.Presenters
public void HideCaret() public void HideCaret()
{ {
_caretBlink = false; _caretBlink = false;
if (TextSelectionHandleCanvas != null) RemoveTextSelectionCanvas();
{
TextSelectionHandleCanvas.ShowHandles = false;
}
_caretTimer?.Stop(); _caretTimer?.Stop();
InvalidateVisual(); InvalidateVisual();
} }
@ -908,8 +906,6 @@ namespace Avalonia.Controls.Presenters
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
ResetCaretTimer(); ResetCaretTimer();
EnsureTextSelectionLayer();
} }
private void EnsureTextSelectionLayer() private void EnsureTextSelectionLayer()
@ -931,6 +927,17 @@ namespace Avalonia.Controls.Presenters
_layer?.Add(TextSelectionHandleCanvas); _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) protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnDetachedFromVisualTree(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(TextBox.TextChangingEvent, TextChanged, handledEventsToo: true);
_textBox.AddHandler(KeyDownEvent, TextBoxKeyDown, handledEventsToo: true); _textBox.AddHandler(KeyDownEvent, TextBoxKeyDown, handledEventsToo: true);
_textBox.AddHandler(LostFocusEvent, TextBoxLostFocus, handledEventsToo: true);
_textBox.AddHandler(PointerReleasedEvent, TextBoxPointerReleased, handledEventsToo: true); _textBox.AddHandler(PointerReleasedEvent, TextBoxPointerReleased, handledEventsToo: true);
_textBox.AddHandler(Gestures.HoldingEvent, TextBoxHolding, handledEventsToo: true); _textBox.AddHandler(Gestures.HoldingEvent, TextBoxHolding, handledEventsToo: true);
@ -289,7 +288,6 @@ namespace Avalonia.Controls.Primitives
_textBox.RemoveHandler(TextBox.TextChangingEvent, TextChanged); _textBox.RemoveHandler(TextBox.TextChangingEvent, TextChanged);
_textBox.RemoveHandler(KeyDownEvent, TextBoxKeyDown); _textBox.RemoveHandler(KeyDownEvent, TextBoxKeyDown);
_textBox.RemoveHandler(PointerReleasedEvent, TextBoxPointerReleased); _textBox.RemoveHandler(PointerReleasedEvent, TextBoxPointerReleased);
_textBox.RemoveHandler(LostFocusEvent, TextBoxLostFocus);
_textBox.RemoveHandler(Gestures.HoldingEvent, TextBoxHolding); _textBox.RemoveHandler(Gestures.HoldingEvent, TextBoxHolding);
_textBox.PropertyChanged -= TextBoxPropertyChanged; _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) private void TextBoxKeyDown(object? sender, KeyEventArgs e)
{ {
ShowHandles = false; ShowHandles = false;

Loading…
Cancel
Save