2
using System.Collections.Generic;
6
using System.Windows.Media;
7
using System.Windows.Media.Animation;
9
using WpfRectangle = System.Windows.Shapes.Rectangle;
10
using WpfCanvas = System.Windows.Controls.Canvas;
13
namespace Xwt.WPFBackend
15
public class SpinnerBackend : WidgetBackend, ISpinnerBackend
17
new WpfSpinButton Widget {
18
get { return (WpfSpinButton) base.Widget; }
19
set { base.Widget = value; }
21
public SpinnerBackend ()
23
Widget = new WpfSpinButton ();
26
public bool IsAnimating {
27
get { return Widget.Storyboard.GetIsPaused (); }
30
public void StartAnimation ()
32
Widget.Storyboard.Begin ();
35
public void StopAnimation ()
37
Widget.Storyboard.Stop ();
41
public class WpfSpinButton : System.Windows.Controls.Canvas, IWpfWidget
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)
51
static double[] Values = new[] {
58
public Storyboard Storyboard {
62
public WidgetBackend Backend {
66
public WpfSpinButton ()
70
Background = new SolidColorBrush (Colors.Transparent);
71
Storyboard = new Storyboard { RepeatBehavior = RepeatBehavior.Forever, Duration = TimeSpan.FromMilliseconds (Duration) };
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);
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;
86
var timeline = new DoubleAnimationUsingKeyFrames ();
87
Storyboard.SetTarget (timeline, rect);
88
Storyboard.SetTargetProperty (timeline, new PropertyPath ("Opacity"));
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] });
95
Storyboard.Children.Add (timeline);
100
protected override System.Windows.Size MeasureOverride (System.Windows.Size constraint)
102
var s = base.MeasureOverride (constraint);
103
return Backend.MeasureOverride (constraint, s);