[gnome-todo] sidebar-list-row: Bring back renaming tasklists



commit 874d16655952a9a25f2ed5f60eb5c3e1cae97bb1
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed Jun 13 00:07:24 2018 -0300

    sidebar-list-row: Bring back renaming tasklists
    
    The port to the new sidebar-based layout had the side effect
    of not having full feature parity with the previous layout.
    The single worst offender was renaming tasklists.
    
    This commit brings it back.

 data/ui/sidebar-list-row.ui        | 65 ++++++++++++++++++++++++++
 src/sidebar/gtd-sidebar-list-row.c | 93 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 158 insertions(+)
---
diff --git a/data/ui/sidebar-list-row.ui b/data/ui/sidebar-list-row.ui
index 88c58be..2302aae 100644
--- a/data/ui/sidebar-list-row.ui
+++ b/data/ui/sidebar-list-row.ui
@@ -47,9 +47,74 @@
     </child>
   </template>
 
+  <!-- Rename popover -->
+  <object class="GtkPopover" id="rename_popover">
+    <property name="can_focus">False</property>
+    <property name="position">right</property>
+    <signal name="hide" handler="on_rename_popover_hidden_cb" object="GtdSidebarListRow" swapped="no" />
+
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">12</property>
+        <property name="margin">12</property>
+
+        <child>
+          <object class="GtkLabel" id="rename_label">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="hexpand">True</property>
+            <property name="xalign">0.0</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+            </attributes>
+          </object>
+        </child>
+
+        <child>
+          <object class="GtkBox">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="spacing">12</property>
+
+            <!-- Entry -->
+            <child>
+              <object class="GtkEntry" id="rename_entry">
+                <property name="visible">True</property>
+                <signal name="activate" handler="on_rename_entry_activated_cb" object="GtdSidebarListRow" 
swapped="no" />
+                <signal name="notify::text" handler="on_rename_entry_text_changed_cb" 
object="GtdSidebarListRow" swapped="no" />
+              </object>
+            </child>
+
+            <!-- Button -->
+            <child>
+              <object class="GtkButton" id="rename_button">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="label" translatable="yes">Rename</property>
+                <signal name="clicked" handler="on_rename_button_clicked_cb" object="GtdSidebarListRow" 
swapped="no" />
+                <style>
+                  <class name="destructive-action"/>
+                </style>
+              </object>
+            </child>
+
+          </object>
+        </child>
+
+      </object>
+    </child>
+  </object>
+
   <!-- Right-click menu -->
   <menu id="menu">
     <section>
+      <item>
+        <attribute name="label" translatable="yes">Rename</attribute>
+        <attribute name="action">list-row.rename</attribute>
+      </item>
       <item>
         <attribute name="label" translatable="yes">Delete</attribute>
         <attribute name="action">list-row.delete</attribute>
diff --git a/src/sidebar/gtd-sidebar-list-row.c b/src/sidebar/gtd-sidebar-list-row.c
index b7cd2ef..9c82c77 100644
--- a/src/sidebar/gtd-sidebar-list-row.c
+++ b/src/sidebar/gtd-sidebar-list-row.c
@@ -36,6 +36,10 @@ struct _GtdSidebarListRow
 
   GtkImage           *color_icon;
   GtkLabel           *name_label;
+  GtkWidget          *rename_button;
+  GtkEntry           *rename_entry;
+  GtkLabel           *rename_label;
+  GtkPopover         *rename_popover;
   GtkLabel           *tasks_counter_label;
 
   GActionMap         *action_group;
@@ -211,6 +215,28 @@ set_list (GtdSidebarListRow *self,
                           G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
 }
 
+static void
+rename_list (GtdSidebarListRow *self)
+{
+  g_assert (gtk_widget_get_visible (GTK_WIDGET (self->rename_popover)));
+  g_assert (g_utf8_validate (gtk_entry_get_text (self->rename_entry), -1, NULL));
+
+  /*
+   * Even though the Rename button is insensitive, we may still reach here
+   * by activating the entry.
+   */
+  if (gtk_entry_get_text_length (self->rename_entry) == 0)
+    return;
+
+  if (g_strcmp0 (gtk_entry_get_text (self->rename_entry), gtd_task_list_get_name (self->list)) != 0)
+    {
+      gtd_task_list_set_name (self->list, gtk_entry_get_text (self->rename_entry));
+      gtd_provider_update_task_list (gtd_task_list_get_provider (self->list), self->list);
+    }
+
+  gtk_popover_popdown (self->rename_popover);
+}
+
 
 /*
  * Callbacks
@@ -267,6 +293,27 @@ on_delete_action_activated_cb (GSimpleAction *action,
   gtk_widget_hide (GTK_WIDGET (self));
 }
 
+static void
+on_rename_action_activated_cb (GSimpleAction *action,
+                               GVariant      *parameters,
+                               gpointer       user_data)
+{
+  GtdSidebarListRow *self;
+  g_autofree gchar *text;
+
+  self = GTD_SIDEBAR_LIST_ROW (user_data);
+
+  g_assert (self->list != NULL);
+
+  text = g_strdup_printf (_("Rename %s"), gtd_task_list_get_name (self->list));
+  gtk_label_set_label (self->rename_label, text);
+
+  gtk_entry_set_text (self->rename_entry, gtd_task_list_get_name (self->list));
+
+  gtk_popover_set_relative_to (self->rename_popover, GTK_WIDGET (self));
+  gtk_popover_popup (self->rename_popover);
+}
+
 static void
 on_list_changed_cb (GtdSidebarListRow *self)
 {
@@ -281,6 +328,42 @@ on_list_color_changed_cb (GtdTaskList       *list,
   update_color_icon (self);
 }
 
+static void
+on_rename_button_clicked_cb (GtkButton         *button,
+                             GtdSidebarListRow *self)
+{
+  rename_list (self);
+}
+
+static void
+on_rename_entry_activated_cb (GtkEntry          *entry,
+                              GtdSidebarListRow *self)
+{
+  rename_list (self);
+}
+
+static void
+on_rename_entry_text_changed_cb (GtkEntry          *entry,
+                                 GParamSpec        *pspec,
+                                 GtdSidebarListRow *self)
+{
+  gboolean valid = gtk_entry_get_text_length (entry) > 0;
+
+  gtk_widget_set_sensitive (self->rename_button, valid);
+}
+
+static void
+on_rename_popover_hidden_cb (GtkPopover        *popover,
+                             GtdSidebarListRow *self)
+{
+  /*
+   * Remove the relative to, to remove the popover from the widget
+   * list and avoid parsing any CSS for it. It's a small performance
+   * improvement.
+   */
+  gtk_popover_set_relative_to (popover, NULL);
+}
+
 
 /*
  * GObject overrides
@@ -358,7 +441,16 @@ gtd_sidebar_list_row_class_init (GtdSidebarListRowClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GtdSidebarListRow, color_icon);
   gtk_widget_class_bind_template_child (widget_class, GtdSidebarListRow, menu);
   gtk_widget_class_bind_template_child (widget_class, GtdSidebarListRow, name_label);
+  gtk_widget_class_bind_template_child (widget_class, GtdSidebarListRow, rename_button);
+  gtk_widget_class_bind_template_child (widget_class, GtdSidebarListRow, rename_entry);
+  gtk_widget_class_bind_template_child (widget_class, GtdSidebarListRow, rename_label);
+  gtk_widget_class_bind_template_child (widget_class, GtdSidebarListRow, rename_popover);
   gtk_widget_class_bind_template_child (widget_class, GtdSidebarListRow, tasks_counter_label);
+
+  gtk_widget_class_bind_template_callback (widget_class, on_rename_button_clicked_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_rename_entry_activated_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_rename_entry_text_changed_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_rename_popover_hidden_cb);
 }
 
 static void
@@ -366,6 +458,7 @@ gtd_sidebar_list_row_init (GtdSidebarListRow *self)
 {
   const GActionEntry entries[] = {
     { "delete", on_delete_action_activated_cb },
+    { "rename", on_rename_action_activated_cb },
   };
 
   gtk_widget_init_template (GTK_WIDGET (self));


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