From d2f45881868d456c291c1504651f3b2f0e9e67c8 Mon Sep 17 00:00:00 2001 From: Poker Date: Tue, 27 Feb 2024 19:54:37 +0800 Subject: [PATCH 1/6] Enhance: DockPanel spacing --- .../src/DockPanel/DockPanel.Properties.cs | 50 +++++++++- .../Primitives/src/DockPanel/DockPanel.cs | 92 ++++++++++++------- 2 files changed, 104 insertions(+), 38 deletions(-) diff --git a/components/Primitives/src/DockPanel/DockPanel.Properties.cs b/components/Primitives/src/DockPanel/DockPanel.Properties.cs index 4b6e22ae..172e18e2 100644 --- a/components/Primitives/src/DockPanel/DockPanel.Properties.cs +++ b/components/Primitives/src/DockPanel/DockPanel.Properties.cs @@ -13,7 +13,7 @@ public partial class DockPanel /// Gets or sets a value that indicates the position of a child element within a parent . /// public static readonly DependencyProperty DockProperty = DependencyProperty.RegisterAttached( - "Dock", + nameof(Dock), typeof(Dock), typeof(FrameworkElement), new PropertyMetadata(Dock.Left, DockChanged)); @@ -53,8 +53,8 @@ public static readonly DependencyProperty LastChildFillProperty /// public bool LastChildFill { - get { return (bool)GetValue(LastChildFillProperty); } - set { SetValue(LastChildFillProperty, value); } + get => (bool)GetValue(LastChildFillProperty); + set => SetValue(LastChildFillProperty, value); } /// @@ -77,7 +77,47 @@ public bool LastChildFill /// public Thickness Padding { - get { return (Thickness)GetValue(PaddingProperty); } - set { SetValue(PaddingProperty, value); } + get => return (Thickness)GetValue(PaddingProperty); + set => SetValue(PaddingProperty, value); + } + + /// + /// Identifies the HorizontalSpacing dependency property. + /// + /// The identifier for the dependency property. + public static readonly DependencyProperty HorizontalSpacingProperty + = DependencyProperty.Register( + nameof(HorizontalSpacing), + typeof(double), + typeof(DockPanel), + new PropertyMetadata(0d, OnPropertyChanged)); + + /// + /// Gets or sets the horizontal distance between the child objects. + /// + public double HorizontalSpacing + { + get => (double)GetValue(HorizontalSpacingProperty); + set => SetValue(HorizontalSpacingProperty, value); + } + + /// + /// Identifies the VerticalSpacing dependency property. + /// + /// The identifier for the dependency property. + public static readonly DependencyProperty VerticalSpacingProperty + = DependencyProperty.Register( + nameof(VerticalSpacing), + typeof(double), + typeof(DockPanel), + new PropertyMetadata(default(double), OnPropertyChanged)); + + /// + /// Gets or sets the vertical distance between the child objects. + /// + public double VerticalSpacing + { + get => (double)GetValue(VerticalSpacingProperty); + set => SetValue(VerticalSpacingProperty, value); } } diff --git a/components/Primitives/src/DockPanel/DockPanel.cs b/components/Primitives/src/DockPanel/DockPanel.cs index b8082d56..0b3c84ba 100644 --- a/components/Primitives/src/DockPanel/DockPanel.cs +++ b/components/Primitives/src/DockPanel/DockPanel.cs @@ -17,13 +17,7 @@ private static void DockChanged(DependencyObject sender, DependencyPropertyChang dockPanel?.InvalidateArrange(); } - private static void LastChildFillChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) - { - var dockPanel = (DockPanel)sender; - dockPanel.InvalidateArrange(); - } - - private static void OnPaddingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) + private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var dockPanel = (DockPanel)sender; dockPanel.InvalidateMeasure(); @@ -32,15 +26,17 @@ private static void OnPaddingChanged(DependencyObject sender, DependencyProperty /// protected override Size ArrangeOverride(Size finalSize) { - if (Children.Count == 0) - { + if (Children.Count is 0) return finalSize; - } - var currentBounds = new Rect(Padding.Left, Padding.Top, finalSize.Width - Padding.Right, finalSize.Height - Padding.Bottom); + var currentBounds = new Rect( + Padding.Left, + Padding.Top, + GetPositiveOrZero(finalSize.Width - Padding.Left - Padding.Right), + GetPositiveOrZero(finalSize.Height - Padding.Top - Padding.Bottom)); var childrenCount = LastChildFill ? Children.Count - 1 : Children.Count; - for (var index = 0; index < childrenCount; index++) + for (var index = 0; index < childrenCount; ++index) { var child = Children[index]; var dock = (Dock)child.GetValue(DockProperty); @@ -49,30 +45,36 @@ protected override Size ArrangeOverride(Size finalSize) { case Dock.Left: - width = Math.Min(child.DesiredSize.Width, GetPositiveOrZero(currentBounds.Width - currentBounds.X)); - child.Arrange(new Rect(currentBounds.X, currentBounds.Y, width, GetPositiveOrZero(currentBounds.Height - currentBounds.Y))); + width = Math.Min(child.DesiredSize.Width, currentBounds.Width); + child.Arrange(new Rect(currentBounds.X, currentBounds.Y, width, currentBounds.Height)); + width += HorizontalSpacing; currentBounds.X += width; + currentBounds.Width = GetPositiveOrZero(currentBounds.Width - width); break; case Dock.Top: - height = Math.Min(child.DesiredSize.Height, GetPositiveOrZero(currentBounds.Height - currentBounds.Y)); - child.Arrange(new Rect(currentBounds.X, currentBounds.Y, GetPositiveOrZero(currentBounds.Width - currentBounds.X), height)); + height = Math.Min(child.DesiredSize.Height, currentBounds.Height); + child.Arrange(new Rect(currentBounds.X, currentBounds.Y, currentBounds.Width, height)); + height += VerticalSpacing; currentBounds.Y += height; + currentBounds.Height = GetPositiveOrZero(currentBounds.Height - height); break; case Dock.Right: - width = Math.Min(child.DesiredSize.Width, GetPositiveOrZero(currentBounds.Width - currentBounds.X)); - child.Arrange(new Rect(GetPositiveOrZero(currentBounds.Width - width), currentBounds.Y, width, GetPositiveOrZero(currentBounds.Height - currentBounds.Y))); - currentBounds.Width -= (currentBounds.Width - width) > 0 ? width : 0; + width = Math.Min(child.DesiredSize.Width, currentBounds.Width); + child.Arrange(new Rect(currentBounds.X + currentBounds.Width - width, currentBounds.Y, width, currentBounds.Height)); + width += HorizontalSpacing; + currentBounds.Width = GetPositiveOrZero(currentBounds.Width - width); break; case Dock.Bottom: - height = Math.Min(child.DesiredSize.Height, GetPositiveOrZero(currentBounds.Height - currentBounds.Y)); - child.Arrange(new Rect(currentBounds.X, GetPositiveOrZero(currentBounds.Height - height), GetPositiveOrZero(currentBounds.Width - currentBounds.X), height)); - currentBounds.Height -= (currentBounds.Height - height) > 0 ? height : 0; + height = Math.Min(child.DesiredSize.Height, currentBounds.Height); + child.Arrange(new Rect(currentBounds.X, currentBounds.Y + currentBounds.Height - height, currentBounds.Width, height)); + height += VerticalSpacing; + currentBounds.Height = GetPositiveOrZero(currentBounds.Height - height); break; } @@ -80,11 +82,8 @@ protected override Size ArrangeOverride(Size finalSize) if (LastChildFill) { - var width = GetPositiveOrZero(currentBounds.Width - currentBounds.X); - var height = GetPositiveOrZero(currentBounds.Height - currentBounds.Y); - var child = Children[Children.Count - 1]; - child.Arrange( - new Rect(currentBounds.X, currentBounds.Y, width, height)); + var child = Children[^1]; + child.Arrange(new Rect(currentBounds.X, currentBounds.Y, currentBounds.Width, currentBounds.Height)); } return finalSize; @@ -98,6 +97,11 @@ protected override Size MeasureOverride(Size availableSize) var accumulatedWidth = Padding.Left + Padding.Right; var accumulatedHeight = Padding.Top + Padding.Bottom; + var leftSpacing = false; + var topSpacing = false; + var rightSpacing = false; + var bottomSpacing = false; + foreach (var child in Children) { var childConstraint = new Size( @@ -110,26 +114,48 @@ protected override Size MeasureOverride(Size availableSize) switch ((Dock)child.GetValue(DockProperty)) { case Dock.Left: + if (childConstraint.Width is not 0) + accumulatedWidth += HorizontalSpacing; + leftSpacing = true; + parentHeight = Math.Max(parentHeight, accumulatedHeight + childDesiredSize.Height - VerticalSpacing); + accumulatedWidth += childDesiredSize.Width; + break; + case Dock.Right: - parentHeight = Math.Max(parentHeight, accumulatedHeight + childDesiredSize.Height); + if (childConstraint.Width is not 0) + accumulatedWidth += HorizontalSpacing; + rightSpacing = true; + parentHeight = Math.Max(parentHeight, accumulatedHeight + childDesiredSize.Height - VerticalSpacing); accumulatedWidth += childDesiredSize.Width; break; case Dock.Top: + if (childConstraint.Height is not 0) + accumulatedHeight += VerticalSpacing; + topSpacing = true; + parentWidth = Math.Max(parentWidth, accumulatedWidth + childDesiredSize.Width - HorizontalSpacing); + accumulatedHeight += childDesiredSize.Height; + break; + case Dock.Bottom: - parentWidth = Math.Max(parentWidth, accumulatedWidth + childDesiredSize.Width); + if (childConstraint.Height is not 0) + accumulatedHeight += VerticalSpacing; + bottomSpacing = true; + parentWidth = Math.Max(parentWidth, accumulatedWidth + childDesiredSize.Width - HorizontalSpacing); accumulatedHeight += childDesiredSize.Height; break; } } + if (leftSpacing || rightSpacing) + accumulatedWidth -= HorizontalSpacing; + if (bottomSpacing || topSpacing) + accumulatedHeight -= VerticalSpacing; + parentWidth = Math.Max(parentWidth, accumulatedWidth); parentHeight = Math.Max(parentHeight, accumulatedHeight); return new Size(parentWidth, parentHeight); } - private static double GetPositiveOrZero(double value) - { - return Math.Max(value, 0); - } + private static double GetPositiveOrZero(double value) => Math.Max(value, 0); } From 2c8f15e66c53844768512cabb59f5a2af4d5b878 Mon Sep 17 00:00:00 2001 From: Poker Date: Fri, 1 Mar 2024 02:08:34 +0800 Subject: [PATCH 2/6] Update DockPanel.Properties.cs --- components/Primitives/src/DockPanel/DockPanel.Properties.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/Primitives/src/DockPanel/DockPanel.Properties.cs b/components/Primitives/src/DockPanel/DockPanel.Properties.cs index 172e18e2..338a56fe 100644 --- a/components/Primitives/src/DockPanel/DockPanel.Properties.cs +++ b/components/Primitives/src/DockPanel/DockPanel.Properties.cs @@ -77,7 +77,7 @@ public bool LastChildFill /// public Thickness Padding { - get => return (Thickness)GetValue(PaddingProperty); + get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); } From d0afa4d1b2a51ff4c0f37c42d945bb4fadf4b405 Mon Sep 17 00:00:00 2001 From: Poker Date: Fri, 1 Mar 2024 02:16:54 +0800 Subject: [PATCH 3/6] Update DockPanel.Properties.cs --- components/Primitives/src/DockPanel/DockPanel.Properties.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/Primitives/src/DockPanel/DockPanel.Properties.cs b/components/Primitives/src/DockPanel/DockPanel.Properties.cs index 338a56fe..f7792fe7 100644 --- a/components/Primitives/src/DockPanel/DockPanel.Properties.cs +++ b/components/Primitives/src/DockPanel/DockPanel.Properties.cs @@ -46,7 +46,7 @@ public static readonly DependencyProperty LastChildFillProperty nameof(LastChildFill), typeof(bool), typeof(DockPanel), - new PropertyMetadata(true, LastChildFillChanged)); + new PropertyMetadata(true, OnPropertyChanged)); /// /// Gets or sets a value indicating whether the last child element within a DockPanel stretches to fill the remaining available space. @@ -66,7 +66,7 @@ public bool LastChildFill nameof(Padding), typeof(Thickness), typeof(DockPanel), - new PropertyMetadata(default(Thickness), OnPaddingChanged)); + new PropertyMetadata(default(Thickness), OnPropertyChanged)); /// /// Gets or sets the distance between the border and its child object. From 87407f650d2cbec117c0f15d110394ac0d0b4f29 Mon Sep 17 00:00:00 2001 From: Poker Date: Sat, 2 Mar 2024 02:07:38 +0800 Subject: [PATCH 4/6] Update DockPanel.cs --- components/Primitives/src/DockPanel/DockPanel.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/Primitives/src/DockPanel/DockPanel.cs b/components/Primitives/src/DockPanel/DockPanel.cs index 0b3c84ba..542b95c7 100644 --- a/components/Primitives/src/DockPanel/DockPanel.cs +++ b/components/Primitives/src/DockPanel/DockPanel.cs @@ -82,7 +82,7 @@ protected override Size ArrangeOverride(Size finalSize) if (LastChildFill) { - var child = Children[^1]; + var child = Children[Children.Count - 1]; child.Arrange(new Rect(currentBounds.X, currentBounds.Y, currentBounds.Width, currentBounds.Height)); } From dab0b87ce02c90beeab6c47ace5a9be922892896 Mon Sep 17 00:00:00 2001 From: Arlo Godfrey Date: Fri, 1 Mar 2024 16:44:26 -0600 Subject: [PATCH 5/6] Add HorizontalSpacing and VerticalSpacing to samples --- components/Primitives/samples/DockPanelSample.xaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/Primitives/samples/DockPanelSample.xaml b/components/Primitives/samples/DockPanelSample.xaml index 982151a0..5a758c05 100644 --- a/components/Primitives/samples/DockPanelSample.xaml +++ b/components/Primitives/samples/DockPanelSample.xaml @@ -34,6 +34,8 @@ From 25e81235f2d46a0ddf49ffe93f2e5bcecf408bd8 Mon Sep 17 00:00:00 2001 From: Arlo Godfrey Date: Fri, 1 Mar 2024 16:47:05 -0600 Subject: [PATCH 6/6] Run XAML Styler --- components/Primitives/samples/DockPanelSample.xaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/Primitives/samples/DockPanelSample.xaml b/components/Primitives/samples/DockPanelSample.xaml index 5a758c05..8cecbb93 100644 --- a/components/Primitives/samples/DockPanelSample.xaml +++ b/components/Primitives/samples/DockPanelSample.xaml @@ -1,4 +1,4 @@ - + + HorizontalSpacing="5" + LastChildFill="False" + VerticalSpacing="5">