Skip to content

[.NET10] Add An API to SearchHandler so users can hide or show the softkeyboard #29600

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

Open
wants to merge 1 commit into
base: net10.0
Choose a base branch
from
Open
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 @@ -31,6 +31,8 @@ public SearchHandlerAppearanceTracker(IShellSearchView searchView, IShellContext
_searchHandler = searchView.SearchHandler;
_control = searchView.View;
_searchHandler.PropertyChanged += SearchHandlerPropertyChanged;
_searchHandler.ShowKeyboardRequested += OnShowKeyboardRequested;
_searchHandler.HideKeyboardRequested += OnHideKeyboardRequested;
_editText = (_control as ViewGroup).GetChildrenOfType<EditText>().FirstOrDefault();
_editText.FocusChange += EditTextFocusChange;
UpdateSearchBarColors();
Expand Down Expand Up @@ -211,6 +213,17 @@ void UpdateImageButtonIconColor(string tagName, Color toColor)
}
}

void OnShowKeyboardRequested(object sender, EventArgs e)
{
_editText?.RequestFocus();
_control?.ShowSoftInput();
}

void OnHideKeyboardRequested(object sender, EventArgs e)
{
_control?.HideSoftInput();
}

void UpdateInputType()
{
var keyboard = _searchHandler.Keyboard;
Expand Down Expand Up @@ -259,6 +272,8 @@ protected virtual void Dispose(bool disposing)
{
_searchHandler.PropertyChanged -= SearchHandlerPropertyChanged;
_editText.FocusChange -= EditTextFocusChange;
_searchHandler.ShowKeyboardRequested -= OnShowKeyboardRequested;
_searchHandler.HideKeyboardRequested -= OnHideKeyboardRequested;
}
_searchHandler = null;
_control = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public SearchHandlerAppearanceTracker(UISearchBar searchBar, SearchHandler searc
_fontManager = fontManager;
_searchHandler = searchHandler;
_searchHandler.PropertyChanged += SearchHandlerPropertyChanged;
_searchHandler.ShowKeyboardRequested += OnShowKeyboardRequested;
_searchHandler.HideKeyboardRequested += OnHideKeyboardRequested;
_searchHandler.FocusChangeRequested += SearchHandlerFocusChangeRequested;
_uiSearchBar = searchBar;
_uiSearchBar.OnEditingStarted += OnEditingStarted;
Expand Down Expand Up @@ -351,6 +353,17 @@ void OnSearchButtonClicked(object sender, EventArgs e)
_uiSearchBar.ResignFirstResponder();
}

void OnShowKeyboardRequested(object sender, EventArgs e)
{
_uiSearchBar?.BecomeFirstResponder();
}

void OnHideKeyboardRequested(object sender, EventArgs e)
{
_uiSearchBar?.ResignFirstResponder();
}


UIToolbar CreateNumericKeyboardAccessoryView()
{
var keyboardWidth = UIScreen.MainScreen.Bounds.Width;
Expand Down Expand Up @@ -408,6 +421,8 @@ protected virtual void Dispose(bool disposing)
{
_searchHandler.FocusChangeRequested -= SearchHandlerFocusChangeRequested;
_searchHandler.PropertyChanged -= SearchHandlerPropertyChanged;
_searchHandler.ShowKeyboardRequested -= OnShowKeyboardRequested;
_searchHandler.HideKeyboardRequested -= OnHideKeyboardRequested;
}
_searchHandler = null;
_uiSearchBar = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,5 @@ override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesSupport
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext? context) -> bool
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValues(System.ComponentModel.ITypeDescriptorContext? context) -> System.ComponentModel.TypeConverter.StandardValuesCollection!
Microsoft.Maui.Controls.IExtendedTypeConverter.ConvertFromInvariantString(string! value, System.IServiceProvider! serviceProvider) -> object?
Microsoft.Maui.Controls.SearchHandler.HideKeyboard() -> void
Microsoft.Maui.Controls.SearchHandler.ShowKeyboard() -> void
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,5 @@ override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesSupport
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext? context) -> bool
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValues(System.ComponentModel.ITypeDescriptorContext? context) -> System.ComponentModel.TypeConverter.StandardValuesCollection!
Microsoft.Maui.Controls.IExtendedTypeConverter.ConvertFromInvariantString(string! value, System.IServiceProvider! serviceProvider) -> object?
Microsoft.Maui.Controls.SearchHandler.HideKeyboard() -> void
Microsoft.Maui.Controls.SearchHandler.ShowKeyboard() -> void
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,5 @@ override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesSupport
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext? context) -> bool
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValues(System.ComponentModel.ITypeDescriptorContext? context) -> System.ComponentModel.TypeConverter.StandardValuesCollection!
Microsoft.Maui.Controls.IExtendedTypeConverter.ConvertFromInvariantString(string! value, System.IServiceProvider! serviceProvider) -> object?
Microsoft.Maui.Controls.SearchHandler.HideKeyboard() -> void
Microsoft.Maui.Controls.SearchHandler.ShowKeyboard() -> void
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,5 @@ override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesSupport
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext? context) -> bool
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValues(System.ComponentModel.ITypeDescriptorContext? context) -> System.ComponentModel.TypeConverter.StandardValuesCollection!
Microsoft.Maui.Controls.IExtendedTypeConverter.ConvertFromInvariantString(string! value, System.IServiceProvider! serviceProvider) -> object?
Microsoft.Maui.Controls.SearchHandler.HideKeyboard() -> void
Microsoft.Maui.Controls.SearchHandler.ShowKeyboard() -> void
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,5 @@ override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesSupport
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext? context) -> bool
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValues(System.ComponentModel.ITypeDescriptorContext? context) -> System.ComponentModel.TypeConverter.StandardValuesCollection!
Microsoft.Maui.Controls.IExtendedTypeConverter.ConvertFromInvariantString(string! value, System.IServiceProvider! serviceProvider) -> object?
Microsoft.Maui.Controls.SearchHandler.HideKeyboard() -> void
Microsoft.Maui.Controls.SearchHandler.ShowKeyboard() -> void
2 changes: 2 additions & 0 deletions src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,5 @@ override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesSupport
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext? context) -> bool
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValues(System.ComponentModel.ITypeDescriptorContext? context) -> System.ComponentModel.TypeConverter.StandardValuesCollection!
Microsoft.Maui.Controls.IExtendedTypeConverter.ConvertFromInvariantString(string! value, System.IServiceProvider! serviceProvider) -> object?
Microsoft.Maui.Controls.SearchHandler.HideKeyboard() -> void
Microsoft.Maui.Controls.SearchHandler.ShowKeyboard() -> void
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,5 @@ override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesSupport
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext? context) -> bool
override Microsoft.Maui.Controls.LayoutOptionsConverter.GetStandardValues(System.ComponentModel.ITypeDescriptorContext? context) -> System.ComponentModel.TypeConverter.StandardValuesCollection!
Microsoft.Maui.Controls.IExtendedTypeConverter.ConvertFromInvariantString(string! value, System.IServiceProvider! serviceProvider) -> object?
Microsoft.Maui.Controls.SearchHandler.HideKeyboard() -> void
Microsoft.Maui.Controls.SearchHandler.ShowKeyboard() -> void
13 changes: 13 additions & 0 deletions src/Controls/src/Core/Shell/SearchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class SearchHandler : BindableObject, ISearchHandlerController, IPlacehol

public event EventHandler<EventArgs> Focused;
public event EventHandler<EventArgs> Unfocused;

internal event EventHandler<EventArgs> ShowKeyboardRequested;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if these keyboard api should live in an interface so we can add to more controls , like entry for example .

internal event EventHandler<EventArgs> HideKeyboardRequested;

/// <summary>Bindable property for <see cref="IsFocused"/>.</summary>
public static readonly BindableProperty IsFocusedProperty = IsFocusedPropertyKey.BindableProperty;
Expand Down Expand Up @@ -84,6 +87,16 @@ public void Unfocus()
FocusChangeRequested?.Invoke(this, new FocusRequestArgs());
}

public void ShowKeyboard()
{
ShowKeyboardRequested?.Invoke(this, new EventArgs());
}

public void HideKeyboard()
{
HideKeyboardRequested?.Invoke(this, new EventArgs());
}

protected virtual void OnFocused()
{

Expand Down
26 changes: 26 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29559.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns:controls="clr-namespace:Maui.Controls.Sample.Issues"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue29559">
<ShellContent
Title="Home"
Route="MainPage">
<ContentPage>
<Shell.SearchHandler>
<SearchHandler x:Name="searchHandler"
Keyboard="Plain"/>
</Shell.SearchHandler>
<StackLayout>
<Button
AutomationId="ShowKeyboardButton"
Text="Show Keyboard"
Clicked="ShowKeyboardClicked"/>
<Button
AutomationId="HideKeyboardButton"
Text="Hide Keyboard"
Clicked="HideKeyboardClicked"/>
</StackLayout>
</ContentPage>
</ShellContent>
</Shell>
16 changes: 16 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29559.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29559, "Add An API to SearchHandler so users can hide or show the softkeyboard", PlatformAffected.Android | PlatformAffected.iOS)]
public partial class Issue29559 : Shell
{
public Issue29559()
{
InitializeComponent();
}

private void ShowKeyboardClicked(object sender, EventArgs e)
=> searchHandler.ShowKeyboard();

private void HideKeyboardClicked(object sender, EventArgs e)
=> searchHandler.HideKeyboard();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_CATALYST //In windows and mac catalyst, keyboard won't visible on focus and unfocus
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue29559 : _IssuesUITest
{
public Issue29559(TestDevice testDevice) : base(testDevice) { }

public override string Issue => "Add An API to SearchHandler so users can hide or show the softkeyboard";

[Test]
[Category(UITestCategories.Shell)]
public void Issue29559KeyboardDismissonSearchHander()
{
App.WaitForElement("ShowKeyboardButton");
App.Click("ShowKeyboardButton");
bool IskeyboardVisible = App.IsKeyboardShown();
Assert.That(IskeyboardVisible, Is.True);

App.WaitForElement("HideKeyboardButton");
App.Click("HideKeyboardButton");

IskeyboardVisible = App.IsKeyboardShown();
Assert.That(IskeyboardVisible, Is.False);
}
}
#endif
Loading