Skip to content
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

[net7.0] Ensure that UIScrollView ContentSize is set #11791

Merged
merged 5 commits into from
Dec 16, 2022
Merged
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
12 changes: 2 additions & 10 deletions src/Controls/src/Core/HandlerImpl/ScrollView/ScrollView.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,9 @@ protected override Size ArrangeOverride(Rect bounds)

Size IContentView.CrossPlatformArrange(Rect bounds)
{
if ((this as IContentView).PresentedContent is IView presentedContent)
if (this is IScrollView scrollView)
{
var padding = Padding;

// Normally we'd just want the content to be arranged within the ContentView's Frame,
// but ScrollView content might be larger than the ScrollView itself (for obvious reasons)
// So in each dimension, we assume the larger of the two values.
bounds.Width = Math.Max(bounds.Width, presentedContent.DesiredSize.Width + padding.HorizontalThickness);
bounds.Height = Math.Max(bounds.Height, presentedContent.DesiredSize.Height + padding.VerticalThickness);

this.ArrangeContent(bounds);
return scrollView.ArrangeContentUnbounded(bounds);
}

return bounds.Size;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class ScrollViewTests
{
[Fact]
public async Task ScrollViewContentSizeSet()
{
EnsureHandlerCreated(builder => { builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler<Entry, EntryHandler>(); }); });

var scrollView = new ScrollView();
var entry = new Entry() { Text = "In a ScrollView", HeightRequest = 10000 };
scrollView.Content = entry;

var scrollViewHandler = await InvokeOnMainThreadAsync(() =>
{
return CreateHandlerAsync<ScrollViewHandler>(scrollView);
});

await InvokeOnMainThreadAsync(async () => {
await scrollViewHandler.PlatformView.AttachAndRun(() =>
{
Assert.Equal(10000, scrollViewHandler.PlatformView.ContentSize.Height);
});
});
}
}
}
12 changes: 7 additions & 5 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,21 @@ static void InsertContentView(UIScrollView platformScrollView, IScrollView scrol
Tag = ContentPanelTag
};

contentContainer.CrossPlatformArrange = ArrangeScrollViewContent(scrollView.CrossPlatformArrange, contentContainer);
contentContainer.CrossPlatformArrange = ArrangeScrollViewContent(scrollView.CrossPlatformArrange, contentContainer, platformScrollView);

platformScrollView.ClearSubviews();
contentContainer.AddSubview(platformContent);
platformScrollView.AddSubview(contentContainer);
}

static Func<Rect, Size> ArrangeScrollViewContent(Func<Rect, Size> internalArrange, ContentView container)
static Func<Rect, Size> ArrangeScrollViewContent(Func<Rect, Size> internalArrange, ContentView container, UIScrollView platformScrollView)
{
return (rect) =>
{
if (container.Superview is UIScrollView scrollView)
{
// Ensure the container is at least the size of the UIScrollView itself, so that the
// cross-platform layout logic makes sense and the contents don't arrange out side the
// cross-platform layout logic makes sense and the contents don't arrange outside the
// container. (Everything will look correct if they do, but hit testing won't work properly.)

var scrollViewBounds = scrollView.Bounds;
Expand All @@ -176,7 +176,9 @@ static Func<Rect, Size> ArrangeScrollViewContent(Func<Rect, Size> internalArrang
container.Center = new CGPoint(container.Bounds.GetMidX(), container.Bounds.GetMidY());
}

return internalArrange(rect);
var contentSize = internalArrange(rect);
platformScrollView.ContentSize = contentSize;
return contentSize;
};
}

Expand Down Expand Up @@ -223,7 +225,7 @@ public override Size GetDesiredSize(double widthConstraint, double heightConstra
var virtualView = VirtualView;
var platformView = PlatformView;

if (platformView == null || virtualView == null || virtualView.PresentedContent == null)
if (platformView == null || virtualView == null)
{
return new Size(widthConstraint, heightConstraint);
}
Expand Down
33 changes: 33 additions & 0 deletions src/Core/src/Layouts/LayoutExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,38 @@ public static Size AdjustForFill(this Size size, Rect bounds, IView view)

return size;
}

/// <summary>
/// Arranges content which can exceed the bounds of the IContentView.
/// </summary>
/// <remarks>
/// Useful for arranging content where the IContentView provides a viewport to a portion of the content (e.g,
/// the content of an IScrollView).
/// </remarks>
/// <param name="contentView"></param>
/// <param name="bounds"></param>
/// <returns>The Size of the arranged content</returns>
public static Size ArrangeContentUnbounded(this IContentView contentView, Rect bounds)
{
var presentedContent = contentView.PresentedContent;

if (presentedContent == null)
{
return bounds.Size;
}

var padding = contentView.Padding;

// Normally we'd just want the content to be arranged within the ContentView's Frame,
// but in this case the content may exceed the size of the Frame.
// So in each dimension, we assume the larger of the two values.

bounds.Width = Math.Max(bounds.Width, presentedContent.DesiredSize.Width + padding.HorizontalThickness);
bounds.Height = Math.Max(bounds.Height, presentedContent.DesiredSize.Height + padding.VerticalThickness);

contentView.ArrangeContent(bounds);

return bounds.Size;
}
}
}
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
override Microsoft.Maui.Handlers.EditorHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect frame) -> void
static Microsoft.Maui.Handlers.StepperHandler.MapIsEnabled(Microsoft.Maui.Handlers.IStepperHandler! handler, Microsoft.Maui.IStepper! stepper) -> void
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#nullable enable
override Microsoft.Maui.Handlers.SwitchHandler.NeedsContainer.get -> bool
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#nullable enable
override Microsoft.Maui.Handlers.SwitchHandler.NeedsContainer.get -> bool
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Microsoft.Maui.IContentView! contentView, Microsoft.Maui.Graphics.Rect bounds) -> Microsoft.Maui.Graphics.Size
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ protected void InitializeViewHandler(IElement element, IElementHandler handler,
var w = size.Width;
var h = size.Height;

if (double.IsPositiveInfinity(w))
w = view.Width;

if (double.IsPositiveInfinity(h))
h = view.Height;
// No measure method should be returning infinite values
Assert.False(double.IsPositiveInfinity(w));
Assert.False(double.IsPositiveInfinity(h));

#else
// Windows cannot measure without the view being loaded
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Platform;
using ObjCRuntime;
using UIKit;
Expand All @@ -16,14 +18,11 @@ public partial class ScrollViewHandlerTests : CoreHandlerTestBase<ScrollViewHand
[Fact]
public async Task ContentInitializesCorrectly()
{
EnsureHandlerCreated(builder => { builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler<EntryStub, EntryHandler>(); }); });

bool result = await InvokeOnMainThreadAsync(() =>
{

var entry = new EntryStub() { Text = "In a ScrollView" };
var entryHandler = Activator.CreateInstance<EntryHandler>();
entryHandler.SetMauiContext(MauiContext);
entryHandler.SetVirtualView(entry);
entry.Handler = entryHandler;

var scrollView = new ScrollViewStub()
{
Expand All @@ -34,8 +33,8 @@ public async Task ContentInitializesCorrectly()

foreach (var platformView in scrollViewHandler.PlatformView.Subviews)
{
// ScrollView on iOS uses an intermediate ContentView to handle conetent measurement/arrangement
if (platformView is ContentView contentView)
// ScrollView on iOS uses an intermediate ContentView to handle content measurement/arrangement
if (platformView is Microsoft.Maui.Platform.ContentView contentView)
{
foreach (var content in contentView.Subviews)
{
Expand All @@ -52,5 +51,34 @@ public async Task ContentInitializesCorrectly()

Assert.True(result, $"Expected (but did not find) a {nameof(MauiTextField)} in the Subviews array");
}

[Fact]
public async Task ScrollViewContentSizeSet()
{
EnsureHandlerCreated(builder => { builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler<EntryStub, EntryHandler>(); }); });

var scrollView = new ScrollViewStub();
var entry = new EntryStub() { Text = "In a ScrollView" };
scrollView.Content = entry;

var scrollViewHandler = await InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler(scrollView);

// Setting an arbitrary value so we can verify that the handler is setting
// the UIScrollView's ContentSize property during AttachAndRun
handler.PlatformView.ContentSize = new CoreGraphics.CGSize(100, 100);
return handler;
});

await InvokeOnMainThreadAsync(async () => {
await scrollViewHandler.PlatformView.AttachAndRun(() =>
{
// Verify that the ContentSize values have been modified
Assert.NotEqual(100, scrollViewHandler.PlatformView.ContentSize.Height);
Assert.NotEqual(100, scrollViewHandler.PlatformView.ContentSize.Width);
});
});
}
}
}
4 changes: 1 addition & 3 deletions src/Core/tests/DeviceTests/Stubs/ScrollViewStub.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui.DeviceTests.Stubs
namespace Microsoft.Maui.DeviceTests.Stubs
{
public partial class ScrollViewStub : StubBase, IScrollView
{
Expand Down
3 changes: 2 additions & 1 deletion src/Core/tests/DeviceTests/Stubs/StubBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public Size Measure(double widthConstraint, double heightConstraint)
{
if (Handler != null)
{
return Handler.GetDesiredSize(widthConstraint, heightConstraint);
DesiredSize = Handler.GetDesiredSize(widthConstraint, heightConstraint);
return DesiredSize;
}

return new Size(widthConstraint, heightConstraint);
Expand Down