~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/SpinnerBackend.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Linq;
 
4
using System.Text;
 
5
using Xwt.Backends;
 
6
using System.Windows.Media;
 
7
using System.Windows.Media.Animation;
 
8
 
 
9
using WpfRectangle = System.Windows.Shapes.Rectangle;
 
10
using WpfCanvas = System.Windows.Controls.Canvas;
 
11
using System.Windows;
 
12
 
 
13
namespace Xwt.WPFBackend
 
14
{
 
15
        public class SpinnerBackend : WidgetBackend, ISpinnerBackend
 
16
        {
 
17
                new WpfSpinButton Widget {
 
18
                        get { return (WpfSpinButton) base.Widget; }
 
19
                        set { base.Widget = value; }
 
20
                }
 
21
                public SpinnerBackend ()
 
22
                {
 
23
                        Widget = new WpfSpinButton ();
 
24
                }
 
25
 
 
26
                public bool IsAnimating {
 
27
                        get { return Widget.Storyboard.GetIsPaused (); }
 
28
                }
 
29
                
 
30
                public void StartAnimation ()
 
31
                {
 
32
                        Widget.Storyboard.Begin ();
 
33
                }
 
34
 
 
35
                public void StopAnimation ()
 
36
                {
 
37
                        Widget.Storyboard.Stop ();
 
38
                }
 
39
        }
 
40
 
 
41
        public class WpfSpinButton : System.Windows.Controls.Canvas, IWpfWidget
 
42
        {
 
43
                const int Duration = 1000;
 
44
                static int[] StartTimes = new[] {
 
45
                        (int)(0.00 * Duration),
 
46
                        (int)(0.70 * Duration),
 
47
                        (int)(0.75 * Duration),
 
48
                        (int)(0.99 * Duration)
 
49
                };
 
50
 
 
51
                static double[] Values = new[] {
 
52
                        0.25,
 
53
                        0.25,
 
54
                        1,
 
55
                        0.25
 
56
                };
 
57
 
 
58
                public Storyboard Storyboard {
 
59
                        get; private set;
 
60
                }
 
61
                
 
62
                public WidgetBackend Backend {
 
63
                        get; set;
 
64
                }
 
65
 
 
66
                public WpfSpinButton ()
 
67
                {
 
68
                        Width = 25;
 
69
                        Height = 25;
 
70
                        Background = new SolidColorBrush (Colors.Transparent);
 
71
                        Storyboard = new Storyboard { RepeatBehavior = RepeatBehavior.Forever, Duration = TimeSpan.FromMilliseconds (Duration) };
 
72
 
 
73
                        for (int i = 0; i < 360; i += 30) {
 
74
                                // Create the rectangle and centre it in our widget
 
75
                                var rect = new WpfRectangle { Width = 2, Height = 8, Fill = new SolidColorBrush (Colors.Black), RadiusX = 1, RadiusY = 1, Opacity = Values[0] };
 
76
                                WpfCanvas.SetTop (rect, (Height - rect.Height) / 2);
 
77
                                WpfCanvas.SetLeft (rect, Width / 2);
 
78
 
 
79
                                // Rotate the element by 'i' degrees, creating a circle out of all the elements
 
80
                                var group = new TransformGroup ();
 
81
                                group.Children.Add (new RotateTransform (i, 0.5, -6));
 
82
                                group.Children.Add (new TranslateTransform (0, 10));
 
83
                                rect.RenderTransform = group;
 
84
 
 
85
                                // Set the animation
 
86
                                var timeline = new DoubleAnimationUsingKeyFrames ();
 
87
                                Storyboard.SetTarget (timeline, rect);
 
88
                                Storyboard.SetTargetProperty (timeline, new PropertyPath ("Opacity"));
 
89
 
 
90
                                var offset = Duration * (i / 360.0);
 
91
                                for (int j = 0; j < StartTimes.Length; j++) {
 
92
                                        var start = (StartTimes[j] + offset) % Duration;
 
93
                                        timeline.KeyFrames.Add (new EasingDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan (TimeSpan.FromMilliseconds (start)), Value = Values[j] });
 
94
                                }
 
95
                                Storyboard.Children.Add (timeline);
 
96
                                Children.Add (rect);
 
97
                        }
 
98
                }
 
99
 
 
100
                protected override System.Windows.Size MeasureOverride (System.Windows.Size constraint)
 
101
                {
 
102
                        var s = base.MeasureOverride (constraint);
 
103
                        return Backend.MeasureOverride (constraint, s);
 
104
                }
 
105
        }
 
106
}