|
| 1 | +using System; |
| 2 | +using Avalonia; |
| 3 | +using Avalonia.Controls; |
| 4 | +using Avalonia.Controls.Primitives; |
| 5 | +using Avalonia.Media; |
| 6 | +using Avalonia.VisualTree; |
| 7 | + |
| 8 | +namespace vj0.Extensions.Animations.Properties; |
| 9 | + |
| 10 | +public static class GradientAnimationService |
| 11 | +{ |
| 12 | + public static readonly AttachedProperty<bool> IsAnimatedProperty = |
| 13 | + AvaloniaProperty.RegisterAttached<Control, bool>("IsAnimated", typeof(GradientAnimationService), inherits: true); |
| 14 | + |
| 15 | + public static readonly AttachedProperty<Color> AnimationStartColorProperty = |
| 16 | + AvaloniaProperty.RegisterAttached<Control, Color>("AnimationStartColor", typeof(GradientAnimationService), Colors.Black, inherits: true); |
| 17 | + |
| 18 | + public static readonly AttachedProperty<Color> AnimationEndColorProperty = |
| 19 | + AvaloniaProperty.RegisterAttached<Control, Color>("AnimationEndColor", typeof(GradientAnimationService), Colors.White, inherits: true); |
| 20 | + |
| 21 | + public static readonly AttachedProperty<double> AnimationDurationProperty = |
| 22 | + AvaloniaProperty.RegisterAttached<Control, double>("AnimationDuration", typeof(GradientAnimationService), 0.5, inherits: true); |
| 23 | + |
| 24 | + public static readonly AttachedProperty<int> AnimationStepsProperty = |
| 25 | + AvaloniaProperty.RegisterAttached<Control, int>("AnimationSteps", typeof(GradientAnimationService), 100, inherits: true); |
| 26 | + |
| 27 | + private static readonly AttachedProperty<GradientAnimator?> AnimatorInstanceProperty = |
| 28 | + AvaloniaProperty.RegisterAttached<Control, GradientAnimator?>("AnimatorInstance", typeof(GradientAnimationService)); |
| 29 | + |
| 30 | + static GradientAnimationService() |
| 31 | + { |
| 32 | + IsAnimatedProperty.Changed.Subscribe(OnIsAnimatedChanged); |
| 33 | + AnimationStartColorProperty.Changed.Subscribe(OnAnimationStartColorChanged); |
| 34 | + AnimationEndColorProperty.Changed.Subscribe(OnAnimationEndColorChanged); |
| 35 | + AnimationDurationProperty.Changed.Subscribe(OnAnimationDurationChanged); |
| 36 | + AnimationStepsProperty.Changed.Subscribe(OnAnimationStepsChanged); |
| 37 | + } |
| 38 | + |
| 39 | + public static bool GetIsAnimated(Control control) => control.GetValue(IsAnimatedProperty); |
| 40 | + public static void SetIsAnimated(Control control, bool value) => control.SetValue(IsAnimatedProperty, value); |
| 41 | + |
| 42 | + public static Color GetAnimationStartColor(Control control) => control.GetValue(AnimationStartColorProperty); |
| 43 | + public static void SetAnimationStartColor(Control control, Color value) => control.SetValue(AnimationStartColorProperty, value); |
| 44 | + |
| 45 | + public static Color GetAnimationEndColor(Control control) => control.GetValue(AnimationEndColorProperty); |
| 46 | + public static void SetAnimationEndColor(Control control, Color value) => control.SetValue(AnimationEndColorProperty, value); |
| 47 | + |
| 48 | + public static double GetAnimationDuration(Control control) => control.GetValue(AnimationDurationProperty); |
| 49 | + public static void SetAnimationDuration(Control control, double value) => control.SetValue(AnimationDurationProperty, value); |
| 50 | + |
| 51 | + public static int GetAnimationSteps(Control control) => control.GetValue(AnimationStepsProperty); |
| 52 | + public static void SetAnimationSteps(Control control, int value) => control.SetValue(AnimationStepsProperty, value); |
| 53 | + |
| 54 | + private static GradientAnimator? GetAnimatorInstance(Control control) => control.GetValue(AnimatorInstanceProperty); |
| 55 | + private static void SetAnimatorInstance(Control control, GradientAnimator? value) => control.SetValue(AnimatorInstanceProperty, value); |
| 56 | + |
| 57 | + private static void OnIsAnimatedChanged(AvaloniaPropertyChangedEventArgs<bool> e) |
| 58 | + { |
| 59 | + if (e.Sender is not Control control) return; |
| 60 | + |
| 61 | + if (e.GetNewValue<bool>()) |
| 62 | + { |
| 63 | + EnsureAnimator(control); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + StopAndDisposeAnimator(control); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static void OnAnimationStartColorChanged(AvaloniaPropertyChangedEventArgs<Color> e) |
| 72 | + { |
| 73 | + if (e.Sender is not Control control || !GetIsAnimated(control)) return; |
| 74 | + |
| 75 | + StopAndDisposeAnimator(control); |
| 76 | + EnsureAnimator(control); |
| 77 | + } |
| 78 | + |
| 79 | + private static void OnAnimationEndColorChanged(AvaloniaPropertyChangedEventArgs<Color> e) |
| 80 | + { |
| 81 | + if (e.Sender is not Control control || !GetIsAnimated(control)) return; |
| 82 | + |
| 83 | + StopAndDisposeAnimator(control); |
| 84 | + EnsureAnimator(control); |
| 85 | + } |
| 86 | + |
| 87 | + private static void OnAnimationDurationChanged(AvaloniaPropertyChangedEventArgs<double> e) |
| 88 | + { |
| 89 | + if (e.Sender is not Control control || !GetIsAnimated(control)) return; |
| 90 | + |
| 91 | + StopAndDisposeAnimator(control); |
| 92 | + EnsureAnimator(control); |
| 93 | + } |
| 94 | + |
| 95 | + private static void OnAnimationStepsChanged(AvaloniaPropertyChangedEventArgs<int> e) |
| 96 | + { |
| 97 | + if (e.Sender is not Control control || !GetIsAnimated(control)) return; |
| 98 | + |
| 99 | + StopAndDisposeAnimator(control); |
| 100 | + EnsureAnimator(control); |
| 101 | + } |
| 102 | + |
| 103 | + private static void EnsureAnimator(Control control) |
| 104 | + { |
| 105 | + if (GetAnimatorInstance(control) != null) return; |
| 106 | + |
| 107 | + LinearGradientBrush? targetBrush = null; |
| 108 | + |
| 109 | + if (control is TextBlock textBlock && textBlock.Foreground is LinearGradientBrush fgBrush) |
| 110 | + { |
| 111 | + targetBrush = fgBrush; |
| 112 | + } |
| 113 | + else if (control is TemplatedControl templated && templated.Background is LinearGradientBrush bgBrush) |
| 114 | + { |
| 115 | + targetBrush = bgBrush; |
| 116 | + } |
| 117 | + |
| 118 | + if (targetBrush != null) |
| 119 | + { |
| 120 | + var startColor = GetAnimationStartColor(control); |
| 121 | + var endColor = GetAnimationEndColor(control); |
| 122 | + var duration = GetAnimationDuration(control); |
| 123 | + var steps = GetAnimationSteps(control); |
| 124 | + |
| 125 | + var animator = new GradientAnimator(targetBrush, startColor, endColor, duration, steps); |
| 126 | + SetAnimatorInstance(control, animator); |
| 127 | + |
| 128 | + control.AttachedToVisualTree += Control_AttachedToVisualTree; |
| 129 | + control.DetachedFromVisualTree += Control_DetachedFromVisualTree; |
| 130 | + |
| 131 | + if (control.GetVisualRoot() != null) |
| 132 | + { |
| 133 | + animator.StartAnimation(); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static void StopAndDisposeAnimator(Control control) |
| 139 | + { |
| 140 | + var animator = GetAnimatorInstance(control); |
| 141 | + if (animator != null) |
| 142 | + { |
| 143 | + animator.Dispose(); |
| 144 | + SetAnimatorInstance(control, null); |
| 145 | + |
| 146 | + control.AttachedToVisualTree -= Control_AttachedToVisualTree; |
| 147 | + control.DetachedFromVisualTree -= Control_DetachedFromVisualTree; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private static void Control_AttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) |
| 152 | + { |
| 153 | + if (sender is Control control) |
| 154 | + { |
| 155 | + GetAnimatorInstance(control)?.StartAnimation(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static void Control_DetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) |
| 160 | + { |
| 161 | + if (sender is Control control) |
| 162 | + { |
| 163 | + GetAnimatorInstance(control)?.StopAnimation(); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments