diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.Uno.cs b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.Uno.cs new file mode 100644 index 000000000000..6cd768873d7b --- /dev/null +++ b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.Uno.cs @@ -0,0 +1,47 @@ +using Microsoft.UI.Xaml; + +namespace Uno.UI.Xaml.Controls; + +/// +/// Contains additional attached properties that provide Uno-specific +/// behavior for TextBox control. +/// +public class TextBox +{ +#if __CROSSRUNTIME__ + /// + /// Gets the value indicating whether the pointer capture on pointer press should be required. + /// + /// + /// This flag is only applied in case of WebAssembly. + /// + /// TextBox. + /// Value. + public static bool GetIsPointerCaptureRequired(Microsoft.UI.Xaml.Controls.TextBox textBox) => + (bool)textBox.GetValue(IsPointerCaptureRequiredProperty); + + /// + /// Sets the value indicating whether the pointer capture on pointer press should be required. + /// + /// + /// This flag is only applied in case of WebAssembly. + /// + /// TextBox. + /// Value. + public static void SetIsPointerCaptureRequired(Microsoft.UI.Xaml.Controls.TextBox textBox, bool value) => + textBox.SetValue(IsPointerCaptureRequiredProperty, value); + + /// + /// Identifies the IsPointerCaptureRequired attached property. + /// + public static DependencyProperty IsPointerCaptureRequiredProperty { get; } = + DependencyProperty.RegisterAttached( + "IsPointerCaptureRequired", + typeof(bool), + typeof(TextBox), + new FrameworkPropertyMetadata( + // We should not capture the pointer on WASM by default because it would prevent the user from scrolling through text on selection. + // See https://github.com/unoplatform/uno/pull/16982, https://issues.chromium.org/issues/344491566 + false)); +#endif +} diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs index 1e15c6cdebec..7416338168f8 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs @@ -986,11 +986,16 @@ protected override void OnPointerPressed(PointerRoutedEventArgs args) { base.OnPointerPressed(args); - if (ShouldFocusOnPointerPressed(args) // UWP Captures if the pointer is not Touch -#if !__WASM__ // We cannot capture the pointer on WASM because it would prevent the user from scrolling through text on selection. - && CapturePointer(args.Pointer) + bool isPointerCaptureRequired = +#if __WASM__ + Uno.UI.Xaml.Controls.TextBox.GetIsPointerCaptureRequired(this); +#else + true; #endif - ) + + if (ShouldFocusOnPointerPressed(args) // UWP Captures if the pointer is not Touch + && isPointerCaptureRequired + && CapturePointer(args.Pointer)) { Focus(FocusState.Pointer); }