From 0a571890e132f9203c4ebe95123eb8cbfe111c0f Mon Sep 17 00:00:00 2001 From: Juan Diego Herrera Date: Mon, 8 May 2023 15:09:32 -0700 Subject: [PATCH] Fix Windows frame not resizing with screen Fixes https://github.com/dotnet/maui/issues/13552 --- .../Handlers/Windows/FrameRenderer.cs | 42 +++++++------------ src/Controls/src/Core/Frame.cs | 20 ++++----- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/src/Controls/src/Core/Compatibility/Handlers/Windows/FrameRenderer.cs b/src/Controls/src/Core/Compatibility/Handlers/Windows/FrameRenderer.cs index 0cf2df0be7ef..fd1743881497 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/Windows/FrameRenderer.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/Windows/FrameRenderer.cs @@ -13,8 +13,6 @@ namespace Microsoft.Maui.Controls.Handlers.Compatibility { public class FrameRenderer : ViewRenderer { - const int FrameBorderThickness = 1; - public static IPropertyMapper Mapper = new PropertyMapper(VisualElementRendererMapper) { @@ -94,35 +92,15 @@ void UpdatePadding() { // We need this so the `Border` control will arrange and have a size Control?.Arrange(new WRect(0, 0, finalSize.Width, finalSize.Height)); - - if (Element is IContentView cv) - { - finalSize = cv.CrossPlatformArrange(new Rect(0, 0, finalSize.Width, finalSize.Height)).ToPlatform(); - } - return new global::Windows.Foundation.Size(Math.Max(0, finalSize.Width), Math.Max(0, finalSize.Height)); } protected override global::Windows.Foundation.Size MeasureOverride(global::Windows.Foundation.Size availableSize) { - if (Element is IContentView cv) - { - // If there's a border specified, include the thickness in our measurements - // multiplied by 2 to account for both sides (left/right or top/bot) - var borderThickness = (Element.BorderColor.IsNotDefault() ? FrameBorderThickness : 0) * 2; - - // Measure content but subtract border from available space - var measureContent = cv.CrossPlatformMeasure( - availableSize.Width - borderThickness, - availableSize.Height - borderThickness).ToPlatform(); + Control?.Measure(availableSize); - // Add the border space to the final calculation - measureContent = new Size( - measureContent.Width + borderThickness, - measureContent.Height + borderThickness).ToPlatform(); - - return measureContent; - } + if (Control?.DesiredSize is not null) + return Control.DesiredSize; return MinimumSize().ToPlatform(); } @@ -155,15 +133,25 @@ void PackChild() return; } - Control.Child = Element.Content.ToPlatform(MauiContext); + var view = new ContentPanel + { + CrossPlatformMeasure = ((IContentView)Element).CrossPlatformMeasure, + CrossPlatformArrange = ((IContentView)Element).CrossPlatformArrange + }; + + view.Content = Element.Content.ToPlatform(MauiContext); + Control.Child = view; } void UpdateBorder() { if (Element.BorderColor.IsNotDefault()) { + var borderWidth = Element is IBorderElement be ? be.BorderWidth : 1; + borderWidth = Math.Max(1, borderWidth); + Control.BorderBrush = Element.BorderColor.ToPlatform(); - Control.BorderThickness = WinUIHelpers.CreateThickness(FrameBorderThickness); + Control.BorderThickness = WinUIHelpers.CreateThickness(borderWidth); } else { diff --git a/src/Controls/src/Core/Frame.cs b/src/Controls/src/Core/Frame.cs index eb1441e34b7b..576f752a3d43 100644 --- a/src/Controls/src/Core/Frame.cs +++ b/src/Controls/src/Core/Frame.cs @@ -55,13 +55,7 @@ public float CornerRadius int IBorderElement.CornerRadius => (int)CornerRadius; - // TODO fix iOS/WinUI to work the same as Android -#if ANDROID double IBorderElement.BorderWidth => 1; -#else - // not currently used by frame - double IBorderElement.BorderWidth => -1d; -#endif int IBorderElement.CornerRadiusDefaultValue => (int)CornerRadiusProperty.DefaultValue; @@ -92,23 +86,23 @@ void IBorderElement.OnBorderColorPropertyChanged(Color oldValue, Color newValue) // TODO fix iOS/WinUI to work the same as Android // once this has been fixed on iOS/WinUI we should centralize // this code and Border into LayoutExtensions - // WinUI being fixed as part of - // https://github.com/dotnet/maui/issues/13552 -#if ANDROID Size IContentView.CrossPlatformArrange(Graphics.Rect bounds) { - bounds = bounds.Inset(((IBorderElement)this).BorderWidth); +#if !WINDOWS + bounds = bounds.Inset(((IBorderElement)this).BorderWidth); // Windows' implementation would cause an incorrect double-counting of the inset +#endif this.ArrangeContent(bounds); return bounds.Size; } Size IContentView.CrossPlatformMeasure(double widthConstraint, double heightConstraint) { - var inset = Padding + ((IBorderElement)this).BorderWidth; + var inset = Padding; +#if !WINDOWS + inset += ((IBorderElement)this).BorderWidth; // Windows' implementation would cause an incorrect double-counting of the inset +#endif return this.MeasureContent(inset, widthConstraint, heightConstraint); } -#endif - } internal static class FrameExtensions