3d5e59d8c7ab7c4e209de97ee71b366fe5987a65
[WebThing.git] / plugins / LoadProgress.cs
1 using System;
2 using Gtk;
3 using WebKit;
4 using bytex64.WebThing;
5
6 public class LoadThrobber : Gtk.DrawingArea {
7         public enum Mode {
8                 LoadStarted,
9                 LoadInProgress,
10                 LoadFinished
11         };
12         public Mode LoadState;
13         int r;
14         uint idletimer;
15
16         public LoadThrobber(WebThing wt) {
17                 LoadState = Mode.LoadStarted;
18
19                 wt.WebView.LoadStarted += WebView_LoadStarted;
20                 wt.WebView.LoadCommitted += WebView_LoadCommitted;
21                 wt.WebView.LoadProgressChanged += WebView_LoadProgressChanged;
22                 wt.WebView.LoadFinished += WebView_LoadFinished;
23
24                 SetSizeRequest(64, 64);
25         }
26
27         protected override bool OnExposeEvent(Gdk.EventExpose e) {
28                 Gdk.Window w = e.Window;
29                 Gdk.GC gc = new Gdk.GC(w);
30                 int width, height;
31                 w.GetSize(out width, out height);
32
33                 gc.Foreground = new Gdk.Color(0, 0, 0);
34                 gc.SetLineAttributes(3, Gdk.LineStyle.Solid, Gdk.CapStyle.Butt, Gdk.JoinStyle.Round);
35                 w.Clear();
36
37                 switch(LoadState) {
38                 case Mode.LoadStarted:
39                         w.DrawArc(gc, false, 4, 4, width-8, width-8, -r*64, 90*64);
40                         w.DrawArc(gc, false, 4, 4, width-8, width-8, -r*64 + 180*64, 90*64);
41                         r += 5;
42                         break;
43                 case Mode.LoadInProgress:
44                 case Mode.LoadFinished:
45                         w.DrawArc(gc, false, 4, 4, width-8, width-8, 90*64, -r*64);
46                         break;
47                 }
48                 return true;
49         }
50
51         void WebView_LoadStarted(object o, LoadStartedArgs e) {
52                 this.Show();
53                 Console.WriteLine("Loading Started");
54                 LoadState = Mode.LoadStarted;
55                 idletimer = GLib.Timeout.Add(100, delegate {
56                         QueueDraw();
57                         return true;
58                 });
59         }
60
61         void WebView_LoadCommitted(object o, LoadCommittedArgs e) {
62                 Console.WriteLine("Loading Committed");
63                 LoadState = Mode.LoadInProgress;
64                 GLib.Source.Remove(idletimer);
65                 r = 0;
66                 QueueDraw();
67         }
68
69         void WebView_LoadProgressChanged(object o, LoadProgressChangedArgs e) {
70                 Console.WriteLine("Loading Progress: {0}", e.Progress);
71                 r = (int) ((e.Progress / 100.0) * 360);
72                 QueueDraw();
73         }
74
75         void WebView_LoadFinished(object o, LoadFinishedArgs e) {
76                 Console.WriteLine("Loading Finished");
77                 LoadState = Mode.LoadFinished;
78                 this.Hide();
79         }
80 }
81
82 public class Plugin {
83         public Plugin(WebThing wt) {
84                 LoadThrobber lt = new LoadThrobber(wt);
85                 wt.AttachWidget(lt, CompassDirection.Interior);
86         }
87 }