[gnome-chess] Compute the width of the time labels at runtime



commit 6d4e61c79aec815123aa461263222ed14e9e5d3c
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Mar 23 23:36:35 2014 -0500

    Compute the width of the time labels at runtime
    
    Set the time labels to be a little bit wider than the widest they might
    ever be, so they never resize.
    
    This assumes that each player will be limited to at most 999 minutes,
    which currently is not the case, but will be soon.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=726955

 data/gnome-chess.ui  |    6 ++++--
 src/gnome-chess.vala |   41 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 3 deletions(-)
---
diff --git a/data/gnome-chess.ui b/data/gnome-chess.ui
index ddc786a..efd197f 100644
--- a/data/gnome-chess.ui
+++ b/data/gnome-chess.ui
@@ -278,7 +278,8 @@
                     <property name="homogeneous">True</property>
                     <child>
                       <object class="GtkDrawingArea" id="white_time_label">
-                        <property name="width_request">20</property>
+                        <!-- -1 means compute at runtime -->
+                        <property name="width_request">-1</property>
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
                         <signal name="draw" handler="white_time_draw_cb" swapped="no"/>
@@ -291,7 +292,8 @@
                     </child>
                     <child>
                       <object class="GtkDrawingArea" id="black_time_label">
-                        <property name="width_request">20</property>
+                        <!-- -1 means compute at runtime -->
+                        <property name="width_request">-1</property>
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
                         <signal name="draw" handler="black_time_draw_cb" swapped="no"/>
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index 6a460e3..791c5f9 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -1433,6 +1433,42 @@ public class Application : Gtk.Application
             return "∶%02d".printf (time);
     }
 
+    /*
+     * Compute the largest possible size the timer label might ever want to take.
+     * The size of the characters may vary by font, but one digit will always
+     * be the largest.
+     */
+    private int compute_time_label_width_request (Cairo.Context c)
+        ensures (result > 0)
+    {
+        Cairo.TextExtents extents;
+        double max = 0;
+
+        c.text_extents ("000∶00", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("111∶11", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("222∶22", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("333∶33", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("444∶44", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("555∶55", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("666∶66", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("777∶77", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("888∶88", out extents);
+        max = (max > extents.width ? max : extents.width);
+        c.text_extents ("999∶99", out extents);
+        max = (max > extents.width ? max : extents.width);
+
+        /* Leave a little bit of room to the sides. */
+        return (int) Math.ceil (max) + 6;
+    }
+
     private void draw_time (Gtk.Widget widget, Cairo.Context c, string text, double[] fg, double[] bg)
     {
         double alpha = 1.0;
@@ -1451,7 +1487,10 @@ public class Application : Gtk.Application
                    (widget.get_allocated_height () - extents.height) / 2 - extents.y_bearing);
         c.show_text (text);
 
-        widget.set_size_request ((int) extents.width + 6, -1);
+        int width;
+        widget.get_size_request (out width, null);
+        if (width == -1)
+            widget.set_size_request (compute_time_label_width_request (c), -1);
     }
 
     [CCode (cname = "G_MODULE_EXPORT history_combo_changed_cb", instance_pos = -1)]


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