Skip to content

Revert "[Android, iOS] Dynamically setting SearchHandler Query property does not update text in the search box (#28400)" #30222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public SearchHandlerAppearanceTracker(IShellSearchView searchView, IShellContext
UpdateHorizontalTextAlignment();
UpdateVerticalTextAlignment();
UpdateInputType();
UpdateCharacterSpacing();
}

protected virtual void SearchHandlerPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
Expand Down Expand Up @@ -87,39 +86,6 @@ protected virtual void SearchHandlerPropertyChanged(object sender, System.Compon
{
UpdateAutomationId();
}
else if (e.Is(SearchHandler.QueryProperty))
{
UpdateText();
}
else if (e.Is(SearchHandler.CharacterSpacingProperty))
{
UpdateCharacterSpacing();
}
}
void UpdateCharacterSpacing()
{
if (_editText is not null)
{
_editText.LetterSpacing = _searchHandler.CharacterSpacing.ToEm();
}
}

void UpdateText()
{
int cursorPosition = _editText.SelectionStart;
bool selectionExists = _editText.HasSelection;

_editText.Text = _searchHandler.Query ?? string.Empty;

UpdateTextTransform();

// If we had a selection, place the cursor at the end of text
// Otherwise try to maintain the cursor at its previous position
int textLength = _editText.Text?.Length ?? 0;
int newPosition = selectionExists ? textLength : Math.Min(cursorPosition, textLength);

// Prevents the cursor from resetting to position zero when text is set programmatically
_editText.SetSelection(newPosition);
}

void EditTextFocusChange(object s, AView.FocusChangeEventArgs args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,45 +114,7 @@ void SearchHandlerPropertyChanged(object sender, System.ComponentModel.PropertyC
else if (e.Is(SearchHandler.VerticalTextAlignmentProperty))
{
UpdateSearchBarVerticalTextAlignment(_uiSearchBar.FindDescendantView<UITextField>());
}
else if (e.Is(SearchHandler.QueryProperty))
{
UpdateText(_uiSearchBar.FindDescendantView<UITextField>());
}
else if (e.Is(SearchHandler.CharacterSpacingProperty))
{
UpdateCharacterSpacing(_uiSearchBar.FindDescendantView<UITextField>());
}
}

void UpdateText(UITextField uiTextField)
{
if (uiTextField is null)
return;

uiTextField.Text = _searchHandler.Query;
UpdateTextTransform(uiTextField);
UpdateCharacterSpacing(uiTextField);
}

void UpdateCharacterSpacing(UITextField textField)
{
if (textField is null)
{
return;
}

var attributedText = textField.AttributedText?.WithCharacterSpacing(_searchHandler.CharacterSpacing);
if (attributedText is not null)
{
textField.AttributedText = attributedText;
}

var placeholderAttributedText = textField.AttributedPlaceholder?.WithCharacterSpacing(_searchHandler.CharacterSpacing);
if (placeholderAttributedText is not null)
{
textField.AttributedPlaceholder = placeholderAttributedText;
}
}
}

void GetDefaultSearchBarColors(UISearchBar searchBar)
Expand Down
57 changes: 0 additions & 57 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue14497.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if TEST_FAILS_ON_WINDOWS // Windows Character Spacing Issue Link - https://github.com/dotnet/maui/issues/29493
#if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_IOS && TEST_FAILS_ON_ANDROID // Windows Character Spacing Issue Link - https://github.com/dotnet/maui/issues/29493
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;
Expand Down