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

ScrollView CrossPlatformMeasure - refactor #27085

Open
wants to merge 1 commit into
base: main
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
23 changes: 11 additions & 12 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ static void InsertInsetView(IScrollViewHandler handler, IScrollView scrollView,

Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double heightConstraint)
{
var scrollView = VirtualView;
if (VirtualView is not { } scrollView)
{
return Size.Zero;
}

var padding = scrollView.Padding;

Expand All @@ -228,26 +231,22 @@ Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double he
return new Size(padding.HorizontalThickness, padding.VerticalThickness);
}

// Exclude the padding while measuring the internal content ...
var measurementWidth = widthConstraint - padding.HorizontalThickness;
var measurementHeight = heightConstraint - padding.VerticalThickness;

var result = (scrollView as ICrossPlatformLayout).CrossPlatformMeasure(measurementWidth, measurementHeight);

// ... and add the padding back in to the final result
var fullSize = new Size(result.Width + padding.HorizontalThickness, result.Height + padding.VerticalThickness);
var scrollOrientation = scrollView.Orientation;
var contentWidthConstraint = scrollOrientation is ScrollOrientation.Horizontal or ScrollOrientation.Both ? double.PositiveInfinity : widthConstraint;
var contentHeightConstraint = scrollOrientation is ScrollOrientation.Vertical or ScrollOrientation.Both ? double.PositiveInfinity : heightConstraint;
var contentSize = scrollView.MeasureContent(scrollView.Padding, contentWidthConstraint, contentHeightConstraint, !double.IsInfinity(contentWidthConstraint), !double.IsInfinity(contentHeightConstraint));

if (double.IsInfinity(widthConstraint))
{
widthConstraint = result.Width;
widthConstraint = contentSize.Width;
}

if (double.IsInfinity(heightConstraint))
{
heightConstraint = result.Height;
heightConstraint = contentSize.Height;
}

return fullSize.AdjustForFill(new Rect(0, 0, widthConstraint, heightConstraint), scrollView.PresentedContent);
return contentSize.AdjustForFill(new Rect(0, 0, widthConstraint, heightConstraint), scrollView.PresentedContent);
}

Size ICrossPlatformLayout.CrossPlatformArrange(Rect bounds)
Expand Down
27 changes: 12 additions & 15 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,37 +164,34 @@ static void InsertContentPanel(ScrollViewer scrollViewer, IScrollView scrollView

Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double heightConstraint)
{
var scrollView = VirtualView;
if (VirtualView is not { } scrollView)
{
return Size.Zero;
}

var padding = scrollView.Padding;
var presentedContent = scrollView.PresentedContent;

if (presentedContent == null)
if (scrollView.PresentedContent == null)
{
return new Size(padding.HorizontalThickness, padding.VerticalThickness);
}

// Exclude the padding while measuring the internal content ...
var measurementWidth = widthConstraint - padding.HorizontalThickness;
var measurementHeight = heightConstraint - padding.VerticalThickness;

var result = (scrollView as ICrossPlatformLayout).CrossPlatformMeasure(measurementWidth, measurementHeight);

// ... and add the padding back in to the final result
var fullSize = new Size(result.Width + padding.HorizontalThickness, result.Height + padding.VerticalThickness);
var scrollOrientation = scrollView.Orientation;
var contentWidthConstraint = scrollOrientation is ScrollOrientation.Horizontal or ScrollOrientation.Both ? double.PositiveInfinity : widthConstraint;
var contentHeightConstraint = scrollOrientation is ScrollOrientation.Vertical or ScrollOrientation.Both ? double.PositiveInfinity : heightConstraint;
var contentSize = scrollView.MeasureContent(scrollView.Padding, contentWidthConstraint, contentHeightConstraint, !double.IsInfinity(contentWidthConstraint), !double.IsInfinity(contentHeightConstraint));

if (double.IsInfinity(widthConstraint))
{
widthConstraint = result.Width;
widthConstraint = contentSize.Width;
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to delete both of these if checks on Android and Windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without them AdjustForFill doesn't work correctly

}

if (double.IsInfinity(heightConstraint))
{
heightConstraint = result.Height;
heightConstraint = contentSize.Height;
}

// If the presented content has LayoutAlignment Fill, we'll need to adjust the measurement to account for that
return fullSize.AdjustForFill(new Rect(0, 0, widthConstraint, heightConstraint), presentedContent);
return contentSize.AdjustForFill(new Rect(0, 0, widthConstraint, heightConstraint), scrollView.PresentedContent);
}

Size ICrossPlatformLayout.CrossPlatformArrange(Rect bounds)
Expand Down
27 changes: 1 addition & 26 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double he
var scrollOrientation = scrollView.Orientation;
var contentWidthConstraint = scrollOrientation is ScrollOrientation.Horizontal or ScrollOrientation.Both ? double.PositiveInfinity : widthConstraint;
var contentHeightConstraint = scrollOrientation is ScrollOrientation.Vertical or ScrollOrientation.Both ? double.PositiveInfinity : heightConstraint;
var contentSize = MeasureContent(scrollView, scrollView.Padding, contentWidthConstraint, contentHeightConstraint);
var contentSize = scrollView.MeasureContent(scrollView.Padding, contentWidthConstraint, contentHeightConstraint, !double.IsInfinity(contentWidthConstraint), !double.IsInfinity(contentHeightConstraint));

// Our target size is the smaller of it and the constraints
var width = contentSize.Width <= widthConstraint ? contentSize.Width : widthConstraint;
Expand All @@ -166,31 +166,6 @@ Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double he
return new Size(width, height);
}

static Size MeasureContent(IContentView contentView, Thickness inset, double widthConstraint, double heightConstraint)
{
var content = contentView.PresentedContent;

var contentSize = Size.Zero;

if (!double.IsInfinity(widthConstraint) && Dimension.IsExplicitSet(contentView.Width))
{
widthConstraint = contentView.Width;
}

if (!double.IsInfinity(heightConstraint) && Dimension.IsExplicitSet(contentView.Height))
{
heightConstraint = contentView.Height;
}

if (content is not null)
{
contentSize = content.Measure(widthConstraint - inset.HorizontalThickness,
heightConstraint - inset.VerticalThickness);
}

return new Size(contentSize.Width + inset.HorizontalThickness, contentSize.Height + inset.VerticalThickness);
}

Size ICrossPlatformLayout.CrossPlatformArrange(Rect bounds)
{
return (VirtualView as ICrossPlatformLayout)?.CrossPlatformArrange(bounds) ?? Size.Zero;
Expand Down
25 changes: 25 additions & 0 deletions src/Core/src/Layouts/LayoutExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,31 @@ public static Size MeasureContent(this IContentView contentView, Thickness inset
return new Size(contentSize.Width + inset.HorizontalThickness, contentSize.Height + inset.VerticalThickness);
}

internal static Size MeasureContent(this IContentView contentView, Thickness inset, double widthConstraint, double heightConstraint, bool constrainPresentedContentWidthToExplicitDimsOnContentView, bool constrainPresentedContentHeightToExplicitDimsOnContentView)
{
var content = contentView.PresentedContent;

if (Dimension.IsExplicitSet(contentView.Width) && constrainPresentedContentWidthToExplicitDimsOnContentView)
{
widthConstraint = contentView.Width;
}

if (Dimension.IsExplicitSet(contentView.Height) && constrainPresentedContentHeightToExplicitDimsOnContentView)
{
heightConstraint = contentView.Height;
}

var contentSize = Size.Zero;

if (content != null)
{
contentSize = content.Measure(widthConstraint - inset.HorizontalThickness,
heightConstraint - inset.VerticalThickness);
}

return new Size(contentSize.Width + inset.HorizontalThickness, contentSize.Height + inset.VerticalThickness);
}

public static void ArrangeContent(this IContentView contentView, Rect bounds)
{
if (contentView.PresentedContent == null)
Expand Down
Loading