[gnome-clocks] Use an ui template for the timer



commit 634d667d12b1a0ac303e27b8309191df7595bf4e
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Aug 18 11:32:56 2013 +0200

    Use an ui template for the timer

 data/ui/timer.ui |  422 ++++++++++++++++++++++++++++--------------------------
 src/timer.vala   |  104 ++++++--------
 2 files changed, 264 insertions(+), 262 deletions(-)
---
diff --git a/data/ui/timer.ui b/data/ui/timer.ui
index 2bdd71c..cd2a564 100644
--- a/data/ui/timer.ui
+++ b/data/ui/timer.ui
@@ -16,34 +16,166 @@
     <property name="step_increment">1</property>
     <property name="page_increment">10</property>
   </object>
-  <object class="ClocksAnalogFrame" id="setup_frame">
+  <template class="ClocksTimerMainPanel" parent="GtkStack">
     <property name="visible">True</property>
-    <property name="can_focus">False</property>
-    <property name="margin_left">48</property>
-    <property name="margin_right">48</property>
-    <property name="margin_top">48</property>
-    <property name="margin_bottom">48</property>
     <child>
-      <object class="GtkGrid" id="setup_grid">
+      <object class="ClocksAnalogFrame" id="setup_frame">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
-        <property name="halign">center</property>
-        <property name="valign">center</property>
-        <property name="hexpand">True</property>
-        <property name="vexpand">True</property>
-        <property name="row_spacing">24</property>
+        <property name="margin_left">48</property>
+        <property name="margin_right">48</property>
+        <property name="margin_top">48</property>
+        <property name="margin_bottom">48</property>
         <child>
-          <object class="GtkGrid" id="grid_spinbuttons">
+          <object class="GtkGrid" id="setup_grid">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
             <property name="halign">center</property>
             <property name="valign">center</property>
+            <property name="hexpand">True</property>
+            <property name="vexpand">True</property>
+            <property name="row_spacing">24</property>
             <child>
-              <object class="GtkLabel" id="dummy_label1">
+              <object class="GtkGrid" id="grid_spinbuttons">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="hexpand">True</property>
-                <property name="label"></property>
+                <property name="halign">center</property>
+                <property name="valign">center</property>
+                <child>
+                  <object class="GtkLabel" id="dummy_label1">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="hexpand">True</property>
+                    <property name="label"></property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">0</property>
+                    <property name="top_attach">0</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkSpinButton" id="h_spinbutton">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="invisible_char">●</property>
+                    <property name="xalign">0.5</property>
+                    <property name="invisible_char_set">True</property>
+                    <property name="input_purpose">number</property>
+                    <property name="orientation">vertical</property>
+                    <property name="adjustment">adjustment_hours</property>
+                    <property name="numeric">True</property>
+                    <property name="wrap">True</property>
+                    <signal name="output" handler="show_leading_zeros" swapped="no"/>
+                    <signal name="changed" handler="update_start_button" swapped="no"/>
+                    <style>
+                      <class name="clocks-spinbutton"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">0</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="colon_label1">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label">∶</property>
+                    <style>
+                      <class name="clocks-timer-label"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="left_attach">2</property>
+                    <property name="top_attach">0</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkSpinButton" id="m_spinbutton">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="invisible_char">●</property>
+                    <property name="xalign">0.5</property>
+                    <property name="invisible_char_set">True</property>
+                    <property name="input_purpose">number</property>
+                    <property name="orientation">vertical</property>
+                    <property name="adjustment">adjustment_minutes</property>
+                    <property name="numeric">True</property>
+                    <property name="wrap">True</property>
+                    <signal name="output" handler="show_leading_zeros" swapped="no"/>
+                    <signal name="changed" handler="update_start_button" swapped="no"/>
+                    <style>
+                      <class name="clocks-spinbutton"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="left_attach">3</property>
+                    <property name="top_attach">0</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="colon_label2">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label">∶</property>
+                    <style>
+                      <class name="clocks-timer-label"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="left_attach">4</property>
+                    <property name="top_attach">0</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkSpinButton" id="s_spinbutton">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="invisible_char">●</property>
+                    <property name="xalign">0.5</property>
+                    <property name="invisible_char_set">True</property>
+                    <property name="input_purpose">number</property>
+                    <property name="orientation">vertical</property>
+                    <property name="adjustment">adjustment_seconds</property>
+                    <property name="numeric">True</property>
+                    <property name="wrap">True</property>
+                    <signal name="output" handler="show_leading_zeros" swapped="no"/>
+                    <signal name="changed" handler="update_start_button" swapped="no"/>
+                    <style>
+                      <class name="clocks-spinbutton"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="left_attach">5</property>
+                    <property name="top_attach">0</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="dummy_label2">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="hexpand">True</property>
+                    <property name="label"></property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">6</property>
+                    <property name="top_attach">0</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
               </object>
               <packing>
                 <property name="left_attach">0</property>
@@ -53,234 +185,118 @@
               </packing>
             </child>
             <child>
-              <object class="GtkSpinButton" id="spinbutton_hours">
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="invisible_char">●</property>
-                <property name="xalign">0.5</property>
-                <property name="invisible_char_set">True</property>
-                <property name="input_purpose">number</property>
-                <property name="orientation">vertical</property>
-                <property name="adjustment">adjustment_hours</property>
-                <property name="numeric">True</property>
-                <property name="wrap">True</property>
-                <style>
-                  <class name="clocks-spinbutton"/>
-                </style>
-              </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">0</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkLabel" id="colon_label1">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="label">∶</property>
-                <style>
-                  <class name="clocks-timer-label"/>
-                </style>
-              </object>
-              <packing>
-                <property name="left_attach">2</property>
-                <property name="top_attach">0</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkSpinButton" id="spinbutton_minutes">
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="invisible_char">●</property>
-                <property name="xalign">0.5</property>
-                <property name="invisible_char_set">True</property>
-                <property name="input_purpose">number</property>
-                <property name="orientation">vertical</property>
-                <property name="adjustment">adjustment_minutes</property>
-                <property name="numeric">True</property>
-                <property name="wrap">True</property>
-                <style>
-                  <class name="clocks-spinbutton"/>
-                </style>
-              </object>
-              <packing>
-                <property name="left_attach">3</property>
-                <property name="top_attach">0</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkLabel" id="colon_label2">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="label">∶</property>
-                <style>
-                  <class name="clocks-timer-label"/>
-                </style>
-              </object>
-              <packing>
-                <property name="left_attach">4</property>
-                <property name="top_attach">0</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkSpinButton" id="spinbutton_seconds">
+              <object class="GtkButton" id="start_button">
+                <property name="label" translatable="yes">Start</property>
+                <property name="width_request">280</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <property name="invisible_char">●</property>
-                <property name="xalign">0.5</property>
-                <property name="invisible_char_set">True</property>
-                <property name="input_purpose">number</property>
-                <property name="orientation">vertical</property>
-                <property name="adjustment">adjustment_seconds</property>
-                <property name="numeric">True</property>
-                <property name="wrap">True</property>
+                <property name="receives_default">True</property>
+                <signal name="clicked" handler="on_start_button_clicked" swapped="no"/>
                 <style>
-                  <class name="clocks-spinbutton"/>
+                  <class name="clocks-button"/>
                 </style>
               </object>
               <packing>
-                <property name="left_attach">5</property>
-                <property name="top_attach">0</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkLabel" id="dummy_label2">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="hexpand">True</property>
-                <property name="label"></property>
-              </object>
-              <packing>
-                <property name="left_attach">6</property>
-                <property name="top_attach">0</property>
+                <property name="left_attach">0</property>
+                <property name="top_attach">1</property>
                 <property name="width">1</property>
                 <property name="height">1</property>
               </packing>
             </child>
           </object>
-          <packing>
-            <property name="left_attach">0</property>
-            <property name="top_attach">0</property>
-            <property name="width">1</property>
-            <property name="height">1</property>
-          </packing>
-        </child>
-        <child>
-          <object class="GtkButton" id="start_button">
-            <property name="label" translatable="yes">Start</property>
-            <property name="width_request">280</property>
-            <property name="visible">True</property>
-            <property name="can_focus">True</property>
-            <property name="receives_default">True</property>
-            <style>
-              <class name="clocks-button"/>
-            </style>
-          </object>
-          <packing>
-            <property name="left_attach">0</property>
-            <property name="top_attach">1</property>
-            <property name="width">1</property>
-            <property name="height">1</property>
-          </packing>
         </child>
       </object>
     </child>
-  </object>
-  <object class="ClocksTimerCountdownFrame" id="countdown_frame">
-    <property name="visible">True</property>
-    <property name="can_focus">False</property>
-    <property name="margin_left">48</property>
-    <property name="margin_right">48</property>
-    <property name="margin_top">48</property>
-    <property name="margin_bottom">48</property>
     <child>
-      <object class="GtkGrid" id="countdown_grid">
+      <object class="ClocksTimerCountdownFrame" id="countdown_frame">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
-        <property name="halign">center</property>
-        <property name="valign">center</property>
-        <property name="hexpand">True</property>
-        <property name="vexpand">True</property>
-        <property name="row_spacing">24</property>
+        <property name="margin_left">48</property>
+        <property name="margin_right">48</property>
+        <property name="margin_top">48</property>
+        <property name="margin_bottom">48</property>
         <child>
-          <object class="GtkLabel" id="countdown_label">
+          <object class="GtkGrid" id="countdown_grid">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
             <property name="halign">center</property>
             <property name="valign">center</property>
-            <style>
-              <class name="clocks-timer-label"/>
-            </style>
-          </object>
-          <packing>
-            <property name="left_attach">0</property>
-            <property name="top_attach">0</property>
-            <property name="width">1</property>
-            <property name="height">1</property>
-          </packing>
-        </child>
-        <child>
-          <object class="GtkBox" id="button_box">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
             <property name="hexpand">True</property>
-            <property name="homogeneous">True</property>
-            <property name="spacing">16</property>
+            <property name="vexpand">True</property>
+            <property name="row_spacing">24</property>
             <child>
-              <object class="GtkButton" id="left_button">
-                <property name="label" translatable="yes">Pause</property>
-                <property name="width_request">132</property>
+              <object class="GtkLabel" id="countdown_label">
                 <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="receives_default">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">center</property>
+                <property name="valign">center</property>
                 <style>
-                  <class name="clocks-button"/>
+                  <class name="clocks-timer-label"/>
                 </style>
               </object>
               <packing>
-                <property name="expand">True</property>
-                <property name="fill">True</property>
-                <property name="position">0</property>
+                <property name="left_attach">0</property>
+                <property name="top_attach">0</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
               </packing>
             </child>
             <child>
-              <object class="GtkButton" id="right_button">
-                <property name="label" translatable="yes">Reset</property>
-                <property name="width_request">132</property>
+              <object class="GtkBox" id="button_box">
                 <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="receives_default">True</property>
-                <style>
-                  <class name="clocks-button"/>
-                </style>
+                <property name="can_focus">False</property>
+                <property name="hexpand">True</property>
+                <property name="homogeneous">True</property>
+                <property name="spacing">16</property>
+                <child>
+                  <object class="GtkButton" id="left_button">
+                    <property name="label" translatable="yes">Pause</property>
+                    <property name="width_request">132</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">True</property>
+                    <signal name="clicked" handler="on_left_button_clicked" swapped="no"/>
+                    <style>
+                      <class name="clocks-button"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkButton" id="right_button">
+                    <property name="label" translatable="yes">Reset</property>
+                    <property name="width_request">132</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">True</property>
+                    <signal name="clicked" handler="on_right_button_clicked" swapped="no"/>
+                    <style>
+                      <class name="clocks-button"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
               </object>
               <packing>
-                <property name="expand">True</property>
-                <property name="fill">True</property>
-                <property name="position">1</property>
+                <property name="left_attach">0</property>
+                <property name="top_attach">1</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
               </packing>
             </child>
           </object>
-          <packing>
-            <property name="left_attach">0</property>
-            <property name="top_attach">1</property>
-            <property name="width">1</property>
-            <property name="height">1</property>
-          </packing>
         </child>
       </object>
     </child>
-  </object>
+  </template>
   <object class="GtkSizeGroup" id="sizegroup1">
     <widgets>
       <widget name="grid_spinbuttons"/>
diff --git a/src/timer.vala b/src/timer.vala
index 8dc3008..78c037b 100644
--- a/src/timer.vala
+++ b/src/timer.vala
@@ -64,6 +64,7 @@ public class CountdownFrame : AnalogFrame {
     }
 }
 
+[GtkTemplate (ui = "/org/gnome/clocks/ui/timer.ui")]
 public class MainPanel : Gtk.Stack, Clocks.Clock {
     enum State {
         STOPPED,
@@ -78,100 +79,56 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
     private State state;
     private GLib.Settings settings;
     private uint tick_id;
+    private double span;
+    private GLib.Timer timer;
     private Utils.Bell bell;
+    [GtkChild]
+    private AnalogFrame setup_frame;
+    [GtkChild]
     private Gtk.Grid grid_spinbuttons;
+    [GtkChild]
     private Gtk.SpinButton h_spinbutton;
+    [GtkChild]
     private Gtk.SpinButton m_spinbutton;
+    [GtkChild]
     private Gtk.SpinButton s_spinbutton;
+    [GtkChild]
     private Gtk.Button start_button;
-    private AnalogFrame setup_frame;
+    [GtkChild]
     private CountdownFrame countdown_frame;
+    [GtkChild]
     private Gtk.Label countdown_label;
+    [GtkChild]
     private Gtk.Button left_button;
-    private Gtk.Button right_button;
-    private double span;
-    private GLib.Timer timer;
 
     public MainPanel (HeaderBar header_bar) {
         Object (label: _("Timer"), header_bar: header_bar, transition_type: 
Gtk.StackTransitionType.CROSSFADE, panel_id: PanelId.TIMER);
 
         settings = new GLib.Settings ("org.gnome.clocks");
 
-        bell = new Utils.Bell ("complete", _("Time is up!"), _("Timer countdown finished"));
-
         tick_id = 0;
         span = 0;
         timer = new GLib.Timer ();
 
-        var builder = Utils.load_ui ("timer.ui");
-
-        setup_frame = builder.get_object ("setup_frame") as AnalogFrame;
-        grid_spinbuttons = builder.get_object ("grid_spinbuttons") as Gtk.Grid;
-        h_spinbutton = builder.get_object ("spinbutton_hours") as Gtk.SpinButton;
-        m_spinbutton = builder.get_object ("spinbutton_minutes") as Gtk.SpinButton;
-        s_spinbutton = builder.get_object ("spinbutton_seconds") as Gtk.SpinButton;
-        start_button = builder.get_object ("start_button") as Gtk.Button;
+        bell = new Utils.Bell ("complete", _("Time is up!"), _("Timer countdown finished"));
 
         // Force LTR since we do not want to reverse [hh] : [mm] : [ss]
         grid_spinbuttons.set_direction (Gtk.TextDirection.LTR);
 
-        h_spinbutton.output.connect (show_leading_zeros);
-        m_spinbutton.output.connect (show_leading_zeros);
-        s_spinbutton.output.connect (show_leading_zeros);
-
-        h_spinbutton.value_changed.connect (update_start_button);
-        m_spinbutton.value_changed.connect (update_start_button);
-        s_spinbutton.value_changed.connect (update_start_button);
-
-        start_button.clicked.connect (() => {
-            start ();
-        });
-
-        countdown_frame = builder.get_object ("countdown_frame") as CountdownFrame;
-        countdown_label = builder.get_object ("countdown_label") as Gtk.Label;
-        left_button = builder.get_object ("left_button") as Gtk.Button;
-        right_button = builder.get_object ("right_button") as Gtk.Button;
-
-        left_button.clicked.connect (() => {
-            switch (state) {
-            case State.RUNNING:
-                pause ();
-                left_button.set_label (_("Continue"));
-                left_button.get_style_context ().add_class ("clocks-go");
-                break;
-            case State.PAUSED:
-                restart ();
-                left_button.set_label (_("Pause"));
-                left_button.get_style_context ().remove_class("clocks-go");
-                break;
-            default:
-                assert_not_reached ();
-            }
-        });
-
-        right_button.clicked.connect (() => {
-            reset ();
-            left_button.set_label (_("Pause"));
-        });
-
-        add (setup_frame);
-        add (countdown_frame);
-
         reset ();
-
-        visible_child = setup_frame;
-        show_all ();
     }
 
     public virtual signal void ring () {
         bell.ring_once ();
     }
 
+    [GtkCallback]
     private bool show_leading_zeros (Gtk.SpinButton spin_button) {
         spin_button.set_text ("%02i".printf(spin_button.get_value_as_int ()));
         return true;
     }
 
+    [GtkCallback]
     private void update_start_button () {
         var h = h_spinbutton.get_value_as_int ();
         var m = m_spinbutton.get_value_as_int ();
@@ -186,6 +143,35 @@ public class MainPanel : Gtk.Stack, Clocks.Clock {
         }
     }
 
+    [GtkCallback]
+    private void on_start_button_clicked () {
+        start ();
+    }
+
+    [GtkCallback]
+    private void on_left_button_clicked () {
+        switch (state) {
+        case State.RUNNING:
+            pause ();
+            left_button.set_label (_("Continue"));
+            left_button.get_style_context ().add_class ("clocks-go");
+            break;
+        case State.PAUSED:
+            restart ();
+            left_button.set_label (_("Pause"));
+            left_button.get_style_context ().remove_class("clocks-go");
+            break;
+        default:
+            assert_not_reached ();
+        }
+    }
+
+    [GtkCallback]
+    private void on_right_button_clicked () {
+        reset ();
+        left_button.set_label (_("Pause"));
+    }
+
     private void reset () {
         state = State.STOPPED;
         timer.reset ();


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