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

Addition of size calculation for Label placed in Popup and correction of size calculation when Popup Size is not specified #1456

Merged
merged 4 commits into from
Oct 22, 2023
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 @@ -2,7 +2,9 @@
using Android.Graphics.Drawables;
using Android.OS;
using Android.Views;
using AndroidX.AppCompat.Widget;
using Microsoft.Maui.Platform;
using static Android.Views.View;
using AColorRes = Android.Resource.Color;
using APoint = Android.Graphics.Point;
using ARect = Android.Graphics.Rect;
Expand Down Expand Up @@ -174,14 +176,26 @@ static void CalculateSizes(IPopup popup, Context context, Size windowSize, ref i
ArgumentNullException.ThrowIfNull(popup.Content);

var density = context.Resources?.DisplayMetrics?.Density ?? throw new InvalidOperationException($"Unable to determine density. {nameof(context.Resources.DisplayMetrics)} cannot be null");
var view = popup.Content.ToPlatform();

if (popup.Size.IsZero)
{
if (double.IsNaN(popup.Content.Width) || double.IsNaN(popup.Content.Height))
{
var size = popup.Content.Measure(windowSize.Width / density, windowSize.Height / density);
realContentWidth = (int)context.ToPixels(size.Width);
realContentHeight = (int)context.ToPixels(size.Height);
if (view is not null)
{
bool isRootView = true;
Measure(
view,
(int)(double.IsNaN(popup.Content.Width) ? windowSize.Width : (int)context.ToPixels(popup.Content.Width)),
(int)(double.IsNaN(popup.Content.Height) ? windowSize.Height : (int)context.ToPixels(popup.Content.Height)),
double.IsNaN(popup.Content.Width) && popup.HorizontalOptions != LayoutAlignment.Fill,
double.IsNaN(popup.Content.Height) && popup.VerticalOptions != LayoutAlignment.Fill,
ref isRootView
);
realContentWidth = view.MeasuredWidth;
realContentHeight = view.MeasuredHeight;
}

if (double.IsNaN(popup.Content.Width))
{
Expand All @@ -200,12 +214,20 @@ static void CalculateSizes(IPopup popup, Context context, Size windowSize, ref i
}
else
{
realWidth = (int)context.ToPixels(popup.Size.Width);
realHeight = (int)context.ToPixels(popup.Size.Height);

var size = popup.Content.Measure(popup.Size.Width, popup.Size.Height);
realContentWidth = (int)context.ToPixels(size.Width);
realContentHeight = (int)context.ToPixels(size.Height);
if (view is not null)
{
bool isRootView = true;
Measure(
view,
(int)context.ToPixels(popup.Size.Width),
(int)context.ToPixels(popup.Size.Height),
false,
false,
ref isRootView
);
realContentWidth = view.MeasuredWidth;
realContentHeight = view.MeasuredHeight;
}
}

realWidth = Math.Min(realWidth is 0 ? realContentWidth : realWidth, (int)windowSize.Width);
Expand All @@ -218,6 +240,40 @@ static void CalculateSizes(IPopup popup, Context context, Size windowSize, ref i
}
}

static void Measure(AView view, int width, int height, bool isNanWidth, bool isNanHeight, ref bool isRootView)
{
if (isRootView)
{
isRootView = false;
view.Measure(
MeasureSpec.MakeMeasureSpec(width, !isNanWidth ? MeasureSpecMode.Exactly : MeasureSpecMode.AtMost),
MeasureSpec.MakeMeasureSpec(height, !isNanHeight ? MeasureSpecMode.Exactly : MeasureSpecMode.AtMost)
);
}

if (view is AppCompatTextView)
{
// https://github.com/dotnet/maui/issues/2019
// https://github.com/dotnet/maui/pull/2059
var layoutParams = view.LayoutParameters;
view.Measure(
MeasureSpec.MakeMeasureSpec(width, (layoutParams?.Width == LinearLayout.LayoutParams.WrapContent && !isNanWidth) ? MeasureSpecMode.Exactly : MeasureSpecMode.Unspecified),
MeasureSpec.MakeMeasureSpec(height, (layoutParams?.Height == LinearLayout.LayoutParams.WrapContent && !isNanHeight) ? MeasureSpecMode.Exactly : MeasureSpecMode.Unspecified)
);
}

if (view is ViewGroup viewGroup)
{
for (int i = 0; i < viewGroup.ChildCount; i++)
{
if (viewGroup.GetChildAt(i) is AView childView)
{
Measure(childView, width, height, isNanWidth, isNanHeight, ref isRootView);
}
}
}
}

static Size GetWindowSize(IWindowManager? windowManager, ViewGroup decorView)
{
ArgumentNullException.ThrowIfNull(windowManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void SetSize(this MauiPopup mauiPopup, in IPopup popup)
if (double.IsNaN(popup.Content.Width) || double.IsNaN(popup.Content.Height))
{
var content = popup.Content.ToPlatform(popup.Handler?.MauiContext ?? throw new InvalidOperationException($"{nameof(popup.Handler.MauiContext)} Cannot Be Null"));
var contentSize = content.SizeThatFits(new CGSize(frame.Width, frame.Height));
var contentSize = content.SizeThatFits(new CGSize(double.IsNaN(popup.Content.Width) ? frame.Width : popup.Content.Width, double.IsNaN(popup.Content.Height) ? frame.Height : popup.Content.Height));
var width = contentSize.Width;
var height = contentSize.Height;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void SetSize(this Popup mauiPopup, IPopup popup, IMauiContext? mau
{
if (double.IsNaN(popup.Content.Width) || (double.IsNaN(popup.Content.Height)))
{
currentSize = popup.Content.Measure(popupParent.Bounds.Width, popupParent.Bounds.Height);
currentSize = popup.Content.Measure(double.IsNaN(popup.Content.Width) ? popupParent.Bounds.Width : popup.Content.Width, double.IsNaN(popup.Content.Height) ? popupParent.Bounds.Height : popup.Content.Height);

if (double.IsNaN(popup.Content.Width))
{
Expand Down