From 64c211f9c0fa9d880d82b4dfbea30e166f388925 Mon Sep 17 00:00:00 2001
From: NaBian <836904362@qq.com>
Date: Tue, 3 Dec 2024 23:21:54 +0800
Subject: [PATCH] chore: add avalonia ScrollViewer demo.
---
.../Styles/NativeScrollViewerDemoCtl.axaml | 139 ++++++++++++++++++
.../Styles/NativeScrollViewerDemoCtl.axaml.cs | 9 ++
.../Controls/Attach/BorderElement.cs | 4 +-
.../Controls/Attach/ScrollViewerAttach.cs | 57 +++++++
.../Controls/Other/DashedBorder.cs | 4 +-
.../Controls/Panel/UniformSpacingPanel.cs | 4 +-
6 files changed, 211 insertions(+), 6 deletions(-)
create mode 100644 src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeScrollViewerDemoCtl.axaml
create mode 100644 src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeScrollViewerDemoCtl.axaml.cs
create mode 100644 src/Avalonia/HandyControl_Avalonia/Controls/Attach/ScrollViewerAttach.cs
diff --git a/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeScrollViewerDemoCtl.axaml b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeScrollViewerDemoCtl.axaml
new file mode 100644
index 000000000..dfb56ce44
--- /dev/null
+++ b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeScrollViewerDemoCtl.axaml
@@ -0,0 +1,139 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeScrollViewerDemoCtl.axaml.cs b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeScrollViewerDemoCtl.axaml.cs
new file mode 100644
index 000000000..e0a28a0eb
--- /dev/null
+++ b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/NativeScrollViewerDemoCtl.axaml.cs
@@ -0,0 +1,9 @@
+namespace HandyControlDemo.UserControl;
+
+public partial class NativeScrollViewerDemoCtl : Avalonia.Controls.UserControl
+{
+ public NativeScrollViewerDemoCtl()
+ {
+ InitializeComponent();
+ }
+}
diff --git a/src/Avalonia/HandyControl_Avalonia/Controls/Attach/BorderElement.cs b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/BorderElement.cs
index 8281b7cdd..47ddad274 100644
--- a/src/Avalonia/HandyControl_Avalonia/Controls/Attach/BorderElement.cs
+++ b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/BorderElement.cs
@@ -27,9 +27,9 @@ static BorderElement()
CircularProperty.Changed.AddClassHandler(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;
}
diff --git a/src/Avalonia/HandyControl_Avalonia/Controls/Attach/ScrollViewerAttach.cs b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/ScrollViewerAttach.cs
new file mode 100644
index 000000000..66e3c5e78
--- /dev/null
+++ b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/ScrollViewerAttach.cs
@@ -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 OrientationProperty =
+ AvaloniaProperty.RegisterAttached("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(OnOrientationChanged);
+ }
+
+ private static void OnOrientationChanged(AvaloniaObject element, AvaloniaPropertyChangedEventArgs e)
+ {
+ if (element is not ScrollViewer scrollViewer)
+ {
+ return;
+ }
+
+ if (e.GetNewValue() == 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;
+ }
+}
diff --git a/src/Avalonia/HandyControl_Avalonia/Controls/Other/DashedBorder.cs b/src/Avalonia/HandyControl_Avalonia/Controls/Other/DashedBorder.cs
index e3bd4363d..ba0037167 100644
--- a/src/Avalonia/HandyControl_Avalonia/Controls/Other/DashedBorder.cs
+++ b/src/Avalonia/HandyControl_Avalonia/Controls/Other/DashedBorder.cs
@@ -71,9 +71,9 @@ static DashedBorder()
BorderDashOffsetProperty.Changed.AddClassHandler(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;
diff --git a/src/Avalonia/HandyControl_Avalonia/Controls/Panel/UniformSpacingPanel.cs b/src/Avalonia/HandyControl_Avalonia/Controls/Panel/UniformSpacingPanel.cs
index d7bc2bbb2..605a8942f 100644
--- a/src/Avalonia/HandyControl_Avalonia/Controls/Panel/UniformSpacingPanel.cs
+++ b/src/Avalonia/HandyControl_Avalonia/Controls/Panel/UniformSpacingPanel.cs
@@ -63,9 +63,9 @@ static UniformSpacingPanel()
OrientationProperty.Changed.AddClassHandler(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();
}