From 7e21bee63148badbe52b169914ccf45ee25f4fb1 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Mon, 4 Nov 2024 11:37:54 -0500 Subject: [PATCH] fix(tests): Fix several Given_TextBox tests on macOS/skia --- .../Given_TextBox.skia.cs | 32 ++++++++++--------- .../UI/Xaml/Controls/TextBox/TextBox.skia.cs | 32 +++++++++++++++++-- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.skia.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.skia.cs index 4000c357e36c..591e8ce269a0 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.skia.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBox.skia.cs @@ -1431,7 +1431,7 @@ public async Task When_Cut_While_Pointer_Held() Assert.AreEqual(1, SUT.SelectionStart); Assert.AreEqual(9, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.X, VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.X, _platformCtrlKey)); await WindowHelper.WaitForIdle(); Assert.AreEqual("Hd", SUT.Text); @@ -2151,67 +2151,69 @@ public async Task When_Multiline_Keyboard_Chunking() Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(0, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + // on macOS selecting the next word is `shift` + `option` (alt/menu) + `right` + var mod = VirtualKeyModifiers.Shift | (OperatingSystem.IsMacOS() ? VirtualKeyModifiers.Menu : VirtualKeyModifiers.Control); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(6, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(7, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(12, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(13, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(14, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(19, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(20, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(21, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(24, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(25, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(29, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(30, SUT.SelectionLength); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, VirtualKeyModifiers.Shift | VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.Right, mod)); await WindowHelper.WaitForIdle(); Assert.AreEqual(0, SUT.SelectionStart); Assert.AreEqual(31, SUT.SelectionLength); @@ -3075,7 +3077,7 @@ public async Task When_Cut_Paste_Breaks_Typing(VirtualKey key) SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.E, VirtualKeyModifiers.None, unicodeKey: 'e')); await WindowHelper.WaitForIdle(); - SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, key, VirtualKeyModifiers.Control)); + SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, key, _platformCtrlKey)); await WindowHelper.WaitForIdle(); SUT.SafeRaiseEvent(UIElement.KeyDownEvent, new KeyRoutedEventArgs(SUT, VirtualKey.L, VirtualKeyModifiers.None, unicodeKey: 'l')); diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs index d82ae614c8a4..3d14858a5cca 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs @@ -431,10 +431,26 @@ args.Key is not (VirtualKey.Up or VirtualKey.Down or VirtualKey.Left or VirtualK switch (args.Key) { case VirtualKey.Up: - KeyDownUpArrow(args, text, ctrl, shift, ref selectionStart, ref selectionLength); + // on macOS start of document is `Command` and `Up` + if (ctrl && OperatingSystem.IsMacOS()) + { + KeyDownHome(args, text, ctrl, shift, ref selectionStart, ref selectionLength); + } + else + { + KeyDownUpArrow(args, text, ctrl, shift, ref selectionStart, ref selectionLength); + } break; case VirtualKey.Down: - KeyDownDownArrow(args, text, ctrl, shift, ref selectionStart, ref selectionLength); + // on macOS end of document is `Command` and `Down` + if (ctrl && OperatingSystem.IsMacOS()) + { + KeyDownEnd(args, text, ctrl, shift, ref selectionStart, ref selectionLength); + } + else + { + KeyDownDownArrow(args, text, ctrl, shift, ref selectionStart, ref selectionLength); + } break; case VirtualKey.Left: KeyDownLeftArrow(args, text, shift, ctrl, ref selectionStart, ref selectionLength); @@ -689,6 +705,12 @@ private void KeyDownLeftArrow(KeyRoutedEventArgs args, string text, bool shift, private void KeyDownRightArrow(KeyRoutedEventArgs args, string text, bool ctrl, bool shift, ref int selectionStart, ref int selectionLength) { + // on macOS it is `shift` + `option` + `right` that select the next word + if (shift && OperatingSystem.IsMacOS()) + { + ctrl = args.KeyboardModifiers.HasFlag(VirtualKeyModifiers.Menu); + } + if (HasPointerCapture) { return; @@ -814,6 +836,12 @@ private void KeyDownEnd(KeyRoutedEventArgs args, string text, bool ctrl, bool sh private void KeyDownDelete(KeyRoutedEventArgs args, ref string text, bool ctrl, bool shift, ref int selectionStart, ref int selectionLength) { + // on macOS it is `option` + `delete>` that removes the next word + if (OperatingSystem.IsMacOS()) + { + ctrl = args.KeyboardModifiers.HasFlag(VirtualKeyModifiers.Menu); + } + if (HasPointerCapture) { return;