Skip to content

Commit

Permalink
Rework LayoutFrame for WinUI (#21287)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Mar 19, 2024
1 parent 0b2d387 commit 37da156
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Frame/FrameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public async Task FrameRespectsMinimums()
Assert.True(100 <= layoutFrame.Width);
}

#if !WINDOWS
[Fact]
public async Task FrameDoesNotInterpretConstraintsAsMinimums()
{
Expand Down Expand Up @@ -301,6 +302,7 @@ public async Task FrameIncludesPadding()
Assert.Equal(expected, layoutFrame.Width, 1.0d);
Assert.Equal(expected, layoutFrame.Height, 1.0d);
}
#endif

#if !ANDROID && !IOS && !MACCATALYST
[Fact]
Expand Down Expand Up @@ -429,6 +431,72 @@ public async Task FramesWithinFrames(double widthConstraint, double heightConstr
Assert.True(layoutFrame.Width > minExpectedWidth);
}

#if WINDOWS

class ContentLayoutPanel : WPanel
{
IView _view;
readonly double _widthConstraint;
readonly double _heightConstraint;

public ContentLayoutPanel(IView view, double widthConstraint, double heightConstraint)
{
if (!double.IsPositiveInfinity(widthConstraint))
this.Width = widthConstraint;

if (!double.IsPositiveInfinity(heightConstraint))
this.Height = heightConstraint;

_view = view;
_widthConstraint = widthConstraint;
_heightConstraint = heightConstraint;
var platformView = view.ToPlatform();

// Just in case this view is already parented to a wrapper that's been cycled out
if (platformView.Parent is ContentLayoutPanel clp)
clp.Children.Remove(platformView);

Children.Add(platformView);
}

protected override WSize ArrangeOverride(WSize finalSize) => _view.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height)).ToPlatform();

protected override WSize MeasureOverride(WSize availableSize) => _view.Measure(_widthConstraint, _heightConstraint).ToPlatform();
}

async Task<Rect> LayoutFrame(Layout layout, Frame frame, double widthConstraint, double heightConstraint, Func<Task> additionalTests = null)
{
additionalTests ??= () => Task.CompletedTask;

await InvokeOnMainThreadAsync(async () =>
{
// create platform views
layout.ToHandler(MauiContext);
frame.ToHandler(MauiContext);
await new ContentLayoutPanel(layout, widthConstraint, heightConstraint).AttachAndRun(async () =>
{
await OnFrameSetToNotEmpty(layout);
await OnFrameSetToNotEmpty(frame);
// verify that the PlatformView was measured
var frameControlSize = (frame.Handler as IPlatformViewHandler).PlatformView.GetBoundingBox();
Assert.True(frameControlSize.Width > 0);
Assert.True(frameControlSize.Height > 0);
// if the control sits inside a container make sure that also measured
var containerControlSize = frame.ToPlatform().GetBoundingBox();
Assert.True(containerControlSize.Width > 0);
Assert.True(containerControlSize.Height > 0);
await additionalTests.Invoke();
}, MauiContext);
}
);

return layout.Frame;
}
#else
async Task<Rect> LayoutFrame(Layout layout, Frame frame, double widthConstraint, double heightConstraint, Func<Task> additionalTests = null)
{
additionalTests ??= () => Task.CompletedTask;
Expand All @@ -455,5 +523,6 @@ async Task<Rect> LayoutFrame(Layout layout, Frame frame, double widthConstraint,
return layout.Frame;
});
}
#endif
}
}

0 comments on commit 37da156

Please sign in to comment.