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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26158.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 26158, "SelectionLength property not applied when an entry is focused", PlatformAffected.Android | PlatformAffected.iOS)]
public class Issue26158 : ContentPage
{
public Issue26158()
{
Entry entry = new Entry()
{
AutomationId = "entry",
Text = "Microsoft Maui Entry",
};

entry.Focused += (s, e) =>
{
entry.CursorPosition = 0;
entry.SelectionLength = 4;
};

Content = entry;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if ANDROID || IOS

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there a reason this is not for windows and mac as well?

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.

I'm not sure it will work, but let's try

using NUnit.Framework;
using UITest.Appium;
using UITest.Core;
namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue26158 : _IssuesUITest
{
public Issue26158(TestDevice device) : base(device) { }

public override string Issue => "SelectionLength property not applied when an entry is focused";

[Test]
[Category(UITestCategories.Entry)]
public void SelectionLengthShouldUpdateWhenEntryIsFocused()
{
App.WaitForElement("entry");
App.Click("entry");
#if ANDROID
App.DismissKeyboard();
#endif
VerifyScreenshot();
}
Comment on lines +17 to +23
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 14 additions & 1 deletion src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,20 @@ static void UpdateCursorSelection(EditText editText, ITextInput entry)
int start = GetSelectionStart(editText, entry);
int end = GetSelectionEnd(editText, entry, start);

editText.SetSelection(start, end);
if (editText.IsFocused)
{
editText.Post(() =>
{
if (editText.IsAlive())
{
editText.SetSelection(start, end);
}
});
}
else
{
editText.SetSelection(start, end);
}
}
}

Expand Down
24 changes: 20 additions & 4 deletions src/Core/src/Platform/iOS/TextFieldExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,28 @@ public static void UpdateSelectionLength(this UITextField textField, IEntry entr
/* Updates both the IEntry.CursorPosition and IEntry.SelectionLength properties. */
static void UpdateCursorSelection(this UITextField textField, IEntry entry)
{
if (!entry.IsReadOnly)
if (entry.IsReadOnly)
{
UITextPosition start = GetSelectionStart(textField, entry, out int startOffset);
UITextPosition end = GetSelectionEnd(textField, entry, start, startOffset);
return;
}

textField.SelectedTextRange = textField.GetTextRange(start, end);
void UpdateSelection()
{
if (textField is not null && textField.Handle != IntPtr.Zero)
{
UITextPosition start = GetSelectionStart(textField, entry, out int startOffset);
UITextPosition end = GetSelectionEnd(textField, entry, start, startOffset);
textField.SelectedTextRange = textField.GetTextRange(start, end);
}
}

if (entry.IsFocused)
{
CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(UpdateSelection);
}
else
{
UpdateSelection();
}
}

Expand Down
Loading