Redid layout, so padding is taken care of at the abstract level. This lets it be re-used by all layouts.

This commit is contained in:
James Clancey 2022-01-13 16:54:42 -09:00
Родитель 27c6aaf8e6
Коммит 7a5911aa21
8 изменённых файлов: 126 добавлений и 107 удалений

Просмотреть файл

@ -9,9 +9,61 @@ namespace Comet.Samples
public class ViewLayoutTestCase : View
{
[Body]
View view() => new ScrollView
{
View view() => new ScrollView {
new VStack(){
new VStack
{
new Text(()=> "Recommended")
.Color(Colors.Black)
.FontFamily("Rockolf Bold")
.FontSize(20)
.FontWeight(FontWeight.Bold)
.Margin(new Thickness(0, 6)),
new ScrollView(Orientation.Horizontal) {
new HStack
{
Enumerable.Range(0,10).Select(destination => new ZStack
{
// Destination Background Image
new Image()
.Background(Colors.SkyBlue).FillHorizontal().FillVertical()
.ClipShape(new RoundedRectangle(36)),
new VStack(Comet.HorizontalAlignment.Leading) {
new VStack
{
new Text(() => "$100")
.Color(Colors.White)
.FitHorizontal()
.FontSize(14)
.FontFamily("Rockolf Bold")
.FontSize(14)
.FontWeight(FontWeight.Bold),
}.FitHorizontal().Frame(alignment: Alignment.Trailing)
.Background(Color.FromArgb("#67AEE9"))
.ClipShape(new RoundedRectangle(12))
.Padding(6)
.Margin(12),
new Spacer(),
new Text("Japan Street")
.Color(Colors.White)
.FontFamily("Rockolf Bold")
.FontSize(18)
.FontWeight(FontWeight.Bold)
.Shadow(radius: 6),
new Text("Awesome Sauce")
.Color(Colors.White)
.FontFamily("Rockolf")
.FontSize(14),
}
.Padding(new Thickness(16, 0, 0, 16))
}.Frame(height: 250, width: 200))
}
}
},
new Text("ZSTack Alignment"),
new ZStack
{

Просмотреть файл

@ -46,4 +46,5 @@
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<WindowsPackageType>MSIX</WindowsPackageType>
</PropertyGroup>
<ProjectExtensions><VisualStudio><UserProperties XamarinHotReloadDebuggerTimeoutExceptionCometSingleProjectSampleHideInfoBar="True" /></VisualStudio></ProjectExtensions>
</Project>

Просмотреть файл

@ -4,6 +4,7 @@ using System.Collections.Generic;
using Microsoft.Maui;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Layouts;
using Microsoft.Maui.Primitives;
namespace Comet
{
@ -43,11 +44,7 @@ namespace Comet
{
base.LayoutSubviews(frame);
var padding = this.GetPadding();
var bounds = new Rectangle(
padding.Left,
padding.Right,
frame.Width - padding.HorizontalThickness,
frame.Height - padding.VerticalThickness);
var bounds = new Rectangle(Point.Zero, Frame.Size);//.ApplyPadding(padding);
CrossPlatformArrange(bounds);
}
@ -55,9 +52,51 @@ namespace Comet
{
if (IsMeasureValid)
return MeasuredSize;
MeasuredSize = LayoutManager.Measure(availableSize.Width, availableSize.Height);
var frameConstraints = this.GetFrameConstraints();
var layoutVerticalSizing = ((IView)this).VerticalLayoutAlignment;
var layoutHorizontalSizing = ((IView)this).HorizontalLayoutAlignment;
double widthConstraint = frameConstraints?.Width > 0 ? frameConstraints.Width.Value : availableSize.Width;
double heightConstraint = frameConstraints?.Height > 0 ? frameConstraints.Height.Value : availableSize.Height;
//Lets adjust for padding
var padding = this.GetPadding();
if(!double.IsInfinity(widthConstraint))
widthConstraint -= padding.HorizontalThickness;
if (!double.IsInfinity(heightConstraint))
heightConstraint -= padding.VerticalThickness;
var measured = LayoutManager.Measure(widthConstraint, heightConstraint);
if (frameConstraints?.Height > 0 && frameConstraints?.Width > 0)
{
measured = new Size(frameConstraints.Width.Value, frameConstraints.Height.Value);
}
else
{
if (layoutVerticalSizing == LayoutAlignment.Fill && !double.IsInfinity(heightConstraint))
measured.Height = heightConstraint;
if (layoutHorizontalSizing == LayoutAlignment.Fill && !double.IsInfinity(widthConstraint))
measured.Width = widthConstraint;
if (!double.IsInfinity(measured.Width))
measured.Width += padding.HorizontalThickness;
if (!double.IsInfinity(measured.Height))
measured.Height += padding.VerticalThickness;
}
var margin = this.GetMargin();
if(!double.IsInfinity(measured.Width))
measured.Width += margin.HorizontalThickness;
if (!double.IsInfinity(measured.Height))
measured.Height += margin.VerticalThickness;
IsMeasureValid = true;
return MeasuredSize;
return MeasuredSize = measured;
}
protected override void Dispose(bool disposing)
@ -68,7 +107,9 @@ namespace Comet
public virtual Size CrossPlatformMeasure(double widthConstraint, double heightConstraint) => GetDesiredSize(new Size(widthConstraint,heightConstraint));
public virtual Size CrossPlatformArrange(Rectangle bounds) {
LayoutManager?.ArrangeChildren(bounds);
var padding = this.GetPadding();
var b = bounds.ApplyPadding(padding);
LayoutManager?.ArrangeChildren(b);
return this.MeasuredSize;
}
}

Просмотреть файл

@ -556,6 +556,7 @@ namespace Comet
var fe = (IView)this;
var frameConstraints = this.GetFrameConstraints();
var margins = fe.Margin;
if (frameConstraints?.Height > 0 && frameConstraints?.Width > 0)
return new Size(frameConstraints.Width.Value, frameConstraints.Height.Value);
@ -564,8 +565,9 @@ namespace Comet
ms.Width = fe.Width;
if (fe.Height > 0)
ms.Height = fe.Height;
//TODO: Remove this when we get some LayoutOptions...
//This check ignores MArgin which is bad
ms.Width += margins.HorizontalThickness;
ms.Height += margins.HorizontalThickness;
var hSizing = this.GetHorizontalLayoutAlignment(this.Parent as ContainerView);
var vSizing = this.GetVerticalLayoutAlignment(this.Parent as ContainerView);
if (hSizing == LayoutAlignment.Fill && !double.IsInfinity(availableSize.Width))

Просмотреть файл

@ -106,13 +106,16 @@ namespace Comet
frame.Width -= margin.HorizontalThickness;
frame.Height -= margin.VerticalThickness;
}
if (!view.MeasurementValid)
{
var sizeThatFits = view.Measure(frame.Size.Width, frame.Size.Height);
view.MeasuredSize = sizeThatFits;
view.MeasurementValid = true;
}
var sizeThatFits = view.Measure(frame.Size.Width,frame.Size.Height);
view.MeasuredSize = sizeThatFits;
view.MeasurementValid = true;
var width = sizeThatFits.Width;
var height = sizeThatFits.Height;
var width = view.MeasuredSize.Width - margin.HorizontalThickness;
var height = view.MeasuredSize.Height - margin.VerticalThickness;
var frameConstraints = view.GetFrameConstraints();
@ -164,7 +167,7 @@ namespace Comet
var x = frame.X + ((frame.Width - width) * xFactor);
var y = frame.Y + ((frame.Height - height) * yFactor);
view.Frame = new Rectangle((float)x, (float)y, width, height);
view.Frame = new Rectangle(x, y, width, height);
}
public static T FillHorizontal<T>(this T view, bool cascades = false) where T : View

Просмотреть файл

@ -20,8 +20,7 @@ namespace Comet.Layout
ContainerView layout;
public Size ArrangeChildren(Rectangle rect)
{
var padding = layout.GetPadding();
var layoutRect = rect.ApplyPadding(padding);
var layoutRect = rect;
double spacerWidth = (layoutRect.Width - childrenWidth) / spacerCount;
foreach (var view in layout)
@ -34,7 +33,7 @@ namespace Comet.Layout
var size = view.MeasuredSize;
layoutRect.Width = size.Width;
view.SetFrameFromNativeView(layoutRect);
view.LayoutSubviews(layoutRect);
layoutRect.X = view.Frame.Right + _spacing;
}
return new Size(layoutRect.Left, layoutRect.Bottom);
@ -42,23 +41,9 @@ namespace Comet.Layout
int spacerCount;
double childrenWidth;
public Size Measure(double wConstraint, double hConstraint)
public Size Measure(double widthConstraint, double heightConstraint)
{
//Lets adjust for Frame settings
var frameConstraints = layout.GetFrameConstraints();
var layoutVerticalSizing = ((IView)layout).VerticalLayoutAlignment;
var layoutHorizontalSizing = ((IView)layout).HorizontalLayoutAlignment;
double widthConstraint = frameConstraints?.Width > 0 ? frameConstraints.Width.Value : wConstraint;
double heightConstraint = frameConstraints?.Height > 0 ? frameConstraints.Height.Value : hConstraint;
//Lets adjust for padding
var padding = layout.GetPadding();
widthConstraint -= padding.HorizontalThickness;
heightConstraint -= padding.VerticalThickness;
var index = 0;
double width = 0;
double height = 0;
@ -109,16 +94,6 @@ namespace Comet.Layout
if (spacerCount > 0)
width = widthConstraint;
if (layoutVerticalSizing == LayoutAlignment.Fill && !double.IsInfinity(heightConstraint))
height = heightConstraint;
if (layoutHorizontalSizing == LayoutAlignment.Fill && !double.IsInfinity(widthConstraint))
width = widthConstraint;
width += padding.VerticalThickness;
height += padding.HorizontalThickness;
if (frameConstraints?.Height > 0 && frameConstraints?.Width > 0)
return new Size(frameConstraints.Width.Value, frameConstraints.Height.Value);
return new Size(width, height);
}

Просмотреть файл

@ -18,8 +18,7 @@ public class VStackLayoutManager : Microsoft.Maui.Layouts.ILayoutManager
ContainerView layout;
public Size ArrangeChildren(Rectangle rect)
{
var padding = layout.GetPadding();
var layoutRect = rect.ApplyPadding(padding);
var layoutRect = rect;
double spacerHeight = (layoutRect.Height - childrenHeight) / spacerCount;
foreach (var view in layout)
{
@ -32,7 +31,7 @@ public class VStackLayoutManager : Microsoft.Maui.Layouts.ILayoutManager
var size = view.MeasuredSize;
layoutRect.Height = size.Height;
view.SetFrameFromNativeView(layoutRect);
view.LayoutSubviews(layoutRect);
layoutRect.Y = view.Frame.Bottom + _spacing;
}
@ -41,22 +40,8 @@ public class VStackLayoutManager : Microsoft.Maui.Layouts.ILayoutManager
int spacerCount;
double childrenHeight;
public Size Measure(double wConstraint, double hConstraint)
public Size Measure(double widthConstraint, double heightConstraint)
{
//Lets adjust for Frame settings
var frameConstraints = layout.GetFrameConstraints();
var layoutVerticalSizing = ((IView)layout).VerticalLayoutAlignment;
var layoutHorizontalSizing = ((IView)layout).HorizontalLayoutAlignment;
double widthConstraint = frameConstraints?.Width > 0 ? frameConstraints.Width.Value : wConstraint;
double heightConstraint = frameConstraints?.Height > 0 ? frameConstraints.Height.Value : hConstraint;
//Lets adjust for padding
var padding = layout.GetPadding();
widthConstraint -= padding.HorizontalThickness;
heightConstraint -= padding.VerticalThickness;
var index = 0;
double width = 0;
@ -111,21 +96,6 @@ public class VStackLayoutManager : Microsoft.Maui.Layouts.ILayoutManager
if (spacerCount > 0)
height = heightConstraint;
var layoutMargin = layout.GetMargin();
if (layoutHorizontalSizing == LayoutAlignment.Fill && !double.IsInfinity(widthConstraint))
width = widthConstraint;
if (layoutVerticalSizing == LayoutAlignment.Fill && !double.IsInfinity(heightConstraint))
height = heightConstraint - layoutMargin.VerticalThickness;
width += padding.VerticalThickness;
height += padding.HorizontalThickness;
if (frameConstraints?.Height > 0 && frameConstraints?.Width > 0)
{
return new Size(frameConstraints.Width.Value, frameConstraints.Height.Value);
}
return new Size(width, height);
}
}

Просмотреть файл

@ -13,21 +13,7 @@ namespace Comet.Layout
ILayout layout;
public Size Measure(double wConstraint, double hConstraint) {
var v = layout as View;
var frameConstraints = v?.GetFrameConstraints();
var layoutVerticalSizing = layout.VerticalLayoutAlignment;
var layoutHorizontalSizing = layout.HorizontalLayoutAlignment;
double widthConstraint = frameConstraints?.Width > 0 ? frameConstraints.Width.Value : wConstraint;
double heightConstraint = frameConstraints?.Height > 0 ? frameConstraints.Height.Value : hConstraint;
var padding = layout.Padding;
widthConstraint -= padding.HorizontalThickness;
heightConstraint -= padding.VerticalThickness;
public Size Measure(double widthConstraint, double heightConstraint) {
Size measuredSize = new ();
foreach(var c in layout)
@ -36,28 +22,17 @@ namespace Comet.Layout
measuredSize.Height = Math.Max(measuredSize.Height, s.Height);
measuredSize.Width = Math.Max(measuredSize.Width, s.Width);
};
measuredSize.Height += padding.VerticalThickness;
measuredSize.Width += padding.HorizontalThickness;
if (layoutVerticalSizing == LayoutAlignment.Fill && !double.IsInfinity(hConstraint))
measuredSize.Height = hConstraint;
if (layoutHorizontalSizing == LayoutAlignment.Fill && !double.IsInfinity(wConstraint))
measuredSize.Width = wConstraint;
if (frameConstraints?.Height > 0 && frameConstraints?.Width > 0)
return new Size(frameConstraints.Width.Value, frameConstraints.Height.Value);
return measuredSize;
}
public Size ArrangeChildren(Rectangle bounds)
{
var padding = layout.Padding;
var b = bounds.ApplyPadding(padding);
var b = bounds;
foreach (var v in layout)
{
if (v is View cv)
cv.SetFrameFromNativeView(b);
cv.LayoutSubviews(b);
else
v.Arrange(b);
}