Skip to content
Open
21 changes: 21 additions & 0 deletions src/Controls/src/Core/InputView/InputView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,27 @@ string ITextInput.Text
set => SetValue(TextProperty, value, SetterSpecificity.FromHandler);
}

private protected override void OnBindablePropertySet(BindableProperty property, object original, object value, bool changed, bool willFirePropertyChanged)
{
base.OnBindablePropertySet(property, original, value, changed, willFirePropertyChanged);

// Note: these properties 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.
if (!changed)
{
if (property.PropertyName == nameof(CursorPosition))
{
Handler?.UpdateValue(nameof(CursorPosition));
}
if (property.PropertyName == nameof(SelectionLength))
{
Handler?.UpdateValue(nameof(SelectionLength));
}
}
}

private protected override string GetDebuggerDisplay()
{
return $"Text = {Text}, {base.GetDebuggerDisplay()}";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.EntrySelectionTest"
Title="EntrySelectionTest"
Background="White">
<VerticalStackLayout>
<Entry
x:Name="TextBox"
AutomationId="TextBox"
WidthRequest="300"
Focused="OnTextBoxFocused"/>
<Button
x:Name="OtherElement"
AutomationId="OtherElement"
Text="Other"
WidthRequest="128"/>
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.ManualTest, "EntrySelectionTest", "Entry select all text issue", PlatformAffected.UWP)]
public partial class EntrySelectionTest : ContentPage
{
public EntrySelectionTest()
{
InitializeComponent();
}

void OnTextBoxFocused(object sender, FocusEventArgs e)
{
if (TextBox.Text != null)
{
TextBox.CursorPosition = 0;
TextBox.SelectionLength = TextBox.Text.Length;
}
}
}
31 changes: 31 additions & 0 deletions src/Controls/tests/TestCases.WinUI.Tests/EntrySelectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests;

public class EntrySelectionTest : _IssuesUITest
{
public EntrySelectionTest(TestDevice device)
: base(device)
{ }

public override string Issue => "Entry select all text issue";

[Test]
[Category(UITestCategories.Entry)]
public void VerifySelectWorks()
{
App.WaitForElement("TextBox");
App.Click("TextBox");
App.EnterText("TextBox", "Hello World");

App.Click("OtherElement");
App.Click("TextBox");

App.Click("OtherElement");
App.Click("TextBox");

VerifyScreenshot();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions src/Core/src/Platform/Windows/TextBoxExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -204,7 +205,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;
Expand All @@ -213,7 +217,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;
Expand Down
Loading