Skip to content

Commit

Permalink
chore: add avalonia Slider demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Dec 14, 2024
1 parent d81d99d commit b4734cd
Show file tree
Hide file tree
Showing 10 changed files with 723 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="HandyControlDemo.UserControl.SliderDemo"
xmlns:hc="https://handyorg.github.io/handycontrol"
Background="{DynamicResource RegionBrush}">
<ScrollViewer>
<hc:UniformSpacingPanel Margin="32"
Orientation="Horizontal"
Spacing="32"
ClipToBounds="False"
ChildWrapping="Wrap">
<hc:UniformSpacingPanel Orientation="Vertical"
Spacing="32">
<Slider Width="400"
IsSnapToTickEnabled="True"
TickFrequency="1"
Maximum="10"
Value="8" />
<Slider Width="400"
IsSnapToTickEnabled="True"
TickFrequency="1"
Maximum="10"
Value="3"
IsEnabled="False" />
<Slider Width="400"
IsSnapToTickEnabled="True"
TickFrequency="5"
Maximum="100"
TickPlacement="TopLeft"
Value="10" />
<Slider Width="400"
hc:TipElement.IsVisible="True"
hc:TipElement.Placement="Top"
IsSnapToTickEnabled="True"
Maximum="100"
Value="60"
TickFrequency="10"
TickPlacement="BottomRight" />
<Slider Width="400"
hc:TipElement.IsVisible="True"
hc:TipElement.Placement="Bottom"
hc:TipElement.StringFormat="N2"
TickFrequency="1"
Maximum="10"
Value="5"
TickPlacement="Outside" />
</hc:UniformSpacingPanel>
<hc:UniformSpacingPanel Spacing="32">
<Slider Height="400"
IsSnapToTickEnabled="True"
TickFrequency="1"
Maximum="10"
Value="8"
Orientation="Vertical" />
<Slider Height="400"
IsSnapToTickEnabled="True"
TickFrequency="1"
Maximum="10"
Value="3"
IsEnabled="False"
Orientation="Vertical" />
<Slider Height="400"
IsSnapToTickEnabled="True"
TickFrequency="5"
Maximum="100"
TickPlacement="TopLeft"
Value="10"
Orientation="Vertical" />
<Slider Height="400"
hc:TipElement.IsVisible="True"
hc:TipElement.Placement="Right"
IsSnapToTickEnabled="True"
Maximum="100"
Value="60"
TickFrequency="10"
TickPlacement="BottomRight"
Orientation="Vertical" />
<Slider Height="400"
hc:TipElement.IsVisible="True"
hc:TipElement.Placement="Left"
hc:TipElement.StringFormat="N2"
TickFrequency="1"
Maximum="10"
Value="5"
TickPlacement="Outside"
Orientation="Vertical" />
</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 SliderDemo : Avalonia.Controls.UserControl
{
public SliderDemo()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;

namespace HandyControl.Controls;

public class ContentControlAttach
{
public static readonly AttachedProperty<string> ContentStringFormatProperty =
AvaloniaProperty.RegisterAttached<ContentControlAttach, AvaloniaObject, string>("ContentStringFormat");

public static void SetContentStringFormat(AvaloniaObject element, string value) =>
element.SetValue(ContentStringFormatProperty, value);

public static string GetContentStringFormat(AvaloniaObject element) =>
element.GetValue(ContentStringFormatProperty);

public static readonly AttachedProperty<object?> ContentProperty =
AvaloniaProperty.RegisterAttached<ContentControlAttach, AvaloniaObject, object?>("Content");

public static void SetContent(AvaloniaObject element, object? value) => element.SetValue(ContentProperty, value);

public static object? GetContent(AvaloniaObject element) => element.GetValue(ContentProperty);

static ContentControlAttach()
{
ContentStringFormatProperty.Changed.AddClassHandler<AvaloniaObject>(OnContentChanged);
ContentProperty.Changed.AddClassHandler<AvaloniaObject>(OnContentChanged);
}

private static void OnContentChanged(AvaloniaObject element, AvaloniaPropertyChangedEventArgs e)
{
if (element is not ContentControl contentControl)
{
return;
}

contentControl.ClearValue(ContentControl.ContentProperty);

var binding = new Binding("(ContentControlAttach.Content)")
{
Source = contentControl,
StringFormat = contentControl.GetValue(ContentStringFormatProperty),
TypeResolver = (_, _) => typeof(ContentControlAttach)
};

contentControl.Bind(ContentControl.ContentProperty, binding);
}
}
30 changes: 30 additions & 0 deletions src/Avalonia/HandyControl_Avalonia/Controls/Attach/TipElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Avalonia;
using HandyControl.Data;

namespace HandyControl.Controls;

public class TipElement
{
public static readonly AttachedProperty<bool> IsVisibleProperty =
AvaloniaProperty.RegisterAttached<TipElement, AvaloniaObject, bool>("IsVisible");

public static void SetIsVisible(AvaloniaObject element, bool value) => element.SetValue(IsVisibleProperty, value);

public static bool GetIsVisible(AvaloniaObject element) => element.GetValue(IsVisibleProperty);

public static readonly AttachedProperty<PlacementType> PlacementProperty =
AvaloniaProperty.RegisterAttached<TipElement, AvaloniaObject, PlacementType>("Placement");

public static void SetPlacement(AvaloniaObject element, PlacementType value) =>
element.SetValue(PlacementProperty, value);

public static PlacementType GetPlacement(AvaloniaObject element) => element.GetValue(PlacementProperty);

public static readonly AttachedProperty<string> StringFormatProperty =
AvaloniaProperty.RegisterAttached<TipElement, AvaloniaObject, string>("StringFormat", defaultValue: "N1");

public static void SetStringFormat(AvaloniaObject element, string value) =>
element.SetValue(StringFormatProperty, value);

public static string GetStringFormat(AvaloniaObject element) => element.GetValue(StringFormatProperty);
}
73 changes: 73 additions & 0 deletions src/Avalonia/HandyControl_Avalonia/Controls/Panel/AxleCanvas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;

namespace HandyControl.Controls;

public class AxleCanvas : Canvas
{
public static readonly StyledProperty<Orientation> OrientationProperty =
StackPanel.OrientationProperty.AddOwner<AxleCanvas>(new StyledPropertyMetadata<Orientation>(
defaultValue: Orientation.Horizontal));

public Orientation Orientation
{
get => GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}

protected override Size ArrangeOverride(Size finalSize)
{
foreach (Control? internalChild in Children)
{
if (internalChild == null)
{
continue;
}

double x = 0.0;
double y = 0.0;

if (Orientation == Orientation.Horizontal)
{
x = (finalSize.Width - internalChild.DesiredSize.Width) / 2;

double top = GetTop(internalChild);
if (!double.IsNaN(top))
{
y = top;
}
else
{
double bottom = GetBottom(internalChild);
if (!double.IsNaN(bottom))
{
y = finalSize.Height - internalChild.DesiredSize.Height - bottom;
}
}
}
else
{
y = (finalSize.Height - internalChild.DesiredSize.Height) / 2;

double left = GetLeft(internalChild);
if (!double.IsNaN(left))
{
x = left;
}
else
{
double right = GetRight(internalChild);
if (!double.IsNaN(right))
{
x = finalSize.Width - internalChild.DesiredSize.Width - right;
}
}
}

internalChild.Arrange(new Rect(new Point(x, y), internalChild.DesiredSize));
}

return finalSize;
}
}
17 changes: 17 additions & 0 deletions src/Avalonia/HandyControl_Avalonia/Data/Enum/PlacementType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace HandyControl.Data;

public enum PlacementType
{
LeftTop,
Left,
LeftBottom,
TopLeft,
Top,
TopRight,
RightTop,
Right,
RightBottom,
BottomLeft,
Bottom,
BottomRight,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Color x:Key="EffectShadowColor">#88000000</Color>
<Color x:Key="EffectShadowColor">Black</Color>

<DropShadowEffect x:Key="EffectShadow1"
BlurRadius="5"
Expand Down
Loading

0 comments on commit b4734cd

Please sign in to comment.