Skip to content
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
86 changes: 86 additions & 0 deletions Nimble.Layout.Tests/TestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,92 @@ public void AnchorRightMargin2()
Assert.AreEqual(new(40, 40, 50, 50), child.Rect);
}

[TestMethod]
public void Padding1()
{
var root = new LayoutItem {
RequestedPadding = new(10),
};

var child = new LayoutItem {
RequestedSize = new(15),
};

root.AddChild(child);
root.Run();

Assert.AreEqual(new(0, 0, 35, 35), root.Rect);
Assert.AreEqual(new(10, 10, 15, 15), child.Rect);
}

[TestMethod]
public void PaddingRow()
{
var root = new LayoutItem {
RequestedPadding = new(10),
Contain = ContainFlags.Row,
};

var child1 = new LayoutItem { RequestedSize = new(15) };
var child2 = new LayoutItem { RequestedSize = new(15) };

root.AddChild(child1);
root.AddChild(child2);
root.Run();

Assert.AreEqual(new(0, 0, 50, 35), root.Rect);
Assert.AreEqual(new(10, 10, 15, 15), child1.Rect);
Assert.AreEqual(new(25, 10, 15, 15), child2.Rect);
}

[TestMethod]
public void PaddingColumn()
{
var root = new LayoutItem {
RequestedPadding = new(10),
Contain = ContainFlags.Column,
};

var child1 = new LayoutItem { RequestedSize = new(15) };
var child2 = new LayoutItem { RequestedSize = new(15) };

root.AddChild(child1);
root.AddChild(child2);
root.Run();

Assert.AreEqual(new(0, 0, 35, 50), root.Rect);
Assert.AreEqual(new(10, 10, 15, 15), child1.Rect);
Assert.AreEqual(new(10, 25, 15, 15), child2.Rect);
}

[TestMethod]
public void PaddingFixedAndFill()
{
var root = new LayoutItem {
RequestedSize = new(50, 60),
RequestedPadding = new(10),
Contain = ContainFlags.Column,
};

var fixedA = new LayoutItem {
RequestedSize = new(50, 15),
};
var fixedB = new LayoutItem {
RequestedSize = new(50, 15),
};
var filler = new LayoutItem {
Behave = BehaveFlags.Fill,
};

root.AddChildren([fixedA, filler, fixedB]);
root.Run();

Assert.AreEqual(new(0, 0, 50, 60), root.Rect);
Assert.AreEqual(new(10, 10, 30, 15), fixedA.Rect);
Assert.AreEqual(new(10, 25, 30, 10), filler.Rect);
Assert.AreEqual(new(10, 35, 30, 15), fixedB.Rect);
}

[TestMethod]
public void IssueUpstream15()
{
Expand Down
26 changes: 15 additions & 11 deletions Nimble.Layout/LayoutItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class LayoutItem
public LayoutItem? NextSibling { get; internal set; }

public LayoutEdges RequestedMargin = new();
public LayoutEdges RequestedPadding = new();
public LayoutVector RequestedSize = new();

public LayoutRect Rect = new();
Expand Down Expand Up @@ -111,6 +112,7 @@ public void Reset(bool withChildren = false)
FirstChild = null;
NextSibling = null;
RequestedMargin = new();
RequestedPadding = new();
RequestedSize = new();
Rect = new();
}
Expand Down Expand Up @@ -268,7 +270,7 @@ private float CalcStackedSize(int dim)
foreach (var child in Children) {
need_size += child.Rect[dim] + child.Rect[wdim] + child.RequestedMargin[wdim];
}
return need_size;
return RequestedPadding.GetDimension(dim) + need_size;
}

private float CalcOverlayedSize(int dim)
Expand All @@ -280,7 +282,7 @@ private float CalcOverlayedSize(int dim)
float child_size = child.Rect[dim] + child.Rect[wdim] + child.RequestedMargin[wdim];
need_size = Math.Max(need_size, child_size);
}
return need_size;
return RequestedPadding.GetDimension(dim) + need_size;
}

private float CalcWrappedStackedSize(int dim)
Expand All @@ -295,7 +297,7 @@ private float CalcWrappedStackedSize(int dim)
}
need_size += child.Rect[dim] + child.Rect[wdim] + child.RequestedMargin[wdim];
}
return Math.Max(need_size2, need_size);
return RequestedPadding.GetDimension(dim) + Math.Max(need_size2, need_size);
}

private float CalcWrappedOverlayedSize(int dim)
Expand All @@ -311,7 +313,7 @@ private float CalcWrappedOverlayedSize(int dim)
float child_size = child.Rect[dim] + child.Rect[wdim] + child.RequestedMargin[wdim];
need_size = Math.Max(need_size, child_size);
}
return need_size2 + need_size;
return RequestedPadding.GetDimension(dim) + need_size2 + need_size;
}

private void Arrange(int dim)
Expand Down Expand Up @@ -339,7 +341,9 @@ private void Arrange(int dim)
if ((Flags & 1) == (uint)dim) {
ArrangeStacked(dim, false);
} else {
ArrangeOverlaySqueezedRange(dim, FirstChild, null, Rect[dim], Rect[2 + dim]);
ArrangeOverlaySqueezedRange(dim, FirstChild, null,
Rect[dim] + RequestedPadding[dim],
Rect[2 + dim] - RequestedPadding.GetDimension(dim));
}
break;

Expand All @@ -358,8 +362,8 @@ private void ArrangeStacked(int dim, bool wrap)
{
int wdim = dim + 2;

float space = Rect[wdim];
float max_x2 = Rect[dim] + space;
float space = Rect[wdim] - RequestedPadding.GetDimension(dim);
float max_x2 = Rect[dim] + space + RequestedPadding[dim];

var startChild = FirstChild;
while (startChild != null) {
Expand Down Expand Up @@ -441,7 +445,7 @@ private void ArrangeStacked(int dim, bool wrap)
}

// distribute width among items
float x = Rect[dim];
float x = Rect[dim] + RequestedPadding[dim];
float x1;
// second pass: distribute and rescale
child = startChild;
Expand Down Expand Up @@ -479,8 +483,8 @@ private void ArrangeStacked(int dim, bool wrap)
private void ArrangeOverlay(int dim)
{
int wdim = dim + 2;
float offset = Rect[dim];
float space = Rect[wdim];
float offset = Rect[dim] + RequestedPadding[dim];
float space = Rect[wdim] - RequestedPadding.GetDimension(dim);

foreach (var child in Children) {
var b_flags = (BehaveFlags)((uint)child.Behave >> dim);
Expand Down Expand Up @@ -509,7 +513,7 @@ private void ArrangeOverlay(int dim)
private float ArrangeWrappedOverlaySqueezed(int dim)
{
int wdim = dim + 2;
float offset = Rect[dim];
float offset = Rect[dim] + RequestedPadding[dim];
float need_size = 0;
var startChild = FirstChild;
foreach (var child in Children) {
Expand Down