Skip to content

Commit efbaf96

Browse files
Update DockPanel.cs (#416)
Co-authored-by: Arlo <arlo.godfrey@outlook.com>
1 parent 5a995b9 commit efbaf96

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

components/Primitives/src/DockPanel/DockPanel.cs

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,64 +96,83 @@ protected override Size MeasureOverride(Size availableSize)
9696
var parentHeight = 0.0;
9797
var accumulatedWidth = Padding.Left + Padding.Right;
9898
var accumulatedHeight = Padding.Top + Padding.Bottom;
99-
99+
100100
var leftSpacing = false;
101101
var topSpacing = false;
102102
var rightSpacing = false;
103103
var bottomSpacing = false;
104-
105-
foreach (var child in Children)
104+
var childrenCount = LastChildFill ? Children.Count - 1 : Children.Count;
105+
106+
for (var index = 0; index < childrenCount; ++index)
106107
{
108+
var child = Children[index];
107109
var childConstraint = new Size(
108110
GetPositiveOrZero(availableSize.Width - accumulatedWidth),
109111
GetPositiveOrZero(availableSize.Height - accumulatedHeight));
110-
112+
111113
child.Measure(childConstraint);
112114
var childDesiredSize = child.DesiredSize;
113-
115+
114116
switch ((Dock)child.GetValue(DockProperty))
115117
{
116118
case Dock.Left:
119+
leftSpacing = true;
120+
parentHeight = Math.Max(parentHeight, accumulatedHeight + childDesiredSize.Height);
117121
if (childConstraint.Width is not 0)
118122
accumulatedWidth += HorizontalSpacing;
119-
leftSpacing = true;
120-
parentHeight = Math.Max(parentHeight, accumulatedHeight + childDesiredSize.Height - VerticalSpacing);
121123
accumulatedWidth += childDesiredSize.Width;
122124
break;
123-
125+
124126
case Dock.Right:
127+
rightSpacing = true;
128+
parentHeight = Math.Max(parentHeight, accumulatedHeight + childDesiredSize.Height);
125129
if (childConstraint.Width is not 0)
126130
accumulatedWidth += HorizontalSpacing;
127-
rightSpacing = true;
128-
parentHeight = Math.Max(parentHeight, accumulatedHeight + childDesiredSize.Height - VerticalSpacing);
129131
accumulatedWidth += childDesiredSize.Width;
130132
break;
131-
133+
132134
case Dock.Top:
135+
topSpacing = true;
136+
parentWidth = Math.Max(parentWidth, accumulatedWidth + childDesiredSize.Width);
133137
if (childConstraint.Height is not 0)
134138
accumulatedHeight += VerticalSpacing;
135-
topSpacing = true;
136-
parentWidth = Math.Max(parentWidth, accumulatedWidth + childDesiredSize.Width - HorizontalSpacing);
137139
accumulatedHeight += childDesiredSize.Height;
138140
break;
139-
141+
140142
case Dock.Bottom:
143+
bottomSpacing = true;
144+
parentWidth = Math.Max(parentWidth, accumulatedWidth + childDesiredSize.Width);
141145
if (childConstraint.Height is not 0)
142146
accumulatedHeight += VerticalSpacing;
143-
bottomSpacing = true;
144-
parentWidth = Math.Max(parentWidth, accumulatedWidth + childDesiredSize.Width - HorizontalSpacing);
145147
accumulatedHeight += childDesiredSize.Height;
146148
break;
147149
}
148150
}
149-
150-
if (leftSpacing || rightSpacing)
151-
accumulatedWidth -= HorizontalSpacing;
152-
if (bottomSpacing || topSpacing)
153-
accumulatedHeight -= VerticalSpacing;
154-
155-
parentWidth = Math.Max(parentWidth, accumulatedWidth);
156-
parentHeight = Math.Max(parentHeight, accumulatedHeight);
151+
152+
if (LastChildFill)
153+
{
154+
var child = Children[Children.Count - 1];
155+
var childConstraint = new Size(
156+
GetPositiveOrZero(availableSize.Width - accumulatedWidth),
157+
GetPositiveOrZero(availableSize.Height - accumulatedHeight));
158+
159+
child.Measure(childConstraint);
160+
var childDesiredSize = child.DesiredSize;
161+
parentHeight = Math.Max(parentHeight, accumulatedHeight + childDesiredSize.Height);
162+
parentWidth = Math.Max(parentWidth, accumulatedWidth + childDesiredSize.Width);
163+
accumulatedHeight += childDesiredSize.Height;
164+
accumulatedWidth += childDesiredSize.Width;
165+
}
166+
else
167+
{
168+
if (leftSpacing || rightSpacing)
169+
accumulatedWidth -= HorizontalSpacing;
170+
if (bottomSpacing || topSpacing)
171+
accumulatedHeight -= VerticalSpacing;
172+
}
173+
174+
parentWidth = Math.Min(availableSize.Width, Math.Max(parentWidth, accumulatedWidth));
175+
parentHeight = Math.Min(availableSize.Height, Math.Max(parentHeight, accumulatedHeight));
157176
return new Size(parentWidth, parentHeight);
158177
}
159178

0 commit comments

Comments
 (0)