[gnome-clocks/wip/stopwatchlistbox] Factor out a widget to be used both in timer and stopwatch



commit 5b946781fd0c77c9aaff2a74d50334f1efa640b8
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Aug 12 21:54:39 2013 +0200

    Factor out a widget to be used both in timer and stopwatch

 data/css/gnome-clocks.css |   12 +++++
 src/stopwatch.vala        |   99 ++++-----------------------------------------
 src/widgets.vala          |   85 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 91 deletions(-)
---
diff --git a/data/css/gnome-clocks.css b/data/css/gnome-clocks.css
index 43f93f1..041fc97 100644
--- a/data/css/gnome-clocks.css
+++ b/data/css/gnome-clocks.css
@@ -189,3 +189,15 @@
     font-size: larger;
 }
 
+.clocks-round-frame.trough {
+    color: #babdb6;
+}
+
+.clocks-round-frame.progress {
+    color: #2e3436;
+}
+
+.clocks-round-frame.progress.inverted {
+    color: #eeeeec;
+}
+
diff --git a/src/stopwatch.vala b/src/stopwatch.vala
index acbc731..af30e04 100644
--- a/src/stopwatch.vala
+++ b/src/stopwatch.vala
@@ -19,24 +19,9 @@
 namespace Clocks {
 namespace Stopwatch {
 
-public class AnalogFrame : Gtk.Frame {
-    private const int RADIUS_PAD = 128;
-
+public class AnalogFrame : RoundFrame {
     private int seconds;
     private double millisecs;
-    private Cairo.Pattern cursor_pattern;
-
-    private int calculate_diameter () {
-        var child = get_child ();
-        if (child != null && child.visible) {
-            int w, h;
-            child.get_preferred_width (out w, null);
-            child.get_preferred_height (out h, null);
-            return RADIUS_PAD + (int) Math.sqrt (w * w + h * h);
-        } else {
-            return RADIUS_PAD;
-        }
-    }
 
     public void update (int s, double ms) {
         seconds = s;
@@ -47,65 +32,17 @@ public class AnalogFrame : Gtk.Frame {
         update (0, 0);
     }
 
-    public override void get_preferred_width (out int min_w, out int natural_w) {
-        var d = calculate_diameter ();
-        min_w = d;
-        natural_w = d;
-    }
-
-    public override void get_preferred_height (out int min_h, out int natural_h) {
-        var d = calculate_diameter ();
-        min_h = d;
-        natural_h = d;
-    }
-
-    public override void size_allocate (Gtk.Allocation allocation) {
-        set_allocation (allocation);
-        var child = get_child ();
-        if (child != null && child.visible) {
-            int w, h;
-            child.get_preferred_width (out w, null);
-            child.get_preferred_height (out h, null);
-
-            Gtk.Allocation child_allocation = {};
-            child_allocation.x = allocation.x + (allocation.width - w) / 2;
-            child_allocation.y = allocation.y + (allocation.height - h) / 2;
-            child_allocation.width = w;
-            child_allocation.height =  h;
-            child.size_allocate (child_allocation);
-        }
-    }
-
-    public override bool draw (Cairo.Context cr) {
+    public override void draw_progress (Cairo.Context cr, int center_x, int center_y, int radius) {
         var context = get_style_context ();
-        Gtk.Allocation allocation;
-        get_allocation (out allocation);
-        var center_x = allocation.width / 2;
-        var center_y = allocation.height / 2;
-
-        var radius = calculate_diameter () / 2;
-
-        cr.save ();
-        cr.move_to (center_x + radius, center_y);
 
         context.save ();
-        context.add_class ("clocks-timer-inner");
-        var color = context.get_background_color (Gtk.StateFlags.NORMAL);
-        Gdk.cairo_set_source_rgba (cr, color);
-        cr.arc (center_x, center_y, radius, 0, 2 * Math.PI);
-        cr.fill ();
-        context.restore ();
-
-        cr.set_line_width (1);
-        color = context.get_border_color (Gtk.StateFlags.SELECTED);
-        Gdk.cairo_set_source_rgba (cr, color);
-        cr.arc (center_x, center_y, radius, 0, 2 * Math.PI);
-        cr.stroke ();
+        context.add_class ("progress");
 
+        var color = context.get_color (Gtk.StateFlags.NORMAL);
         var progress = ((double) seconds + millisecs) / 60;
         if (progress > 0) {
             cr.set_line_width (10);
-            color = context.get_background_color (Gtk.StateFlags.SELECTED);
+            cr.set_line_cap  (Cairo.LineCap.ROUND);
             cr.arc (center_x, center_y, radius, 1.5  * Math.PI, (1.5 + progress * 2 ) * Math.PI);
             Gdk.cairo_set_source_rgba (cr, color);
             cr.stroke ();
@@ -116,36 +53,16 @@ public class AnalogFrame : Gtk.Frame {
             if (progress > 0) {
                 cr.set_line_width (2);
                 if (progress < (seconds + millisecs) / 60) {
-                    color = context.get_color (Gtk.StateFlags.SELECTED);
-                } else {
-                    color = context.get_background_color (Gtk.StateFlags.SELECTED);
+                    context.add_class ("inverted");
+                    color = context.get_color (Gtk.StateFlags.NORMAL);
                 }
                 cr.arc (center_x, center_y, radius, 1.5  * Math.PI, (1.5 + progress * 2 ) * Math.PI);
                 Gdk.cairo_set_source_rgba (cr, color);
                 cr.stroke ();
-
-                if (progress < 1) {
-                    if (cursor_pattern == null) {
-                        cursor_pattern = new Cairo.Pattern.radial (0, 0, 3, 0, 0, 9);
-                        cursor_pattern.add_color_stop_rgba (0, color.red, color.green, color.blue, 1);
-                        cursor_pattern.add_color_stop_rgba (0.5, color.red, color.green, color.blue, 0);
-                    }
-
-                    var x = center_x + (radius) * Math.cos((1.5 + progress * 2) * Math.PI);
-                    var y = center_y + (radius) * Math.sin((1.5 + progress * 2) * Math.PI);
-
-                    var cursor_radius = 9;
-                    cr.arc (x, y, cursor_radius, 0, 2 * Math.PI);
-                    cr.translate (x, y);
-                    cr.set_source (cursor_pattern);
-                    cr.fill ();
-                }
             }
         }
 
-        cr.restore ();
-
-        return base.draw(cr);
+        context.restore ();
     }
 }
 
diff --git a/src/widgets.vala b/src/widgets.vala
index aabbcd1..a93c4ef 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -618,4 +618,89 @@ public class AmPmToggleButton : Gtk.Button {
     }
 }
 
+public class RoundFrame : Gtk.Frame {
+    private const int RADIUS_PAD = 64;
+
+    private int calculate_diameter () {
+        int ret = 2 * RADIUS_PAD;
+        var child = get_child ();
+        if (child != null && child.visible) {
+            int w, h;
+            child.get_preferred_width (out w, null);
+            child.get_preferred_height (out h, null);
+            ret += (int) Math.sqrt (w * w + h * h);
+        }
+
+        return ret;
+    }
+
+    public override void get_preferred_width (out int min_w, out int natural_w) {
+        var d = calculate_diameter ();
+        min_w = d;
+        natural_w = d;
+    }
+
+    public override void get_preferred_height (out int min_h, out int natural_h) {
+        var d = calculate_diameter ();
+        min_h = d;
+        natural_h = d;
+    }
+
+    public override void size_allocate (Gtk.Allocation allocation) {
+        set_allocation (allocation);
+        var child = get_child ();
+        if (child != null && child.visible) {
+            int w, h;
+            child.get_preferred_width (out w, null);
+            child.get_preferred_height (out h, null);
+
+            Gtk.Allocation child_allocation = {};
+            child_allocation.x = allocation.x + (allocation.width - w) / 2;
+            child_allocation.y = allocation.y + (allocation.height - h) / 2;
+            child_allocation.width = w;
+            child_allocation.height =  h;
+            child.size_allocate (child_allocation);
+        }
+    }
+
+    public override bool draw (Cairo.Context cr) {
+        var context = get_style_context ();
+
+        Gtk.Allocation allocation;
+        get_allocation (out allocation);
+        var center_x = allocation.width / 2;
+        var center_y = allocation.height / 2;
+
+        var radius = calculate_diameter () / 2;
+
+        cr.save ();
+        cr.move_to (center_x + radius, center_y);
+
+        context.save ();
+        context.add_class ("clocks-round-frame");
+
+        context.save ();
+        context.add_class (Gtk.STYLE_CLASS_TROUGH);
+
+        var color = context.get_color (Gtk.StateFlags.NORMAL);
+
+        cr.set_line_width (10);
+        Gdk.cairo_set_source_rgba (cr, color);
+        cr.arc (center_x, center_y, radius, 0, 2 * Math.PI);
+        cr.stroke ();
+
+        context.restore ();
+
+        draw_progress (cr, center_x, center_y, radius);
+
+        context.restore ();
+        cr.restore ();
+
+        return base.draw(cr);
+    }
+
+    public virtual void draw_progress (Cairo.Context cr, int center_x, int center_y, int radius) {
+    }
+}
+
 } // namespace Clocks


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]