forked from microsoft/BuildCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualHelpers.cs
212 lines (183 loc) · 8.45 KB
/
VisualHelpers.cs
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
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using System;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Media;
namespace BuildCast.Helpers
{
public static class VisualHelpers
{
public static Visual GetVisual(this UIElement element)
{
return ElementCompositionPreview.GetElementVisual(element);
}
public static CompositionCommitBatch ApplyImplicitAnimation(this Visual target, TimeSpan duration)
{
var myBatch = target.Compositor.GetCommitBatch(CompositionBatchTypes.Animation);
target.Opacity = 0.0f;
ImplicitAnimationCollection implicitAnimationCollection = target.Compositor.CreateImplicitAnimationCollection();
implicitAnimationCollection[nameof(Visual.Opacity)] = CreateOpacityAnimation(target.Compositor, duration);
target.ImplicitAnimations = implicitAnimationCollection;
return myBatch;
}
public static KeyFrameAnimation CreateOpacityAnimation(Compositor compositor, TimeSpan duration)
{
ScalarKeyFrameAnimation kf = compositor.CreateScalarKeyFrameAnimation();
kf.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
kf.Duration = duration;
kf.Target = "Opacity";
return kf;
}
public static void SetSize(this Visual v, FrameworkElement element)
{
v.Size = new System.Numerics.Vector2((float)element.ActualWidth, (float)element.ActualHeight);
}
public static void FadeVisual(this Visual v, double seconds)
{
var fadeAnimation = CreateImplicitFadeAnimation(seconds);
v.ImplicitAnimations = Window.Current.Compositor.CreateImplicitAnimationCollection();
v.ImplicitAnimations.Add(nameof(Visual.Opacity), fadeAnimation);
}
// TODO: kill this function
public static ScalarKeyFrameAnimation CreateOpacityAnimation(double seconds)
{
var animation = Window.Current.Compositor.CreateScalarKeyFrameAnimation();
animation.Target = "Opacity";
animation.Duration = TimeSpan.FromSeconds(seconds);
animation.InsertKeyFrame(0, 0);
animation.InsertKeyFrame(0.25f, 0);
animation.InsertKeyFrame(1, 1);
return animation;
}
public static ICompositionAnimationBase CreateOpacityAnimation(double seconds, float finalvalue)
{
var animation = Window.Current.Compositor.CreateScalarKeyFrameAnimation();
animation.Target = nameof(Visual.Opacity);
animation.Duration = TimeSpan.FromSeconds(seconds);
animation.InsertKeyFrame(1, finalvalue);
return animation;
}
public static ICompositionAnimationBase CreateAnimationGroup(CompositionAnimation listContentShowAnimations, ScalarKeyFrameAnimation listContentOpacityAnimations)
{
var group = Window.Current.Compositor.CreateAnimationGroup();
group.Add(listContentShowAnimations);
group.Add(listContentOpacityAnimations);
return group;
}
public static void EnableLayoutImplicitAnimations(this UIElement element, TimeSpan t)
{
Compositor compositor;
var result = element.GetVisual();
compositor = result.Compositor;
var elementImplicitAnimation = compositor.CreateImplicitAnimationCollection();
elementImplicitAnimation[nameof(Visual.Offset)] = CreateOffsetAnimation(compositor, t);
result.ImplicitAnimations = elementImplicitAnimation;
}
private static CompositionAnimation CreateImplicitFadeAnimation(double seconds)
{
var animation = Window.Current.Compositor.CreateScalarKeyFrameAnimation();
animation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
animation.Target = nameof(Visual.Opacity);
animation.Duration = TimeSpan.FromSeconds(seconds);
return animation;
}
private static KeyFrameAnimation CreateOffsetAnimation(Compositor compositor, TimeSpan duration)
{
Vector3KeyFrameAnimation kf = compositor.CreateVector3KeyFrameAnimation();
kf.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
kf.Duration = duration;
kf.Target = "Offset";
return kf;
}
public static CompositionAnimation CreateHorizontalOffsetAnimation(double seconds, float offset, double delaySeconds, bool from)
{
var animation = Window.Current.Compositor.CreateScalarKeyFrameAnimation();
if (delaySeconds != 0.0)
{
animation.DelayBehavior = AnimationDelayBehavior.SetInitialValueBeforeDelay;
animation.DelayTime = TimeSpan.FromSeconds(delaySeconds);
}
animation.Duration = TimeSpan.FromSeconds(seconds);
animation.Target = "Translation.X";
if (from)
{
animation.InsertKeyFrame(0, offset);
animation.InsertKeyFrame(1, 0);
}
else
{
animation.InsertKeyFrame(1, offset);
}
return animation;
}
public static CompositionAnimation CreateHorizontalOffsetAnimation(double seconds, float offset, double delaySeconds)
{
return CreateHorizontalOffsetAnimation(seconds, offset, delaySeconds, true);
}
public static CompositionAnimation CreateVerticalOffsetAnimation(double seconds, float offset, double delaySeconds, bool from)
{
var animation = Window.Current.Compositor.CreateScalarKeyFrameAnimation();
if (delaySeconds != 0.0)
{
animation.DelayBehavior = AnimationDelayBehavior.SetInitialValueBeforeDelay;
animation.DelayTime = TimeSpan.FromSeconds(delaySeconds);
}
animation.Duration = TimeSpan.FromSeconds(seconds);
animation.Target = "Translation.Y";
if (from)
{
animation.InsertKeyFrame(0, offset);
animation.InsertKeyFrame(1, 0);
}
else
{
animation.InsertKeyFrame(1, offset);
}
return animation;
}
public static CompositionAnimation CreateVerticalOffsetAnimation(double seconds, float offset, double delaySeconds)
{
return CreateVerticalOffsetAnimation(seconds, offset, delaySeconds, true);
}
public static CompositionAnimation CreateVerticalOffsetAnimationFrom(double seconds, float offset)
{
return CreateVerticalOffsetAnimation(seconds, offset, 0.0f);
}
public static CompositionAnimation CreateVerticalOffsetAnimationTo(double seconds, float offset)
{
return CreateVerticalOffsetAnimation(seconds, offset, 0.0f, false);
}
public static T GetVisualChildByName<T>(this FrameworkElement root, string name)
where T : FrameworkElement
{
var chil = VisualTreeHelper.GetChild(root, 0);
FrameworkElement child = null;
int count = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; i < count && child == null; i++)
{
var current = (FrameworkElement)VisualTreeHelper.GetChild(root, i);
if (current != null && current.Name != null && current.Name == name)
{
child = current;
break;
}
else
{
child = current.GetVisualChildByName<FrameworkElement>(name);
}
}
return child as T;
}
}
}