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

For Android, respect Minimum/Maximum size requests even when size is set #25164

Merged
merged 1 commit into from
Nov 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
var widthSpec = Context.CreateMeasureSpec(targetWidth,
double.IsInfinity(targetWidth) ? double.NaN : targetWidth
, targetWidth);
, minimumSize: double.NaN, maximumSize: targetWidth);

var heightSpec = Context.CreateMeasureSpec(targetHeight, double.IsInfinity(targetHeight) ? double.NaN : targetHeight
, targetHeight);
, minimumSize: double.NaN, maximumSize: targetHeight);

var size = pvh.MeasureVirtualView(widthSpec, heightSpec);

Expand Down
39 changes: 39 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Layout/LayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,5 +535,44 @@ await AttachAndRun(grid, async _ =>
});
});
}

[Fact]
public async Task SizeRequestIsClampedToMinimumAndMaximum()
{
EnsureHandlerCreated((builder) =>
{
builder.ConfigureMauiHandlers(handler =>
{
handler.AddHandler(typeof(Button), typeof(ButtonHandler));
handler.AddHandler(typeof(Layout), typeof(LayoutHandler));
});
});

var button = new Button()
{
WidthRequest = 20, // request smaller than the minimum
MinimumWidthRequest = 200,
MaximumWidthRequest = 300,

HeightRequest = 400, // request larger than the maximum
MinimumHeightRequest = 200,
MaximumHeightRequest = 300,

HorizontalOptions = LayoutOptions.Start,
VerticalOptions = LayoutOptions.Start,
};

var grid = new Grid { button };

await InvokeOnMainThreadAsync(async () =>
{
await AttachAndRun(grid, _ =>
{
// The size should be the minimum requested size, since that will easily hold the "X" text
Assert.Equal(button.MinimumWidthRequest, button.Width, 0.5);
Assert.Equal(button.MaximumHeightRequest, button.Height, 0.5);
});
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public override Size GetDesiredSize(double widthConstraint, double heightConstra
}

// Create a spec to handle the native measure
var widthSpec = Context.CreateMeasureSpec(widthConstraint, virtualView.Width, virtualView.MaximumWidth);
var heightSpec = Context.CreateMeasureSpec(heightConstraint, virtualView.Height, virtualView.MaximumHeight);
var widthSpec = Context.CreateMeasureSpec(widthConstraint, virtualView.Width, virtualView.MinimumWidth, virtualView.MaximumWidth);
var heightSpec = Context.CreateMeasureSpec(heightConstraint, virtualView.Height, virtualView.MinimumHeight, virtualView.MaximumHeight);

if (platformView.FillViewport)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Handlers/ViewHandlerExtensions.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ internal static Size GetDesiredSizeFromHandler(this IViewHandler viewHandler, do
}

// Create a spec to handle the native measure
var widthSpec = Context.CreateMeasureSpec(widthConstraint, virtualView.Width, virtualView.MaximumWidth);
var heightSpec = Context.CreateMeasureSpec(heightConstraint, virtualView.Height, virtualView.MaximumHeight);
var widthSpec = Context.CreateMeasureSpec(widthConstraint, virtualView.Width, virtualView.MinimumWidth, virtualView.MaximumWidth);
var heightSpec = Context.CreateMeasureSpec(heightConstraint, virtualView.Height, virtualView.MinimumHeight, virtualView.MaximumHeight);

var packed = PlatformInterop.MeasureAndGetWidthAndHeight(platformView, widthSpec, heightSpec);
var measuredWidth = (int)(packed >> 32);
Expand Down
12 changes: 9 additions & 3 deletions src/Core/src/Platform/Android/ContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.IO;
using Android.Content;
using Android.Content.Res;
using Android.OS;
using Android.Util;
using Android.Views;
using Android.Views.InputMethods;
Expand Down Expand Up @@ -378,15 +377,22 @@ internal static int GetNavigationBarHeight(this Context context)
return _navigationBarHeight ?? 0;
}

internal static int CreateMeasureSpec(this Context context, double constraint, double explicitSize, double maximumSize)
internal static int CreateMeasureSpec(this Context context, double constraint, double explicitSize, double minimumSize, double maximumSize)
{
var mode = MeasureSpecMode.AtMost;

if (IsExplicitSet(explicitSize))
{
// We have a set value (i.e., a Width or Height)
mode = MeasureSpecMode.Exactly;
constraint = explicitSize;

// Since the mode is "Exactly", we have to return the exact final value clamped to the minimum/maximum.
constraint = Math.Max(explicitSize, ResolveMinimum(minimumSize));

if (IsMaximumSet(maximumSize))
{
constraint = Math.Min(constraint, maximumSize);
}
}
else if (IsMaximumSet(maximumSize) && maximumSize < constraint)
{
Expand Down
Loading