Skip to content

Commit

Permalink
Fix Windows frame not resizing with screen
Browse files Browse the repository at this point in the history
Fixes #13552
  • Loading branch information
jknaudt21 committed May 8, 2023
1 parent bd1c013 commit 0a57189
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Microsoft.Maui.Controls.Handlers.Compatibility
{
public class FrameRenderer : ViewRenderer<Frame, WBorder>
{
const int FrameBorderThickness = 1;

public static IPropertyMapper<Frame, FrameRenderer> Mapper
= new PropertyMapper<Frame, FrameRenderer>(VisualElementRendererMapper)
{
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
{
Expand Down
20 changes: 7 additions & 13 deletions src/Controls/src/Core/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0a57189

Please sign in to comment.