2
2
3
3
public static class FluidAnimationsExtensions
4
4
{
5
- public static Flow Flows ( this VisualElement element , params ( BindableProperty , object value ) [ ] values )
5
+ /// <summary>
6
+ /// Defines the flow properties for the given view.
7
+ /// </summary>
8
+ /// <param name="view">the view.</param>
9
+ /// <param name="values">The property/value pair.</param>
10
+ /// <returns>da flow.</returns>
11
+ public static Flow Flows ( this View view , params ( BindableProperty , object value ) [ ] values )
6
12
{
7
- var flow = new Flow ( element ) ;
13
+ var flow = new Flow ( view ) ;
8
14
9
15
foreach ( var ( property , value ) in values )
10
16
{
11
17
if ( property . ReturnType == typeof ( double ) )
12
- _ = flow . ToDouble ( property , ( double ) value ) ;
18
+ {
19
+ _ = flow . ToDouble ( property , Convert . ToDouble ( value ) ) ;
20
+ }
13
21
else if ( property . ReturnType == typeof ( Thickness ) )
14
- _ = flow . ToThickness ( property , ( Thickness ) value ) ;
22
+ {
23
+ _ = value . IsNumber ( )
24
+ ? flow . ToThickness ( property , new Thickness ( Convert . ToDouble ( value ) ) )
25
+ : flow . ToThickness ( property , ( Thickness ) value ) ;
26
+ }
15
27
else if ( property . ReturnType == typeof ( Color ) )
28
+ {
16
29
_ = flow . ToColor ( property , ( Color ) value ) ;
17
- else if ( property . ReturnType == typeof ( Rect ) )
18
- _ = flow . ToLayoutBounds ( ( ( Rect ) value ) . X , ( ( Rect ) value ) . Y , ( ( Rect ) value ) . Width , ( ( Rect ) value ) . Height ) ;
30
+ }
31
+ else if ( property == AbsoluteLayout . LayoutBoundsProperty )
32
+ {
33
+ #pragma warning disable IDE0045 // Convert to conditional expression
34
+ if ( value is Rect rect )
35
+ {
36
+ _ = flow . ToLayoutBounds ( ( ( Rect ) value ) . X , ( ( Rect ) value ) . Y , ( ( Rect ) value ) . Width , ( ( Rect ) value ) . Height ) ;
37
+ }
38
+ else if ( value is Point point )
39
+ {
40
+ _ = flow . ToLayoutBounds ( ( ( Point ) value ) . X , ( ( Point ) value ) . Y ) ;
41
+ }
42
+ else
43
+ {
44
+ throw new ArgumentException (
45
+ $ "The given type is not supported for the LayoutBoundsProperty property") ;
46
+ }
47
+ #pragma warning restore IDE0045 // Convert to conditional expression
48
+ }
19
49
else
20
- throw new ArgumentException (
21
- "Property flow error. Property must be of type double, Thickness, Color or Rect" , nameof ( property ) ) ;
50
+ {
51
+ throw new ArgumentException ( $ "The given type is not supported.", nameof ( property ) ) ;
52
+ }
22
53
}
23
54
24
55
return flow ;
25
56
}
26
57
27
- public static Flow Flows ( this VisualElement element )
58
+ public static Flow Flows ( this View view )
28
59
{
29
- return new Flow ( element ) ;
60
+ return new Flow ( view ) ;
30
61
}
31
62
32
63
public static Flow ToDouble (
@@ -35,16 +66,16 @@ public static Flow ToDouble(
35
66
if ( property . ReturnType != typeof ( double ) )
36
67
throw new ArgumentException ( "Property flow error. Property must be of type double" , nameof ( property ) ) ;
37
68
38
- var a = ( double ) flow . VisualElement . GetValue ( property ) ;
69
+ var a = ( double ) flow . View . GetValue ( property ) ;
39
70
40
71
return flow . Add (
41
72
new FlowProperty (
42
- flow . VisualElement ,
73
+ flow . View ,
43
74
property ,
44
75
value ,
45
76
( ) =>
46
77
{
47
- var start = ( double ) flow . VisualElement . GetValue ( property ) ;
78
+ var start = ( double ) flow . View . GetValue ( property ) ;
48
79
return t => start + t * ( value - start ) ;
49
80
} ,
50
81
start ,
@@ -66,17 +97,19 @@ public static Flow ToThickness(
66
97
public static Flow ToThickness (
67
98
this Flow flow , BindableProperty property , Thickness value , double start = 0 , double end = 1 )
68
99
{
100
+ #pragma warning disable IDE0046 // Convert to conditional expression
69
101
if ( property . ReturnType != typeof ( Thickness ) )
70
102
throw new ArgumentException ( "Property flow error. Property must be of type Thickness" , nameof ( property ) ) ;
103
+ #pragma warning restore IDE0046 // Convert to conditional expression
71
104
72
105
return flow . Add (
73
106
new FlowProperty (
74
- flow . VisualElement ,
107
+ flow . View ,
75
108
property ,
76
109
value ,
77
110
( ) =>
78
111
{
79
- var start = ( Thickness ) flow . VisualElement . GetValue ( property ) ;
112
+ var start = ( Thickness ) flow . View . GetValue ( property ) ;
80
113
return t => new Thickness (
81
114
start . Left + t * ( value . Left - start . Left ) ,
82
115
start . Top + t * ( value . Top - start . Top ) ,
@@ -90,17 +123,19 @@ public static Flow ToThickness(
90
123
public static Flow ToColor (
91
124
this Flow flow , BindableProperty property , Color color , double start = 0 , double end = 1 )
92
125
{
126
+ #pragma warning disable IDE0046 // Convert to conditional expression
93
127
if ( property . ReturnType != typeof ( Color ) )
94
128
throw new ArgumentException ( "Property flow error. Property must be of type Color" , nameof ( property ) ) ;
129
+ #pragma warning restore IDE0046 // Convert to conditional expression
95
130
96
131
return flow . Add (
97
132
new FlowProperty (
98
- flow . VisualElement ,
133
+ flow . View ,
99
134
property ,
100
135
color ,
101
136
( ) =>
102
137
{
103
- var start = ( Color ) flow . VisualElement . GetValue ( property ) ;
138
+ var start = ( Color ) flow . View . GetValue ( property ) ;
104
139
return t => Color . FromRgba (
105
140
start . Red + t * ( color . Red - start . Red ) ,
106
141
start . Green + t * ( color . Green - start . Green ) ,
@@ -122,12 +157,12 @@ public static Flow ToLayoutBounds(
122
157
{
123
158
return flow . Add (
124
159
new FlowProperty (
125
- flow . VisualElement ,
160
+ flow . View ,
126
161
AbsoluteLayout . LayoutBoundsProperty ,
127
162
new Rect ( x , y , width , height ) ,
128
163
( ) =>
129
164
{
130
- var start = ( Rect ) flow . VisualElement . GetValue ( AbsoluteLayout . LayoutBoundsProperty ) ;
165
+ var start = ( Rect ) flow . View . GetValue ( AbsoluteLayout . LayoutBoundsProperty ) ;
131
166
return t => new Rect (
132
167
start . X + t * ( x - start . X ) ,
133
168
start . Y + t * ( y - start . Y ) ,
@@ -144,9 +179,9 @@ public static Flow ToLayoutBounds(
144
179
/// <param name="view">The target view.</param>
145
180
/// <param name="flowBuilder">da flow builder.</param>
146
181
/// <returns></returns>
147
- public static T FlowToResult < T > ( this T view , Func < T , IEnumerable < Flow > > flowBuilder ) where T : View
182
+ public static T Complete < T > ( this T view , Func < T , IEnumerable < Flow > > flowBuilder ) where T : View
148
183
{
149
- return view . FlowToResult ( flowBuilder ( view ) ) ;
184
+ return view . Complete ( flowBuilder ( view ) ) ;
150
185
}
151
186
152
187
/// <summary>
@@ -155,9 +190,19 @@ public static T FlowToResult<T>(this T view, Func<T, IEnumerable<Flow>> flowBuil
155
190
/// <param name="view">The target view.</param>
156
191
/// <param name="flowBuilder">da flow builder.</param>
157
192
/// <returns></returns>
158
- public static T FlowToResult < T > ( this T view , Func < T , Flow > flowBuilder ) where T : View
193
+ public static T Complete < T > ( this T view , Func < T , Flow > flowBuilder ) where T : View
194
+ {
195
+ return view . Complete ( [ flowBuilder ( view ) ] ) ;
196
+ }
197
+
198
+ /// <summary>
199
+ /// Sets all the flow properties to their target values (without animations).
200
+ /// </summary>
201
+ /// <param name="flow">da flow.</param>
202
+ /// <returns></returns>
203
+ public static T Complete < T > ( this Flow flow ) where T : View
159
204
{
160
- return view . FlowToResult ( [ flowBuilder ( view ) ] ) ;
205
+ return ( T ) flow . View . Complete ( [ flow ] ) ;
161
206
}
162
207
163
208
/// <summary>
@@ -166,7 +211,7 @@ public static T FlowToResult<T>(this T view, Func<T, Flow> flowBuilder) where T
166
211
/// <param name="view">The target view.</param>
167
212
/// <param name="flowCollection">da flow.</param>
168
213
/// <returns></returns>
169
- public static T FlowToResult < T > ( this T view , IEnumerable < Flow > flowCollection ) where T : View
214
+ public static T Complete < T > ( this T view , IEnumerable < Flow > flowCollection ) where T : View
170
215
{
171
216
foreach ( var flow in flowCollection )
172
217
foreach ( var flowProperty in flow )
@@ -186,12 +231,12 @@ public static T FlowToResult<T>(this T view, IEnumerable<Flow> flowCollection) w
186
231
/// <param name="fps">Frames per second, default is 60.</param>
187
232
/// <param name="animationName">The animation identifier name, by default a new Guid is used.</param>
188
233
/// <returns>A task that completes when the animations ends.</returns>
189
- public static Task < bool > Flow < T > (
234
+ public static Task < bool > Animate < T > (
190
235
this T view , Func < T , IEnumerable < Flow > > flowBuilder , VisualElement ? owner = null , uint duration = 500 ,
191
236
Easing ? easing = null , uint fps = 60 , string ? animationName = null )
192
237
where T : View
193
238
{
194
- return view . Flow ( flowBuilder ( view ) , owner , duration , easing , fps , animationName ) ;
239
+ return view . Animate ( flowBuilder ( view ) , owner , duration , easing , fps , animationName ) ;
195
240
}
196
241
197
242
/// <summary>
@@ -205,12 +250,30 @@ public static Task<bool> Flow<T>(
205
250
/// <param name="fps">Frames per second, default is 60.</param>
206
251
/// <param name="animationName">The animation identifier name, by default a new Guid is used.</param>
207
252
/// <returns>A task that completes when the animations ends.</returns>
208
- public static Task < bool > Flow < T > (
253
+ public static Task < bool > Animate < T > (
209
254
this T view , Func < T , Flow > flowBuilder , VisualElement ? owner = null , uint duration = 500 ,
210
255
Easing ? easing = null , uint fps = 60 , string ? animationName = null )
211
256
where T : View
212
257
{
213
- return view . Flow ( [ flowBuilder ( view ) ] , owner , duration , easing , fps , animationName ) ;
258
+ return view . Animate ( [ flowBuilder ( view ) ] , owner , duration , easing , fps , animationName ) ;
259
+ }
260
+
261
+ /// <summary>
262
+ /// Starts a flow animation and returns a task that completes when the animation ends.
263
+ /// </summary>
264
+ /// <param name="flow">da flow.</param>
265
+ /// <param name="owner">Identifies the owner of the animation. This can be the visual element on which the animation is applied, or another visual element, such as the page</param>
266
+ /// <param name="duration">The duration in milliseconds.</param>
267
+ /// <param name="easing">The easing function.</param>
268
+ /// <param name="fps">Frames per second, default is 60.</param>
269
+ /// <param name="animationName">The animation identifier name, by default a new Guid is used.</param>
270
+ /// <returns>A task that completes when the animations ends.</returns>
271
+ public static Task < bool > Animate < T > (
272
+ this Flow flow , VisualElement ? owner = null , uint duration = 500 ,
273
+ Easing ? easing = null , uint fps = 60 , string ? animationName = null )
274
+ where T : View
275
+ {
276
+ return flow . View . Animate ( [ flow ] , owner , duration , easing , fps , animationName ) ;
214
277
}
215
278
216
279
/// <summary>
@@ -224,7 +287,7 @@ public static Task<bool> Flow<T>(
224
287
/// <param name="fps">Frames per second, default is 60.</param>
225
288
/// <param name="animationName">The animation identifier name, by default a new Guid is used.</param>
226
289
/// <returns>A task that completes when the animations ends.</returns>
227
- public static Task < bool > Flow (
290
+ public static Task < bool > Animate (
228
291
this View view , IEnumerable < Flow > flowCollection , VisualElement ? owner = null , uint duration = 500 ,
229
292
Easing ? easing = null , uint fps = 60 , string ? animationName = null )
230
293
{
@@ -249,4 +312,19 @@ public static Task<bool> Flow(
249
312
250
313
return taskCompletionSource . Task ;
251
314
}
315
+
316
+ private static bool IsNumber ( this object value )
317
+ {
318
+ return value is sbyte
319
+ or byte
320
+ or short
321
+ or ushort
322
+ or int
323
+ or uint
324
+ or long
325
+ or ulong
326
+ or float
327
+ or double
328
+ or decimal ;
329
+ }
252
330
}
0 commit comments