[nautilus/wip/antoniof/gtk4-preparation-step-container-api: 19/20] extensions/image-properties: Don't subclass widget




commit c1760ff543822851719b82702de22fc577f9b8ae
Author: António Fernandes <antoniof gnome org>
Date:   Thu Nov 25 19:31:20 2021 +0000

    extensions/image-properties: Don't subclass widget
    
    GTK discourages subclassing widgets.
    
    The only reason we are subclassing here is to add struct fields. But we
    don't need to subclass a widget for that. We don't even need to subclass
    GObject. A plain data struct is enough.

 .../nautilus-image-properties-page-provider.c      |  8 +--
 .../nautilus-image-properties-page.c               | 77 ++++++++++------------
 .../nautilus-image-properties-page.h               | 12 +---
 3 files changed, 37 insertions(+), 60 deletions(-)
---
diff --git a/extensions/image-properties/nautilus-image-properties-page-provider.c 
b/extensions/image-properties/nautilus-image-properties-page-provider.c
index b270ea945..5ad8b72c0 100644
--- a/extensions/image-properties/nautilus-image-properties-page-provider.c
+++ b/extensions/image-properties/nautilus-image-properties-page-provider.c
@@ -81,7 +81,7 @@ get_pages (NautilusPropertyPageProvider *provider,
 {
     NautilusFileInfo *file_info;
     g_autofree char *mime_type = NULL;
-    NautilusImagesPropertiesPage *image_properties_page;
+    GtkWidget *image_properties_page;
     NautilusPropertyPage *property_page;
 
     if (files == NULL || files->next != NULL)
@@ -95,12 +95,10 @@ get_pages (NautilusPropertyPageProvider *provider,
     {
         return NULL;
     }
-    image_properties_page = nautilus_image_properties_page_new ();
+    image_properties_page = nautilus_image_properties_page_new (file_info);
     property_page = nautilus_property_page_new (NAUTILUS_IMAGE_PROPERTIES_PAGE_NAME,
                                                 gtk_label_new (_("Image")),
-                                                GTK_WIDGET (image_properties_page));
-
-    nautilus_image_properties_page_load_from_file_info (image_properties_page, file_info);
+                                                image_properties_page);
 
     return g_list_prepend (NULL, property_page);
 }
diff --git a/extensions/image-properties/nautilus-image-properties-page.c 
b/extensions/image-properties/nautilus-image-properties-page.c
index 45c85575a..345e85791 100644
--- a/extensions/image-properties/nautilus-image-properties-page.c
+++ b/extensions/image-properties/nautilus-image-properties-page.c
@@ -26,9 +26,9 @@
 
 #define LOAD_BUFFER_SIZE 8192
 
-struct _NautilusImagesPropertiesPage
+typedef struct
 {
-    GtkGrid parent;
+    GtkWidget *page_widget;
 
     GCancellable *cancellable;
     GtkWidget *grid;
@@ -41,36 +41,17 @@ struct _NautilusImagesPropertiesPage
 
     GExiv2Metadata *md;
     gboolean md_ready;
-};
-
-G_DEFINE_TYPE (NautilusImagesPropertiesPage,
-               nautilus_image_properties_page,
-               GTK_TYPE_GRID);
+} NautilusImagesPropertiesPage;
 
 static void
-finalize (GObject *object)
+nautilus_images_properties_page_free (NautilusImagesPropertiesPage *page)
 {
-    NautilusImagesPropertiesPage *page;
-
-    page = NAUTILUS_IMAGE_PROPERTIES_PAGE (object);
-
     if (page->cancellable != NULL)
     {
         g_cancellable_cancel (page->cancellable);
         g_clear_object (&page->cancellable);
     }
-
-    G_OBJECT_CLASS (nautilus_image_properties_page_parent_class)->finalize (object);
-}
-
-static void
-nautilus_image_properties_page_class_init (NautilusImagesPropertiesPageClass *klass)
-{
-    GObjectClass *object_class;
-
-    object_class = G_OBJECT_CLASS (klass);
-
-    object_class->finalize = finalize;
+    g_free (page);
 }
 
 static void
@@ -110,26 +91,28 @@ append_item (NautilusImagesPropertiesPage *page,
 static void
 nautilus_image_properties_page_init (NautilusImagesPropertiesPage *self)
 {
-    GtkWidget *scrolled_window;
-
-    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
-
-    gtk_widget_set_vexpand (GTK_WIDGET (scrolled_window), TRUE);
-    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
+    self->page_widget = gtk_scrolled_window_new (NULL, NULL);
+
+    g_object_set (self->page_widget,
+                  "margin-bottom", 6,
+                  "margin-end", 12,
+                  "margin-start", 12,
+                  "margin-top", 6,
+                  NULL);
+    gtk_widget_set_vexpand (self->page_widget, TRUE);
+    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (self->page_widget),
                                     GTK_POLICY_NEVER,
                                     GTK_POLICY_AUTOMATIC);
 
-    gtk_container_add (GTK_CONTAINER (self), scrolled_window);
-
     self->grid = gtk_grid_new ();
 
     gtk_orientable_set_orientation (GTK_ORIENTABLE (self->grid), GTK_ORIENTATION_VERTICAL);
     gtk_grid_set_row_spacing (GTK_GRID (self->grid), 6);
     gtk_grid_set_column_spacing (GTK_GRID (self->grid), 18);
     append_item (self, _("Loading…"), NULL);
-    gtk_container_add (GTK_CONTAINER (scrolled_window), self->grid);
+    gtk_container_add (GTK_CONTAINER (self->page_widget), self->grid);
 
-    gtk_widget_show_all (GTK_WIDGET (self));
+    gtk_widget_show_all (GTK_WIDGET (self->page_widget));
 }
 
 static void
@@ -467,7 +450,7 @@ file_open_callback (GObject      *object,
     }
 }
 
-void
+static void
 nautilus_image_properties_page_load_from_file_info (NautilusImagesPropertiesPage *self,
                                                     NautilusFileInfo             *file_info)
 {
@@ -476,7 +459,6 @@ nautilus_image_properties_page_load_from_file_info (NautilusImagesPropertiesPage
     g_autofree char *path = NULL;
     FileOpenData *data;
 
-    g_return_if_fail (NAUTILUS_IS_IMAGE_PROPERTIES_PAGE (self));
     g_return_if_fail (file_info != NULL);
 
     self->cancellable = g_cancellable_new ();
@@ -522,13 +504,20 @@ nautilus_image_properties_page_load_from_file_info (NautilusImagesPropertiesPage
                        data);
 }
 
-NautilusImagesPropertiesPage *
-nautilus_image_properties_page_new (void)
+GtkWidget *
+nautilus_image_properties_page_new (NautilusFileInfo *file_info)
 {
-    return g_object_new (NAUTILUS_TYPE_IMAGE_PROPERTIES_PAGE,
-                         "margin-bottom", 6,
-                         "margin-end", 12,
-                         "margin-start", 12,
-                         "margin-top", 6,
-                         NULL);
+    NautilusImagesPropertiesPage *self;
+
+    self = g_new0 (NautilusImagesPropertiesPage, 1);
+
+    nautilus_image_properties_page_init (self);
+    nautilus_image_properties_page_load_from_file_info (self, file_info);
+
+    g_object_set_data_full (G_OBJECT (self->page_widget),
+                            "nautilus-images-properties-page",
+                            self,
+                            (GDestroyNotify) nautilus_images_properties_page_free);
+
+    return self->page_widget;
 }
diff --git a/extensions/image-properties/nautilus-image-properties-page.h 
b/extensions/image-properties/nautilus-image-properties-page.h
index 5a2c3580a..00b131c88 100644
--- a/extensions/image-properties/nautilus-image-properties-page.h
+++ b/extensions/image-properties/nautilus-image-properties-page.h
@@ -22,14 +22,4 @@
 
 #include <nautilus-extension.h>
 
-#define NAUTILUS_TYPE_IMAGE_PROPERTIES_PAGE (nautilus_image_properties_page_get_type ())
-
-G_DECLARE_FINAL_TYPE (NautilusImagesPropertiesPage,
-                      nautilus_image_properties_page,
-                      NAUTILUS, IMAGE_PROPERTIES_PAGE,
-                      GtkGrid)
-
-void                          nautilus_image_properties_page_load_from_file_info 
(NautilusImagesPropertiesPage *page,
-                                                                                  NautilusFileInfo           
  *file_info);
-
-NautilusImagesPropertiesPage *nautilus_image_properties_page_new                 (void);
\ No newline at end of file
+GtkWidget *nautilus_image_properties_page_new (NautilusFileInfo *file_info);


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