From 7065e1b0a53180e4e21ae33083fe05033b67ab39 Mon Sep 17 00:00:00 2001 From: Mike Corsaro Date: Thu, 27 Jun 2024 10:42:57 -0700 Subject: [PATCH 01/11] Ensure `CursorPosition` and `SelectionLength` always fire change events even if the value is the same This allows "select all text on focus" logic to work on WinUI --- src/Controls/src/Core/InputView/InputView.cs | 16 ++++++++++++++-- .../src/Platform/Windows/TextBoxExtensions.cs | 11 +++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Controls/src/Core/InputView/InputView.cs b/src/Controls/src/Core/InputView/InputView.cs index 08bff9eec5cc..b252364aad1e 100644 --- a/src/Controls/src/Core/InputView/InputView.cs +++ b/src/Controls/src/Core/InputView/InputView.cs @@ -178,7 +178,11 @@ public string UpdateFormsText(string original, TextTransform transform) public int CursorPosition { get { return (int)GetValue(CursorPositionProperty); } - set { SetValue(CursorPositionProperty, value); } + set + { + SetValueCore(CursorPositionProperty, value, + SetValueFlags.RaiseOnEqual | SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource, SetValuePrivateFlags.Default, SetterSpecificity.FromHandler); + } } /// @@ -188,7 +192,15 @@ public int CursorPosition public int SelectionLength { get { return (int)GetValue(SelectionLengthProperty); } - set { SetValue(SelectionLengthProperty, value); } + set + { + // Note: this property (and CursorPosition) will always raise change events when set. On platforms like WinUI we + // need to always push the xplat -> native value flow in scenarios such as "select all text on focus". If we + // don't do this the native control will handle the input event and move the cursor after we set the selection + // length/cursor position, which is not the desired behavior. + SetValueCore(SelectionLengthProperty, value, + SetValueFlags.RaiseOnEqual | SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource, SetValuePrivateFlags.Default, SetterSpecificity.FromHandler); + } } /// diff --git a/src/Core/src/Platform/Windows/TextBoxExtensions.cs b/src/Core/src/Platform/Windows/TextBoxExtensions.cs index e353db5a7e43..e2cb9e315b85 100644 --- a/src/Core/src/Platform/Windows/TextBoxExtensions.cs +++ b/src/Core/src/Platform/Windows/TextBoxExtensions.cs @@ -1,5 +1,6 @@ #nullable enable using System; +using System.Diagnostics; using Microsoft.Maui.Graphics; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; @@ -201,7 +202,10 @@ public static void UpdateVerticalTextAlignment(this TextBox textBox, ITextAlignm public static void UpdateCursorPosition(this TextBox textBox, ITextInput entry) { // It seems that the TextBox does not limit the CursorPosition to the Text.Length natively - entry.CursorPosition = Math.Min(entry.CursorPosition, textBox.Text.Length); + var clampedPos = Math.Min(entry.CursorPosition, textBox.Text.Length); + + if (entry.CursorPosition != clampedPos) + entry.CursorPosition = clampedPos; if (textBox.SelectionStart != entry.CursorPosition) textBox.SelectionStart = entry.CursorPosition; @@ -210,7 +214,10 @@ public static void UpdateCursorPosition(this TextBox textBox, ITextInput entry) public static void UpdateSelectionLength(this TextBox textBox, ITextInput entry) { // It seems that the TextBox does not limit the SelectionLength to the Text.Length natively - entry.SelectionLength = Math.Min(entry.SelectionLength, textBox.Text.Length - textBox.SelectionStart); + var clampedLen = Math.Min(entry.SelectionLength, textBox.Text.Length - textBox.SelectionStart); + + if (entry.SelectionLength != clampedLen) + entry.SelectionLength = clampedLen; if (textBox.SelectionLength != entry.SelectionLength) textBox.SelectionLength = entry.SelectionLength; From 2eb73bd5df51ffcc2fa2f03c3b2e9ab53f57aee8 Mon Sep 17 00:00:00 2001 From: Mike Corsaro Date: Thu, 27 Jun 2024 15:13:44 -0700 Subject: [PATCH 02/11] Add visual test --- .../Issues/EntrySelectionTest.xaml | 19 +++++++++++ .../Issues/EntrySelectionTest.xaml.cs | 20 +++++++++++ .../EntrySelectionTest.cs | 31 ++++++++++++++++++ .../snapshots/windows/VerifySelectWorks.png | Bin 0 -> 4981 bytes 4 files changed, 70 insertions(+) create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/EntrySelectionTest.xaml create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/EntrySelectionTest.xaml.cs create mode 100644 src/Controls/tests/TestCases.WinUI.Tests/EntrySelectionTest.cs create mode 100644 src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/VerifySelectWorks.png diff --git a/src/Controls/tests/TestCases.HostApp/Issues/EntrySelectionTest.xaml b/src/Controls/tests/TestCases.HostApp/Issues/EntrySelectionTest.xaml new file mode 100644 index 000000000000..9f9babfef628 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/EntrySelectionTest.xaml @@ -0,0 +1,19 @@ + + + + +