Skip to content

Commit

Permalink
Merge pull request #19316 from ramezgerges/textbox_right_click
Browse files Browse the repository at this point in the history
fix(textbox): right clicking inside TextBox selection should not clear the selection
  • Loading branch information
jeromelaban authored Jan 24, 2025
2 parents 30b37bc + 04094a8 commit 2011a6a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2895,6 +2895,55 @@ public async Task When_Undo_Redo_ContextMenu_Basic()
Assert.AreEqual("hello", SUT.Text);
}

[TestMethod]
public async Task When_Right_Tap_Selection_Persists()
{
using var _ = new TextBoxFeatureConfigDisposable();
using var __ = new DisposableAction(() => (VisualTreeHelper.GetOpenPopupsForXamlRoot(WindowHelper.XamlRoot)).ForEach((_, p) => p.IsOpen = false));

var SUT = new TextBox
{
Width = 40
};

WindowHelper.WindowContent = SUT;

await WindowHelper.WaitForIdle();
await WindowHelper.WaitForLoaded(SUT);

SUT.Focus(FocusState.Programmatic);
await WindowHelper.WaitForIdle();

SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.H, VirtualKeyModifiers.None, unicodeKey: 'h'));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.E, VirtualKeyModifiers.None, unicodeKey: 'e'));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.L, VirtualKeyModifiers.None, unicodeKey: 'l'));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.L, VirtualKeyModifiers.None, unicodeKey: 'l'));
SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.O, VirtualKeyModifiers.None, unicodeKey: 'o'));
await WindowHelper.WaitForIdle();

Assert.AreEqual("hello", SUT.Text);

var injector = InputInjector.TryCreate() ?? throw new InvalidOperationException("Failed to init the InputInjector");
using var mouse = injector.GetMouse();

mouse.MoveTo(SUT.GetAbsoluteBounds().Location + new Point(10, 10));
await WindowHelper.WaitForIdle();

mouse.Press();
mouse.MoveBy(8, 0);
mouse.Release();
await WindowHelper.WaitForIdle();

var selection = (SUT.SelectionStart, SUT.SelectionLength);

mouse.MoveBy(-4, 0);
mouse.PressRight();
mouse.ReleaseRight();
await WindowHelper.WaitForIdle();

Assert.AreEqual(selection, (SUT.SelectionStart, SUT.SelectionLength));
}

[TestMethod]
public async Task When_Text_Changed_History_Cleared()
{
Expand Down
7 changes: 6 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.pointers.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ protected override void OnRightTapped(RightTappedRoutedEventArgs e)
// this textbox but GetPosition assumes that it is relative to the displayBlock, so we compensate.
// var position = e.GetPosition(displayBlock);
var position = displayBlock.TransformToVisual(this).Inverse.TransformPoint(e.GetPosition(displayBlock));

var index = Math.Max(0, displayBlock.Inlines.GetIndexAt(position, true, true));
Select(index, 0);
if (index < SelectionStart || index >= SelectionStart + SelectionLength)
{
// Right tapping should move the caret to the current pointer location if outside the selection
Select(index, 0);
}

OpenContextMenu(position);
}
Expand Down

0 comments on commit 2011a6a

Please sign in to comment.