[gnome-clocks/wip/analogtimer] Implement size allocation for the timer



commit c538d23d6338664425a2869b04793ea5718abf18
Author: Paolo Borelli <pborelli gnome org>
Date:   Thu Aug 1 15:41:06 2013 +0200

    Implement size allocation for the timer
    
    We want the analog timer to always the circumscript circonference

 src/timer.vala |   51 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 47 insertions(+), 4 deletions(-)
---
diff --git a/src/timer.vala b/src/timer.vala
index dd9a84e..62d324d 100644
--- a/src/timer.vala
+++ b/src/timer.vala
@@ -20,8 +20,51 @@ namespace Clocks {
 namespace Timer {
 
 public class CountdownFrame : Gtk.Frame {
+    private const int RADIUS_PAD = 36;
+
     public double progress { get; set; }
 
+    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 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) {
         base.draw(cr);
 
@@ -32,26 +75,26 @@ public class CountdownFrame : Gtk.Frame {
         var center_x = allocation.width / 2;
         var center_y = allocation.height / 2;
 
-        var radius = int.min (allocation.width, allocation.height) / 3;
+        var radius = calculate_diameter () / 2;
         cr.move_to (center_x + radius, center_y);
 
         cr.set_line_width (1);
         var 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.arc (center_x, center_y, radius + 9, 0, 2 * Math.PI);
+        cr.arc (center_x, center_y, radius - 9, 0, 2 * Math.PI);
         cr.stroke ();
 
         cr.set_line_width (8);
 
         color = context.get_color (Gtk.StateFlags.SELECTED);
-        cr.arc (center_x, center_y, radius + 4.5, 0, 2 * Math.PI);
+        cr.arc (center_x, center_y, radius - 4.5, 0, 2 * Math.PI);
         Gdk.cairo_set_source_rgba (cr, color);
         cr.stroke ();
 
         if (progress > 0) {
             color = context.get_background_color (Gtk.StateFlags.SELECTED);
-            cr.arc (center_x, center_y, radius + 4.5, 1.5  * Math.PI, (1.5 + progress * 2 ) * Math.PI);
+            cr.arc (center_x, center_y, radius - 4.5, 1.5  * Math.PI, (1.5 + progress * 2 ) * Math.PI);
             Gdk.cairo_set_source_rgba (cr, color);
             cr.stroke ();
         }


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