annotate Implab.Fx/AnimationHelpers.cs @ 203:4d9830a9bbb8 v2

Added 'Fail' method to RunnableComponent which allows component to move from Running to Failed state. Added PollingComponent a timer based runnable component More tests Added FailPromise a thin class to wrap exceptions Fixed error handling in SuccessPromise classes.
author cin
date Tue, 18 Oct 2016 17:49:54 +0300
parents d4e38929ce36
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
1 using System;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
2 using System.Collections.Generic;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
3 using System.Linq;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
4 using System.Text;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
5 using System.Windows.Forms;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
6 using System.Diagnostics;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
7
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
8 namespace Implab.Fx
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
9 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
10 public static class AnimationHelpers
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
11 {
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
12 public static Animation<TTarget> AnimateProperty<TTarget, TVal>(this Animation<TTarget> animation, Action<TTarget, TVal> setter, Func<TTarget, TVal> getter, TVal newValue, Func<TVal, TVal, int, int, TVal> fx) where TTarget : class
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
13 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
14 if (animation == null)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
15 throw new ArgumentNullException("animation");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
16
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
17 TVal oldValue = getter(animation.Traget);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
18
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
19 animation.Step += (target, elaped, duration) =>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
20 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
21 var value = fx(oldValue, newValue, elaped, duration);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
22 setter(target, value);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
23 };
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
24
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
25 return animation;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
26 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
27
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
28 public static Animation<T> AnimateTransparency<T>(this T ctl, float newValue) where T : Form
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
29 {
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
30 var anim = new Animation<T>(ctl);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
31
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
32 anim.AnimateProperty(
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
33 (target, value) => target.Opacity = value,
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
34 target => target.Opacity,
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
35 newValue,
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
36 (ov, nv, el, du) => ov + ((float)el / du) * (nv - ov)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
37 );
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
38 return anim;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
39 }
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
40
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 5
diff changeset
41 public static IPromise<T> CloseFadeOut<T>(this T ctl) where T : Form
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
42 {
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
43 var anim = ctl.AnimateTransparency(0);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
44
106
d4e38929ce36 promises refactoring
cin
parents: 30
diff changeset
45 return anim
d4e38929ce36 promises refactoring
cin
parents: 30
diff changeset
46 .Play()
d4e38929ce36 promises refactoring
cin
parents: 30
diff changeset
47 .DispatchToControl(ctl)
d4e38929ce36 promises refactoring
cin
parents: 30
diff changeset
48 .Then(frm => {
d4e38929ce36 promises refactoring
cin
parents: 30
diff changeset
49 frm.Close();
d4e38929ce36 promises refactoring
cin
parents: 30
diff changeset
50 return frm;
d4e38929ce36 promises refactoring
cin
parents: 30
diff changeset
51 });
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
52 }
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
53
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 5
diff changeset
54 public static IPromise<T> OverlayFadeIn<T>(this Form that, T overlay) where T : Form
5
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
55 {
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
56 if (that == null)
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
57 throw new ArgumentNullException("that");
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
58 if (overlay == null)
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
59 throw new ArgumentNullException("overlay");
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
60
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
61 // setup overlay
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
62 overlay.Opacity = 0;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
63 overlay.FormBorderStyle = FormBorderStyle.None;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
64 overlay.ShowInTaskbar = false;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
65
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
66 that.AddOwnedForm(overlay);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
67
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
68 EventHandler handler = (object sender, EventArgs args) =>
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
69 {
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
70 overlay.Bounds = that.RectangleToScreen(that.ClientRectangle);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
71 };
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
72
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
73 // attach handlers
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
74 that.Move += handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
75 that.Resize += handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
76 that.Shown += handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
77
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
78 // remove handlers to release overlay
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
79 overlay.FormClosed += (sender, args) =>
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
80 {
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
81 that.Move -= handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
82 that.Resize -= handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
83 that.Shown -= handler;
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
84 };
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
85
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
86 overlay.Show(that);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
87 overlay.Bounds = that.RectangleToScreen(that.ClientRectangle);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
88
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
89 return overlay
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
90 .AnimateTransparency(1)
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
91 .Play()
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
92 .DispatchToControl(overlay);
f2559580b481 implemented overlay helpers and some animation heplers improvments
cin
parents: 4
diff changeset
93 }
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
94 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
95 }