Skip to content

Commit

Permalink
feat: Add IsPointerCaptureRequired attached property
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jun 4, 2024
1 parent 4874eb9 commit 85abb78
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
47 changes: 47 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.Uno.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.UI.Xaml;

namespace Uno.UI.Xaml.Controls;

/// <summary>
/// Contains additional attached properties that provide Uno-specific
/// behavior for TextBox control.
/// </summary>
public class TextBox
{
#if __CROSSRUNTIME__
/// <summary>
/// Gets the value indicating whether the pointer capture on pointer press should be required.
/// </summary>
/// <remarks>
/// This flag is only applied in case of WebAssembly.
/// </remarks>
/// <param name="textBox">TextBox.</param>
/// <returns>Value.</returns>
public static bool GetIsPointerCaptureRequired(Microsoft.UI.Xaml.Controls.TextBox textBox) =>
(bool)textBox.GetValue(IsPointerCaptureRequiredProperty);

/// <summary>
/// Sets the value indicating whether the pointer capture on pointer press should be required.
/// </summary>
/// <remarks>
/// This flag is only applied in case of WebAssembly.
/// </remarks>
/// <param name="textBox">TextBox.</param>
/// <param name="value">Value.</param>
public static void SetIsPointerCaptureRequired(Microsoft.UI.Xaml.Controls.TextBox textBox, bool value) =>
textBox.SetValue(IsPointerCaptureRequiredProperty, value);

/// <summary>
/// Identifies the IsPointerCaptureRequired attached property.
/// </summary>
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
}
13 changes: 9 additions & 4 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 85abb78

Please sign in to comment.