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
28 changes: 28 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue28419.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 28419, "SearchBar focus/unfocus do not fire on Windows", PlatformAffected.UWP)]
public class Issue28419 : ContentPage
{
public Issue28419()
{
var label = new Label();
var searchBar = new SearchBar { Placeholder = "SearchBar", AutomationId = "SearchBar" };
var entry = new Entry { Placeholder = "Entry", AutomationId = "Entry" };

searchBar.Focused += (sender, e) =>
{
label.Text = "SearchBar Focused";
};

searchBar.Unfocused += (sender, e) =>
{
label.Text = "SearchBar Unfocused";
};

Content = new VerticalStackLayout
{
Spacing = 10,
Children = { label, searchBar, entry }
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;
public class Issue28419 : _IssuesUITest
{
public Issue28419(TestDevice device) : base(device) { }

public override string Issue => "SearchBar focus/unfocus do not fire on Windows";

[Test]
[Category(UITestCategories.SearchBar)]
public void SearchBarShouldTriggerFocusedAndUnFocusedEvents()
{
App.WaitForElement("SearchBar");
App.Tap("SearchBar");
App.WaitForElement("SearchBar Focused");
App.Tap("Entry");
App.WaitForElement("SearchBar Unfocused");
}
}
18 changes: 18 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ protected override void ConnectHandler(AutoSuggestBox platformView)
platformView.Loaded += OnLoaded;
platformView.QuerySubmitted += OnQuerySubmitted;
platformView.TextChanged += OnTextChanged;
//In ViewHandler.Windows, FocusManager.GotFocus and LostFocus are handled for other controls.
// However, for AutoSuggestBox, when handling the GotFocus or LostFocus methods, tapping the AutoSuggestBox causes e.NewFocusedElement and e.OldFocusedElement to be a TextBox (which receives the focus).
// As a result, when comparing the PlatformView with the appropriate handler in FocusManagerMapping, the condition is not satisfied, causing the focus and unfocus methods to not work correctly.
// To address this, I have specifically handled the focus and unfocus events for AutoSuggestBox here.
platformView.GotFocus += OnGotFocus;
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.

Good comment!

I was wondering if we could improve the logic in ViewHandler.Windows to manage the focus, and if comparing also based on derived types would make sense, even though AutoSuggestBox doesn't derive from Textbox (it does from ItemsControl). So, I think changes are fine.

platformView.LostFocus += OnLostFocus;
}

protected override void DisconnectHandler(AutoSuggestBox platformView)
{
platformView.Loaded -= OnLoaded;
platformView.QuerySubmitted -= OnQuerySubmitted;
platformView.TextChanged -= OnTextChanged;
platformView.GotFocus -= OnGotFocus;
platformView.LostFocus -= OnLostFocus;
}

public static void MapBackground(ISearchBarHandler handler, ISearchBar searchBar)
Expand Down Expand Up @@ -148,5 +156,15 @@ void OnTextChanged(AutoSuggestBox? sender, AutoSuggestBoxTextChangedEventArgs e)

VirtualView.Text = sender.Text;
}

void OnGotFocus(object sender, UI.Xaml.RoutedEventArgs e)
{
UpdateIsFocused(true);
}

void OnLostFocus(object sender, UI.Xaml.RoutedEventArgs e)
{
UpdateIsFocused(false);
}
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/View/ViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ static void FocusManager_LostFocus(object? sender, FocusManagerLostFocusEventArg
}
}

void UpdateIsFocused(bool isFocused)
private protected void UpdateIsFocused(bool isFocused)
{
if (VirtualView is not { } virtualView)
{
Expand Down
Loading