windows spinner: cleanup

This commit is contained in:
Hylke Bons 2012-03-15 23:40:10 +00:00
parent 85bec2210a
commit b34ca17bf1

View file

@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Timers;
using System.Windows;
@ -21,39 +22,28 @@ using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace SparkleShare {
// TODO: cleanup
// This is a close implementation of GtkSpinner
public class SparkleSpinner : Image {
public bool Active;
private Image [] Images;
private Timer Timer;
private int CycleDuration;
private int CurrentStep;
private int NumSteps;
private int Size;
private Image [] images;
private Timer timer;
private int num_steps;
private int current_step = 0;
public SparkleSpinner (int size) : base ()
{
Width = size;
{
Width = size;
Height = size;
Size = size;
CycleDuration = 400;
CurrentStep = 0;
BitmapSource spinner_gallery =
SparkleUIHelpers.GetImageSource ("process-working-22");
int frames_in_width = spinner_gallery.PixelWidth / size;
int frames_in_height = spinner_gallery.PixelHeight / size;
BitmapSource spinner_gallery = SparkleUIHelpers.GetImageSource ("process-working-22");
int frames_in_width = spinner_gallery.PixelWidth / Size;
int frames_in_height = spinner_gallery.PixelHeight / Size;
NumSteps = frames_in_width * frames_in_height;
Images = new Image [NumSteps - 1];
this.num_steps = (frames_in_width * frames_in_height) - 1;
this.images = new Image [this.num_steps];
int i = 0;
@ -62,61 +52,57 @@ namespace SparkleShare {
if (!(y == 0 && x == 0)) {
CroppedBitmap crop = new CroppedBitmap (
spinner_gallery,
new Int32Rect (x*Size, y*Size, Size, Size));
Images [i] = new Image ();
Images [i].Source = crop;
new Int32Rect (size * x, size * y, size, size)
);
this.images [i] = new Image ();
this.images [i].Source = crop;
i++;
}
}
}
Timer = new Timer () {
Interval = CycleDuration / NumSteps
this.timer = new Timer () {
Interval = 400 / this.num_steps
};
Timer.Elapsed += delegate {
NextImage ();
this.timer.Elapsed += delegate {
Dispatcher.Invoke ((Action) delegate {
NextImage ();
});
};
Start ();
}
private void NextImage ()
{
if (CurrentStep < NumSteps - 2)
CurrentStep++;
else
CurrentStep = 0;
Dispatcher.Invoke ((Action)delegate { SetImage (); });
}
private void SetImage ()
{
Source = Images [CurrentStep].Source;
}
public bool IsActive ()
{
return Active;
}
public void Start ()
{
CurrentStep = 0;
Active = true;
Timer.Start ();
this.timer.Start ();
}
public void Stop ()
{
Active = false;
Timer.Stop ();
this.timer.Stop ();
this.current_step = 0;
}
private void NextImage ()
{
if (this.current_step < this.num_steps - 1)
this.current_step++;
else
this.current_step = 0;
SetImage ();
}
private void SetImage ()
{
Source = this.images [this.current_step].Source;
}
}
}