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

Commit af9ad3c

Browse files
authored
Fix Search Handler results to measure to the provided content (#13658) fixes #13403
* Force Shell TitleView to height of container * - fix collapse on scroll and scroll * - fix android * - uiitests * - fix header positioning * Fix search handler results to size to view * Update ShellViewRenderer.cs
1 parent a10c2c6 commit af9ad3c

File tree

10 files changed

+154
-14
lines changed

10 files changed

+154
-14
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.ComponentModel;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Xamarin.Forms.CustomAttributes;
10+
using Xamarin.Forms.Internals;
11+
using Xamarin.Forms.PlatformConfiguration;
12+
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
13+
14+
15+
#if UITEST
16+
using Xamarin.UITest;
17+
using NUnit.Framework;
18+
using Xamarin.Forms.Core.UITests;
19+
#endif
20+
21+
namespace Xamarin.Forms.Controls.Issues
22+
{
23+
[Preserve(AllMembers = true)]
24+
[Issue(IssueTracker.None, 0, "Shell Search Handler Item Sizing",
25+
PlatformAffected.All)]
26+
#if UITEST
27+
[NUnit.Framework.Category(UITestCategories.Shell)]
28+
[NUnit.Framework.Category(UITestCategories.UwpIgnore)]
29+
#endif
30+
public class ShellSearchHandlerItemSizing : TestShell
31+
{
32+
protected override void Init()
33+
{
34+
ContentPage contentPage = new ContentPage();
35+
AddFlyoutItem(contentPage, "Main Page");
36+
37+
Shell.SetSearchHandler(contentPage, new TestSearchHandler()
38+
{
39+
AutomationId = "SearchHandler"
40+
});
41+
42+
contentPage.Content =
43+
new StackLayout()
44+
{
45+
Children =
46+
{
47+
new Label()
48+
{
49+
Text = "Type into the search handler to display a list. Each item should be measured to the size of the content"
50+
}
51+
}
52+
};
53+
}
54+
55+
56+
public class TestSearchHandler : SearchHandler
57+
{
58+
public TestSearchHandler()
59+
{
60+
ShowsResults = true;
61+
ItemsSource = Enumerable.Range(0, 100)
62+
.Select(_=> "searchresult")
63+
.ToList();
64+
}
65+
}
66+
67+
#if UITEST
68+
69+
[Test]
70+
public void SearchHandlerSizesCorrectly()
71+
{
72+
RunningApp.WaitForElement("SearchHandler");
73+
RunningApp.EnterText("SearchHandler", "Hello");
74+
var contentSize = RunningApp.WaitForElement("searchresult")[0].Rect;
75+
Assert.Less(contentSize.Height, 100);
76+
}
77+
#endif
78+
}
79+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Compile Include="$(MSBuildThisFileDirectory)Issue13126_2.cs" />
1717
<Compile Include="$(MSBuildThisFileDirectory)Issue13551.cs" />
1818
<Compile Include="$(MSBuildThisFileDirectory)RadioButtonTemplateFromStyle.cs" />
19+
<Compile Include="$(MSBuildThisFileDirectory)ShellSearchHandlerItemSizing.cs" />
1920
<Compile Include="$(MSBuildThisFileDirectory)ShellWithCustomRendererDisabledAnimations.cs" />
2021
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContent.cs" />
2122
<Compile Include="$(MSBuildThisFileDirectory)Issue4720.cs" />

Xamarin.Forms.Core/Shell/SearchHandler.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Xamarin.Forms
1010
{
1111
public class SearchHandler : BindableObject, ISearchHandlerController, IPlaceholderElement, IFontElement, ITextElement, ITextAlignmentElement
1212
{
13-
1413
[EditorBrowsable(EditorBrowsableState.Never)]
1514
public static readonly BindablePropertyKey IsFocusedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsFocused),
1615
typeof(bool), typeof(VisualElement), default(bool), propertyChanged: OnIsFocusedPropertyChanged);
@@ -261,6 +260,8 @@ void ISearchHandlerController.QueryConfirmed()
261260
OnQueryConfirmed();
262261
}
263262

263+
public static readonly BindableProperty AutomationIdProperty = BindableProperty.Create(nameof(AutomationId), typeof(string), typeof(SearchHandler), null);
264+
264265
public static readonly BindableProperty ClearIconHelpTextProperty =
265266
BindableProperty.Create(nameof(ClearIconHelpText), typeof(string), typeof(SearchHandler), null, BindingMode.OneTime,
266267
propertyChanged: (b, o, n) => ((SearchHandler)b).UpdateAutomationProperties());
@@ -345,6 +346,12 @@ void ISearchHandlerController.QueryConfirmed()
345346

346347
private ListProxy _listProxy;
347348

349+
public string AutomationId
350+
{
351+
get { return (string)GetValue(AutomationIdProperty); }
352+
set { SetValue(AutomationIdProperty, value); }
353+
}
354+
348355
public ImageSource ClearIcon
349356
{
350357
get { return (ImageSource)GetValue(ClearIconProperty); }

Xamarin.Forms.Platform.Android/FastRenderers/AutomationPropertiesProvider.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,28 @@ internal static void GetDrawerAccessibilityResources(global::Android.Content.Con
3030
resourceIdClose = context.Resources.GetIdentifier($"{automationIdParent}{s_defaultDrawerIdCloseSuffix}", "string", context.ApplicationInfo.PackageName);
3131
}
3232

33+
3334
internal static void SetAutomationId(AView control, Element element, string value = null)
3435
{
35-
if (element == null || control == null)
36+
if (!control.IsAlive() || element == null)
3637
{
3738
return;
3839
}
3940

40-
value = element.AutomationId;
41+
SetAutomationId(control, element.AutomationId, value);
42+
}
43+
44+
internal static void SetAutomationId(AView control, string automationId, string value = null)
45+
{
46+
if (!control.IsAlive())
47+
{
48+
return;
49+
}
4150

42-
if (!string.IsNullOrEmpty(value))
51+
automationId = value ?? automationId;
52+
if (!string.IsNullOrEmpty(automationId))
4353
{
44-
control.ContentDescription = value;
54+
control.ContentDescription = automationId;
4555
}
4656
}
4757

Xamarin.Forms.Platform.Android/Renderers/ContainerView.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ protected ContainerView(IntPtr javaReference, JniHandleOwnership transfer) : bas
2929

3030
public bool MatchHeight { get; set; }
3131

32+
internal bool MeasureHeight { get; set; }
33+
3234
public bool MatchWidth { get; set; }
3335

3436
public View View
@@ -90,7 +92,15 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
9092
var measureWidth = width > 0 ? Context.FromPixels(width) : double.PositiveInfinity;
9193
var measureHeight = height > 0 ? Context.FromPixels(height) : double.PositiveInfinity;
9294

93-
_shellViewRenderer.LayoutView(measureWidth, measureHeight);
95+
double? maxHeight = null;
96+
97+
if(MeasureHeight)
98+
{
99+
maxHeight = measureHeight;
100+
measureHeight = double.PositiveInfinity;
101+
}
102+
103+
_shellViewRenderer.LayoutView(0, 0, measureWidth, measureHeight, null, maxHeight);
94104

95105
SetMeasuredDimension((MatchWidth && width != 0) ? width : (int)Context.ToPixels(View.Width),
96106
(MatchHeight && height != 0) ? height : (int)Context.ToPixels(View.Height));

Xamarin.Forms.Platform.Android/Renderers/SearchHandlerAppearanceTracker.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ protected virtual void SearchHandlerPropertyChanged(object sender, System.Compon
9797
{
9898
UpdateVerticalTextAlignment();
9999
}
100+
else if (e.Is(SearchHandler.AutomationIdProperty))
101+
{
102+
UpdateAutomationId();
103+
}
100104
}
101105

102106
void UpdateSearchBarColors()
@@ -106,6 +110,15 @@ void UpdateSearchBarColors()
106110
UpdateTextTransform();
107111
UpdatePlaceholderColor();
108112
UpdateCancelButtonColor();
113+
UpdateAutomationId();
114+
}
115+
116+
void UpdateAutomationId()
117+
{
118+
FastRenderers
119+
.AutomationPropertiesProvider
120+
.SetAutomationId(_editText, _searchHandler?.AutomationId);
121+
109122
}
110123

111124
void UpdateFont()

Xamarin.Forms.Platform.Android/Renderers/ShellFlyoutTemplatedContentRenderer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void UpdateFooterLayout()
276276
{
277277
if (_footerView != null)
278278
{
279-
_footerView.LayoutView(_shellContext.AndroidContext.FromPixels(_rootView.LayoutParameters.Width), double.PositiveInfinity);
279+
_footerView.LayoutView(0, 0, _shellContext.AndroidContext.FromPixels(_rootView.LayoutParameters.Width), double.PositiveInfinity);
280280
}
281281
}
282282

@@ -294,7 +294,7 @@ void UpdateContentLayout()
294294

295295
var width = View.MeasuredWidth;
296296

297-
_contentView.LayoutView(
297+
_contentView.LayoutView(0, 0,
298298
ShellContext.AndroidContext.FromPixels(width),
299299
ShellContext.AndroidContext.FromPixels(height));
300300
}

Xamarin.Forms.Platform.Android/Renderers/ShellSearchViewAdapter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public override AView GetView(int position, AView convertView, ViewGroup parent)
105105

106106
result = new ContainerView(parent.Context, view);
107107
result.MatchWidth = true;
108+
result.MeasureHeight = true;
108109
}
109110

110111
return result;

Xamarin.Forms.Platform.Android/Renderers/ShellViewRenderer.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ internal class ShellViewRenderer
1515
public IVisualElementRenderer Renderer { get; private set; }
1616
View _view;
1717
WeakReference<Context> _context;
18+
19+
// These are used by layout calls made by android if the layouts
20+
// are invalidated. This ensures that the layout is performed
21+
// using the same input values
1822
public double Width { get; private set; }
1923
public double Height { get; private set; }
24+
public double? MaxWidth { get; private set; }
25+
public double? MaxHeight { get; private set; }
26+
public double X { get; private set; }
27+
public double Y { get; private set; }
2028

2129
public ShellViewRenderer(Context context, View view)
2230
{
@@ -42,11 +50,6 @@ public void TearDown()
4250
_context = null;
4351
}
4452

45-
public void LayoutView(double width, double height, double? maxWidth = null, double? maxHeight = null)
46-
{
47-
LayoutView(0, 0, width, height, maxWidth, maxHeight);
48-
}
49-
5053
public void LayoutView(double x, double y, double width, double height, double? maxWidth = null, double? maxHeight = null)
5154
{
5255
if (width == -1)
@@ -57,6 +60,11 @@ public void LayoutView(double x, double y, double width, double height, double?
5760

5861
Width = width;
5962
Height = height;
63+
MaxWidth = maxWidth;
64+
MaxHeight = maxHeight;
65+
X = x;
66+
Y = y;
67+
6068
Context context;
6169

6270
if (Renderer == null || !(_context.TryGetTarget(out context)) || !Renderer.View.IsAlive())
@@ -134,7 +142,7 @@ public virtual void OnViewSet(View view)
134142
}
135143

136144
void OnViewSizeChanged(object sender, EventArgs e) =>
137-
LayoutView(Width, Height);
145+
LayoutView(X, Y, Width, Height, MaxWidth, MaxHeight);
138146

139147
public AView NativeView
140148
{

Xamarin.Forms.Platform.iOS/Renderers/ShellPageRendererTracker.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,16 @@ protected virtual void OnSearchHandlerPropertyChanged(object sender, PropertyCha
478478
UpdateSearchVisibility(_searchController);
479479
else if (e.PropertyName == SearchHandler.IsSearchEnabledProperty.PropertyName)
480480
UpdateSearchIsEnabled(_searchController);
481+
else if (e.Is(SearchHandler.AutomationIdProperty))
482+
{
483+
UpdateAutomationId();
484+
}
485+
}
486+
487+
void UpdateAutomationId()
488+
{
489+
if (_searchHandler?.AutomationId != null && _searchController?.SearchBar != null)
490+
_searchController.SearchBar.AccessibilityIdentifier = _searchHandler.AutomationId;
481491
}
482492

483493
protected virtual void RemoveSearchController(UINavigationItem navigationItem)
@@ -602,6 +612,7 @@ void AttachSearchController()
602612
_searchHandlerAppearanceTracker = new SearchHandlerAppearanceTracker(searchBar, SearchHandler);
603613

604614
UpdateFlowDirection();
615+
UpdateAutomationId();
605616
}
606617

607618
void BookmarkButtonClicked(object sender, EventArgs e)

0 commit comments

Comments
 (0)