diff --git a/.hgignore b/.hgignore --- a/.hgignore +++ b/.hgignore @@ -7,3 +7,4 @@ Implab/bin/ Implab/obj/ TestResults/ Implab.Fx/obj/ +Implab.Fx/bin/ diff --git a/Implab.Fx/Animation.cs b/Implab.Fx/Animation.cs --- a/Implab.Fx/Animation.cs +++ b/Implab.Fx/Animation.cs @@ -2,14 +2,96 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Timers; +using System.ComponentModel; +using System.Diagnostics; namespace Implab.Fx { - public class Animation + public delegate void AnimationStep(T target, int elapsed, int duration); + + public class Animation where TArg: class { int m_duration; - int m_fps; + int m_delay; + int m_elapsed; + int m_prevTicks; + TArg m_arg; + ISynchronizeInvoke m_syncronizationObject; + + public event AnimationStep Step; + + Promise m_promise; + + public Animation(TArg target, int duration, int delay) + { + if (duration <= 0) + throw new ArgumentOutOfRangeException("duration"); + if (delay <= 0) + throw new ArgumentOutOfRangeException("delay"); + + m_arg = target; + m_syncronizationObject = target as ISynchronizeInvoke; + m_duration = duration; + m_delay = delay; + m_promise = new Promise(); + } + + public Animation(TArg target) + : this(target, 500, 30) + { + } + + public TArg Traget + { + get { return m_arg; } + } + public Promise Play() + { + var timer = new Timer(m_delay); + + timer.AutoReset = true; + timer.SynchronizingObject = m_syncronizationObject; + timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); + m_prevTicks = Environment.TickCount; + + timer.Start(); + + return m_promise; + } + + void timer_Elapsed(object sender, ElapsedEventArgs args) + { + var timer = sender as Timer; + + var dt = Environment.TickCount - m_prevTicks; + m_prevTicks = Environment.TickCount; + + m_elapsed += dt; + + if (m_elapsed > m_duration) + m_elapsed = m_duration; + + try + { + var handler = Step; + if (handler != null) + handler(m_arg, m_elapsed, m_duration); + } + catch (Exception e) + { + Trace.TraceError(e.ToString()); + } + + if (m_elapsed < m_duration) + timer.Start(); + else + { + timer.Dispose(); + m_promise.Resolve(m_arg); + } + } } } diff --git a/Implab.Fx/AnimationHelpers.cs b/Implab.Fx/AnimationHelpers.cs new file mode 100644 --- /dev/null +++ b/Implab.Fx/AnimationHelpers.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using System.Diagnostics; + +namespace Implab.Fx +{ + public static class AnimationHelpers + { + public static Animation AnimateProperty(this Animation animation, Action setter, Func getter, TVal newValue, Func fx) where TTarget: class + { + if (animation == null) + throw new ArgumentNullException("animation"); + + TVal oldValue = getter(animation.Traget); + + animation.Step += (target, elaped, duration) => + { + var value = fx(oldValue, newValue, elaped, duration); + setter(target, value); + }; + + return animation; + } + + public static Animation
AnimateTransparency(this Form ctl, float newValue) + { + var anim = new Animation(ctl); + + anim.AnimateProperty( + (target, value) => target.Opacity = value, + target => target.Opacity, + newValue, + (ov, nv, el, du) => ov + ((float)el / du) * (nv - ov) + ); + return anim; + } + } +} diff --git a/Implab.Fx/ControlHelpers.cs b/Implab.Fx/ControlHelpers.cs deleted file mode 100644 --- a/Implab.Fx/ControlHelpers.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; - -namespace Implab.Fx -{ - public static class ControlHelpers - { - /// - /// Переключает обработку обещания в поток указанного элемента управления. - /// - /// Тип результата обещания - /// Исходное обещание - /// Элемент управления - /// Новое обещание, обработчики которого будут выполнены в потоке элемента управления. - /// Параметр не может быть null. - /// - /// client - /// .Get("description.txt") // returns a promise - /// .DirectToControl(m_ctl) // handle the promise in the thread of the control - /// .Then( - /// description => m_ctl.Text = description // now it's safe - /// ) - /// - public static Promise DirectToControl(this Promise that, Control ctl) - { - if (that == null) - throw new ArgumentNullException("that"); - if (ctl == null) - throw new ArgumentNullException("ctl"); - - var directed = new Promise(); - - that.Then( - res => - { - if (ctl.InvokeRequired) - ctl.Invoke(new Action(directed.Resolve),res); - else - directed.Resolve(res); - }, - err => - { - if (ctl.InvokeRequired) - ctl.Invoke(new Action(directed.Reject), err); - else - directed.Reject(err); - } - ); - - return directed; - } - } -} diff --git a/Implab.Fx/Implab.Fx.csproj b/Implab.Fx/Implab.Fx.csproj --- a/Implab.Fx/Implab.Fx.csproj +++ b/Implab.Fx/Implab.Fx.csproj @@ -41,7 +41,9 @@ - + + + diff --git a/Implab.Fx/PromiseHelpers.cs b/Implab.Fx/PromiseHelpers.cs new file mode 100644 --- /dev/null +++ b/Implab.Fx/PromiseHelpers.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using System.Threading; + +namespace Implab.Fx +{ + public static class PromiseHelpers + { + /// + /// Перенаправляет обработку обещания в поток указанного элемента управления. + /// + /// Тип результата обещания + /// Исходное обещание + /// Элемент управления + /// Новое обещание, обработчики которого будут выполнены в потоке элемента управления. + /// Параметр не может быть null. + /// + /// client + /// .Get("description.txt") // returns a promise + /// .DirectToControl(m_ctl) // handle the promise in the thread of the control + /// .Then( + /// description => m_ctl.Text = description // now it's safe + /// ) + /// + public static Promise DispatchToControl(this Promise that, Control ctl) + { + if (that == null) + throw new ArgumentNullException("that"); + if (ctl == null) + throw new ArgumentNullException("ctl"); + + var directed = new Promise(); + + that.Then( + res => + { + if (ctl.InvokeRequired) + ctl.Invoke(new Action(directed.Resolve), res); + else + directed.Resolve(res); + }, + err => + { + if (ctl.InvokeRequired) + ctl.Invoke(new Action(directed.Reject), err); + else + directed.Reject(err); + } + ); + + return directed; + } + + /// + /// Направляет обработку обещания в текущий поток, если у него существует контекст синхронизации. + /// + /// Тип результата обещания. + /// Обещание которое нужно обработать в текущем потоке. + /// Перенаправленное обещание. + public static Promise DispatchToCurrentThread(this Promise that) + { + var sync = SynchronizationContext.Current; + if (sync == null) + throw new InvalidOperationException("The current thread doesn't have a syncronization context"); + return DispatchToSyncContext(that, sync); + } + + /// + /// Направляет обработку обещания в указанный контекст синхронизации. + /// + /// Тип результата обещания. + /// Обещание, которое требуется обработать в указанном контексте синхронизации. + /// Контекст синхронизации в который будет направлено обещание. + /// Новое обещание, которое будет обрабатываться в указанном контексте. + public static Promise DispatchToSyncContext(this Promise that, SynchronizationContext sync) + { + if (that == null) + throw new ArgumentNullException("that"); + if (sync == null) + throw new ArgumentNullException("sync"); + + var d = new Promise(); + + that.Then( + res => sync.Post(state => d.Resolve(res), null), + err => sync.Post(state => d.Reject(err), null) + ); + + return d; + } + } +} diff --git a/Implab.Test/AsyncTests.cs b/Implab.Test/AsyncTests.cs --- a/Implab.Test/AsyncTests.cs +++ b/Implab.Test/AsyncTests.cs @@ -1,101 +1,101 @@ -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Implab; -using System.Reflection; -using System.Threading; - -namespace Implab.Tests -{ - [TestClass] - public class AsyncTests - { - [TestMethod] - public void ResolveTest () - { - int res = -1; - var p = new Promise (); - p.Then (x => res = x); - p.Resolve (100); - - Assert.AreEqual (res, 100); +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Implab; +using System.Reflection; +using System.Threading; + +namespace Implab.Tests +{ + [TestClass] + public class AsyncTests + { + [TestMethod] + public void ResolveTest () + { + int res = -1; + var p = new Promise (); + p.Then (x => res = x); + p.Resolve (100); + + Assert.AreEqual (res, 100); } - [TestMethod] - public void RejectTest () - { - int res = -1; - Exception err = null; - - var p = new Promise (); - p.Then (x => res = x, e => err = e); - p.Reject (new ApplicationException ("error")); - - Assert.AreEqual (res, -1); - Assert.AreEqual (err.Message, "error"); - + [TestMethod] + public void RejectTest () + { + int res = -1; + Exception err = null; + + var p = new Promise (); + p.Then (x => res = x, e => err = e); + p.Reject (new ApplicationException ("error")); + + Assert.AreEqual (res, -1); + Assert.AreEqual (err.Message, "error"); + } - [TestMethod] - public void JoinSuccessTest () - { - var p = new Promise (); - p.Resolve (100); - Assert.AreEqual (p.Join (), 100); + [TestMethod] + public void JoinSuccessTest () + { + var p = new Promise (); + p.Resolve (100); + Assert.AreEqual (p.Join (), 100); } - [TestMethod] - public void JoinFailTest () - { - var p = new Promise (); - p.Reject (new ApplicationException ("failed")); - - try { - p.Join (); - throw new ApplicationException ("WRONG!"); - } catch (TargetInvocationException err) { - Assert.AreEqual (err.InnerException.Message, "failed"); - } catch { - Assert.Fail ("Got wrong excaption"); - } + [TestMethod] + public void JoinFailTest () + { + var p = new Promise (); + p.Reject (new ApplicationException ("failed")); + + try { + p.Join (); + throw new ApplicationException ("WRONG!"); + } catch (TargetInvocationException err) { + Assert.AreEqual (err.InnerException.Message, "failed"); + } catch { + Assert.Fail ("Got wrong excaption"); + } } - [TestMethod] - public void MapTest () - { - var p = new Promise (); - - var p2 = p.Map (x => x.ToString ()); - p.Resolve (100); - - Assert.AreEqual (p2.Join (), "100"); + [TestMethod] + public void MapTest () + { + var p = new Promise (); + + var p2 = p.Map (x => x.ToString ()); + p.Resolve (100); + + Assert.AreEqual (p2.Join (), "100"); } - [TestMethod] - public void ChainTest () - { - var p1 = new Promise (); - - var p3 = p1.Chain (x => { - var p2 = new Promise (); - p2.Resolve (x.ToString ()); - return p2; - }); - - p1.Resolve (100); - - Assert.AreEqual (p3.Join (), "100"); + [TestMethod] + public void ChainTest () + { + var p1 = new Promise (); + + var p3 = p1.Chain (x => { + var p2 = new Promise (); + p2.Resolve (x.ToString ()); + return p2; + }); + + p1.Resolve (100); + + Assert.AreEqual (p3.Join (), "100"); } - [TestMethod] - public void PoolTest () - { - var pid = Thread.CurrentThread.ManagedThreadId; - var p = AsyncPool.Invoke (() => { - return Thread.CurrentThread.ManagedThreadId; - }); - - Assert.AreNotEqual (pid, p.Join ()); - } - } -} - + [TestMethod] + public void PoolTest () + { + var pid = Thread.CurrentThread.ManagedThreadId; + var p = AsyncPool.Invoke (() => { + return Thread.CurrentThread.ManagedThreadId; + }); + + Assert.AreNotEqual (pid, p.Join ()); + } + } +} + diff --git a/Implab.sln b/Implab.sln --- a/Implab.sln +++ b/Implab.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab", "Implab\Implab.csproj", "{99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab", "Implab\Implab.csproj", "{F550F1F8-8746-4AD0-9614-855F4C4B7F05}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CE8D8D18-437A-445C-B662-4C2CE79A76F6}" ProjectSection(SolutionItems) = preProject @@ -23,10 +23,10 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}.Release|Any CPU.Build.0 = Release|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.Build.0 = Debug|Any CPU {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/Implab.suo b/Implab.suo index 5ef9f84caa1609b501f07d62659a5c03f93635a0..203e41ed2a3711da61caa28207d1625fb6bf3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