-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathExpanderAnimationsHelper.cs
More file actions
283 lines (233 loc) · 11.2 KB
/
ExpanderAnimationsHelper.cs
File metadata and controls
283 lines (233 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace iNKORE.UI.WPF.Modern.Controls.Helpers
{
public static class ExpanderAnimationsHelper
{
#region ToAnimateControlName
public static string GetToAnimateControlName(Expander element) =>
(string)element.GetValue(ToAnimateControlNameProperty);
public static void SetToAnimateControlName(Expander element, string value) =>
element.SetValue(ToAnimateControlNameProperty, value);
private const string DefaultAnimationTargetPartName = "ExpanderContent";
public static readonly DependencyProperty ToAnimateControlNameProperty = DependencyProperty.RegisterAttached(
"ToAnimateControlName",
typeof(string),
typeof(ExpanderAnimationsHelper),
new PropertyMetadata(DefaultAnimationTargetPartName));
#endregion
#region ExpandAnimationDuration
public static TimeSpan GetExpandAnimationDuration(Expander element) =>
(TimeSpan)element.GetValue(ExpandAnimationDurationProperty);
public static void SetExpandAnimationDuration(Expander element, TimeSpan value) =>
element.SetValue(ExpandAnimationDurationProperty, value);
public static readonly DependencyProperty ExpandAnimationDurationProperty = DependencyProperty.RegisterAttached(
"ExpandAnimationDuration",
typeof(TimeSpan),
typeof(ExpanderAnimationsHelper),
new PropertyMetadata(TimeSpan.FromMilliseconds(333)));
#endregion
#region CollapseAnimationDuration
public static TimeSpan GetCollapseAnimationDuration(Expander element) =>
(TimeSpan)element.GetValue(CollapseAnimationDurationProperty);
public static void SetCollapseAnimationDuration(Expander element, TimeSpan value) =>
element.SetValue(CollapseAnimationDurationProperty, value);
public static readonly DependencyProperty CollapseAnimationDurationProperty =
DependencyProperty.RegisterAttached(
"CollapseAnimationDuration",
typeof(TimeSpan),
typeof(ExpanderAnimationsHelper),
new PropertyMetadata(TimeSpan.FromMilliseconds(167)));
#endregion
#region IsEnabled
public static bool GetIsEnabled(Expander element) => (bool)element.GetValue(IsEnabledProperty);
public static void SetIsEnabled(Expander element, bool value) => element.SetValue(IsEnabledProperty, value);
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
"IsEnabled",
typeof(bool),
typeof(ExpanderAnimationsHelper),
new PropertyMetadata(OnIsEnabledChanged));
private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not Expander expander)
{
return;
}
if (e.NewValue is not true)
{
expander.Expanded -= OnExpanderExpandedOrCollapsed;
expander.Collapsed -= OnExpanderExpandedOrCollapsed;
return;
}
expander.Expanded += OnExpanderExpandedOrCollapsed;
expander.Collapsed += OnExpanderExpandedOrCollapsed;
if (expander.IsLoaded)
{
InitializeExpanderState(expander);
}
else
{
expander.Loaded += TriggerExpandAnimationOnLoad;
}
void TriggerExpandAnimationOnLoad(object sender, RoutedEventArgs routedEventArgs)
{
InitializeExpanderState(expander);
expander.Loaded -= TriggerExpandAnimationOnLoad;
}
}
private static void OnExpanderExpandedOrCollapsed(object sender, RoutedEventArgs e)
{
if (sender is not Expander expander)
{
return;
}
RunExpanderAnimation(expander);
}
#endregion
/// <summary>
/// Initializes the visual state of an expander on initial load without animations.
/// This method sets the expander content to the appropriate state (collapsed or expanded)
/// immediately to prevent visual flashing during load.
/// </summary>
/// <remarks>
/// This method is called during the initial load of the expander to avoid triggering
/// layout updates and animations that would cause the content to briefly render at
/// full size before collapsing. For collapsed expanders, it sets visibility directly
/// to Collapsed. For expanded expanders, it ensures visibility is Visible and resets
/// any transforms to identity without animation.
/// </remarks>
/// <seealso href="https://github.com/iNKORE-NET/UI.WPF.Modern/issues/402"/>
private static void InitializeExpanderState(Expander expander)
{
// On initial load, set the content to the appropriate state immediately
// without animation to avoid a visual flash
var toAnimateControl = GetToAnimateControl(expander);
if (toAnimateControl == null)
{
return;
}
if (!expander.IsExpanded)
{
// If collapsed on load, set visibility to collapsed immediately
toAnimateControl.Visibility = Visibility.Collapsed;
}
else
{
// If expanded on load, ensure visibility is set and clear any animations
toAnimateControl.BeginAnimation(UIElement.VisibilityProperty, null);
toAnimateControl.Visibility = Visibility.Visible;
// Clear any transform animations and reset transform to identity
if (toAnimateControl.RenderTransform is TranslateTransform translateTransform)
{
translateTransform.BeginAnimation(TranslateTransform.XProperty, null);
translateTransform.BeginAnimation(TranslateTransform.YProperty, null);
translateTransform.X = 0;
translateTransform.Y = 0;
}
}
}
private static void RunExpanderAnimation(Expander expander)
{
if (expander.IsExpanded)
{
AnimateExpand(expander);
}
else
{
AnimateCollapse(expander);
}
}
private static void AnimateExpand(Expander expander)
{
var toAnimateControl = GetToAnimateControl(expander);
toAnimateControl.BeginAnimation(UIElement.VisibilityProperty, null);
toAnimateControl.Visibility = Visibility.Visible;
UpdateLayout(toAnimateControl);
if (toAnimateControl.RenderTransform is not TranslateTransform translateTransform)
{
toAnimateControl.RenderTransform = translateTransform = new TranslateTransform();
}
var (animationProperty, originValue) = GetToAnimatePropertyAndValue(toAnimateControl, expander.ExpandDirection);
RunTranslationAnimation(GetExpandAnimationDuration(expander), translateTransform, animationProperty, 0, originValue);
}
private static void AnimateCollapse(Expander expander)
{
var toAnimateControl = GetToAnimateControl(expander);
var animationDuration = GetCollapseAnimationDuration(expander);
var visibilityAnimation = new ObjectAnimationUsingKeyFrames
{
KeyFrames =
[
new DiscreteObjectKeyFrame
{
KeyTime = animationDuration,
Value = Visibility.Collapsed
}
]
};
UpdateLayout(toAnimateControl);
if (toAnimateControl.RenderTransform is not TranslateTransform translateTransform)
{
toAnimateControl.RenderTransform = translateTransform = new TranslateTransform();
}
var (animationProperty, toValue) = GetToAnimatePropertyAndValue(toAnimateControl, expander.ExpandDirection);
toAnimateControl.BeginAnimation(UIElement.VisibilityProperty, visibilityAnimation);
RunTranslationAnimation(animationDuration, translateTransform, animationProperty,
toValue);
}
private static (DependencyProperty, double) GetToAnimatePropertyAndValue(FrameworkElement toAnimateControl,
ExpandDirection direction)
{
var (toAnimateProp, toResetProp, toValue) = direction switch
{
ExpandDirection.Down => (TranslateTransform.YProperty, TranslateTransform.XProperty, -toAnimateControl.ActualHeight),
ExpandDirection.Up => (TranslateTransform.YProperty, TranslateTransform.XProperty, toAnimateControl.ActualHeight),
ExpandDirection.Left => (TranslateTransform.XProperty, TranslateTransform.YProperty, toAnimateControl.ActualWidth),
ExpandDirection.Right => (TranslateTransform.XProperty, TranslateTransform.YProperty, -toAnimateControl.ActualWidth),
};
toAnimateControl.RenderTransform.BeginAnimation(toResetProp, null);
return (toAnimateProp, toValue);
}
private static void RunTranslationAnimation(
TimeSpan animationDuration,
TranslateTransform translateTransform,
DependencyProperty toAnimateProperty,
double targetValue,
double? fromValue = null)
{
var keyFrames = new DoubleKeyFrameCollection
{
new SplineDoubleKeyFrame
{
KeySpline = new KeySpline(0, 0, 0, 1),
KeyTime = animationDuration,
Value = targetValue
},
};
if (fromValue is not null)
{
keyFrames.Add(new DiscreteDoubleKeyFrame(fromValue.Value, KeyTime.FromPercent(0)));
}
var animation = new DoubleAnimationUsingKeyFrames
{
KeyFrames = keyFrames,
Duration = animationDuration
};
translateTransform.BeginAnimation(toAnimateProperty, animation);
}
private static void UpdateLayout(FrameworkElement contentControl)
{
contentControl.Measure(new Size(contentControl.MaxWidth, contentControl.MaxHeight));
contentControl.UpdateLayout();
}
private static FrameworkElement GetToAnimateControl(Expander expander)
{
expander.ApplyTemplate();
return expander.Template?.FindName(GetToAnimateControlName(expander), expander) as FrameworkElement ??
throw new InvalidOperationException($"Couldn't find the part to animate. Either update ExpanderAnimationsHelper.ToAnimateControlName, or rename the animation target part to the default: {DefaultAnimationTargetPartName}");
}
}
}