Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add the text selection canvas when the textbox is focused #16643

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/Avalonia.Controls/Presenters/TextPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ public sealed override void Render(DrawingContext context)
public void ShowCaret()
{
EnsureCaretTimer();
EnsureTextSelectionLayer();
_caretBlink = true;
_caretTimer?.Start();
InvalidateVisual();
Expand All @@ -470,10 +471,7 @@ public void ShowCaret()
public void HideCaret()
{
_caretBlink = false;
if (TextSelectionHandleCanvas != null)
{
TextSelectionHandleCanvas.ShowHandles = false;
}
RemoveTextSelectionCanvas();
_caretTimer?.Stop();
InvalidateVisual();
}
Expand Down Expand Up @@ -908,8 +906,6 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
base.OnAttachedToVisualTree(e);

ResetCaretTimer();

EnsureTextSelectionLayer();
}

private void EnsureTextSelectionLayer()
Expand All @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading