Skip to content

Commit

Permalink
chore: add avalonia ScrollViewer demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Dec 3, 2024
1 parent 9122671 commit 64c211f
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="HandyControlDemo.UserControl.NativeScrollViewerDemoCtl"
xmlns:hc="https://handyorg.github.io/handycontrol"
Background="{DynamicResource RegionBrush}"
MaxWidth="550"
Height="600">
<Grid Margin="32"
RowDefinitions="Auto, *">
<Border BorderThickness="1"
BorderBrush="{DynamicResource BorderBrush}">
<ScrollViewer hc:ScrollViewerAttach.Orientation="Horizontal"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Hidden">
<StackPanel Orientation="Horizontal">
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
</StackPanel>
</ScrollViewer>
</Border>
<Border Grid.Row="1"
BorderThickness="1"
BorderBrush="{DynamicResource BorderBrush}"
Margin="0,16,0,0">
<ScrollViewer>
<WrapPanel Orientation="Horizontal">
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
<Border Theme="{StaticResource BorderRegion}"
Width="200"
Height="200"
Margin="10,10,32,10">
<Border Background="{DynamicResource PrimaryBrush}">
<TextBlock Text="Content"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="White" />
</Border>
</Border>
</WrapPanel>
</ScrollViewer>
</Border>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HandyControlDemo.UserControl;

public partial class NativeScrollViewerDemoCtl : Avalonia.Controls.UserControl
{
public NativeScrollViewerDemoCtl()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ static BorderElement()
CircularProperty.Changed.AddClassHandler<AvaloniaObject>(OnCircularChanged);
}

private static void OnCircularChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
private static void OnCircularChanged(AvaloniaObject element, AvaloniaPropertyChangedEventArgs e)
{
if (d is not Border border)
if (element is not Border border)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Layout;

namespace HandyControl.Controls;

public class ScrollViewerAttach
{
public static readonly AttachedProperty<Orientation> OrientationProperty =
AvaloniaProperty.RegisterAttached<ScrollViewerAttach, AvaloniaObject, Orientation>("Orientation",
defaultValue: Orientation.Vertical, inherits: true);

public static void SetOrientation(AvaloniaObject element, Orientation value) =>
element.SetValue(OrientationProperty, value);

public static Orientation GetOrientation(AvaloniaObject element) => element.GetValue(OrientationProperty);

static ScrollViewerAttach()
{
OrientationProperty.Changed.AddClassHandler<AvaloniaObject>(OnOrientationChanged);
}

private static void OnOrientationChanged(AvaloniaObject element, AvaloniaPropertyChangedEventArgs e)
{
if (element is not ScrollViewer scrollViewer)
{
return;
}

if (e.GetNewValue<Orientation>() == Orientation.Horizontal)
{
scrollViewer.PointerWheelChanged += ScrollViewerPointerWheelChanged;
}
else
{
scrollViewer.PointerWheelChanged -= ScrollViewerPointerWheelChanged;
}
}

private static void ScrollViewerPointerWheelChanged(object? sender, PointerWheelEventArgs e)
{
const int step = 50;

if (sender is not ScrollViewer scrollViewer)
{
return;
}

scrollViewer.Offset = new Vector(
scrollViewer.Offset.X - e.Delta.Y * step,
scrollViewer.Offset.Y
);

e.Handled = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ static DashedBorder()
BorderDashOffsetProperty.Changed.AddClassHandler<DashedBorder>(OnClearPenCache);
}

private static void OnClearPenCache(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
private static void OnClearPenCache(AvaloniaObject element, AvaloniaPropertyChangedEventArgs e)
{
var border = (DashedBorder)d;
var border = (DashedBorder)element;
border._leftPenCache = null;
border._rightPenCache = null;
border._topPenCache = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ static UniformSpacingPanel()
OrientationProperty.Changed.AddClassHandler<UniformSpacingPanel>(OnOrientationChanged);
}

private static void OnOrientationChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
private static void OnOrientationChanged(AvaloniaObject element, AvaloniaPropertyChangedEventArgs e)
{
var p = (UniformSpacingPanel)d;
var p = (UniformSpacingPanel)element;
p._orientation = e.GetNewValue<Orientation>();
}

Expand Down

0 comments on commit 64c211f

Please sign in to comment.