Browse Source

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

Emmanuel Hansen 1 year ago
parent
commit
335894c503

+ 13 - 6
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);

+ 0 - 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;