[nautilus/wip/apoos-maximus/gsoc2020: 7/29] properties-window: Populate Name row from template




commit 6f175cf5d965a3d47b0c5c56bbfe22040db00bbe
Author: Apoorv Sachan <apoorv 99 sachan gmail com>
Date:   Fri May 29 23:42:33 2020 +0530

    properties-window: Populate Name row from template
    
    We have been creating either an entry or a label for the name field,
    depending on whether we were able to edit the name or not.
    
    In GtkBuilder UI definitions we cannot do this anymore, as we have
    to define and build both the entry and the label.
    
    So, in order to only show the one we want on each situation, we are
    introducing a GtkStack to contain both.

 src/nautilus-properties-window.c               | 92 ++++++++------------------
 src/resources/ui/nautilus-properties-window.ui | 64 ++++++++++++------
 2 files changed, 73 insertions(+), 83 deletions(-)
---
diff --git a/src/nautilus-properties-window.c b/src/nautilus-properties-window.c
index 615ad88b4..866d1b902 100644
--- a/src/nautilus-properties-window.c
+++ b/src/nautilus-properties-window.c
@@ -92,7 +92,8 @@ struct _NautilusPropertiesWindow
 
     GtkGrid *basic_grid;
 
-    GtkLabel *name_label;
+    GtkLabel *name_title_label;
+    GtkStack *name_stack;
     GtkWidget *name_field;
     unsigned int name_row;
     char *pending_name;
@@ -551,59 +552,29 @@ set_name_field (NautilusPropertiesWindow *window,
                 const gchar              *original_name,
                 const gchar              *name)
 {
-    gboolean new_widget;
+    GtkWidget *stack_child_label;
+    GtkWidget *stack_child_entry;
     gboolean use_label;
 
-    /* There are four cases here:
-     * 1) Changing the text of a label
-     * 2) Changing the text of an entry
-     * 3) Creating label (potentially replacing entry)
-     * 4) Creating entry (potentially replacing label)
-     */
+    stack_child_label = gtk_stack_get_child_by_name (window->name_stack, "name_value_label");
+    stack_child_entry = gtk_stack_get_child_by_name (window->name_stack, "name_value_entry");
+
     use_label = is_multi_file_window (window) || !nautilus_file_can_rename (get_original_file (window));
-    new_widget = !window->name_field || (use_label ? GTK_IS_ENTRY (window->name_field) : GTK_IS_LABEL 
(window->name_field));
 
-    if (new_widget)
+    if (use_label)
     {
-        if (window->name_field)
-        {
-            gtk_widget_destroy (window->name_field);
-        }
-
-        if (use_label)
-        {
-            window->name_field = GTK_WIDGET
-                                     (attach_ellipsizing_value_label (window->basic_grid,
-                                                                      GTK_WIDGET (window->name_label),
-                                                                      name));
-        }
-        else
-        {
-            window->name_field = gtk_entry_new ();
-            gtk_entry_set_text (GTK_ENTRY (window->name_field), name);
-            gtk_widget_show (window->name_field);
-
-            gtk_grid_attach_next_to (window->basic_grid, window->name_field,
-                                     GTK_WIDGET (window->name_label),
-                                     GTK_POS_RIGHT, 1, 1);
-            gtk_label_set_mnemonic_widget (GTK_LABEL (window->name_label), window->name_field);
-
-            g_signal_connect_object (window->name_field, "notify::has-focus",
-                                     G_CALLBACK (name_field_focus_changed), window, 0);
-            g_signal_connect_object (window->name_field, "activate",
-                                     G_CALLBACK (name_field_activate), window, 0);
-        }
-
-        gtk_widget_show (window->name_field);
+        gtk_label_set_text (GTK_LABEL (stack_child_label), name);
+        gtk_stack_set_visible_child (window->name_stack, stack_child_label);
     }
+    else
+    {
+        gtk_stack_set_visible_child (window->name_stack, stack_child_entry);
+    }
+
     /* Only replace text if the file's name has changed. */
-    else if (original_name == NULL || strcmp (original_name, name) != 0)
+    if (original_name == NULL || strcmp (original_name, name) != 0)
     {
-        if (use_label)
-        {
-            gtk_label_set_text (GTK_LABEL (window->name_field), name);
-        }
-        else
+        if (!use_label)
         {
             /* Only reset the text if it's different from what is
              * currently showing. This causes minimal ripples (e.g.
@@ -624,7 +595,7 @@ update_name_field (NautilusPropertiesWindow *window)
 {
     NautilusFile *file;
 
-    gtk_label_set_text_with_mnemonic (window->name_label,
+    gtk_label_set_text_with_mnemonic (window->name_title_label,
                                       ngettext ("_Name:", "_Names:",
                                                 get_not_gone_original_file_count (window)));
 
@@ -681,10 +652,7 @@ update_name_field (NautilusPropertiesWindow *window)
          * an edit in progress. If the name hasn't changed (but some other
          * aspect of the file might have), then don't clobber changes.
          */
-        if (window->name_field)
-        {
-            original_name = (const char *) g_object_get_data (G_OBJECT (window->name_field), 
"original_name");
-        }
+        original_name = (const char *) g_object_get_data (G_OBJECT (window->name_field), "original_name");
 
         set_name_field (window, original_name, current_name);
 
@@ -745,10 +713,7 @@ rename_callback (NautilusFile *file,
                                              window->pending_name,
                                              error,
                                              GTK_WINDOW (window));
-        if (window->name_field != NULL)
-        {
-            name_field_restore_original_name (window->name_field);
-        }
+        name_field_restore_original_name (window->name_field);
     }
 
     g_object_unref (window);
@@ -3153,15 +3118,15 @@ setup_basic_page (NautilusPropertiesWindow *window)
 
     grid = window->basic_grid;
 
-    /* Name label.  The text will be determined in update_name_field */
-    window->name_label = attach_title_field (grid, NULL);
-
-    /* Name field */
-    window->name_field = NULL;
     update_name_field (window);
 
+    g_signal_connect_object (window->name_field, "notify::has-focus",
+                             G_CALLBACK (name_field_focus_changed), window, 0);
+    g_signal_connect_object (window->name_field, "activate",
+                             G_CALLBACK (name_field_activate), window, 0);
+
     /* Start with name field selected, if it's an entry. */
-    if (GTK_IS_ENTRY (window->name_field))
+    if (GTK_IS_ENTRY (gtk_stack_get_visible_child (window->name_stack)))
     {
         gtk_widget_grab_focus (GTK_WIDGET (window->name_field));
     }
@@ -5461,8 +5426,6 @@ real_destroy (GtkWidget *object)
         stop_deep_count_for_file (window, window->deep_count_files->data);
     }
 
-    window->name_field = NULL;
-
     g_list_free (window->permission_buttons);
     window->permission_buttons = NULL;
 
@@ -5770,6 +5733,9 @@ nautilus_properties_window_class_init (NautilusPropertiesWindowClass *klass)
     gtk_widget_class_bind_template_child (widget_class, NautilusPropertiesWindow, icon_button);
     gtk_widget_class_bind_template_child (widget_class, NautilusPropertiesWindow, icon_button_image);
     gtk_widget_class_bind_template_child (widget_class, NautilusPropertiesWindow, basic_grid);
+    gtk_widget_class_bind_template_child (widget_class, NautilusPropertiesWindow, name_title_label);
+    gtk_widget_class_bind_template_child (widget_class, NautilusPropertiesWindow, name_stack);
+    gtk_widget_class_bind_template_child (widget_class, NautilusPropertiesWindow, name_field);
 }
 
 static void
diff --git a/src/resources/ui/nautilus-properties-window.ui b/src/resources/ui/nautilus-properties-window.ui
index b3bacba78..28559dfe2 100644
--- a/src/resources/ui/nautilus-properties-window.ui
+++ b/src/resources/ui/nautilus-properties-window.ui
@@ -75,28 +75,52 @@
                         <property name="row_spacing">6</property>
                         <property name="column_spacing">12</property>
                         <child>
-                          <placeholder/>
-                        </child>
-                        <child>
-                          <placeholder/>
-                        </child>
-                        <child>
-                          <placeholder/>
-                        </child>
-                        <child>
-                          <placeholder/>
-                        </child>
-                        <child>
-                          <placeholder/>
-                        </child>
-                        <child>
-                          <placeholder/>
-                        </child>
-                        <child>
-                          <placeholder/>
+                          <object class="GtkLabel" id="name_title_label">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <property name="label" translatable="yes">_Name:</property>
+                            <property name="use_underline">True</property>
+                            <property name="mnemonic_widget">name_field</property>
+                            <property name="xalign">0</property>
+                          </object>
+                          <packing>
+                            <property name="left_attach">0</property>
+                            <property name="top_attach">0</property>
+                          </packing>
                         </child>
                         <child>
-                          <placeholder/>
+                          <object class="GtkStack" id="name_stack">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <child>
+                              <object class="GtkLabel" id="name_value_label">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                                <property name="hexpand">True</property>
+                                <property name="selectable">True</property>
+                                <property name="ellipsize">end</property>
+                                <property name="max_width_chars">24</property>
+                                <property name="xalign">0</property>
+                              </object>
+                              <packing>
+                                <property name="name">name_value_label</property>
+                              </packing>
+                            </child>
+                            <child>
+                              <object class="GtkEntry" id="name_field">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                              </object>
+                              <packing>
+                                <property name="name">name_value_entry</property>
+                                <property name="position">1</property>
+                              </packing>
+                            </child>
+                          </object>
+                          <packing>
+                            <property name="left_attach">1</property>
+                            <property name="top_attach">0</property>
+                          </packing>
                         </child>
                         <child>
                           <placeholder/>


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