[gnome-clocks/bilelmoussaoui/redesign-timer] Timer: display/edit the name



commit 2c458c73f1e03bc843c3596c721ef0710e3c91cf
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Tue Jan 28 12:59:45 2020 +0100

    Timer: display/edit the name

 data/ui/timer_row.ui | 14 ++++++--------
 src/timer.vala       | 48 ++++++++++++++++++++++++++++++++++++++++++------
 src/widgets.vala     | 18 +++++++++++++++---
 3 files changed, 63 insertions(+), 17 deletions(-)
---
diff --git a/data/ui/timer_row.ui b/data/ui/timer_row.ui
index ed4b462..b024a60 100644
--- a/data/ui/timer_row.ui
+++ b/data/ui/timer_row.ui
@@ -9,7 +9,7 @@
     <property name="margin_top">12</property>
     <property name="margin_bottom">12</property>
     <property name="orientation">vertical</property>
-    <property name="spacing">18</property>
+    <property name="spacing">6</property>
     <child>
       <object class="GtkLabel" id="countdown_label">
         <property name="visible">True</property>
@@ -29,11 +29,11 @@
       </packing>
     </child>
     <child>
-      <object class="GtkStack">
+      <object class="GtkStack" id="name_stack">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <child>
-          <object class="GtkEntry">
+          <object class="GtkEntry" id="name_entry">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <property name="halign">center</property>
@@ -41,12 +41,11 @@
             <property name="placeholder_text" translatable="yes">Title...</property>
           </object>
           <packing>
-            <property name="name">page0</property>
-            <property name="title" translatable="yes">page0</property>
+            <property name="name">edit</property>
           </packing>
         </child>
         <child>
-          <object class="GtkLabel" id="timer_name">
+          <object class="GtkLabel" id="name_label">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
             <property name="halign">center</property>
@@ -55,8 +54,7 @@
             </style>
           </object>
           <packing>
-            <property name="name">page1</property>
-            <property name="title" translatable="yes">page1</property>
+            <property name="name">display</property>
             <property name="position">1</property>
           </packing>
         </child>
diff --git a/src/timer.vala b/src/timer.vala
index 5440107..32c5c1f 100644
--- a/src/timer.vala
+++ b/src/timer.vala
@@ -216,6 +216,7 @@ public class Setup : Gtk.Box {
 [GtkTemplate (ui = "/org/gnome/clocks/ui/timer_row.ui")]
 public class Row : Gtk.Box {
     public signal void deleted (Item item);
+    public signal void updated (Item item);
     public enum State {
         STOPPED,
         RUNNING,
@@ -233,6 +234,14 @@ public class Row : Gtk.Box {
     [GtkChild]
     private Gtk.Stack start_stack;
 
+    [GtkChild]
+    private Gtk.Label name_label;
+    [GtkChild]
+    private Gtk.Entry name_entry;
+
+    [GtkChild]
+    private Gtk.Stack name_stack;
+
     [GtkChild]
     private Gtk.Button reset_button;
     [GtkChild]
@@ -244,16 +253,20 @@ public class Row : Gtk.Box {
         timer = new GLib.Timer ();
 
         timeout_id = 0;
-        destroy.connect(() => {
+        destroy.connect (() => {
             if (timeout_id != 0) {
                 GLib.Source.remove(timeout_id);
                 timeout_id = 0;
             }
         });
-        delete_button.clicked.connect(() => {
+        delete_button.clicked.connect (() => {
             this.deleted (this.item);
         });
-
+        name_entry.bind_property ("text", item, "name", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
+        name_entry.key_release_event.connect (() => {
+            this.updated (this.item);
+            return true;
+        });
         reset ();
     }
 
@@ -286,6 +299,7 @@ public class Row : Gtk.Box {
     }
 
     private void reset () {
+        update_name_label ();
         state = State.STOPPED;
         span = item.duration.get_total_seconds ();
 
@@ -298,9 +312,11 @@ public class Row : Gtk.Box {
         countdown_label.get_style_context ().add_class ("timer-paused");
         countdown_label.get_style_context ().remove_class ("timer-running");
         start_stack.visible_child_name = "start";
+        name_stack.visible_child_name = "edit";
     }
 
     private void start () {
+        update_name_label ();
         countdown_label.get_style_context ().add_class ("timer-running");
         countdown_label.get_style_context ().remove_class ("timer-paused");
 
@@ -308,6 +324,7 @@ public class Row : Gtk.Box {
         delete_button.hide ();
 
         start_stack.visible_child_name = "pause";
+        name_stack.visible_child_name = "display";
         state = State.RUNNING;
         timer.start ();
         timeout_id = GLib.Timeout.add(40, () => {
@@ -338,6 +355,7 @@ public class Row : Gtk.Box {
         timer.stop ();
         span -= timer.elapsed ();
         start_stack.visible_child_name = "start";
+        name_stack.visible_child_name = "display";
     }
 
     private void update_countdown (double elapsed) {
@@ -359,6 +377,14 @@ public class Row : Gtk.Box {
         countdown_label.set_text ("%02i:%02i:%02i".printf(h, m, s));
     }
 
+    private void update_name_label () {
+        if (item.name != null && item.name != "") {
+            name_label.label = item.name;
+        } else {
+            name_label.label = _("%i minutes timer".printf(item.duration.minutes));
+        }
+    }
+
     public override void grab_focus () {
         /*if (timer_stack.visible_child == setup_frame) {
             start_button.grab_focus ();
@@ -415,9 +441,8 @@ public class Face : Gtk.Stack, Clocks.Clock {
         timers_list.set_header_func ((Gtk.ListBoxUpdateHeaderFunc) Hdy.list_box_separator_header);
         timers_list.bind_model (timers, (timer) => {
             var timer_row = new Row ((Item)timer);
-            timer_row.deleted.connect((item) => {
-               this.remove_timer(item);
-            });
+            timer_row.deleted.connect ((item) => this.remove_timer (item));
+            timer_row.updated.connect ((item) => this.update_timer (item));
             return timer_row;
         });
 
@@ -450,6 +475,17 @@ public class Face : Gtk.Stack, Clocks.Clock {
         load ();
     }
 
+    private void update_timer (Item item) {
+        /*
+        var current_position = timers.get_index (item);
+        if (current_position != -1) {
+            Item? timer_item = (Item) timers.get_item (current_position);
+
+            timer_item.name = item.name;
+            save ();
+        }*/
+    }
+
     private void remove_timer (Item item) {
         timers.remove (item);
         save ();
diff --git a/src/widgets.vala b/src/widgets.vala
index f7f8312..d07ccb5 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -72,16 +72,28 @@ public class ContentStore : GLib.Object, GLib.ListModel {
         item.notify["selected"].connect (on_item_selection_toggle);
     }
 
-    public void remove (ContentItem item) {
-
+    public int get_index (ContentItem item) {
+        int position = -1;
         var n = store.get_n_items ();
         for (int i = 0; i < n; i++) {
             var compared_item = (ContentItem) store.get_object (i);
             if (compared_item == item) {
-                store.remove (i);
+                position = i;
                 break;
             }
         }
+        return position;
+    }
+
+    public void insert (uint position, ContentItem item) {
+        store.insert (position, item);
+    }
+
+    public void remove (ContentItem item) {
+        var index = get_index (item);
+        if (index != -1) {
+            store.remove (index);
+        }
     }
 
     public delegate void ForeachFunc (ContentItem item);


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