Skip to content

Commit 4977f87

Browse files
committed
Added OverflowBehaviors for StretchPanel
1 parent 379769c commit 4977f87

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

components/StretchPanel/samples/StretchPanelBasicSample.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
HorizontalAlignment="{x:Bind LayoutHorizontalAlignment, Mode=OneWay}"
2828
VerticalAlignment="{x:Bind LayoutVerticalAlignment, Mode=OneWay}"
2929
FixedRowLengths="{x:Bind FixedRowLengths, Mode=OneWay}"
30-
ForcedStretchMethod="{x:Bind local:StretchPanelBasicSample.ConvertStringToForcedStretchMethod(LayoutForcedStretchMethod), Mode=OneWay}">
30+
ForcedStretchMethod="{x:Bind local:StretchPanelBasicSample.ConvertStringToForcedStretchMethod(LayoutForcedStretchMethod), Mode=OneWay}"
31+
OverflowBehavior="{x:Bind local:StretchPanelBasicSample.ConvertStringToOverflowBehavior(LayoutOverflowBehavior), Mode=OneWay}">
3132
<Border controls:StretchPanel.LayoutLength="2*" >
3233
<TextBlock Text="2*" HorizontalAlignment="Center"/>
3334
</Border>

components/StretchPanel/samples/StretchPanelBasicSample.xaml.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace StretchPanelExperiment.Samples;
1616
[ToolkitSampleNumericOption("VerticalSpacing", 2, 0, 16, Title = "Vertical Spacing")]
1717
[ToolkitSampleBoolOption("FixedRowLengths", false, Title = "Fixed Row Lengths")]
1818
[ToolkitSampleMultiChoiceOption("LayoutForcedStretchMethod", "None", "First", "Last", "Equal", "Proportional", Title = "Forced Stretch Method")]
19+
[ToolkitSampleMultiChoiceOption("LayoutOverflowBehavior", "Wrap", "Drop", Title = "Overflow Behavior")]
1920

2021
[ToolkitSample(id: nameof(StretchPanelBasicSample), "Custom control", description: $"A sample for showing how to create and use a {nameof(StretchPanel)} custom control.")]
2122
public sealed partial class StretchPanelBasicSample : Page
@@ -63,4 +64,12 @@ public StretchPanelBasicSample()
6364
"Proportional" => ForcedStretchMethod.Proportional,
6465
_ => throw new System.NotImplementedException(),
6566
};
67+
68+
// TODO: See https://github.com/CommunityToolkit/Labs-Windows/issues/149
69+
public static OverflowBehavior ConvertStringToOverflowBehavior(string overflowBehavior) => overflowBehavior switch
70+
{
71+
"Wrap" => OverflowBehavior.Wrap,
72+
"Drop" => OverflowBehavior.Drop,
73+
_ => throw new System.NotImplementedException(),
74+
};
6675
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace CommunityToolkit.WinUI.Controls;
6+
7+
/// <summary>
8+
/// Describes the behavior of items that exceed the available space in the panel.
9+
/// </summary>
10+
public enum OverflowBehavior
11+
{
12+
/// <summary>
13+
/// When an item exceeds the available space, it will be moved to a new row or column.
14+
/// </summary>
15+
Wrap,
16+
17+
/// <summary>
18+
/// Items which do not fit within the available space will be removed from the layout.
19+
/// </summary>
20+
Drop,
21+
}

components/StretchPanel/src/StretchPanel.Properties.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ public partial class StretchPanel
5959
nameof(ForcedStretchMethod),
6060
typeof(ForcedStretchMethod),
6161
typeof(StretchPanel),
62-
new PropertyMetadata(default(bool), OnPropertyChanged));
62+
new PropertyMetadata(default(ForcedStretchMethod), OnPropertyChanged));
63+
64+
/// <summary>
65+
/// Backing <see cref="DependencyProperty"/> for the <see cref="OverflowBehavior"/> property.
66+
/// </summary>
67+
public static readonly DependencyProperty OverflowBehaviorProperty = DependencyProperty.Register(
68+
nameof(OverflowBehavior),
69+
typeof(OverflowBehavior),
70+
typeof(StretchPanel),
71+
new PropertyMetadata(default(OverflowBehavior), OnPropertyChanged));
6372

6473
/// <summary>
6574
/// Gets or sets the panel orientation.
@@ -106,6 +115,15 @@ public ForcedStretchMethod ForcedStretchMethod
106115
set => SetValue(ForcedStretchMethodProperty, value);
107116
}
108117

118+
/// <summary>
119+
/// Gets or sets how the panel handles content overflowing the available space.
120+
/// </summary>
121+
public OverflowBehavior OverflowBehavior
122+
{
123+
get => (OverflowBehavior)GetValue(OverflowBehaviorProperty);
124+
set => SetValue(OverflowBehaviorProperty, value);
125+
}
126+
109127
/// <summary>
110128
/// Gets the <see cref="LayoutLengthProperty"/> of an item in the <see cref="StretchPanel"/>.
111129
/// </summary>

components/StretchPanel/src/StretchPanel.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ protected override Size MeasureOverride(Size availableSize)
4343
var spec = new RowSpec(layoutLength, uvDesiredSize);
4444
if (!currentRowSpec.TryAdd(spec, uvSpacing.U, uvAvailableSize.U))
4545
{
46+
// If the overflow behavior is drop, just end the row here.
47+
if (OverflowBehavior is OverflowBehavior.Drop)
48+
break;
49+
4650
// Could not add to current row/column
4751
// Start a new row/column
4852
_rowSpecs.Add(currentRowSpec);
@@ -62,6 +66,9 @@ protected override Size MeasureOverride(Size availableSize)
6266
V = _rowSpecs.Sum(static rs => rs.MaxOffAxisSize) + (uvSpacing.V * (_rowSpecs.Count - 1))
6367
};
6468

69+
// Clamp to available size and return
70+
uvSize.U = Math.Min(uvSize.U, uvAvailableSize.U);
71+
uvSize.V = Math.Min(uvSize.V, uvAvailableSize.V);
6572
return uvSize.Size;
6673
}
6774

@@ -89,6 +96,13 @@ protected override Size ArrangeOverride(Size finalSize)
8996
ArrangeRow(ref pos, row, uvFinalSize, uvSpacing, childQueue);
9097
}
9198

99+
// "Arrange" remaning children by rendering them with zero size
100+
while (childQueue.TryDequeue(out var child))
101+
{
102+
// Arrange with zero size
103+
child.Arrange(new Rect(0, 0, 0, 0));
104+
}
105+
92106
return finalSize;
93107
}
94108

0 commit comments

Comments
 (0)