Skip to content

Commit

Permalink
[Android] SearchHandler - added focus/unfocus support
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo committed Sep 22, 2024
1 parent 4b8e604 commit 3836719
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public SearchHandlerAppearanceTracker(IShellSearchView searchView, IShellContext
_control = searchView.View;
_searchHandler.PropertyChanged += SearchHandlerPropertyChanged;
_editText = (_control as ViewGroup).GetChildrenOfType<EditText>().FirstOrDefault();
_editText.FocusChange += EditTextFocusChange;
UpdateSearchBarColors();
UpdateFont();
UpdateHorizontalTextAlignment();
Expand Down Expand Up @@ -83,6 +84,11 @@ protected virtual void SearchHandlerPropertyChanged(object sender, System.Compon
}
}

void EditTextFocusChange(object s, AView.FocusChangeEventArgs args)
{
_searchHandler.SetIsFocused(_editText.IsFocused);
}

void UpdateSearchBarColors()
{
UpdateBackgroundColor();
Expand Down Expand Up @@ -221,6 +227,7 @@ protected virtual void Dispose(bool disposing)
if (_searchHandler != null)
{
_searchHandler.PropertyChanged -= SearchHandlerPropertyChanged;
_editText.FocusChange -= EditTextFocusChange;
}
_searchHandler = null;
_control = null;
Expand Down
27 changes: 27 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24670.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.ManualTest, "24670", "SearchHandler.Focused event never fires", PlatformAffected.All)]
public partial class Issue24670 : Shell
{
public Issue24670()
{
InitializeComponent();
}

private void SearchHandler_Focused(object sender, EventArgs e)
{
focusedLabel.Text = "Focused: True";
}

private void SearchHandler_Unfocused(object sender, EventArgs e)
{
unfocusedLabel.Text = "Unfocused: True";
}

private void Button_Clicked(object sender, EventArgs e)
{
searchHandler.SetIsFocused(!searchHandler.IsFocused);
}
}
}
31 changes: 31 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue24670">
<ShellContent
Title="Home"
Route="MainPage">
<ContentPage>
<Shell.SearchHandler>
<SearchHandler
x:Name="searchHandler"
SearchBoxVisibility="Expanded"
Focused="SearchHandler_Focused"
Unfocused="SearchHandler_Unfocused"
Placeholder="Click to focus.."/>
</Shell.SearchHandler>

<StackLayout>
<Button AutomationId="button"
Clicked="Button_Clicked"
Text="Click to focus/unfocus search handler"/>
<Label x:Name="focusedLabel"
AutomationId="focusedLabel"
Text="Focused: False"/>
<Label x:Name="unfocusedLabel"
AutomationId="unfocusedLabel"
Text="Unfocused: False"/>
</StackLayout>
</ContentPage>
</ShellContent>
</Shell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue24670 : _IssuesUITest
{
public override string Issue => "SearchHandler.Focused event never fires";

public Issue24670(TestDevice testDevice) : base(testDevice)
{
}

[Test]
[Category(UITestCategories.Shell)]
[Category(UITestCategories.SearchBar)]
public void SearchHandlerFocusAndUnfocusEventsShouldWork()
{
App.Click("button");
App.Click("button");

var focusedLabelText = App.WaitForElement("focusedLabel").GetText();
var unfocusedLabelText = App.WaitForElement("unfocusedLabel").GetText();

Assert.That(focusedLabelText, Is.EqualTo("Focused: True"));
Assert.That(unfocusedLabelText, Is.EqualTo("Unfocused: True"));
}
}
}

0 comments on commit 3836719

Please sign in to comment.