Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue20911.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Controls.TestCases.HostApp.Issues;

[Issue(IssueTracker.Github, 20911, "Updating text in the Entry does not update CursorPosition during the TextChanged event", PlatformAffected.iOS)]
public class Issue20911 : ContentPage
{
Entry entry;
Label cursorPositonStatusLabel;

public Issue20911()
{
entry = new Entry
{
AutomationId = "ValidateEntryCursorPosition",
};

cursorPositonStatusLabel = new Label
{
AutomationId = "CursorPositionStatusLabel",
Text = "Cursor Position: 0",
FontSize = 16,
};

Button button = new Button
{
AutomationId = "ValidateEntryCursorPositionBtn",
Text = "Validate Entry Cursor Position",
};

button.Clicked += (sender, e) =>
{
cursorPositonStatusLabel.Text = $"{entry.CursorPosition}";
};

Content = new VerticalStackLayout
{
Padding = new Thickness(20),
Spacing = 10,
Children = { entry, cursorPositonStatusLabel, button }
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

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

public override string Issue => "Updating text in the Entry does not update CursorPosition during the TextChanged event";

[Test]
[Category(UITestCategories.Entry)]
public void VerifyEntryCursorPositionOnTextChanged()
{
App.WaitForElement("ValidateEntryCursorPosition");

App.EnterText("ValidateEntryCursorPosition", "Test");

App.WaitForElement("ValidateEntryCursorPositionBtn");
App.Tap("ValidateEntryCursorPositionBtn");

var cursorPositionStatus = App.FindElement("CursorPositionStatusLabel").GetText();
Assert.That(cursorPositionStatus, Is.EqualTo("4"));
}
}
8 changes: 8 additions & 0 deletions src/Core/src/Core/Extensions/ITextInputExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public static bool TextWithinMaxLength(this ITextInput textInput, string? text,

return shouldChange;
}

internal static void UpdateCursorPosition(this ITextInput textInput, int cursorPosition)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entry and Editor both implement ITextInput. Does Editor on iOS have the same issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz , I have checked it in the editor, and the issue does not reproduce there. While typing in the editor, the OnSelectionChanged method is called from the native side, which updates the cursor position. A reference video is attached below.

Editor.mov

{
if (textInput.CursorPosition != cursorPosition)
{
textInput.CursorPosition = cursorPosition;
}
}
#endif

#if ANDROID
Expand Down
12 changes: 8 additions & 4 deletions src/Core/src/Handlers/Entry/EntryHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,16 @@ void OnEditingBegan(object? sender, EventArgs e)

void OnEditingChanged(object? sender, EventArgs e)
{
if (sender is MauiTextField platformView)
if (sender is MauiTextField platformView && VirtualView is not null)
{
VirtualView?.UpdateText(platformView.Text);
}
}
// Update cursor position before updating text so that when TextChanged event fires,
// the CursorPosition property reflects the current native cursor position
VirtualView.UpdateCursorPosition(platformView.GetCursorPosition());

VirtualView.UpdateText(platformView.Text);
}
}

void OnEditingEnded(object? sender, EventArgs e)
{
if (sender is MauiTextField platformView && VirtualView is IEntry virtualView)
Expand Down
Loading