Browse Source

don't show selector when no text is ready (#15770)

pull/16129/head
Emmanuel Hansen 2 years ago
committed by GitHub
parent
commit
fb4a83903e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 25
      src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs
  2. 40
      src/Avalonia.Controls/Primitives/TextSelectionHandle.cs

25
src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs

@ -4,6 +4,7 @@ using Avalonia.Controls.Presenters;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Layout; using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.VisualTree; using Avalonia.VisualTree;
namespace Avalonia.Controls.Primitives namespace Avalonia.Controls.Primitives
@ -18,6 +19,7 @@ namespace Avalonia.Controls.Primitives
private TextPresenter? _presenter; private TextPresenter? _presenter;
private TextBox? _textBox; private TextBox? _textBox;
private bool _showHandle; private bool _showHandle;
private bool _canShowContextMenu = true;
internal bool ShowHandles internal bool ShowHandles
{ {
@ -33,7 +35,7 @@ namespace Avalonia.Controls.Primitives
_caretHandle.IsVisible = false; _caretHandle.IsVisible = false;
} }
IsVisible = value; IsVisible = !string.IsNullOrEmpty(_presenter?.Text) && value;
} }
} }
@ -65,11 +67,20 @@ namespace Avalonia.Controls.Primitives
_caretHandle.SetTopLeft(default); _caretHandle.SetTopLeft(default);
_endHandle.SetTopLeft(default); _endHandle.SetTopLeft(default);
_startHandle.PointerReleased += Handle_PointerReleased;
_caretHandle.PointerReleased += Handle_PointerReleased;
_endHandle.PointerReleased += Handle_PointerReleased;
IsVisible = ShowHandles; IsVisible = ShowHandles;
ClipToBounds = false; ClipToBounds = false;
} }
private void Handle_PointerReleased(object? sender, PointerReleasedEventArgs e)
{
ShowContextMenu();
}
private void Handle_DragStarted(object? sender, VectorEventArgs e) private void Handle_DragStarted(object? sender, VectorEventArgs e)
{ {
if (_textBox?.ContextFlyout is { } flyout) if (_textBox?.ContextFlyout is { } flyout)
@ -92,6 +103,7 @@ namespace Avalonia.Controls.Primitives
private void CaretHandle_DragDelta(object? sender, VectorEventArgs e) private void CaretHandle_DragDelta(object? sender, VectorEventArgs e)
{ {
_canShowContextMenu = false;
if (_presenter != null && _textBox != null) if (_presenter != null && _textBox != null)
{ {
var point = ToPresenter(_caretHandle.IndicatorPosition); var point = ToPresenter(_caretHandle.IndicatorPosition);
@ -267,6 +279,7 @@ namespace Avalonia.Controls.Primitives
_textBox.PropertyChanged += TextBoxPropertyChanged; _textBox.PropertyChanged += TextBoxPropertyChanged;
_textBox.EffectiveViewportChanged += TextBoxEffectiveViewportChanged; _textBox.EffectiveViewportChanged += TextBoxEffectiveViewportChanged;
_textBox.SizeChanged += TextBox_SizeChanged;
} }
} }
else else
@ -281,12 +294,18 @@ namespace Avalonia.Controls.Primitives
_textBox.PropertyChanged -= TextBoxPropertyChanged; _textBox.PropertyChanged -= TextBoxPropertyChanged;
_textBox.EffectiveViewportChanged -= TextBoxEffectiveViewportChanged; _textBox.EffectiveViewportChanged -= TextBoxEffectiveViewportChanged;
_textBox.SizeChanged -= TextBox_SizeChanged;
} }
_textBox = null; _textBox = null;
} }
} }
private void TextBox_SizeChanged(object? sender, SizeChangedEventArgs e)
{
InvalidateMeasure();
}
private void TextBoxEffectiveViewportChanged(object? sender, EffectiveViewportChangedEventArgs e) private void TextBoxEffectiveViewportChanged(object? sender, EffectiveViewportChangedEventArgs e)
{ {
if (ShowHandles) if (ShowHandles)
@ -304,7 +323,7 @@ namespace Avalonia.Controls.Primitives
internal bool ShowContextMenu() internal bool ShowContextMenu()
{ {
if (_textBox != null) if (_textBox != null && _canShowContextMenu)
{ {
if (_textBox.ContextFlyout is PopupFlyoutBase flyout) if (_textBox.ContextFlyout is PopupFlyoutBase flyout)
{ {
@ -345,6 +364,8 @@ namespace Avalonia.Controls.Primitives
} }
} }
_canShowContextMenu = true;
return false; return false;
} }

40
src/Avalonia.Controls/Primitives/TextSelectionHandle.cs

@ -134,32 +134,42 @@ namespace Avalonia.Controls.Primitives
protected override void OnPointerMoved(PointerEventArgs 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, RoutedEvent = DragStartedEvent,
Vector = e.GetPosition(VisualRoot as Visual) - _lastPoint.Value, 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) protected override void OnPointerPressed(PointerPressedEventArgs e)
{ {
e.Handled = true; e.Handled = true;
_lastPoint = e.GetPosition(VisualRoot as Visual);
var ev = new VectorEventArgs
{
RoutedEvent = DragStartedEvent,
Vector = (Vector)_lastPoint,
};
PseudoClasses.Add(":pressed"); PseudoClasses.Add(":pressed");
RaiseEvent(ev);
} }
protected override void OnPointerReleased(PointerReleasedEventArgs e) protected override void OnPointerReleased(PointerReleasedEventArgs e)

Loading…
Cancel
Save