forked from CommunityToolkit/Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualPanel.cs
More file actions
174 lines (150 loc) · 6.05 KB
/
Copy pathEqualPanel.cs
File metadata and controls
174 lines (150 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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.
using Microsoft.UI.Xaml.Controls;
using System.Data;
namespace CommunityToolkit.WinUI.Controls;
/// <summary>
/// A panel that arranges its children in equal columns.
/// </summary>
public partial class EqualPanel : Panel
{
private double _maxItemWidth = 0;
private double _maxItemHeight = 0;
private int _visibleItemsCount = 0;
/// <summary>
/// Identifies the Spacing dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="Spacing"/> dependency property.</returns>
public static readonly DependencyProperty SpacingProperty = DependencyProperty.Register(
nameof(Spacing),
typeof(double),
typeof(EqualPanel),
new PropertyMetadata(default(double), OnPropertyChanged));
/// <summary>
/// Backing <see cref="DependencyProperty"/> for the <see cref="Orientation"/> property.
/// </summary>
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
nameof(Orientation),
typeof(Orientation),
typeof(EqualPanel),
new PropertyMetadata(default(Orientation), OnPropertyChanged));
/// <summary>
/// Gets or sets the spacing between items.
/// </summary>
public double Spacing
{
get => (double)GetValue(SpacingProperty);
set => SetValue(SpacingProperty, value);
}
/// <summary>
/// Gets or sets the panel orientation.
/// </summary>
public Orientation Orientation
{
get => (Orientation)GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
/// <summary>
/// Creates a new instance of the <see cref="EqualPanel"/> class.
/// </summary>
public EqualPanel()
{
RegisterPropertyChangedCallback(HorizontalAlignmentProperty, OnAlignmentChanged);
}
/// <inheritdoc/>
protected override Size MeasureOverride(Size availableSize)
{
_maxItemWidth = 0;
_maxItemHeight = 0;
var elements = Children.Where(static e => e.Visibility == Visibility.Visible);
_visibleItemsCount = elements.Count();
foreach (var child in elements)
{
child.Measure(availableSize);
_maxItemWidth = Math.Max(_maxItemWidth, child.DesiredSize.Width);
_maxItemHeight = Math.Max(_maxItemHeight, child.DesiredSize.Height);
}
if (_visibleItemsCount > 0)
{
bool stretch = Orientation switch
{
Orientation.Horizontal => HorizontalAlignment is HorizontalAlignment.Stretch && !double.IsInfinity(availableSize.Width),
Orientation.Vertical or _ => VerticalAlignment is VerticalAlignment.Stretch && !double.IsInfinity(availableSize.Height),
};
// Define XY coords
double xSize = 0, ySize = 0;
// Define UV coords for orientation agnostic XY manipulation
ref double uSize = ref SelectAxis(Orientation, ref xSize, ref ySize, true);
ref double vSize = ref SelectAxis(Orientation, ref xSize, ref ySize, false);
ref double maxItemU = ref SelectAxis(Orientation, ref _maxItemWidth, ref _maxItemHeight, true);
ref double maxItemV = ref SelectAxis(Orientation, ref _maxItemWidth, ref _maxItemHeight, false);
double availableU = Orientation is Orientation.Horizontal ? availableSize.Width : availableSize.Height;
if (stretch)
{
// Adjust maxItemU to form equal rows/columns by available U space (adjust for spacing)
double totalU = availableU - (Spacing * (_visibleItemsCount - 1));
maxItemU = totalU / _visibleItemsCount;
// Set uSize/vSize for XY result construction
uSize = availableU;
vSize = maxItemV;
}
else
{
uSize = (maxItemU * _visibleItemsCount) + (Spacing * (_visibleItemsCount - 1));
vSize = maxItemV;
}
return new Size(xSize, ySize);
}
else
{
return new Size(0, 0);
}
}
/// <inheritdoc/>
protected override Size ArrangeOverride(Size finalSize)
{
// Define X and Y
double x = 0;
double y = 0;
// Define UV axis
ref double u = ref x;
ref double maxItemU = ref _maxItemWidth;
double finalSizeU = finalSize.Width;
if (Orientation is Orientation.Vertical)
{
u = ref y;
maxItemU = ref _maxItemHeight;
finalSizeU = finalSize.Height;
}
// Check if there's more (little) width available - if so, set max item width to the maximum possible as we have an almost perfect height.
if (finalSizeU > _visibleItemsCount * maxItemU + (Spacing * (_visibleItemsCount - 1)))
{
maxItemU = (finalSizeU - (Spacing * (_visibleItemsCount - 1))) / _visibleItemsCount;
}
var elements = Children.Where(static e => e.Visibility == Visibility.Visible);
foreach (var child in elements)
{
// NOTE: The arrange method is still in X/Y coordinate system
child.Arrange(new Rect(x, y, _maxItemWidth, _maxItemHeight));
u += maxItemU + Spacing;
}
return finalSize;
}
private void OnAlignmentChanged(DependencyObject sender, DependencyProperty dp)
{
InvalidateMeasure();
}
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var panel = (EqualPanel)d;
panel.InvalidateMeasure();
}
private static ref double SelectAxis(Orientation orientation, ref double x, ref double y, bool u)
{
if ((orientation is Orientation.Horizontal && u) || (orientation is Orientation.Vertical && !u))
return ref x;
else
return ref y;
}
}