[nautilus/wip/antoniof/flow-box-preparation: 34/68] view-icon-item-ui: Build from template




commit 452df8daa1fce938447ca4e5dc6d7ad133ac46ae
Author: António Fernandes <antoniof gnome org>
Date:   Thu Aug 26 19:46:27 2021 +0100

    view-icon-item-ui: Build from template
    
    Declarative UI allows us to drop a lot of code, including GtkContainer
    methods, which helps with preparing for the GTK 4 port.

 po/POTFILES.in                                 |   1 +
 src/nautilus-view-icon-item-ui.c               | 149 ++++++++-----------------
 src/nautilus-view-icon-ui.c                    |   2 +-
 src/resources/nautilus.gresource.xml           |   1 +
 src/resources/ui/nautilus-view-icon-item-ui.ui |  52 +++++++++
 5 files changed, 102 insertions(+), 103 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 48e7fd43f..825b8769b 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -99,6 +99,7 @@ src/resources/ui/nautilus-starred-is-empty.ui
 src/resources/ui/nautilus-toolbar.ui
 src/resources/ui/nautilus-toolbar-view-menu.ui
 src/resources/ui/nautilus-trash-is-empty.ui
+src/resources/ui/nautilus-view-icon-item-ui.ui
 src/resources/ui/nautilus-window.ui
 src/gtk/nautilusgtkplacesview.c
 src/gtk/nautilusgtkplacesviewrow.c
diff --git a/src/nautilus-view-icon-item-ui.c b/src/nautilus-view-icon-item-ui.c
index 5946f5546..4a534e864 100644
--- a/src/nautilus-view-icon-item-ui.c
+++ b/src/nautilus-view-icon-item-ui.c
@@ -9,9 +9,9 @@ struct _NautilusViewIconItemUi
 
     NautilusViewItemModel *model;
 
-    GtkBox *container;
+    GtkWidget *fixed_height_box;
     GtkWidget *icon;
-    GtkLabel *label;
+    GtkWidget *label;
 };
 
 G_DEFINE_TYPE (NautilusViewIconItemUi, nautilus_view_icon_item_ui, GTK_TYPE_FLOW_BOX_CHILD)
@@ -23,13 +23,11 @@ enum
     N_PROPS
 };
 
-static GtkWidget *
-create_icon (NautilusViewIconItemUi *self)
+static void
+update_icon (NautilusViewIconItemUi *self)
 {
     NautilusFileIconFlags flags;
     g_autoptr (GdkPixbuf) icon_pixbuf = NULL;
-    GtkImage *icon;
-    GtkBox *fixed_height_box;
     GtkStyleContext *style_context;
     NautilusFile *file;
     guint icon_size;
@@ -43,39 +41,19 @@ create_icon (NautilusViewIconItemUi *self)
 
     icon_pixbuf = nautilus_file_get_icon_pixbuf (file, icon_size,
                                                  TRUE, 1, flags);
-    icon = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_pixbuf));
-    gtk_widget_set_hexpand (GTK_WIDGET (icon), TRUE);
-    gtk_widget_set_vexpand (GTK_WIDGET (icon), TRUE);
-    gtk_widget_set_valign (GTK_WIDGET (icon), GTK_ALIGN_CENTER);
-    gtk_widget_set_halign (GTK_WIDGET (icon), GTK_ALIGN_CENTER);
-
-    fixed_height_box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 0));
-    gtk_widget_set_valign (GTK_WIDGET (fixed_height_box), GTK_ALIGN_CENTER);
-    gtk_widget_set_halign (GTK_WIDGET (fixed_height_box), GTK_ALIGN_CENTER);
-    gtk_widget_set_size_request (GTK_WIDGET (fixed_height_box), icon_size, icon_size);
+    gtk_image_set_from_pixbuf (GTK_IMAGE (self->icon), icon_pixbuf);
 
+    gtk_widget_set_size_request (self->fixed_height_box, icon_size, icon_size);
+    style_context = gtk_widget_get_style_context (self->fixed_height_box);
     if (nautilus_can_thumbnail (file) &&
         nautilus_file_should_show_thumbnail (file))
     {
-        style_context = gtk_widget_get_style_context (GTK_WIDGET (fixed_height_box));
         gtk_style_context_add_class (style_context, "icon-background");
     }
-
-    gtk_box_pack_start (fixed_height_box, GTK_WIDGET (icon), FALSE, FALSE, 0);
-
-    return GTK_WIDGET (fixed_height_box);
-}
-
-static void
-update_icon (NautilusViewIconItemUi *self)
-{
-    if (self->icon)
+    else
     {
-        gtk_container_remove (GTK_CONTAINER (self->container), GTK_WIDGET (self->icon));
+        gtk_style_context_remove_class (style_context, "icon-background");
     }
-    self->icon = create_icon (self);
-    gtk_widget_show_all (GTK_WIDGET (self->icon));
-    gtk_box_pack_start (self->container, GTK_WIDGET (self->icon), FALSE, FALSE, 0);
 }
 
 static void
@@ -88,16 +66,10 @@ on_view_item_file_changed (GObject    *object,
 
     file = nautilus_view_item_model_get_file (self->model);
 
-    if (self->icon)
-    {
-        update_icon (self);
-    }
+    update_icon (self);
 
-    if (self->label)
-    {
-        gtk_label_set_text (self->label,
-                            nautilus_file_get_display_name (file));
-    }
+    gtk_label_set_text (GTK_LABEL (self->label),
+                        nautilus_file_get_display_name (file));
 }
 
 static void
@@ -107,67 +79,7 @@ on_view_item_size_changed (GObject    *object,
 {
     NautilusViewIconItemUi *self = NAUTILUS_VIEW_ICON_ITEM_UI (user_data);
 
-    if (self->icon)
-    {
-        update_icon (self);
-    }
-}
-
-static void
-constructed (GObject *object)
-{
-    NautilusViewIconItemUi *self = NAUTILUS_VIEW_ICON_ITEM_UI (object);
-    GtkBox *container;
-    GtkLabel *label;
-    GtkStyleContext *style_context;
-    NautilusFile *file;
-    guint icon_size;
-
-    G_OBJECT_CLASS (nautilus_view_icon_item_ui_parent_class)->constructed (object);
-
-    file = nautilus_view_item_model_get_file (self->model);
-    icon_size = nautilus_view_item_model_get_icon_size (self->model);
-    container = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 0));
-    self->icon = create_icon (self);
-    gtk_box_pack_start (container, GTK_WIDGET (self->icon), FALSE, FALSE, 0);
-
-    label = GTK_LABEL (gtk_label_new (nautilus_file_get_display_name (file)));
-    gtk_widget_set_vexpand (GTK_WIDGET (label), TRUE);
-    gtk_widget_show (GTK_WIDGET (label));
-
-#if PANGO_VERSION_CHECK (1, 44, 4)
-    {
-        PangoAttrList *attr_list = pango_attr_list_new ();
-        pango_attr_list_insert (attr_list, pango_attr_insert_hyphens_new (FALSE));
-        gtk_label_set_attributes (label, attr_list);
-        pango_attr_list_unref (attr_list);
-    }
-#endif
-
-    gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_MIDDLE);
-    gtk_label_set_line_wrap (label, TRUE);
-    gtk_label_set_line_wrap_mode (label, PANGO_WRAP_WORD_CHAR);
-    gtk_label_set_lines (label, 3);
-    gtk_label_set_max_width_chars (label, 0);
-    gtk_label_set_justify (label, GTK_JUSTIFY_CENTER);
-    gtk_widget_set_valign (GTK_WIDGET (label), GTK_ALIGN_START);
-    gtk_box_pack_end (container, GTK_WIDGET (label), FALSE, TRUE, 0);
-
-    style_context = gtk_widget_get_style_context (GTK_WIDGET (container));
-    gtk_style_context_add_class (style_context, "icon-item-background");
-
-    gtk_widget_set_valign (GTK_WIDGET (container), GTK_ALIGN_START);
-    gtk_widget_set_halign (GTK_WIDGET (container), GTK_ALIGN_CENTER);
-
-    self->container = container;
-    gtk_container_add (GTK_CONTAINER (self),
-                       GTK_WIDGET (container));
-    gtk_widget_show_all (GTK_WIDGET (self));
-
-    g_signal_connect (self->model, "notify::icon-size",
-                      (GCallback) on_view_item_size_changed, self);
-    g_signal_connect (self->model, "notify::file",
-                      (GCallback) on_view_item_file_changed, self);
+    update_icon (self);
 }
 
 static void
@@ -204,7 +116,20 @@ static void
 set_model (NautilusViewIconItemUi *self,
            NautilusViewItemModel  *model)
 {
+    NautilusFile *file;
+
     self->model = g_object_ref (model);
+
+    file = nautilus_view_item_model_get_file (self->model);
+
+    update_icon (self);
+    gtk_label_set_text (GTK_LABEL (self->label),
+                        nautilus_file_get_display_name (file));
+
+    g_signal_connect (self->model, "notify::icon-size",
+                      (GCallback) on_view_item_size_changed, self);
+    g_signal_connect (self->model, "notify::file",
+                      (GCallback) on_view_item_file_changed, self);
 }
 
 static void
@@ -232,11 +157,11 @@ static void
 nautilus_view_icon_item_ui_class_init (NautilusViewIconItemUiClass *klass)
 {
     GObjectClass *object_class = G_OBJECT_CLASS (klass);
+    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
     object_class->finalize = finalize;
     object_class->get_property = get_property;
     object_class->set_property = set_property;
-    object_class->constructed = constructed;
 
     g_object_class_install_property (object_class,
                                      PROP_MODEL,
@@ -245,11 +170,31 @@ nautilus_view_icon_item_ui_class_init (NautilusViewIconItemUiClass *klass)
                                                           "The item model that this UI reprensents",
                                                           NAUTILUS_TYPE_VIEW_ITEM_MODEL,
                                                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
+    gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/nautilus/ui/nautilus-view-icon-item-ui.ui");
+
+    gtk_widget_class_bind_template_child (widget_class, NautilusViewIconItemUi, fixed_height_box);
+    gtk_widget_class_bind_template_child (widget_class, NautilusViewIconItemUi, icon);
+    gtk_widget_class_bind_template_child (widget_class, NautilusViewIconItemUi, label);
 }
 
 static void
 nautilus_view_icon_item_ui_init (NautilusViewIconItemUi *self)
 {
+    gtk_widget_init_template (GTK_WIDGET (self));
+
+#if PANGO_VERSION_CHECK (1, 44, 4)
+    {
+        PangoAttrList *attr_list;
+
+        /* GTK4 TODO: This attribute is set in the UI file but GTK 3 ignores it.
+         * Remove this block after the switch to GTK 4. */
+        attr_list = pango_attr_list_new ();
+        pango_attr_list_insert (attr_list, pango_attr_insert_hyphens_new (FALSE));
+        gtk_label_set_attributes (GTK_LABEL (self->label), attr_list);
+        pango_attr_list_unref (attr_list);
+    }
+#endif
 }
 
 NautilusViewIconItemUi *
diff --git a/src/nautilus-view-icon-ui.c b/src/nautilus-view-icon-ui.c
index 15656d321..05af3de77 100644
--- a/src/nautilus-view-icon-ui.c
+++ b/src/nautilus-view-icon-ui.c
@@ -137,7 +137,7 @@ create_widget_func (gpointer item,
 
     child = nautilus_view_icon_item_ui_new (item_model);
     nautilus_view_item_model_set_item_ui (item_model, GTK_WIDGET (child));
-    gtk_widget_show (GTK_WIDGET (child));
+    gtk_widget_show_all (GTK_WIDGET (child));
 
     return GTK_WIDGET (child);
 }
diff --git a/src/resources/nautilus.gresource.xml b/src/resources/nautilus.gresource.xml
index ccb2ef6ff..6ccb94cdd 100644
--- a/src/resources/nautilus.gresource.xml
+++ b/src/resources/nautilus.gresource.xml
@@ -26,6 +26,7 @@
     <file>ui/nautilus-file-conflict-dialog.ui</file>
     <file>ui/nautilus-files-view-select-items.ui</file>
     <file>ui/nautilus-operations-ui-manager-request-passphrase.ui</file>
+    <file>ui/nautilus-view-icon-item-ui.ui</file>
     <file alias="gtk/ui/nautilusgtkplacesview.ui">../gtk/nautilusgtkplacesview.ui</file>
     <file alias="gtk/ui/nautilusgtkplacesviewrow.ui">../gtk/nautilusgtkplacesviewrow.ui</file>
     <file alias="icons/filmholes.png">../../icons/filmholes.png</file>
diff --git a/src/resources/ui/nautilus-view-icon-item-ui.ui b/src/resources/ui/nautilus-view-icon-item-ui.ui
new file mode 100644
index 000000000..bbb929770
--- /dev/null
+++ b/src/resources/ui/nautilus-view-icon-item-ui.ui
@@ -0,0 +1,52 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<interface>
+  <requires lib="gtk+" version="3.0"/>
+  <template class="NautilusViewIconItemUi" parent="GtkFlowBoxChild">
+    <child>
+      <object class="GtkBox">
+        <property name="orientation">vertical</property>
+        <property name="halign">center</property>
+        <property name="valign">start</property>
+        <style>
+          <class name="icon-item-background"/>
+        </style>
+        <child>
+          <object class="GtkBox" id="fixed_height_box">
+            <property name="orientation">vertical</property>
+            <property name="halign">center</property>
+            <property name="height-request">96</property>
+            <property name="valign">center</property>
+            <property name="width-request">96</property>
+            <child>
+              <object class="GtkImage" id="icon">
+                <property name="halign">center</property>
+                <property name="hexpand">True</property>
+                <property name="valign">center</property>
+                <property name="vexpand">True</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="orientation">vertical</property>
+            <property name="valign">start</property>
+            <child>
+              <object class="GtkLabel" id="label">
+                <property name="ellipsize">middle</property>
+                <property name="justify">center</property>
+                <property name="lines">3</property>
+                <property name="max-width-chars">0</property>
+                <property name="wrap">True</property>
+                <property name="wrap-mode">word-char</property>
+                <attributes>
+                  <attribute name="insert-hyphens" value="false"/>
+                </attributes>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>


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