Skip to content

Commit

Permalink
chore: add avalonia border demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Jul 22, 2024
1 parent 40bb601 commit 17c2b84
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<DependentUpon>BoderDemoCtl.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Update="UserControl\Styles\BorderDemoCtl.axaml.cs">
<DependentUpon>BorderDemoCtl.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Grid.Row="0"
CornerRadius="10,10,0,0"
Background="{DynamicResource TitleBrush}"
Classes="clip">
Theme="{StaticResource BorderClip}">
<Panel>
<TextBlock Foreground="White"
HorizontalAlignment="Center"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hc="https://handyorg.github.io/handycontrol"
x:Class="HandyControlDemo.UserControl.BorderDemoCtl"
Background="{DynamicResource RegionBrush}">
<ScrollViewer>
<hc:UniformSpacingPanel Margin="32"
Orientation="Vertical"
Spacing="32"
MaxWidth="664">
<hc:UniformSpacingPanel Spacing="32"
ItemWidth="200"
ItemHeight="200"
ChildWrapping="Wrap"
HorizontalAlignment="Center">
<Border Theme="{StaticResource BorderRegion}">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Effect="{StaticResource EffectShadow1}">
<Border Background="{DynamicResource InfoBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Effect="{StaticResource EffectShadow2}">
<Border Background="{DynamicResource WarningBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Effect="{StaticResource EffectShadow3}">
<Border Background="{DynamicResource DangerBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Effect="{StaticResource EffectShadow4}">
<Border Background="{DynamicResource AccentBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Effect="{StaticResource EffectShadow5}">
<Border Background="{DynamicResource BorderBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Border>
</Border>
</hc:UniformSpacingPanel>
<hc:UniformSpacingPanel Orientation="Vertical"
Spacing="10">
<Border Theme="{StaticResource BorderTipPrimary}">
<TextBlock Text="TitleTitleTitleTitleTitleTitleTitleTitleTitleTitle" />
</Border>
<Border Theme="{StaticResource BorderTipDanger}">
<TextBlock Text="TitleTitleTitleTitleTitleTitleTitleTitleTitleTitle" />
</Border>
<Border Theme="{StaticResource BorderTipWarning}">
<TextBlock Text="TitleTitleTitleTitleTitleTitleTitleTitleTitleTitle" />
</Border>
<Border Theme="{StaticResource BorderTipInfo}">
<TextBlock Text="TitleTitleTitleTitleTitleTitleTitleTitleTitleTitle" />
</Border>
<Border Theme="{StaticResource BorderTipSuccess}">
<TextBlock Text="TitleTitleTitleTitleTitleTitleTitleTitleTitleTitle" />
</Border>
</hc:UniformSpacingPanel>
</hc:UniformSpacingPanel>
</ScrollViewer>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HandyControlDemo.UserControl;

public partial class BorderDemoCtl : Avalonia.Controls.UserControl
{
public BorderDemoCtl()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace HandyControl.Controls;
public class UniformSpacingPanel : Panel
{
public static readonly StyledProperty<Orientation> OrientationProperty =
StackPanel.OrientationProperty.AddOwner<UniformSpacingPanel>();
StackPanel.OrientationProperty.AddOwner<UniformSpacingPanel>(new StyledPropertyMetadata<Orientation>(
defaultValue: Orientation.Horizontal));

public static readonly StyledProperty<VisualWrapping> ChildWrappingProperty =
AvaloniaProperty.Register<UniformSpacingPanel, VisualWrapping>(nameof(ChildWrapping));
Expand Down Expand Up @@ -43,8 +44,6 @@ public class UniformSpacingPanel : Panel
AvaloniaProperty.Register<UniformSpacingPanel, VerticalAlignment?>(nameof(ItemVerticalAlignment),
defaultValue: VerticalAlignment.Stretch);

private Orientation _orientation = Orientation.Horizontal;

static UniformSpacingPanel()
{
AffectsMeasure<StackPanel>(OrientationProperty);
Expand All @@ -56,16 +55,6 @@ static UniformSpacingPanel()
AffectsMeasure<StackPanel>(ItemHeightProperty);
AffectsMeasure<StackPanel>(ItemHorizontalAlignmentProperty);
AffectsMeasure<StackPanel>(ItemVerticalAlignmentProperty);

OrientationProperty.Changed.AddClassHandler<UniformSpacingPanel>(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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -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);
}
}
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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,
Expand All @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions src/Avalonia/HandyControl_Avalonia/Themes/Basic/Effects.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@

<DropShadowEffect x:Key="EffectShadow1"
BlurRadius="5"
OffsetX="0"
OffsetY="1"
Color="{StaticResource EffectShadowColor}"
Opacity=".2" />
<DropShadowEffect x:Key="EffectShadow2"
BlurRadius="8"
OffsetX="0"
OffsetY="1.5"
Color="{StaticResource EffectShadowColor}"
Opacity=".2" />
<DropShadowEffect x:Key="EffectShadow3"
BlurRadius="14"
OffsetX="0"
OffsetY="4.5"
Color="{StaticResource EffectShadowColor}"
Opacity=".2" />
<DropShadowEffect x:Key="EffectShadow4"
BlurRadius="25"
OffsetX="0"
OffsetY="8"
Color="{StaticResource EffectShadowColor}"
Opacity=".2" />
<DropShadowEffect x:Key="EffectShadow5"
BlurRadius="35"
OffsetX="0"
OffsetY="13"
Color="{StaticResource EffectShadowColor}"
Opacity=".2" />
Expand Down
Loading

0 comments on commit 17c2b84

Please sign in to comment.