From 17c2b8473b05be9312caee4bbe2275f3f6b45232 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Mon, 22 Jul 2024 23:30:59 +0800 Subject: [PATCH] chore: add avalonia border demo. --- .../HandyControlDemo_Avalonia.csproj | 4 + .../UserControl/Main/MainContent.axaml | 2 +- .../UserControl/Styles/BorderDemoCtl.axaml | 89 +++++++++++++ .../UserControl/Styles/BorderDemoCtl.axaml.cs | 9 ++ .../Controls/Panel/UniformSpacingPanel.cs | 80 +++++------- .../Themes/Basic/Effects.axaml | 5 + .../Themes/Styles/Border.axaml | 119 +++++++++++++++--- .../Controls/Panel/UniformSpacingPanel.cs | 15 +-- 8 files changed, 249 insertions(+), 74 deletions(-) create mode 100644 src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/BorderDemoCtl.axaml create mode 100644 src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/BorderDemoCtl.axaml.cs diff --git a/src/Avalonia/HandyControlDemo_Avalonia/HandyControlDemo_Avalonia.csproj b/src/Avalonia/HandyControlDemo_Avalonia/HandyControlDemo_Avalonia.csproj index eeae15a11..15d065568 100644 --- a/src/Avalonia/HandyControlDemo_Avalonia/HandyControlDemo_Avalonia.csproj +++ b/src/Avalonia/HandyControlDemo_Avalonia/HandyControlDemo_Avalonia.csproj @@ -61,6 +61,10 @@ BoderDemoCtl.axaml Code + + BorderDemoCtl.axaml + Code + diff --git a/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Main/MainContent.axaml b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Main/MainContent.axaml index f53871819..7f72e59aa 100644 --- a/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Main/MainContent.axaml +++ b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Main/MainContent.axaml @@ -16,7 +16,7 @@ Grid.Row="0" CornerRadius="10,10,0,0" Background="{DynamicResource TitleBrush}" - Classes="clip"> + Theme="{StaticResource BorderClip}"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/BorderDemoCtl.axaml.cs b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/BorderDemoCtl.axaml.cs new file mode 100644 index 000000000..e5ce5d374 --- /dev/null +++ b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/BorderDemoCtl.axaml.cs @@ -0,0 +1,9 @@ +namespace HandyControlDemo.UserControl; + +public partial class BorderDemoCtl : Avalonia.Controls.UserControl +{ + public BorderDemoCtl() + { + InitializeComponent(); + } +} diff --git a/src/Avalonia/HandyControl_Avalonia/Controls/Panel/UniformSpacingPanel.cs b/src/Avalonia/HandyControl_Avalonia/Controls/Panel/UniformSpacingPanel.cs index 68d9a7b42..ddc533cb1 100644 --- a/src/Avalonia/HandyControl_Avalonia/Controls/Panel/UniformSpacingPanel.cs +++ b/src/Avalonia/HandyControl_Avalonia/Controls/Panel/UniformSpacingPanel.cs @@ -10,7 +10,8 @@ namespace HandyControl.Controls; public class UniformSpacingPanel : Panel { public static readonly StyledProperty OrientationProperty = - StackPanel.OrientationProperty.AddOwner(); + StackPanel.OrientationProperty.AddOwner(new StyledPropertyMetadata( + defaultValue: Orientation.Horizontal)); public static readonly StyledProperty ChildWrappingProperty = AvaloniaProperty.Register(nameof(ChildWrapping)); @@ -43,8 +44,6 @@ public class UniformSpacingPanel : Panel AvaloniaProperty.Register(nameof(ItemVerticalAlignment), defaultValue: VerticalAlignment.Stretch); - private Orientation _orientation = Orientation.Horizontal; - static UniformSpacingPanel() { AffectsMeasure(OrientationProperty); @@ -56,16 +55,6 @@ static UniformSpacingPanel() AffectsMeasure(ItemHeightProperty); AffectsMeasure(ItemHorizontalAlignmentProperty); AffectsMeasure(ItemVerticalAlignmentProperty); - - OrientationProperty.Changed.AddClassHandler(OnOrientationChanged); - } - - private static void OnOrientationChanged(UniformSpacingPanel self, AvaloniaPropertyChangedEventArgs args) - { - if (args.NewValue is Orientation orientation) - { - self._orientation = orientation; - } } private static double CoerceLength(AvaloniaObject _, double length) => length < 0 ? 0 : length; @@ -126,9 +115,10 @@ public VerticalAlignment? ItemVerticalAlignment protected override Size MeasureOverride(Size availableSize) { - var curLineSize = new PanelUvSize(_orientation); - var panelSize = new PanelUvSize(_orientation); - var uvConstraint = new PanelUvSize(_orientation, availableSize); + var orientation = Orientation; + var curLineSize = new PanelUvSize(orientation); + var panelSize = new PanelUvSize(orientation); + var uvConstraint = new PanelUvSize(orientation, availableSize); double itemWidth = ItemWidth; double itemHeight = ItemHeight; bool itemWidthSet = !double.IsNaN(itemWidth); @@ -148,15 +138,15 @@ protected override Size MeasureOverride(Size availableSize) if (childWrapping == VisualWrapping.NoWrap) { - var layoutSlotSize = new PanelUvSize(_orientation, availableSize); + var layoutSlotSize = new PanelUvSize(orientation, availableSize); - if (_orientation == Orientation.Horizontal) + if (orientation == Orientation.Horizontal) { - layoutSlotSize.U = double.PositiveInfinity; + layoutSlotSize.V = double.PositiveInfinity; } else { - layoutSlotSize.V = double.PositiveInfinity; + layoutSlotSize.U = double.PositiveInfinity; } for (int i = 0, count = Children.Count; i < count; ++i) @@ -176,7 +166,7 @@ protected override Size MeasureOverride(Size availableSize) child.Measure(new Size(layoutSlotSize.Width, layoutSlotSize.Height)); var sz = new PanelUvSize( - _orientation, + orientation, itemWidthSet ? itemWidth : child.DesiredSize.Width, itemHeightSet ? itemHeight : child.DesiredSize.Height); @@ -205,30 +195,23 @@ protected override Size MeasureOverride(Size availableSize) child.Measure(childConstraint); var sz = new PanelUvSize( - _orientation, + orientation, itemWidthSet ? itemWidth : child.DesiredSize.Width, itemHeightSet ? itemHeight : child.DesiredSize.Height); - if (MathHelper.GreaterThan(curLineSize.U + sz.U + spacingSize.U, uvConstraint.U)) + if (!isFirst && MathHelper.GreaterThan(curLineSize.U + sz.U + spacingSize.U, uvConstraint.U)) { panelSize.U = Math.Max(curLineSize.U, panelSize.U); panelSize.V += curLineSize.V + spacingSize.V; curLineSize = sz; - - if (MathHelper.GreaterThan(sz.U, uvConstraint.U)) - { - panelSize.U = Math.Max(sz.U, panelSize.U); - panelSize.V += sz.V + spacingSize.V; - curLineSize = new PanelUvSize(_orientation); - } } else { curLineSize.U += isFirst ? sz.U : sz.U + spacingSize.U; curLineSize.V = Math.Max(sz.V, curLineSize.V); - - isFirst = false; } + + isFirst = false; } } @@ -240,16 +223,17 @@ protected override Size MeasureOverride(Size availableSize) protected override Size ArrangeOverride(Size finalSize) { + var orientation = Orientation; int firstInLine = 0; double itemWidth = ItemWidth; double itemHeight = ItemHeight; double accumulatedV = 0; - double itemU = _orientation == Orientation.Horizontal ? itemWidth : itemHeight; - var curLineSize = new PanelUvSize(_orientation); - var uvFinalSize = new PanelUvSize(_orientation, finalSize); + double itemU = orientation == Orientation.Horizontal ? itemWidth : itemHeight; + var curLineSize = new PanelUvSize(orientation); + var uvFinalSize = new PanelUvSize(orientation, finalSize); bool itemWidthSet = !double.IsNaN(itemWidth); bool itemHeightSet = !double.IsNaN(itemHeight); - bool useItemU = _orientation == Orientation.Horizontal ? itemWidthSet : itemHeightSet; + bool useItemU = orientation == Orientation.Horizontal ? itemWidthSet : itemHeightSet; var childWrapping = ChildWrapping; var spacingSize = GetSpacingSize(); @@ -266,13 +250,14 @@ protected override Size ArrangeOverride(Size finalSize) var child = Children[i]; var sz = new PanelUvSize( - _orientation, + orientation, itemWidthSet ? itemWidth : child.DesiredSize.Width, itemHeightSet ? itemHeight : child.DesiredSize.Height); - if (MathHelper.GreaterThan(curLineSize.U + (isFirst ? sz.U : sz.U + spacingSize.U), uvFinalSize.U)) + if (!isFirst && MathHelper.GreaterThan(curLineSize.U + sz.U + spacingSize.U, uvFinalSize.U)) { - ArrangeWrapLine(accumulatedV, curLineSize.V, firstInLine, i, useItemU, itemU, spacingSize.U); + ArrangeWrapLine(orientation, accumulatedV, curLineSize.V, firstInLine, i, useItemU, itemU, + spacingSize.U); accumulatedV += curLineSize.V + spacingSize.V; curLineSize = sz; @@ -290,7 +275,7 @@ protected override Size ArrangeOverride(Size finalSize) if (firstInLine < Children.Count) { - ArrangeWrapLine(accumulatedV, curLineSize.V, firstInLine, Children.Count, useItemU, itemU, + ArrangeWrapLine(orientation, accumulatedV, curLineSize.V, firstInLine, Children.Count, useItemU, itemU, spacingSize.U); } } @@ -300,11 +285,12 @@ protected override Size ArrangeOverride(Size finalSize) private PanelUvSize GetSpacingSize() { + var orientation = Orientation; double spacing = Spacing; if (!double.IsNaN(spacing)) { - return new PanelUvSize(_orientation, spacing, spacing); + return new PanelUvSize(orientation, spacing, spacing); } double horizontalSpacing = HorizontalSpacing; @@ -319,19 +305,20 @@ private PanelUvSize GetSpacingSize() verticalSpacing = 0; } - return new PanelUvSize(_orientation, horizontalSpacing, verticalSpacing); + return new PanelUvSize(orientation, horizontalSpacing, verticalSpacing); } private void ArrangeLine(double lineV, bool useItemU, double itemU, double spacing) { + var orientation = Orientation; double u = 0; - bool isHorizontal = _orientation == Orientation.Horizontal; + bool isHorizontal = orientation == Orientation.Horizontal; // ReSharper disable once ForCanBeConvertedToForeach for (int i = 0; i < Children.Count; i++) { Control child = Children[i]; - var childSize = new PanelUvSize(_orientation, child.DesiredSize); + var childSize = new PanelUvSize(orientation, child.DesiredSize); double layoutSlotU = useItemU ? itemU : childSize.U; child.Arrange(isHorizontal ? new Rect(u, 0, layoutSlotU, lineV) : new Rect(0, u, lineV, layoutSlotU)); @@ -344,6 +331,7 @@ private void ArrangeLine(double lineV, bool useItemU, double itemU, double spaci } private void ArrangeWrapLine( + Orientation orientation, double v, double lineV, int start, @@ -353,13 +341,13 @@ private void ArrangeWrapLine( double spacing) { double u = 0; - bool isHorizontal = _orientation == Orientation.Horizontal; + bool isHorizontal = orientation == Orientation.Horizontal; for (int i = start; i < end; i++) { var child = Children[i]; - var childSize = new PanelUvSize(_orientation, child.DesiredSize); + var childSize = new PanelUvSize(orientation, child.DesiredSize); double layoutSlotU = useItemU ? itemU : childSize.U; child.Arrange(isHorizontal ? new Rect(u, v, layoutSlotU, lineV) : new Rect(v, u, lineV, layoutSlotU)); diff --git a/src/Avalonia/HandyControl_Avalonia/Themes/Basic/Effects.axaml b/src/Avalonia/HandyControl_Avalonia/Themes/Basic/Effects.axaml index c82d9c6d6..9af2a1116 100644 --- a/src/Avalonia/HandyControl_Avalonia/Themes/Basic/Effects.axaml +++ b/src/Avalonia/HandyControl_Avalonia/Themes/Basic/Effects.axaml @@ -5,26 +5,31 @@ diff --git a/src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml b/src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml index 191205cc4..d1d80ddd2 100644 --- a/src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml +++ b/src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml @@ -1,20 +1,107 @@  - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Shared/HandyControl_Shared/Controls/Panel/UniformSpacingPanel.cs b/src/Shared/HandyControl_Shared/Controls/Panel/UniformSpacingPanel.cs index 2df57c6fb..ea7d729fb 100644 --- a/src/Shared/HandyControl_Shared/Controls/Panel/UniformSpacingPanel.cs +++ b/src/Shared/HandyControl_Shared/Controls/Panel/UniformSpacingPanel.cs @@ -263,26 +263,19 @@ protected override Size MeasureOverride(Size constraint) itemWidthSet ? itemWidth : child.DesiredSize.Width, itemHeightSet ? itemHeight : child.DesiredSize.Height); - if (MathHelper.GreaterThan(curLineSize.U + sz.U + spacingSize.U, uvConstraint.U)) + if (!isFirst && MathHelper.GreaterThan(curLineSize.U + sz.U + spacingSize.U, uvConstraint.U)) { panelSize.U = Math.Max(curLineSize.U, panelSize.U); panelSize.V += curLineSize.V + spacingSize.V; curLineSize = sz; - - if (MathHelper.GreaterThan(sz.U, uvConstraint.U)) - { - panelSize.U = Math.Max(sz.U, panelSize.U); - panelSize.V += sz.V + spacingSize.V; - curLineSize = new PanelUvSize(_orientation); - } } else { curLineSize.U += isFirst ? sz.U : sz.U + spacingSize.U; curLineSize.V = Math.Max(sz.V, curLineSize.V); - - isFirst = false; } + + isFirst = false; } } @@ -350,7 +343,7 @@ protected override Size ArrangeOverride(Size finalSize) itemWidthSet ? itemWidth : child.DesiredSize.Width, itemHeightSet ? itemHeight : child.DesiredSize.Height); - if (MathHelper.GreaterThan(curLineSize.U + (isFirst ? sz.U : sz.U + spacingSize.U), uvFinalSize.U)) + if (!isFirst && MathHelper.GreaterThan(curLineSize.U + sz.U + spacingSize.U, uvFinalSize.U)) { ArrangeWrapLine(accumulatedV, curLineSize.V, firstInLine, i, useItemU, itemU, spacingSize.U);