From 961acfeadfeb4443ab1bf408c964de46934efa23 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Fri, 9 Aug 2024 13:40:51 +0000 Subject: [PATCH] only add the text selection canvas when the textbox is focused --- .../Presenters/TextPresenter.cs | 19 +++++++++++++------ .../Primitives/TextSelectionCanvas.cs | 7 ------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 246c12cf3cb..e35d683a1f7 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -462,6 +462,7 @@ public sealed override void Render(DrawingContext context) public void ShowCaret() { EnsureCaretTimer(); + EnsureTextSelectionLayer(); _caretBlink = true; _caretTimer?.Start(); InvalidateVisual(); @@ -470,10 +471,7 @@ public void ShowCaret() public void HideCaret() { _caretBlink = false; - if (TextSelectionHandleCanvas != null) - { - TextSelectionHandleCanvas.ShowHandles = false; - } + RemoveTextSelectionCanvas(); _caretTimer?.Stop(); InvalidateVisual(); } @@ -908,8 +906,6 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) base.OnAttachedToVisualTree(e); ResetCaretTimer(); - - EnsureTextSelectionLayer(); } private void EnsureTextSelectionLayer() @@ -931,6 +927,17 @@ private void EnsureTextSelectionLayer() _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); diff --git a/src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs b/src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs index 3ec92215395..6e23495cb1f 100644 --- a/src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs +++ b/src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs @@ -273,7 +273,6 @@ internal void SetPresenter(TextPresenter? textPresenter) { _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 @@ internal void SetPresenter(TextPresenter? textPresenter) _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 @@ private void TextBoxPropertyChanged(object? sender, AvaloniaPropertyChangedEvent } } - private void TextBoxLostFocus(object? sender, RoutedEventArgs e) - { - ShowHandles = false; - } - private void TextBoxKeyDown(object? sender, KeyEventArgs e) { ShowHandles = false;