Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
34 changes: 33 additions & 1 deletion src/Controls/src/Core/Handlers/Shell/ShellItemHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ protected override void DisconnectHandler(FrameworkElement platformView)
_currentShellSection.PropertyChanged -= OnCurrentShellSectionPropertyChanged;

if (_currentSearchHandler != null)
{
_currentSearchHandler.PropertyChanged -= OnCurrentSearchHandlerPropertyChanged;
_currentSearchHandler.ShowSoftInputRequested -= OnShowSoftInputRequested;
_currentSearchHandler.HideSoftInputRequested -= OnHideSoftInputRequested;
}

if (_shellItem?.Parent is IShellController controller)
{
Expand Down Expand Up @@ -303,6 +307,8 @@ void UpdateSearchHandler()
if (_currentSearchHandler is not null)
{
_currentSearchHandler.PropertyChanged -= OnCurrentSearchHandlerPropertyChanged;
_currentSearchHandler.ShowSoftInputRequested -= OnShowSoftInputRequested;
_currentSearchHandler.HideSoftInputRequested -= OnHideSoftInputRequested;
}

_currentSearchHandler = newSearchHandler;
Expand All @@ -321,6 +327,9 @@ void UpdateSearchHandler()
mauiNavView.AutoSuggestBox = autoSuggestBox;
}

_currentSearchHandler.ShowSoftInputRequested += OnShowSoftInputRequested;
_currentSearchHandler.HideSoftInputRequested += OnHideSoftInputRequested;

autoSuggestBox.PlaceholderText = _currentSearchHandler.Placeholder;
autoSuggestBox.IsEnabled = _currentSearchHandler.IsSearchEnabled;
autoSuggestBox.ItemsSource = CreateSearchHandlerItemsSource();
Expand Down Expand Up @@ -363,6 +372,29 @@ void OnSearchBoxLostFocus(object sender, RoutedEventArgs e)
_currentSearchHandler?.SetIsFocused(false);
}

void OnShowSoftInputRequested(object? sender, EventArgs e)
{
if (PlatformView is not NavigationView mauiNavView || mauiNavView.AutoSuggestBox is not { } autoSuggestBox)
return;

autoSuggestBox.Focus(FocusState.Programmatic);
}

void OnHideSoftInputRequested(object? sender, EventArgs e)
{
if (PlatformView is not NavigationView mauiNavView || mauiNavView.AutoSuggestBox is not { } autoSuggestBox)
return;

if (!autoSuggestBox.IsEnabled)
return;

var isTabStop = autoSuggestBox.IsTabStop;
autoSuggestBox.IsTabStop = false;
autoSuggestBox.IsEnabled = false;
autoSuggestBox.IsEnabled = true;
autoSuggestBox.IsTabStop = isTabStop;
}

void OnSearchBoxTextChanged(Microsoft.UI.Xaml.Controls.AutoSuggestBox sender, Microsoft.UI.Xaml.Controls.AutoSuggestBoxTextChangedEventArgs args)
{
if (_currentSearchHandler == null)
Expand Down Expand Up @@ -645,4 +677,4 @@ void OnApplyTemplateFinished(object? sender, EventArgs e)
UpdateAppearance(_shellAppearanceElement);
}
}
}
}
103 changes: 103 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue34930.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 34930, "SearchHandler.ShowSoftInputAsync() does not focus the SearchHandler", PlatformAffected.UWP)]
public class Issue34930 : Shell
{
public Issue34930()
{
var flyoutItem = new FlyoutItem
{
Route = "home",
FlyoutDisplayOptions = FlyoutDisplayOptions.AsSingleItem
};

flyoutItem.Items.Add(new Tab
{
Title = "Home",
Route = "hometab",
Items =
{
new ShellContent
{
Title = "Home",
Route = "homepage",
ContentTemplate = new DataTemplate(typeof(_34930TestPage))
}
}
});

Items.Add(flyoutItem);
}

public class _34930TestPage : ContentPage
{
public _34930TestPage()
{
Title = "Home";

var isFocusedLabel = new Label
{
Text = "IsFocused: False",
AutomationId = "isFocusedLabel"
};

var focusedEventLabel = new Label
{
Text = "FocusedEvent: False",
AutomationId = "focusedEventLabel"
};

var searchHandler = new SearchHandler
{
AutomationId = "searchHandler",
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
Outdated
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
Outdated
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
Outdated
Placeholder = "Search...",
SearchBoxVisibility = SearchBoxVisibility.Expanded,
ShowsResults = false
};

searchHandler.Focused += (s, e) =>
{
focusedEventLabel.Text = "FocusedEvent: True";
};

var showKeyboardButton = new Button
{
Text = "Show Soft Input",
AutomationId = "ShowKeyboardButton"
};

showKeyboardButton.Clicked += async (s, e) =>
{
searchHandler.ShowSoftInputAsync();
await Task.Delay(200);
isFocusedLabel.Text = $"IsFocused: {searchHandler.IsFocused}";
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
Outdated
};

var hideKeyboardButton = new Button
{
Text = "Hide Soft Input",
AutomationId = "HideKeyboardButton"
};

hideKeyboardButton.Clicked += (s, e) =>
{
searchHandler.HideSoftInputAsync();
};

Shell.SetSearchHandler(this, searchHandler);

Content = new VerticalStackLayout
{
Spacing = 15,
Padding = 20,
Children =
{
showKeyboardButton,
hideKeyboardButton,
isFocusedLabel,
focusedEventLabel
}
};
}
}
}
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 Issue34930 : _IssuesUITest
{
public override string Issue => "SearchHandler.ShowSoftInputAsync() does not focus the SearchHandler on Windows";

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

[Test]
[Category(UITestCategories.SearchBar)]
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
Outdated
public void SearchHandlerShowSoftInputShouldFocusSearchHandlerOnWindows()
{
App.WaitForElement("ShowKeyboardButton");
App.Tap("ShowKeyboardButton");

Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
var isFocused = App.WaitForElement("isFocusedLabel").GetText();
Assert.That(isFocused, Is.EqualTo("IsFocused: True"));

Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
var focusedEvent = App.WaitForElement("focusedEventLabel").GetText();
Assert.That(focusedEvent, Is.EqualTo("FocusedEvent: True"));
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
Outdated
}
}
}
Loading