ScrollableExtensions.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Numerics;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Rendering.Composition;
  8. using Avalonia.Rendering.Composition.Animations;
  9. namespace SukiUI.Theme
  10. {
  11. public static class Scrollable
  12. {
  13. public static void MakeScrollable(CompositionVisual compositionVisual)
  14. {
  15. if (compositionVisual == null)
  16. return;
  17. Compositor compositor = compositionVisual.Compositor;
  18. var animationGroup = compositor.CreateAnimationGroup();
  19. Vector3KeyFrameAnimation offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
  20. offsetAnimation.Target = "Offset";
  21. offsetAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
  22. offsetAnimation.Duration = TimeSpan.FromMilliseconds(250);
  23. ImplicitAnimationCollection implicitAnimationCollection = compositor.CreateImplicitAnimationCollection();
  24. animationGroup.Add(offsetAnimation);
  25. implicitAnimationCollection["Offset"] = animationGroup;
  26. compositionVisual.ImplicitAnimations = implicitAnimationCollection;
  27. }
  28. }
  29. public static class StackPanelExtensions
  30. {
  31. public static readonly AttachedProperty<bool> AnimatedScrollProperty =
  32. AvaloniaProperty.RegisterAttached<StackPanel, bool>("AnimatedScroll", typeof(StackPanel), defaultValue: false);
  33. static StackPanelExtensions()
  34. {
  35. AnimatedScrollProperty.Changed.AddClassHandler<StackPanel>(HandleAnimatedScrollChanged);
  36. }
  37. private static void HandleAnimatedScrollChanged(StackPanel interactElem, AvaloniaPropertyChangedEventArgs args)
  38. {
  39. if(GetAnimatedScroll(interactElem))
  40. interactElem.AttachedToVisualTree += (sender, args) => Scrollable.MakeScrollable(ElementComposition.GetElementVisual(interactElem));
  41. }
  42. public static bool GetAnimatedScroll(StackPanel wrap)
  43. {
  44. return wrap.GetValue(AnimatedScrollProperty);
  45. }
  46. public static void SetAnimatedScroll(StackPanel wrap, bool value)
  47. {
  48. wrap.SetValue(AnimatedScrollProperty, value);
  49. }
  50. }
  51. public static class WrapPanelExtensions
  52. {
  53. public static readonly AttachedProperty<bool> AnimatedScrollProperty =
  54. AvaloniaProperty.RegisterAttached<WrapPanel, bool>("AnimatedScroll", typeof(WrapPanel), defaultValue: false);
  55. static WrapPanelExtensions()
  56. {
  57. AnimatedScrollProperty.Changed.AddClassHandler<WrapPanel>(HandleAnimatedScrollChanged);
  58. }
  59. private static void HandleAnimatedScrollChanged(WrapPanel interactElem, AvaloniaPropertyChangedEventArgs args)
  60. {
  61. if(GetAnimatedScroll(interactElem))
  62. interactElem.AttachedToVisualTree += (sender, args) => Scrollable.MakeScrollable(ElementComposition.GetElementVisual(interactElem));
  63. }
  64. public static bool GetAnimatedScroll(WrapPanel wrap)
  65. {
  66. return wrap.GetValue(AnimatedScrollProperty);
  67. }
  68. public static void SetAnimatedScroll(WrapPanel wrap, bool value)
  69. {
  70. wrap.SetValue(AnimatedScrollProperty, value);
  71. }
  72. }
  73. public static class ItemsPresenterExtensions
  74. {
  75. public static readonly AttachedProperty<bool> AnimatedScrollProperty =
  76. AvaloniaProperty.RegisterAttached<ItemsPresenter, bool>("AnimatedScroll", typeof(ItemsPresenter), defaultValue: false);
  77. static ItemsPresenterExtensions()
  78. {
  79. AnimatedScrollProperty.Changed.AddClassHandler<ItemsPresenter>(HandleAnimatedScrollChanged);
  80. }
  81. private static void HandleAnimatedScrollChanged(ItemsPresenter interactElem, AvaloniaPropertyChangedEventArgs args)
  82. {
  83. if(GetAnimatedScroll(interactElem))
  84. interactElem.AttachedToVisualTree += (sender, args) => Scrollable.MakeScrollable(ElementComposition.GetElementVisual(interactElem));
  85. }
  86. public static bool GetAnimatedScroll(ItemsPresenter wrap)
  87. {
  88. return wrap.GetValue(AnimatedScrollProperty);
  89. }
  90. public static void SetAnimatedScroll(ItemsPresenter wrap, bool value)
  91. {
  92. wrap.SetValue(AnimatedScrollProperty, value);
  93. }
  94. }
  95. public static class ItemsControlExtensions
  96. {
  97. public static readonly AttachedProperty<bool> AnimatedScrollProperty =
  98. AvaloniaProperty.RegisterAttached<ItemsControl, bool>("AnimatedScroll", typeof(ItemsControl), defaultValue: false);
  99. static ItemsControlExtensions()
  100. {
  101. AnimatedScrollProperty.Changed.AddClassHandler<ItemsControl>(HandleAnimatedScrollChanged);
  102. }
  103. private static void HandleAnimatedScrollChanged(ItemsControl interactElem, AvaloniaPropertyChangedEventArgs args)
  104. {
  105. if(GetAnimatedScroll(interactElem))
  106. interactElem.AttachedToVisualTree += (sender, args) => Scrollable.MakeScrollable(ElementComposition.GetElementVisual(interactElem));
  107. }
  108. public static bool GetAnimatedScroll(ItemsControl wrap)
  109. {
  110. return wrap.GetValue(AnimatedScrollProperty);
  111. }
  112. public static void SetAnimatedScroll(ItemsControl wrap, bool value)
  113. {
  114. wrap.SetValue(AnimatedScrollProperty, value);
  115. }
  116. }
  117. }