Skip to content

Commit 14e5889

Browse files
committed
Refine ToolBar spacing and collapsed layout
1 parent ee5bd83 commit 14e5889

3 files changed

Lines changed: 42 additions & 14 deletions

File tree

src/Wpf.Ui.Violeta/Controls/ToolBar/ToolBar.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,24 +236,24 @@ private static void OnOverflowPanelWrapWidthChanged(DependencyObject d, Dependen
236236

237237
#endregion OverflowPanelWrapWidth
238238

239-
#region ItemSpacing
239+
#region Spacing
240240

241-
public static readonly DependencyProperty ItemSpacingProperty = DependencyProperty.Register(
242-
nameof(ItemSpacing),
241+
public static readonly DependencyProperty SpacingProperty = DependencyProperty.Register(
242+
nameof(Spacing),
243243
typeof(double),
244244
typeof(ToolBar),
245-
new FrameworkPropertyMetadata(6d, FrameworkPropertyMetadataOptions.AffectsMeasure, OnItemSpacingChanged));
245+
new FrameworkPropertyMetadata(6d, FrameworkPropertyMetadataOptions.AffectsMeasure, OnSpacingChanged));
246246

247247
/// <summary>
248248
/// Gap between adjacent items in the primary bar and the overflow flyout.
249249
/// </summary>
250-
public double ItemSpacing
250+
public double Spacing
251251
{
252-
get => (double)GetValue(ItemSpacingProperty);
253-
set => SetValue(ItemSpacingProperty, value);
252+
get => (double)GetValue(SpacingProperty);
253+
set => SetValue(SpacingProperty, value);
254254
}
255255

256-
private static void OnItemSpacingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
256+
private static void OnSpacingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
257257
{
258258
if (d is ToolBar toolBar)
259259
{
@@ -262,7 +262,7 @@ private static void OnItemSpacingChanged(DependencyObject d, DependencyPropertyC
262262
}
263263
}
264264

265-
#endregion ItemSpacing
265+
#endregion Spacing
266266

267267
#region OverflowButtonStyle
268268

src/Wpf.Ui.Violeta/Controls/ToolBar/ToolBarOverflowPanel.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected override Size MeasureOverride(Size constraint)
5454
{
5555
SyncOverflowChildren();
5656

57-
double spacing = Math.Max(0, ToolBar?.ItemSpacing ?? 0);
57+
double spacing = Math.Max(0, ToolBar?.Spacing ?? 0);
5858
double wrapWidth = WrapWidth;
5959
int maxItemsPerRow = MaxItemsPerRow;
6060
bool hasWidthLimit = wrapWidth > 0;
@@ -74,6 +74,11 @@ protected override Size MeasureOverride(Size constraint)
7474
foreach (UIElement child in Children)
7575
{
7676
child.Measure(infinite);
77+
if (child.Visibility == Visibility.Collapsed)
78+
{
79+
continue;
80+
}
81+
7782
Size size = child.DesiredSize;
7883

7984
double gap = rowItemCount > 0 ? spacing : 0;
@@ -106,7 +111,7 @@ protected override Size MeasureOverride(Size constraint)
106111

107112
protected override Size ArrangeOverride(Size finalSize)
108113
{
109-
double spacing = Math.Max(0, ToolBar?.ItemSpacing ?? 0);
114+
double spacing = Math.Max(0, ToolBar?.Spacing ?? 0);
110115
double wrapWidth = WrapWidth;
111116
int maxItemsPerRow = MaxItemsPerRow;
112117
bool hasWidthLimit = wrapWidth > 0;
@@ -120,6 +125,12 @@ protected override Size ArrangeOverride(Size finalSize)
120125

121126
foreach (UIElement child in Children)
122127
{
128+
if (child.Visibility == Visibility.Collapsed)
129+
{
130+
child.Arrange(new Rect());
131+
continue;
132+
}
133+
123134
Size size = child.DesiredSize;
124135
bool exceedsWidth = hasWidthLimit && rowItemCount > 0 && x + spacing + size.Width > limit;
125136
bool exceedsItemCount = hasItemLimit && rowItemCount >= maxItemsPerRow;

src/Wpf.Ui.Violeta/Controls/ToolBar/ToolBarPanel.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected override Size MeasureOverride(Size constraint)
3838
return new Size(0, 0);
3939
}
4040

41-
double spacing = Math.Max(0, toolBar.ItemSpacing);
41+
double spacing = Math.Max(0, toolBar.Spacing);
4242
var infinite = new Size(double.PositiveInfinity, constraint.Height);
4343

4444
double maxHeight = 0;
@@ -133,7 +133,7 @@ private static OverflowMeasureResult MeasureOverflowPass(
133133

134134
for (int i = 0; i < count; i++)
135135
{
136-
if (children[i] is null)
136+
if (children[i] is not { } child || child.Visibility == Visibility.Collapsed)
137137
{
138138
continue;
139139
}
@@ -203,7 +203,7 @@ protected override Size ArrangeOverride(Size finalSize)
203203
return ArrangeAsStack(finalSize, spacing: 0);
204204
}
205205

206-
double spacing = Math.Max(0, toolBar.ItemSpacing);
206+
double spacing = Math.Max(0, toolBar.Spacing);
207207
var generator = toolBar.ItemContainerGenerator;
208208
double x = 0;
209209
double height = finalSize.Height;
@@ -226,6 +226,12 @@ protected override Size ArrangeOverride(Size finalSize)
226226
continue;
227227
}
228228

229+
if (child.Visibility == Visibility.Collapsed)
230+
{
231+
child.Arrange(new Rect());
232+
continue;
233+
}
234+
229235
if (!first)
230236
{
231237
x += spacing;
@@ -320,6 +326,11 @@ private Size MeasureAsStack(Size constraint, double spacing)
320326
foreach (UIElement child in Children)
321327
{
322328
child.Measure(infinite);
329+
if (child.Visibility == Visibility.Collapsed)
330+
{
331+
continue;
332+
}
333+
323334
if (index > 0)
324335
{
325336
width += spacing;
@@ -339,6 +350,12 @@ private Size ArrangeAsStack(Size finalSize, double spacing)
339350
bool first = true;
340351
foreach (UIElement child in Children)
341352
{
353+
if (child.Visibility == Visibility.Collapsed)
354+
{
355+
child.Arrange(new Rect());
356+
continue;
357+
}
358+
342359
if (!first)
343360
{
344361
x += spacing;

0 commit comments

Comments
 (0)