Skip to content

Commit 36efeba

Browse files
committed
Changed ItemsJustification to an enum
1 parent 6af1a89 commit 36efeba

File tree

9 files changed

+240
-108
lines changed

9 files changed

+240
-108
lines changed
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
2-
<Page x:Class="WrapPanel2Experiment.Samples.WrapPanel2BasicSample"
3-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
6-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7-
xmlns:local="using:WrapPanel2Experiment.Samples"
8-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9-
mc:Ignorable="d">
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
2+
<Page
3+
x:Class="WrapPanel2Experiment.Samples.WrapPanel2BasicSample"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
7+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8+
xmlns:local="using:WrapPanel2Experiment.Samples"
9+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
10+
mc:Ignorable="d">
1011

1112
<Page.Resources>
1213
<Style TargetType="Border">
@@ -20,32 +21,30 @@
2021
</Style>
2122
</Page.Resources>
2223

23-
<Grid MaxHeight="600"
24-
Margin="16">
24+
<Grid MaxHeight="600" Margin="16">
2525
<Grid.RowDefinitions>
2626
<RowDefinition Height="auto" />
2727
<RowDefinition />
2828
</Grid.RowDefinitions>
2929

30-
<StackPanel Margin="8"
31-
Orientation="Horizontal"
32-
Spacing="8">
33-
<Button Click="AddItemClick"
34-
Content="Add Item" />
35-
<Button Click="Add5ItemsClick"
36-
Content="Add 5 Items" />
37-
<Button Click="ClearItemsClick"
38-
Content="Clear" />
30+
<StackPanel
31+
Margin="8"
32+
Orientation="Horizontal"
33+
Spacing="8">
34+
<Button Click="AddItemClick" Content="Add Item" />
35+
<Button Click="Add5ItemsClick" Content="Add 5 Items" />
36+
<Button Click="ClearItemsClick" Content="Clear" />
3937
</StackPanel>
4038

41-
<controls:WrapPanel2 x:Name="WrapPanel"
42-
Grid.Row="1"
43-
HorizontalAlignment="{x:Bind LayoutHorizontalAlignment, Mode=OneWay}"
44-
VerticalAlignment="{x:Bind LayoutVerticalAlignment, Mode=OneWay}"
45-
ItemJustification="{x:Bind ItemJustification, Mode=OneWay}"
46-
ItemSpacing="{x:Bind ItemSpacing, Mode=OneWay}"
47-
ItemsStretch="{x:Bind local:WrapPanel2BasicSample.ConvertStringToItemsStretch(LayoutItemsStretch), Mode=OneWay}"
48-
LineSpacing="{x:Bind LineSpacing, Mode=OneWay}"
49-
Orientation="{x:Bind local:WrapPanel2BasicSample.ConvertStringToOrientation(LayoutOrientation), Mode=OneWay}" />
39+
<controls:WrapPanel2
40+
x:Name="WrapPanel"
41+
Grid.Row="1"
42+
HorizontalAlignment="{x:Bind LayoutHorizontalAlignment, Mode=OneWay}"
43+
VerticalAlignment="{x:Bind LayoutVerticalAlignment, Mode=OneWay}"
44+
ItemSpacing="{x:Bind ItemSpacing, Mode=OneWay}"
45+
ItemsJustification="{x:Bind local:WrapPanel2BasicSample.ConvertStringToItemsJustification(LayoutItemsJustification), Mode=OneWay}"
46+
ItemsStretch="{x:Bind local:WrapPanel2BasicSample.ConvertStringToItemsStretch(LayoutItemsStretch), Mode=OneWay}"
47+
LineSpacing="{x:Bind LineSpacing, Mode=OneWay}"
48+
Orientation="{x:Bind local:WrapPanel2BasicSample.ConvertStringToOrientation(LayoutOrientation), Mode=OneWay}" />
5049
</Grid>
5150
</Page>

components/WrapPanel2/samples/WrapPanel2BasicSample.xaml.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace WrapPanel2Experiment.Samples;
1414
[ToolkitSampleMultiChoiceOption("LayoutVerticalAlignment", "Top", "Center", "Bottom", "Stretch", Title = "Vertical Alignment")]
1515
[ToolkitSampleNumericOption("ItemSpacing", 8, 0, 16, Title = "Item Spacing")]
1616
[ToolkitSampleNumericOption("LineSpacing", 2, 0, 16, Title = "Line Spacing")]
17-
[ToolkitSampleBoolOption("ItemJustification", false, Title = "Item Justification")]
17+
[ToolkitSampleMultiChoiceOption("LayoutItemsJustification", "Automatic", "Start", "Center", "End", "SpaceAround", "SpaceBetween", "SpaceEvenly", Title = "Items Justification")]
1818
[ToolkitSampleMultiChoiceOption("LayoutItemsStretch", "None", "First", "Last", "Equal", "Proportional", Title = "Items Stretch")]
1919

2020
[ToolkitSample(id: nameof(WrapPanel2BasicSample), $"Basic demo of the {nameof(WrapPanel2)} with auto-sized items.", description: $"A sample showing every property of the {nameof(WrapPanel2)} panel.")]
@@ -53,6 +53,19 @@ public WrapPanel2BasicSample()
5353
_ => throw new System.NotImplementedException(),
5454
};
5555

56+
// TODO: See https://github.com/CommunityToolkit/Labs-Windows/issues/149
57+
public static WrapPanelItemsJustification ConvertStringToItemsJustification(string itemsJustification) => itemsJustification switch
58+
{
59+
"Automatic" => WrapPanelItemsJustification.Automatic,
60+
"Start" => WrapPanelItemsJustification.Start,
61+
"Center" => WrapPanelItemsJustification.Center,
62+
"End" => WrapPanelItemsJustification.End,
63+
"SpaceAround" => WrapPanelItemsJustification.SpaceAround,
64+
"SpaceBetween" => WrapPanelItemsJustification.SpaceBetween,
65+
"SpaceEvenly" => WrapPanelItemsJustification.SpaceEvenly,
66+
_ => throw new System.NotImplementedException(),
67+
};
68+
5669
// TODO: See https://github.com/CommunityToolkit/Labs-Windows/issues/149
5770
public static WrapPanelItemsStretch ConvertStringToItemsStretch(string stretchMethod) => stretchMethod switch
5871
{

components/WrapPanel2/samples/WrapPanel2MegaSample.xaml

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
2-
<Page x:Class="WrapPanel2Experiment.Samples.WrapPanel2MegaSample"
3-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
6-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7-
xmlns:local="using:WrapPanel2Experiment.Samples"
8-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9-
mc:Ignorable="d">
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
2+
<Page
3+
x:Class="WrapPanel2Experiment.Samples.WrapPanel2MegaSample"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
7+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8+
xmlns:local="using:WrapPanel2Experiment.Samples"
9+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
10+
mc:Ignorable="d">
1011

1112
<Page.Resources>
1213
<Style TargetType="Border">
@@ -20,62 +21,50 @@
2021
</Style>
2122
</Page.Resources>
2223

23-
<Grid MaxHeight="600"
24-
Margin="16">
25-
<controls:WrapPanel2 HorizontalAlignment="{x:Bind LayoutHorizontalAlignment, Mode=OneWay}"
26-
VerticalAlignment="{x:Bind LayoutVerticalAlignment, Mode=OneWay}"
27-
ItemJustification="{x:Bind ItemJustification, Mode=OneWay}"
28-
ItemSpacing="{x:Bind ItemSpacing, Mode=OneWay}"
29-
ItemsStretch="{x:Bind local:WrapPanel2MegaSample.ConvertStringToItemsStretch(LayoutItemsStretch), Mode=OneWay}"
30-
LineSpacing="{x:Bind LineSpacing, Mode=OneWay}"
31-
Orientation="{x:Bind local:WrapPanel2MegaSample.ConvertStringToOrientation(LayoutOrientation), Mode=OneWay}">
24+
<Grid MaxHeight="600" Margin="16">
25+
<controls:WrapPanel2
26+
HorizontalAlignment="{x:Bind LayoutHorizontalAlignment, Mode=OneWay}"
27+
VerticalAlignment="{x:Bind LayoutVerticalAlignment, Mode=OneWay}"
28+
ItemSpacing="{x:Bind ItemSpacing, Mode=OneWay}"
29+
ItemsJustification="{x:Bind local:WrapPanel2MegaSample.ConvertStringToItemsJustification(LayoutItemsJustification), Mode=OneWay}"
30+
ItemsStretch="{x:Bind local:WrapPanel2MegaSample.ConvertStringToItemsStretch(LayoutItemsStretch), Mode=OneWay}"
31+
LineSpacing="{x:Bind LineSpacing, Mode=OneWay}"
32+
Orientation="{x:Bind local:WrapPanel2MegaSample.ConvertStringToOrientation(LayoutOrientation), Mode=OneWay}">
3233
<Border controls:WrapPanel2.LayoutLength="2*">
33-
<TextBlock HorizontalAlignment="Center"
34-
Text="2*" />
34+
<TextBlock HorizontalAlignment="Center" Text="2*" />
3535
</Border>
3636
<Border controls:WrapPanel2.LayoutLength="Auto">
37-
<TextBlock HorizontalAlignment="Center"
38-
Text="Auto" />
37+
<TextBlock HorizontalAlignment="Center" Text="Auto" />
3938
</Border>
4039
<Border controls:WrapPanel2.LayoutLength="400">
41-
<TextBlock HorizontalAlignment="Center"
42-
Text="400px" />
40+
<TextBlock HorizontalAlignment="Center" Text="400px" />
4341
</Border>
4442
<Border controls:WrapPanel2.LayoutLength="1*">
45-
<TextBlock HorizontalAlignment="Center"
46-
Text="1*" />
43+
<TextBlock HorizontalAlignment="Center" Text="1*" />
4744
</Border>
4845
<Border controls:WrapPanel2.LayoutLength="200">
49-
<TextBlock HorizontalAlignment="Center"
50-
Text="200px" />
46+
<TextBlock HorizontalAlignment="Center" Text="200px" />
5147
</Border>
5248
<Border controls:WrapPanel2.LayoutLength="300">
53-
<TextBlock HorizontalAlignment="Center"
54-
Text="300px" />
49+
<TextBlock HorizontalAlignment="Center" Text="300px" />
5550
</Border>
5651
<Border controls:WrapPanel2.LayoutLength="600">
57-
<TextBlock HorizontalAlignment="Center"
58-
Text="600px" />
52+
<TextBlock HorizontalAlignment="Center" Text="600px" />
5953
</Border>
6054
<Border controls:WrapPanel2.LayoutLength="2*">
61-
<TextBlock HorizontalAlignment="Center"
62-
Text="2*" />
55+
<TextBlock HorizontalAlignment="Center" Text="2*" />
6356
</Border>
6457
<Border controls:WrapPanel2.LayoutLength="1.5*">
65-
<TextBlock HorizontalAlignment="Center"
66-
Text="1.5*" />
58+
<TextBlock HorizontalAlignment="Center" Text="1.5*" />
6759
</Border>
6860
<Border controls:WrapPanel2.LayoutLength="Auto">
69-
<TextBlock HorizontalAlignment="Center"
70-
Text="Auto with longer text" />
61+
<TextBlock HorizontalAlignment="Center" Text="Auto with longer text" />
7162
</Border>
7263
<Border controls:WrapPanel2.LayoutLength="400">
73-
<TextBlock HorizontalAlignment="Center"
74-
Text="400px" />
64+
<TextBlock HorizontalAlignment="Center" Text="400px" />
7565
</Border>
7666
<Border controls:WrapPanel2.LayoutLength="1*">
77-
<TextBlock HorizontalAlignment="Center"
78-
Text="1*" />
67+
<TextBlock HorizontalAlignment="Center" Text="1*" />
7968
</Border>
8069
</controls:WrapPanel2>
8170
</Grid>

components/WrapPanel2/samples/WrapPanel2MegaSample.xaml.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace WrapPanel2Experiment.Samples;
1414
[ToolkitSampleMultiChoiceOption("LayoutVerticalAlignment", "Top", "Center", "Bottom", "Stretch", Title = "Vertical Alignment")]
1515
[ToolkitSampleNumericOption("ItemSpacing", 8, 0, 16, Title = "Item Spacing")]
1616
[ToolkitSampleNumericOption("LineSpacing", 2, 0, 16, Title = "Line Spacing")]
17-
[ToolkitSampleBoolOption("ItemJustification", false, Title = "Item Justification")]
17+
[ToolkitSampleMultiChoiceOption("LayoutItemsJustification", "Automatic", "Start", "Center", "End", "SpaceAround", "SpaceBetween", "SpaceEvenly", Title = "Items Justification")]
1818
[ToolkitSampleMultiChoiceOption("LayoutItemsStretch", "None", "First", "Last", "Equal", "Proportional", Title = "Items Stretch")]
1919

2020
[ToolkitSample(id: nameof(WrapPanel2MegaSample), "Demo of all WrapPanel2 feature", description: $"A sample showing every property of the {nameof(WrapPanel2)} panel.")]
@@ -53,6 +53,19 @@ public WrapPanel2MegaSample()
5353
_ => throw new System.NotImplementedException(),
5454
};
5555

56+
// TODO: See https://github.com/CommunityToolkit/Labs-Windows/issues/149
57+
public static WrapPanelItemsJustification ConvertStringToItemsJustification(string itemsJustification) => itemsJustification switch
58+
{
59+
"Automatic" => WrapPanelItemsJustification.Automatic,
60+
"Start" => WrapPanelItemsJustification.Start,
61+
"Center" => WrapPanelItemsJustification.Center,
62+
"End" => WrapPanelItemsJustification.End,
63+
"SpaceAround" => WrapPanelItemsJustification.SpaceAround,
64+
"SpaceBetween" => WrapPanelItemsJustification.SpaceBetween,
65+
"SpaceEvenly" => WrapPanelItemsJustification.SpaceEvenly,
66+
_ => throw new System.NotImplementedException(),
67+
};
68+
5669
// TODO: See https://github.com/CommunityToolkit/Labs-Windows/issues/149
5770
public static WrapPanelItemsStretch ConvertStringToItemsStretch(string stretchMethod) => stretchMethod switch
5871
{

components/WrapPanel2/src/WrapPanel2.Properties.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public partial class WrapPanel2
4444
new PropertyMetadata(default(double), OnPropertyChanged));
4545

4646
/// <summary>
47-
/// Backing <see cref="DependencyProperty"/> for the <see cref="ItemJustification"/> property.
47+
/// Backing <see cref="DependencyProperty"/> for the <see cref="ItemsJustification"/> property.
4848
/// </summary>
49-
public static readonly DependencyProperty ItemJustificationProperty = DependencyProperty.Register(
50-
nameof(ItemJustification),
51-
typeof(bool),
49+
public static readonly DependencyProperty ItemsJustificationProperty = DependencyProperty.Register(
50+
nameof(ItemsJustification),
51+
typeof(WrapPanelItemsJustification),
5252
typeof(WrapPanel2),
5353
new PropertyMetadata(default(bool), OnPropertyChanged));
5454

@@ -74,7 +74,7 @@ public Orientation Orientation
7474
/// Gets or sets the spacing between items.
7575
/// </summary>
7676
/// <remarks>
77-
/// When <see cref="ItemJustification"/> is enabled and <see cref="ItemsStretch"/> is <see cref="WrapPanelItemsStretch.None"/>,
77+
/// When <see cref="ItemsJustification"/> is in a spacing mode and <see cref="ItemsStretch"/> is <see cref="WrapPanelItemsStretch.None"/>,
7878
/// this may instead be used as the minimum space between items, while the exact spacing is adjusted to ensure the items span from margin to margin.
7979
/// </remarks>
8080
public double ItemSpacing
@@ -98,14 +98,14 @@ public double LineSpacing
9898
/// <remarks>
9999
/// This will not apply on lines without star-sized items unless a <see cref="ItemsStretch"/> behavior is selected.
100100
/// </remarks>
101-
public bool ItemJustification
101+
public WrapPanelItemsJustification ItemsJustification
102102
{
103-
get => (bool)GetValue(ItemJustificationProperty);
104-
set => SetValue(ItemJustificationProperty, value);
103+
get => (WrapPanelItemsJustification)GetValue(ItemsJustificationProperty);
104+
set => SetValue(ItemsJustificationProperty, value);
105105
}
106106

107107
/// <summary>
108-
/// Gets or sets the method used to fill rows without a star-sized item when <see cref="ItemJustification"/> is enabled.
108+
/// Gets or sets the method used to fill rows without a star-sized item when <see cref="ItemsJustification"/> is in a spacing mode.
109109
/// </summary>
110110
public WrapPanelItemsStretch ItemsStretch
111111
{

components/WrapPanel2/src/WrapPanel2.Structs.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,26 @@ public RowSpec(GridLength layout, UVCoord desiredSize)
7474
/// </summary>
7575
public int ItemsCount { get; private set; }
7676

77-
public bool TryAdd(RowSpec addend, double spacing, double maxSize, bool equalStretching)
77+
/// <remarks>
78+
/// New size is only set if the size changes.
79+
/// Otherwise it will be 0.
80+
/// </remarks>
81+
public bool TryAdd(RowSpec addend, double spacing, double maxSize, WrapPanelItemsStretch stretching, WrapPanelItemsJustification justification)
7882
{
7983
// Check if adding the new spec would exceed the maximum size
8084
var sum = this + addend;
81-
if (sum.Measure(spacing, equalStretching) > maxSize)
85+
if (sum.Measure(spacing, stretching, justification) > maxSize)
8286
return false;
8387

8488
// Update the current spec to include the new spec
8589
this = sum;
8690
return true;
8791
}
8892

89-
public readonly double Measure(double spacing, bool equalStretching)
93+
public readonly double Measure(double spacing, WrapPanelItemsStretch stretching, WrapPanelItemsJustification justification)
9094
{
91-
var totalSpacing = (ItemsCount - 1) * spacing;
95+
var totalSpacing = GetTotalSpacing(ItemsCount, spacing, justification);
96+
var equalStretching = stretching is WrapPanelItemsStretch.Equal && IsSpacingJustified(justification);
9297

9398
// Handle equal-sized items child stretching.
9499
// Without this check, children might become scrunched in the arrange

0 commit comments

Comments
 (0)