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

Don't show text selector when no text is ready #15770

Merged
merged 1 commit into from
Jun 25, 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
25 changes: 23 additions & 2 deletions src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.VisualTree;

namespace Avalonia.Controls.Primitives
Expand All @@ -18,6 +19,7 @@ internal class TextSelectionHandleCanvas : Canvas
private TextPresenter? _presenter;
private TextBox? _textBox;
private bool _showHandle;
private bool _canShowContextMenu = true;

internal bool ShowHandles
{
Expand All @@ -33,7 +35,7 @@ internal bool ShowHandles
_caretHandle.IsVisible = false;
}

IsVisible = value;
IsVisible = !string.IsNullOrEmpty(_presenter?.Text) && value;
}
}

Expand Down Expand Up @@ -65,11 +67,20 @@ public TextSelectionHandleCanvas()
_caretHandle.SetTopLeft(default);
_endHandle.SetTopLeft(default);

_startHandle.PointerReleased += Handle_PointerReleased;
_caretHandle.PointerReleased += Handle_PointerReleased;
_endHandle.PointerReleased += Handle_PointerReleased;

IsVisible = ShowHandles;

ClipToBounds = false;
}

private void Handle_PointerReleased(object? sender, PointerReleasedEventArgs e)
{
ShowContextMenu();
}

private void Handle_DragStarted(object? sender, VectorEventArgs e)
{
if (_textBox?.ContextFlyout is { } flyout)
Expand All @@ -92,6 +103,7 @@ private void StartHandle_DragDelta(object? sender, VectorEventArgs e)

private void CaretHandle_DragDelta(object? sender, VectorEventArgs e)
{
_canShowContextMenu = false;
if (_presenter != null && _textBox != null)
{
var point = ToPresenter(_caretHandle.IndicatorPosition);
Expand Down Expand Up @@ -267,6 +279,7 @@ internal void SetPresenter(TextPresenter? textPresenter)

_textBox.PropertyChanged += TextBoxPropertyChanged;
_textBox.EffectiveViewportChanged += TextBoxEffectiveViewportChanged;
_textBox.SizeChanged += TextBox_SizeChanged;
}
}
else
Expand All @@ -281,12 +294,18 @@ internal void SetPresenter(TextPresenter? textPresenter)

_textBox.PropertyChanged -= TextBoxPropertyChanged;
_textBox.EffectiveViewportChanged -= TextBoxEffectiveViewportChanged;
_textBox.SizeChanged -= TextBox_SizeChanged;
}

_textBox = null;
}
}

private void TextBox_SizeChanged(object? sender, SizeChangedEventArgs e)
{
InvalidateMeasure();
}

private void TextBoxEffectiveViewportChanged(object? sender, EffectiveViewportChangedEventArgs e)
{
if (ShowHandles)
Expand All @@ -304,7 +323,7 @@ private void TextBoxHolding(object? sender, HoldingRoutedEventArgs e)

internal bool ShowContextMenu()
{
if (_textBox != null)
if (_textBox != null && _canShowContextMenu)
{
if (_textBox.ContextFlyout is PopupFlyoutBase flyout)
{
Expand Down Expand Up @@ -345,6 +364,8 @@ internal bool ShowContextMenu()
}
}

_canShowContextMenu = true;

return false;
}

Expand Down
40 changes: 25 additions & 15 deletions src/Avalonia.Controls/Primitives/TextSelectionHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,32 +134,42 @@ protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)

protected override void OnPointerMoved(PointerEventArgs e)
{
if (_lastPoint.HasValue)
VectorEventArgs ev;

if (!_lastPoint.HasValue)
{
var ev = new VectorEventArgs
_lastPoint = e.GetPosition(VisualRoot as Visual);
e.Pointer.Capture(this);

ev = new VectorEventArgs
{
RoutedEvent = DragDeltaEvent,
Vector = e.GetPosition(VisualRoot as Visual) - _lastPoint.Value,
RoutedEvent = DragStartedEvent,
Vector = (Vector)_lastPoint,
};
}
else
{
var vector = e.GetPosition(VisualRoot as Visual) - _lastPoint.Value;

RaiseEvent(ev);
var tapSize = TopLevel.GetTopLevel(this)?.PlatformSettings?.GetTapSize(PointerType.Touch) ?? new Size(10, 10);

if (Math.Abs(vector.X) < tapSize.Width && Math.Abs(vector.Y) < tapSize.Height)
return;

ev = new VectorEventArgs
{
RoutedEvent = DragDeltaEvent,
Vector = vector,
};
}

RaiseEvent(ev);
}

protected override void OnPointerPressed(PointerPressedEventArgs e)
{
e.Handled = true;
_lastPoint = e.GetPosition(VisualRoot as Visual);

var ev = new VectorEventArgs
{
RoutedEvent = DragStartedEvent,
Vector = (Vector)_lastPoint,
};

PseudoClasses.Add(":pressed");

RaiseEvent(ev);
}

protected override void OnPointerReleased(PointerReleasedEventArgs e)
Expand Down
Loading