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

Commit

Permalink
Fix Search Handler results to measure to the provided content (#13658)
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
PureWeen committed Feb 9, 2021
1 parent a10c2c6 commit af9ad3c
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;


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

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.None, 0, "Shell Search Handler Item Sizing",
PlatformAffected.All)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.Shell)]
[NUnit.Framework.Category(UITestCategories.UwpIgnore)]
#endif
public class ShellSearchHandlerItemSizing : TestShell
{
protected override void Init()
{
ContentPage contentPage = new ContentPage();
AddFlyoutItem(contentPage, "Main Page");

Shell.SetSearchHandler(contentPage, new TestSearchHandler()
{
AutomationId = "SearchHandler"
});

contentPage.Content =
new StackLayout()
{
Children =
{
new Label()
{
Text = "Type into the search handler to display a list. Each item should be measured to the size of the content"
}
}
};
}


public class TestSearchHandler : SearchHandler
{
public TestSearchHandler()
{
ShowsResults = true;
ItemsSource = Enumerable.Range(0, 100)
.Select(_=> "searchresult")
.ToList();
}
}

#if UITEST

[Test]
public void SearchHandlerSizesCorrectly()
{
RunningApp.WaitForElement("SearchHandler");
RunningApp.EnterText("SearchHandler", "Hello");
var contentSize = RunningApp.WaitForElement("searchresult")[0].Rect;
Assert.Less(contentSize.Height, 100);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue13126_2.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13551.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RadioButtonTemplateFromStyle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellSearchHandlerItemSizing.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellWithCustomRendererDisabledAnimations.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue4720.cs" />
Expand Down
9 changes: 8 additions & 1 deletion Xamarin.Forms.Core/Shell/SearchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Xamarin.Forms
{
public class SearchHandler : BindableObject, ISearchHandlerController, IPlaceholderElement, IFontElement, ITextElement, ITextAlignmentElement
{

[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindablePropertyKey IsFocusedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsFocused),
typeof(bool), typeof(VisualElement), default(bool), propertyChanged: OnIsFocusedPropertyChanged);
Expand Down Expand Up @@ -261,6 +260,8 @@ void ISearchHandlerController.QueryConfirmed()
OnQueryConfirmed();
}

public static readonly BindableProperty AutomationIdProperty = BindableProperty.Create(nameof(AutomationId), typeof(string), typeof(SearchHandler), null);

public static readonly BindableProperty ClearIconHelpTextProperty =
BindableProperty.Create(nameof(ClearIconHelpText), typeof(string), typeof(SearchHandler), null, BindingMode.OneTime,
propertyChanged: (b, o, n) => ((SearchHandler)b).UpdateAutomationProperties());
Expand Down Expand Up @@ -345,6 +346,12 @@ void ISearchHandlerController.QueryConfirmed()

private ListProxy _listProxy;

public string AutomationId
{
get { return (string)GetValue(AutomationIdProperty); }
set { SetValue(AutomationIdProperty, value); }
}

public ImageSource ClearIcon
{
get { return (ImageSource)GetValue(ClearIconProperty); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,28 @@ internal static void GetDrawerAccessibilityResources(global::Android.Content.Con
resourceIdClose = context.Resources.GetIdentifier($"{automationIdParent}{s_defaultDrawerIdCloseSuffix}", "string", context.ApplicationInfo.PackageName);
}


internal static void SetAutomationId(AView control, Element element, string value = null)
{
if (element == null || control == null)
if (!control.IsAlive() || element == null)
{
return;
}

value = element.AutomationId;
SetAutomationId(control, element.AutomationId, value);
}

internal static void SetAutomationId(AView control, string automationId, string value = null)
{
if (!control.IsAlive())
{
return;
}

if (!string.IsNullOrEmpty(value))
automationId = value ?? automationId;
if (!string.IsNullOrEmpty(automationId))
{
control.ContentDescription = value;
control.ContentDescription = automationId;
}
}

Expand Down
12 changes: 11 additions & 1 deletion Xamarin.Forms.Platform.Android/Renderers/ContainerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected ContainerView(IntPtr javaReference, JniHandleOwnership transfer) : bas

public bool MatchHeight { get; set; }

internal bool MeasureHeight { get; set; }

public bool MatchWidth { get; set; }

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

_shellViewRenderer.LayoutView(measureWidth, measureHeight);
double? maxHeight = null;

if(MeasureHeight)
{
maxHeight = measureHeight;
measureHeight = double.PositiveInfinity;
}

_shellViewRenderer.LayoutView(0, 0, measureWidth, measureHeight, null, maxHeight);

SetMeasuredDimension((MatchWidth && width != 0) ? width : (int)Context.ToPixels(View.Width),
(MatchHeight && height != 0) ? height : (int)Context.ToPixels(View.Height));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ protected virtual void SearchHandlerPropertyChanged(object sender, System.Compon
{
UpdateVerticalTextAlignment();
}
else if (e.Is(SearchHandler.AutomationIdProperty))
{
UpdateAutomationId();
}
}

void UpdateSearchBarColors()
Expand All @@ -106,6 +110,15 @@ void UpdateSearchBarColors()
UpdateTextTransform();
UpdatePlaceholderColor();
UpdateCancelButtonColor();
UpdateAutomationId();
}

void UpdateAutomationId()
{
FastRenderers
.AutomationPropertiesProvider
.SetAutomationId(_editText, _searchHandler?.AutomationId);

}

void UpdateFont()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void UpdateFooterLayout()
{
if (_footerView != null)
{
_footerView.LayoutView(_shellContext.AndroidContext.FromPixels(_rootView.LayoutParameters.Width), double.PositiveInfinity);
_footerView.LayoutView(0, 0, _shellContext.AndroidContext.FromPixels(_rootView.LayoutParameters.Width), double.PositiveInfinity);
}
}

Expand All @@ -294,7 +294,7 @@ void UpdateContentLayout()

var width = View.MeasuredWidth;

_contentView.LayoutView(
_contentView.LayoutView(0, 0,
ShellContext.AndroidContext.FromPixels(width),
ShellContext.AndroidContext.FromPixels(height));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public override AView GetView(int position, AView convertView, ViewGroup parent)

result = new ContainerView(parent.Context, view);
result.MatchWidth = true;
result.MeasureHeight = true;
}

return result;
Expand Down
20 changes: 14 additions & 6 deletions Xamarin.Forms.Platform.Android/Renderers/ShellViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ internal class ShellViewRenderer
public IVisualElementRenderer Renderer { get; private set; }
View _view;
WeakReference<Context> _context;

// These are used by layout calls made by android if the layouts
// are invalidated. This ensures that the layout is performed
// using the same input values
public double Width { get; private set; }
public double Height { get; private set; }
public double? MaxWidth { get; private set; }
public double? MaxHeight { get; private set; }
public double X { get; private set; }
public double Y { get; private set; }

public ShellViewRenderer(Context context, View view)
{
Expand All @@ -42,11 +50,6 @@ public void TearDown()
_context = null;
}

public void LayoutView(double width, double height, double? maxWidth = null, double? maxHeight = null)
{
LayoutView(0, 0, width, height, maxWidth, maxHeight);
}

public void LayoutView(double x, double y, double width, double height, double? maxWidth = null, double? maxHeight = null)
{
if (width == -1)
Expand All @@ -57,6 +60,11 @@ public void LayoutView(double x, double y, double width, double height, double?

Width = width;
Height = height;
MaxWidth = maxWidth;
MaxHeight = maxHeight;
X = x;
Y = y;

Context context;

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

void OnViewSizeChanged(object sender, EventArgs e) =>
LayoutView(Width, Height);
LayoutView(X, Y, Width, Height, MaxWidth, MaxHeight);

public AView NativeView
{
Expand Down
11 changes: 11 additions & 0 deletions Xamarin.Forms.Platform.iOS/Renderers/ShellPageRendererTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,16 @@ protected virtual void OnSearchHandlerPropertyChanged(object sender, PropertyCha
UpdateSearchVisibility(_searchController);
else if (e.PropertyName == SearchHandler.IsSearchEnabledProperty.PropertyName)
UpdateSearchIsEnabled(_searchController);
else if (e.Is(SearchHandler.AutomationIdProperty))
{
UpdateAutomationId();
}
}

void UpdateAutomationId()
{
if (_searchHandler?.AutomationId != null && _searchController?.SearchBar != null)
_searchController.SearchBar.AccessibilityIdentifier = _searchHandler.AutomationId;
}

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

UpdateFlowDirection();
UpdateAutomationId();
}

void BookmarkButtonClicked(object sender, EventArgs e)
Expand Down

0 comments on commit af9ad3c

Please sign in to comment.