Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

#7956 [Shell] Unable to focus and unfocus search handler for Android #14375

Closed
wants to merge 11 commits into from
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestShell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:Xamarin.Forms.Controls"
xmlns:issues="clr-namespace:Xamarin.Forms.Controls.Issues"
x:Class="Xamarin.Forms.Controls.Issues.Issue7956">

<ShellContent Title="SearchHandler Focus">
<ContentPage>
<StackLayout>
<Label Text="Please, click on the Shell SearchHandler search box above, in order to fire the Focus EventHandler. This test will pass if: on focus, you see another text below saying 'Focus EventHandler triggered!'; on unfocus, you see another text below saying 'Focus EventHandler triggered!'. If nothing happens, or only one of the two previous cases happen, this test would have failed."
Padding="10"/>
<Label x:Name="lblFocus" Text="Focus EventHandler triggered!'" IsVisible="false" />
<Label x:Name="lblUnfocus" Text="Unfocus EventHandler triggered!'" IsVisible="false" />
</StackLayout>
<Shell.SearchHandler>
<SearchHandler Focused="SearchHandler_Focused" Unfocused="SearchHandler_Unfocused"/>
</Shell.SearchHandler>
</ContentPage>
</ShellContent>

</local:TestShell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 7956, "[Shell] Unable to focus and unfocus search handler for Android",
PlatformAffected.Android)]
public partial class Issue7956 : TestShell
{
public Issue7956()
{
BindingContext = this;
#if APP
InitializeComponent();
#endif
}

protected override void Init()
{

}

private void SearchHandler_Focused(object sender, System.EventArgs e)
{
lblFocus.IsVisible = true;
lblUnfocus.IsVisible = false;
}
private void SearchHandler_Unfocused(object sender, System.EventArgs e)
{
lblFocus.IsVisible = false;
lblUnfocus.IsVisible = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestShell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:Xamarin.Forms.Controls"
xmlns:issues="clr-namespace:Xamarin.Forms.Controls.Issues"
x:Class="Xamarin.Forms.Controls.Issues.Issue7956_1">

<ShellContent Title="SearchHandler Focus">
<ContentPage>
<StackLayout>
<Label Text="Please, click on the Shell SearchHandler search box above, in order to fire the Focus EventHandler. This test will pass if: on focus, you see another text below saying 'Focus EventHandler triggered!'; on unfocus, you see another text below saying 'Focus EventHandler triggered!'. If nothing happens, or only one of the two previous cases happen, this test would have failed."
Padding="10"/>
<Label x:Name="lblFocus" Text="Focus EventHandler triggered!'" IsVisible="false" />
<Label x:Name="lblUnfocus" Text="Unfocus EventHandler triggered!'" IsVisible="false" />
</StackLayout>
<Shell.SearchHandler>
<issues:InheritedSearchHandler Focused="SearchHandler_Focused" Unfocused="SearchHandler_Unfocused"/>
</Shell.SearchHandler>
</ContentPage>
</ShellContent>

</local:TestShell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 7956, "[Shell] Unable to focus and unfocus search handler for Android",
PlatformAffected.Android, issueTestNumber:1)]
public partial class Issue7956_1 : TestShell
{
public Issue7956_1()
{
BindingContext = this;
#if APP
InitializeComponent();
#endif
}

protected override void Init()
{

}

private void SearchHandler_Focused(object sender, System.EventArgs e)
{
lblFocus.IsVisible = true;
lblUnfocus.IsVisible = false;
}
private void SearchHandler_Unfocused(object sender, System.EventArgs e)
{
lblFocus.IsVisible = false;
lblUnfocus.IsVisible = true;
}
}

public class InheritedSearchHandler : SearchHandler
{
protected override void OnFocused()
{
base.OnFocused();
}
protected override void OnUnfocus()
{
base.OnUnfocus();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue13126.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13126_2.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13551.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7956.xaml.cs">
<DependentUpon>Issue7956.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue7956_1.xaml.cs">
<DependentUpon>Issue7956_1.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue13819.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13916.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14523.xaml.cs">
Expand Down Expand Up @@ -2780,6 +2787,16 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue7956.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue7956_1.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue14523.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
Expand Down
9 changes: 8 additions & 1 deletion Xamarin.Forms.Platform.Android/Renderers/ShellSearchView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ protected override void Dispose(bool disposing)
SearchHandler.PropertyChanged -= OnSearchHandlerPropertyChanged;

_textBlock.ItemClick -= OnTextBlockItemClicked;
_textBlock.FocusChange -= OnSearchHandlerFocusChange;
_textBlock.RemoveTextChangedListener(this);
_textBlock.SetOnEditorActionListener(null);
_textBlock.DropDownBackground.Dispose();
Expand Down Expand Up @@ -209,6 +210,7 @@ protected virtual void LoadView(SearchHandler searchHandler)
_textBlock.Adapter = new ShellSearchViewAdapter(SearchHandler, _shellContext);
_textBlock.ItemClick += OnTextBlockItemClicked;
_textBlock.SetDropDownBackgroundDrawable(new ClipDrawableWrapper(_textBlock.DropDownBackground));
_textBlock.FocusChange += OnSearchHandlerFocusChange;

// A note on accessibility. The _textBlocks hint is what android defaults to reading in the screen
// reader. Therefore, we do not need to set something else.
Expand Down Expand Up @@ -236,6 +238,11 @@ protected virtual void LoadView(SearchHandler searchHandler)
linearLayout.Dispose();
}

void OnSearchHandlerFocusChange(object sender, FocusChangeEventArgs e)
{
SearchHandler?.SetValue(SearchHandler.IsFocusedPropertyKey, e.HasFocus);
}

protected virtual void OnSearchHandlerPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == SearchHandler.IsSearchEnabledProperty.PropertyName)
Expand Down Expand Up @@ -394,4 +401,4 @@ public override void Draw(Canvas canvas)
}
}
}
}
}