[gnome-software: 24/110] Replace gtk_container_get_children with GtkWidget APIs




commit 03641a7846b0820a5dcc7f7434e5633d4448c0ed
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sun Aug 22 18:23:13 2021 -0300

    Replace gtk_container_get_children with GtkWidget APIs
    
    Use gtk_widget_get_first_child() and gtk_widget_get_next_sibling()
    to iterate over children. This has the nice property of reducing
    the number of iterations for each container widget.

 src/gs-app-context-bar.c     |  9 ++++----
 src/gs-common.c              | 10 +++++----
 src/gs-details-page.c        | 34 +++++++++++++++---------------
 src/gs-extras-page.c         | 50 +++++++++++++++++++++-----------------------
 src/gs-featured-carousel.c   | 19 ++++++++++++-----
 src/gs-installed-page.c      | 23 ++++++++------------
 src/gs-moderate-page.c       | 20 +++++++++---------
 src/gs-removal-dialog.c      | 14 ++++++-------
 src/gs-screenshot-carousel.c | 10 +++++++--
 src/gs-updates-section.c     |  9 +++-----
 10 files changed, 103 insertions(+), 95 deletions(-)
---
diff --git a/src/gs-app-context-bar.c b/src/gs-app-context-bar.c
index 938650342..d1ed21c3f 100644
--- a/src/gs-app-context-bar.c
+++ b/src/gs-app-context-bar.c
@@ -85,8 +85,8 @@ static gboolean
 show_tile_for_non_applications (GsAppContextBar      *self,
                                 GsAppContextTileType  tile_type)
 {
+       GtkWidget *sibling;
        GtkBox *parent_box;
-       g_autoptr(GList) siblings = NULL;
        gboolean any_siblings_visible;
        AsComponentKind app_kind = gs_app_get_kind (self->app);
        gboolean is_application = (app_kind == AS_COMPONENT_KIND_DESKTOP_APP ||
@@ -98,10 +98,11 @@ show_tile_for_non_applications (GsAppContextBar      *self,
        parent_box = GTK_BOX (gtk_widget_get_parent (self->tiles[tile_type].tile));
        g_assert (GTK_IS_BOX (parent_box));
 
-       siblings = gtk_container_get_children (GTK_CONTAINER (parent_box));
        any_siblings_visible = FALSE;
-       for (GList *l = siblings; l != NULL; l = l->next) {
-               GtkWidget *sibling = GTK_WIDGET (l->data);
+
+       for (sibling = gtk_widget_get_first_child (GTK_WIDGET (parent_box));
+            sibling != NULL;
+            sibling = gtk_widget_get_next_sibling (sibling)) {
                g_assert (GTK_IS_BUTTON (sibling));
                any_siblings_visible |= gtk_widget_get_visible (sibling);
        }
diff --git a/src/gs-common.c b/src/gs-common.c
index 84fc7620b..03ddec467 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -480,8 +480,8 @@ insert_details_widget (GtkMessageDialog *dialog, const gchar *details)
 {
        GtkWidget *message_area, *sw, *label;
        GtkWidget *box, *tv;
+       GtkWidget *child;
        GtkTextBuffer *buffer;
-       GList *children;
        g_autoptr(GString) msg = NULL;
 
        g_assert (GTK_IS_MESSAGE_DIALOG (dialog));
@@ -508,9 +508,11 @@ insert_details_widget (GtkMessageDialog *dialog, const gchar *details)
 
        /* Find the secondary label and set its width_chars.   */
        /* Otherwise the label will tend to expand vertically. */
-       children = gtk_container_get_children (GTK_CONTAINER (message_area));
-       if (children && children->next && GTK_IS_LABEL (children->next->data)) {
-               gtk_label_set_width_chars (GTK_LABEL (children->next->data), 40);
+       child = gtk_widget_get_first_child (message_area);
+       if (child) {
+               GtkWidget *next = gtk_widget_get_next_sibling (child);
+               if (next && GTK_IS_LABEL (next))
+                       gtk_label_set_width_chars (GTK_LABEL (next), 40);
        }
 
        label = gtk_label_new (_("Details"));
diff --git a/src/gs-details-page.c b/src/gs-details-page.c
index f1b592ee7..ce2178a8c 100644
--- a/src/gs-details-page.c
+++ b/src/gs-details-page.c
@@ -969,7 +969,6 @@ static void
 gs_details_page_refresh_all (GsDetailsPage *self)
 {
        g_autoptr(GIcon) icon = NULL;
-       GList *addons;
        const gchar *tmp;
        g_autofree gchar *origin = NULL;
        g_autoptr(GPtrArray) version_history = NULL;
@@ -1142,9 +1141,7 @@ gs_details_page_refresh_all (GsDetailsPage *self)
        /* update progress */
        gs_details_page_refresh_progress (self);
 
-       addons = gtk_container_get_children (GTK_CONTAINER (self->list_box_addons));
-       gtk_widget_set_visible (self->box_addons, addons != NULL);
-       g_list_free (addons);
+       gtk_widget_set_visible (self->box_addons, gtk_widget_get_first_child (self->list_box_addons) != NULL);
 }
 
 static gint
@@ -1829,7 +1826,7 @@ gs_details_page_app_cancel_button_cb (GtkWidget *widget, GsDetailsPage *self)
 static void
 gs_details_page_app_install_button_cb (GtkWidget *widget, GsDetailsPage *self)
 {
-       g_autoptr(GList) addons = NULL;
+       GtkWidget *child;
 
        switch (gs_app_get_state (self->app)) {
        case GS_APP_STATE_PENDING_INSTALL:
@@ -1842,10 +1839,12 @@ gs_details_page_app_install_button_cb (GtkWidget *widget, GsDetailsPage *self)
        }
 
        /* Mark ticked addons to be installed together with the app */
-       addons = gtk_container_get_children (GTK_CONTAINER (self->list_box_addons));
-       for (GList *l = addons; l; l = l->next) {
-               if (gs_app_addon_row_get_selected (l->data)) {
-                       GsApp *addon = gs_app_addon_row_get_addon (l->data);
+       for (child = gtk_widget_get_first_child (self->list_box_addons);
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               GsAppAddonRow *row = GS_APP_ADDON_ROW (child);
+               if (gs_app_addon_row_get_selected (row)) {
+                       GsApp *addon = gs_app_addon_row_get_addon (row);
 
                        if (gs_app_get_state (addon) == GS_APP_STATE_AVAILABLE)
                                gs_app_set_to_be_installed (addon, TRUE);
@@ -2026,18 +2025,19 @@ gs_details_page_app_removed (GsPage *page, GsApp *app)
        gs_details_page_app_installed (page, app);
 }
 
-static void
-show_all_cb (GtkWidget *widget, gpointer user_data)
-{
-       gtk_widget_show (widget);
-}
-
 static void
 gs_details_page_more_reviews_button_cb (GtkWidget *widget, GsDetailsPage *self)
 {
+       GtkWidget *child;
+
        self->show_all_reviews = TRUE;
-       gtk_container_foreach (GTK_CONTAINER (self->list_box_reviews),
-                              show_all_cb, NULL);
+
+       for (child = gtk_widget_get_first_child (self->list_box_reviews);
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               gtk_widget_show (child);
+       }
+
        gtk_widget_set_visible (self->button_more_reviews, FALSE);
 }
 
diff --git a/src/gs-extras-page.c b/src/gs-extras-page.c
index cb2e862bc..4d45e944a 100644
--- a/src/gs-extras-page.c
+++ b/src/gs-extras-page.c
@@ -292,18 +292,17 @@ app_row_button_clicked_cb (GsAppRow *app_row,
 static void
 gs_extras_page_add_app (GsExtrasPage *self, GsApp *app, GsAppList *list, SearchData *search_data)
 {
-       GtkWidget *app_row;
-       g_autoptr(GList) existing_apps = NULL;
+       GtkWidget *app_row, *child;
 
        /* Don't add same app twice */
-       existing_apps = gtk_container_get_children (GTK_CONTAINER (self->list_box_results));
-       for (GList *l = existing_apps; l != NULL; l = l->next) {
+       for (child = gtk_widget_get_first_child (self->list_box_results);
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
                GsApp *existing_app;
 
-               existing_app = gs_app_row_get_app (GS_APP_ROW (l->data));
+               existing_app = gs_app_row_get_app (GS_APP_ROW (child));
                if (app == existing_app)
-                       gtk_list_box_remove (GTK_LIST_BOX (self->list_box_results),
-                                            GTK_WIDGET (l->data));
+                       gtk_list_box_remove (GTK_LIST_BOX (self->list_box_results), child);
        }
 
        app_row = gs_app_row_new (app);
@@ -452,22 +451,20 @@ static gchar *
 build_no_results_label (GsExtrasPage *self)
 {
        GsApp *app = NULL;
-       guint num;
+       guint num = 0;
        g_autofree gchar *codec_titles = NULL;
        g_autofree gchar *url = NULL;
-       g_autoptr(GList) list = NULL;
        g_autoptr(GPtrArray) array = NULL;
-
-       list = gtk_container_get_children (GTK_CONTAINER (self->list_box_results));
-       num = g_list_length (list);
-
-       g_assert (num > 0);
+       GtkWidget *child;
 
        array = g_ptr_array_new ();
-       for (GList *l = list; l != NULL; l = l->next) {
-               app = gs_app_row_get_app (GS_APP_ROW (l->data));
+       for (child = gtk_widget_get_first_child (self->list_box_results);
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               app = gs_app_row_get_app (GS_APP_ROW (child));
                g_ptr_array_add (array,
-                                g_object_get_data (G_OBJECT (l->data), "missing-title"));
+                                g_object_get_data (G_OBJECT (child), "missing-title"));
+               num++;
        }
        g_ptr_array_add (array, NULL);
 
@@ -499,21 +496,22 @@ build_no_results_label (GsExtrasPage *self)
 static void
 show_search_results (GsExtrasPage *self)
 {
+       GtkWidget *first_child, *child;
        GsApp *app;
        guint n_children;
        guint n_missing;
-       g_autoptr(GList) list = NULL;
-
-       list = gtk_container_get_children (GTK_CONTAINER (self->list_box_results));
-       n_children = g_list_length (list);
 
        /* count the number of rows with missing codecs */
-       n_missing = 0;
-       for (GList *l = list; l != NULL; l = l->next) {
-               app = gs_app_row_get_app (GS_APP_ROW (l->data));
+       n_children = n_missing = 0;
+       first_child = gtk_widget_get_first_child (self->list_box_results);
+       for (child = first_child;
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               app = gs_app_row_get_app (GS_APP_ROW (child));
                if (g_strcmp0 (gs_app_get_id (app), "missing-codec") == 0) {
                        n_missing++;
                }
+               n_children++;
        }
 
        if (n_children == 0 || n_children == n_missing) {
@@ -527,8 +525,8 @@ show_search_results (GsExtrasPage *self)
        } else if (n_children == 1) {
                /* switch directly to details view */
                g_debug ("extras: found one result, showing in details view");
-               g_assert (list != NULL);
-               app = gs_app_row_get_app (GS_APP_ROW (list->data));
+               g_assert (first_child != NULL);
+               app = gs_app_row_get_app (GS_APP_ROW (first_child));
                gs_shell_show_app (self->shell, app);
                if (gs_app_is_installed (app))
                        gs_extras_page_maybe_emit_installed_resources_done (self);
diff --git a/src/gs-featured-carousel.c b/src/gs-featured-carousel.c
index 20007b834..3bc816a9f 100644
--- a/src/gs-featured-carousel.c
+++ b/src/gs-featured-carousel.c
@@ -66,6 +66,18 @@ typedef enum {
 
 static guint obj_signals[SIGNAL_CLICKED + 1] = { 0, };
 
+static GtkWidget *
+get_nth_page_widget (GsFeaturedCarousel *self,
+                     guint               page_number)
+{
+       GtkWidget *page = gtk_widget_get_first_child (GTK_WIDGET (self->carousel));
+       guint i = 0;
+
+       while (page && i++ < page_number)
+               page = gtk_widget_get_next_sibling (page);
+       return page;
+}
+
 static void
 show_relative_page (GsFeaturedCarousel *self,
                     gint                delta)
@@ -73,7 +85,6 @@ show_relative_page (GsFeaturedCarousel *self,
        gdouble current_page = adw_carousel_get_position (self->carousel);
        guint n_pages = adw_carousel_get_n_pages (self->carousel);
        gdouble new_page;
-       g_autoptr(GList) children = gtk_container_get_children (GTK_CONTAINER (self->carousel));
        GtkWidget *new_page_widget;
        gint64 animation_duration_ms = adw_carousel_get_animation_duration (self->carousel);
 
@@ -84,7 +95,7 @@ show_relative_page (GsFeaturedCarousel *self,
         * a page by index, rather than by GtkWidget pointer.
         * See https://gitlab.gnome.org/GNOME/libhandy/-/issues/413 */
        new_page = ((guint) current_page + delta + n_pages) % n_pages;
-       new_page_widget = g_list_nth_data (children, new_page);
+       new_page_widget = get_nth_page_widget (self, new_page);
        g_assert (new_page_widget != NULL);
 
        /* Don’t animate if we’re wrapping from the last page back to the first
@@ -276,13 +287,11 @@ carousel_clicked_cb (GsFeaturedCarousel *carousel,
        GsAppTile *current_tile;
        GsApp *app;
        gdouble current_page;
-       g_autoptr(GList) children = NULL;
 
        /* Get the currently visible tile. */
        current_page = adw_carousel_get_position (self->carousel);
-       children = gtk_container_get_children (GTK_CONTAINER (self->carousel));
+       current_tile = GS_APP_TILE (get_nth_page_widget (self, current_page));
 
-       current_tile = g_list_nth_data (children, current_page);
        if (current_tile == NULL)
                return;
 
diff --git a/src/gs-installed-page.c b/src/gs-installed-page.c
index de8485c0e..7c451673d 100644
--- a/src/gs-installed-page.c
+++ b/src/gs-installed-page.c
@@ -139,11 +139,10 @@ gs_installed_page_app_removed (GsPage *page, GsApp *app)
        };
 
        for (gsize i = 0; lists[i]; i++) {
-               g_autoptr(GList) children = NULL;
-
-               children = gtk_container_get_children (GTK_CONTAINER (lists[i]));
-               for (GList *l = children; l; l = l->next) {
-                       GsAppRow *app_row = GS_APP_ROW (l->data);
+               for (GtkWidget *child = gtk_widget_get_first_child (lists[i]);
+                    child != NULL;
+                    child = gtk_widget_get_next_sibling (child)) {
+                       GsAppRow *app_row = GS_APP_ROW (child);
                        if (gs_app_row_get_app (app_row) == app) {
                                gs_installed_page_unreveal_row (app_row);
                        }
@@ -490,11 +489,10 @@ gs_installed_page_has_app (GsInstalledPage *self,
        };
 
        for (gsize i = 0; lists[i]; i++) {
-               g_autoptr(GList) children = NULL;
-
-               children = gtk_container_get_children (GTK_CONTAINER (lists[i]));
-               for (GList *l = children; l; l = l->next) {
-                       GsAppRow *app_row = GS_APP_ROW (l->data);
+               for (GtkWidget *child = gtk_widget_get_first_child (lists[i]);
+                    child != NULL;
+                    child = gtk_widget_get_next_sibling (child)) {
+                       GsAppRow *app_row = GS_APP_ROW (child);
                        if (gs_app_row_get_app (app_row) == app)
                                return TRUE;
                }
@@ -643,10 +641,7 @@ update_group_visibility_cb (GtkWidget *group,
                            GtkWidget *widget,
                            GtkWidget *list_box)
 {
-       g_autoptr(GList) children = NULL;
-
-       children = gtk_container_get_children (GTK_CONTAINER (list_box));
-       gtk_widget_set_visible (group, children != NULL);
+       gtk_widget_set_visible (group, gtk_widget_get_first_child (list_box) != NULL);
 }
 
 static void
diff --git a/src/gs-moderate-page.c b/src/gs-moderate-page.c
index bebc3ede2..a36247e08 100644
--- a/src/gs-moderate-page.c
+++ b/src/gs-moderate-page.c
@@ -51,25 +51,25 @@ static GParamSpec *obj_props[PROP_ODRS_PROVIDER + 1]  = { NULL, };
 static void
 gs_moderate_page_perhaps_hide_app_row (GsModeratePage *self, GsApp *app)
 {
+       GtkWidget *child;
        GsAppRow *app_row = NULL;
        gboolean is_visible = FALSE;
-       g_autoptr(GList) children = NULL;
 
-       children = gtk_container_get_children (GTK_CONTAINER (self->list_box_install));
-       for (GList *l = children; l != NULL; l = l->next) {
-               GtkWidget *w = GTK_WIDGET (l->data);
-               if (!gtk_widget_get_visible (w))
+       for (child = gtk_widget_get_first_child (self->list_box_install);
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               if (!gtk_widget_get_visible (child))
                        continue;
-               if (GS_IS_APP_ROW (w)) {
-                       GsApp *app_tmp = gs_app_row_get_app (GS_APP_ROW (w));
+               if (GS_IS_APP_ROW (child)) {
+                       GsApp *app_tmp = gs_app_row_get_app (GS_APP_ROW (child));
                        if (g_strcmp0 (gs_app_get_id (app),
                                       gs_app_get_id (app_tmp)) == 0) {
-                               app_row = GS_APP_ROW (w);
+                               app_row = GS_APP_ROW (child);
                                continue;
                        }
                }
-               if (GS_IS_REVIEW_ROW (w)) {
-                       GsApp *app_tmp = g_object_get_data (G_OBJECT (w), "GsApp");
+               if (GS_IS_REVIEW_ROW (child)) {
+                       GsApp *app_tmp = g_object_get_data (G_OBJECT (child), "GsApp");
                        if (g_strcmp0 (gs_app_get_id (app),
                                       gs_app_get_id (app_tmp)) == 0) {
                                is_visible = TRUE;
diff --git a/src/gs-removal-dialog.c b/src/gs-removal-dialog.c
index fcbde8a54..20e4318db 100644
--- a/src/gs-removal-dialog.c
+++ b/src/gs-removal-dialog.c
@@ -75,20 +75,20 @@ add_app (GtkListBox *listbox, GsApp *app)
 static void
 insert_details_widget (GtkMessageDialog *dialog, GtkWidget *widget)
 {
-       GList *children, *l;
-       GtkWidget *message_area;
+       GtkWidget *message_area, *child;
 
        message_area = gtk_message_dialog_get_message_area (dialog);
        g_assert (GTK_IS_BOX (message_area));
 
        /* find all label children and set the width chars properties */
-       children = gtk_container_get_children (GTK_CONTAINER (message_area));
-       for (l = children; l != NULL; l = l->next) {
-               if (!GTK_IS_LABEL (l->data))
+       for (child = gtk_widget_get_first_child (message_area);
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               if (!GTK_IS_LABEL (child))
                        continue;
 
-               gtk_label_set_width_chars (GTK_LABEL (l->data), 40);
-               gtk_label_set_max_width_chars (GTK_LABEL (l->data), 40);
+               gtk_label_set_width_chars (GTK_LABEL (child), 40);
+               gtk_label_set_max_width_chars (GTK_LABEL (child), 40);
        }
 
        gtk_box_append (GTK_BOX (message_area), widget);
diff --git a/src/gs-screenshot-carousel.c b/src/gs-screenshot-carousel.c
index 8857590c3..6dca947e9 100644
--- a/src/gs-screenshot-carousel.c
+++ b/src/gs-screenshot-carousel.c
@@ -184,8 +184,14 @@ _carousel_navigate (AdwCarousel *carousel, AdwNavigationDirection direction)
        gdouble position;
        guint n_children;
 
-       children = gtk_container_get_children (GTK_CONTAINER (carousel));
-       n_children = g_list_length (children);
+       n_children = 0;
+       for (child = gtk_widget_get_first_child (GTK_WIDGET (carousel));
+            child != NULL;
+            child = gtk_widget_get_next_sibling (child)) {
+               children = g_list_prepend (children, child);
+               n_children++;
+       }
+       children = g_list_reverse (children);
 
        position = adw_carousel_get_position (carousel);
        position += (direction == ADW_NAVIGATION_DIRECTION_BACK) ? -1 : 1;
diff --git a/src/gs-updates-section.c b/src/gs-updates-section.c
index f09a33e8f..e6203f126 100644
--- a/src/gs-updates-section.c
+++ b/src/gs-updates-section.c
@@ -163,12 +163,9 @@ gs_updates_section_add_app (GsUpdatesSection *self, GsApp *app)
 void
 gs_updates_section_remove_all (GsUpdatesSection *self)
 {
-       g_autoptr(GList) children = NULL;
-       children = gtk_container_get_children (GTK_CONTAINER (self));
-       for (GList *l = children; l != NULL; l = l->next) {
-               GtkWidget *w = GTK_WIDGET (l->data);
-               gtk_list_box_remove (GTK_LIST_BOX (self->listbox), GTK_WIDGET (w));
-       }
+       GtkWidget *child;
+       while ((child = gtk_widget_get_first_child (self->listbox)) != NULL)
+               gtk_list_box_remove (GTK_LIST_BOX (self->listbox), child);
        gs_app_list_remove_all (self->list);
        gtk_widget_hide (GTK_WIDGET (self));
 }


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