Skip to content

Commit

Permalink
feat: Implement TextBox.Select, TextBox.SelectAll on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Aug 21, 2021
1 parent 60d4a34 commit b0f7646
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBox\TextBox_Selection.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBox\TextBox_TextChanging.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -5471,6 +5475,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBox\TextBox_RoundedCorners.xaml.cs">
<DependentUpon>TextBox_RoundedCorners.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBox\TextBox_Selection.xaml.cs">
<DependentUpon>TextBox_Selection.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\TextBox\TextBox_TextChanging.xaml.cs">
<DependentUpon>TextBox_TextChanging.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<UserControl
x:Class="UITests.Shared.Windows_UI_Xaml_Controls.TextBoxTests.TextBox_Selection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Windows_UI_Xaml_Controls.TextBox"
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<StackPanel>
<controls:NumberBox x:Name="startNumber" PlaceholderText="Start" />
<controls:NumberBox x:Name="lengthNumber" PlaceholderText="Length" />
<TextBox x:Name="myTextBox" />
<Button Click="Select_OnClick" Content="Select"></Button>
<Button Click="SelectAll_OnClick" Content="Select all"></Button>
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Uno.UI.Samples.Controls;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using static Uno.UI.FeatureConfiguration;


namespace UITests.Shared.Windows_UI_Xaml_Controls.TextBoxTests
{
[Sample("TextBox")]
public sealed partial class TextBox_Selection : UserControl
{
public TextBox_Selection()
{
this.InitializeComponent();
}
private void Select_OnClick(object sender, RoutedEventArgs args)
{
myTextBox.Focus(FocusState.Programmatic);
myTextBox.Select((int)startNumber.Value, (int)lengthNumber.Value);
}

private void SelectAll_OnClick(object sender, RoutedEventArgs args)
{
myTextBox.Focus(FocusState.Programmatic);
myTextBox.SelectAll();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,14 @@ public void SetIsPassword(bool isPassword)
entry.Visibility = !isPassword;
}
}

public void Select(int start, int length)
{
if (_currentInputWidget is Entry entry)
{
entry.SelectRegion(start_pos: start, end_pos: start + length);
}
// TODO: Handle TextView..
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,7 @@ public void SetIsPassword(bool isPassword)
{
// No support for now.
}

public void Select(int start, int length) => _currentInputWidget?.Select(start, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,14 @@ public bool CanUndo
// Forced skipping of method Windows.UI.Xaml.Controls.TextBox.SelectionChanged.remove
// Forced skipping of method Windows.UI.Xaml.Controls.TextBox.ContextMenuOpening.add
// Forced skipping of method Windows.UI.Xaml.Controls.TextBox.ContextMenuOpening.remove
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public void Select( int start, int length)
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.UI.Xaml.Controls.TextBox", "void TextBox.Select(int start, int length)");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public void SelectAll()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/ITextBoxExtension.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ internal interface ITextBoxViewExtension
void SetTextNative(string text);

void SetIsPassword(bool isPassword);

void Select(int start, int length);
}
}
7 changes: 6 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public ImeAction ImeOptions
set { this.SetValue(ImeOptionsProperty, value); }
}

public static DependencyProperty ImeOptionsProperty { get ; } =
public static DependencyProperty ImeOptionsProperty { get; } =
DependencyProperty.Register("ImeOptions",
typeof(ImeAction),
typeof(TextBox),
Expand Down Expand Up @@ -188,6 +188,11 @@ public override bool RequestFocus(FocusSearchDirection direction, Rect previousl
}
}

partial void SelectPartial(int start, int length)
=> _textBoxView.SetSelection(start: start, stop: start + length);

public void SelectAll() => _textBoxView.SelectAll();

/// <summary>
/// Applies PreventKeyboardDisplayOnProgrammaticFocus by temporarily disabling soft input display.
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,37 @@ protected override void OnVerticalContentAlignmentChanged(VerticalAlignment oldV

partial void OnVerticalContentAlignmentChangedPartial(VerticalAlignment oldVerticalContentAlignment, VerticalAlignment newVerticalContentAlignment);

public void Select(int start, int length)
{
if (start < 0)
{
throw new ArgumentOutOfRangeException(nameof(start), $"'{start}' cannot be negative.");
}

if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length), $"'{length}' cannot be negative.");
}

// TODO: Test and adjust (if needed) this logic for surrogate pairs.

var textLength = Text.Length;

if (start >= textLength)
{
start = textLength;
length = 0;
}
else if (start + length > textLength)
{
length = textLength - start;
}

SelectPartial(start, length);
}

partial void SelectPartial(int start, int length);

internal override bool CanHaveChildren() => true;
}
}
12 changes: 10 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ partial void OnTextAlignmentChangedPartial(DependencyPropertyChangedEventArgs e)
_textBoxView?.UpdateTextAlignment();
}

partial void SelectPartial(int start, int length)
{
if (_textBoxView != null)
{
_textBoxView.SelectedTextRange = _textBoxView.GetTextRange(start: start, end: start + length);
}
}

internal MultilineTextBoxView MultilineTextBox
{
get
Expand Down Expand Up @@ -267,7 +275,7 @@ public UIReturnKeyType ReturnKeyType
set { SetValue(ReturnKeyTypeProperty, value); }
}

public static DependencyProperty ReturnKeyTypeProperty { get ; } =
public static DependencyProperty ReturnKeyTypeProperty { get; } =
DependencyProperty.Register(
"ReturnKeyType",
typeof(UIReturnKeyType),
Expand Down Expand Up @@ -304,7 +312,7 @@ public UIKeyboardAppearance KeyboardAppearance
set { SetValue(KeyboardAppearanceProperty, value); }
}

public static DependencyProperty KeyboardAppearanceProperty { get ; } =
public static DependencyProperty KeyboardAppearanceProperty { get; } =
DependencyProperty.Register(
"KeyboardAppearance",
typeof(UIKeyboardAppearance),
Expand Down
8 changes: 8 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ partial void OnTextAlignmentChangedPartial(DependencyPropertyChangedEventArgs e)
{
}

partial void SelectPartial(int start, int length)
{
if (_textBoxView != null)
{
_textBoxView.SelectedTextRange = _textBoxView.GetTextRange(start: start, end: start + length);
}
}

private void UpdateTextBoxView()
{
if (_contentElement != null)
Expand Down
7 changes: 7 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ private void UpdateTextBoxView()

partial void OnFocusStateChangedPartial(FocusState focusState) => _textBoxView?.OnFocusStateChanged(focusState);

partial void SelectPartial(int start, int length)
{
_textBoxView?.Select(start, length);
}

public void SelectAll() => Select(0, Text.Length);

protected void SetIsPassword(bool isPassword) => _textBoxView?.SetIsPassword(isPassword);
}
}
8 changes: 8 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.wasm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Uno.Extensions;
using Uno.Foundation;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

Expand Down Expand Up @@ -116,6 +117,13 @@ partial void OnIsReadonlyChangedPartial(DependencyPropertyChangedEventArgs e)

private void ApplyIsReadonly(bool? isReadOnly = null) => _textBoxView?.SetIsReadOnly(isReadOnly ?? IsReadOnly);

partial void SelectPartial(int start, int length)
{
_textBoxView?.Select(start, length);
}

public void SelectAll() => Select(0, Text.Length);

public int SelectionStart
{
get => _textBoxView?.SelectionStart ?? 0;
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ internal void SetTextNative(string text)
_textBoxExtension?.SetTextNative(text);
}

internal void Select(int start, int length)
{
_textBoxExtension.Select(start, length);
}

internal void OnForegroundChanged(Brush brush) => DisplayBlock.Foreground = brush;

internal void OnFocusStateChanged(FocusState focusState)
Expand Down
4 changes: 4 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Windows.Foundation;
using System.Globalization;
using Uno.Disposables;
using Uno.Foundation;

namespace Windows.UI.Xaml.Controls
{
Expand Down Expand Up @@ -105,6 +106,9 @@ internal void SetTextNative(string text)
InvalidateMeasure();
}

internal void Select(int start, int length)
=> WebAssemblyRuntime.InvokeJS($"document.getElementById({HtmlId}).setSelectionRange({start}, {start + length})");

protected override Size MeasureOverride(Size availableSize) => MeasureView(availableSize);

internal void SetIsPassword(bool isPassword)
Expand Down

0 comments on commit b0f7646

Please sign in to comment.