[gnome-software: 10/24] gs-category-page: Decouple from GsShell




commit 50e0c1eaa178a2c767b7e1c8fafa9036d5d1c48a
Author: Philip Withnall <pwithnall endlessos org>
Date:   Thu Apr 22 14:34:11 2021 +0100

    gs-category-page: Decouple from GsShell
    
    Add a signal for when an app is clicked, and connect to that in the
    `GsShell` to show an app’s details — rather than keeping a pointer to
    the shell in `GsCategoryPage` and calling a method on it directly.
    
    This removes a pointer loop between `GsShell` and `GsCategoryPage`, and
    makes the category page more reusable in future.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 src/gs-category-page.c | 25 ++++++++++++++++++++++---
 src/gs-shell.c         | 11 +++++++++++
 src/gs-shell.ui        |  1 +
 3 files changed, 34 insertions(+), 3 deletions(-)
---
diff --git a/src/gs-category-page.c b/src/gs-category-page.c
index cc1e676ad..78fd5fd1f 100644
--- a/src/gs-category-page.c
+++ b/src/gs-category-page.c
@@ -25,7 +25,6 @@ struct _GsCategoryPage
 
        GsPluginLoader  *plugin_loader;
        GCancellable    *cancellable;
-       GsShell         *shell;
        GsCategory      *category;
        GsCategory      *subcategory;
 
@@ -40,6 +39,12 @@ typedef enum {
        PROP_TITLE = 1,
 } GsCategoryPageProperty;
 
+typedef enum {
+       SIGNAL_APP_CLICKED,
+} GsCategoryPageSignal;
+
+static guint obj_signals[SIGNAL_APP_CLICKED + 1] = { 0, };
+
 static void
 app_tile_clicked (GsAppTile *tile, gpointer data)
 {
@@ -47,7 +52,7 @@ app_tile_clicked (GsAppTile *tile, gpointer data)
        GsApp *app;
 
        app = gs_app_tile_get_app (tile);
-       gs_shell_show_app (self->shell, app);
+       g_signal_emit (self, obj_signals[SIGNAL_APP_CLICKED], 0, app);
 }
 
 static void
@@ -335,7 +340,6 @@ gs_category_page_setup (GsPage *page,
        GsCategoryPage *self = GS_CATEGORY_PAGE (page);
 
        self->plugin_loader = g_object_ref (plugin_loader);
-       self->shell = shell;
 
        return TRUE;
 }
@@ -355,6 +359,21 @@ gs_category_page_class_init (GsCategoryPageClass *klass)
 
        g_object_class_override_property (object_class, PROP_TITLE, "title");
 
+       /**
+        * GsCategoryPage::app-clicked:
+        * @app: the #GsApp which was clicked on
+        *
+        * Emitted when one of the app tiles is clicked. Typically the caller
+        * should display the details of the given app in the callback.
+        *
+        * Since: 41
+        */
+       obj_signals[SIGNAL_APP_CLICKED] =
+               g_signal_new ("app-clicked",
+                             G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+                             0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
+                             G_TYPE_NONE, 1, GS_TYPE_APP);
+
        gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Software/gs-category-page.ui");
 
        gtk_widget_class_bind_template_child (widget_class, GsCategoryPage, category_detail_box);
diff --git a/src/gs-shell.c b/src/gs-shell.c
index e1308a003..64bc01161 100644
--- a/src/gs-shell.c
+++ b/src/gs-shell.c
@@ -2071,6 +2071,16 @@ updates_page_notify_counter_cb (GObject    *obj,
                                 NULL);
 }
 
+static void
+category_page_app_clicked_cb (GsCategoryPage *page,
+                              GsApp          *app,
+                              gpointer        user_data)
+{
+       GsShell *shell = GS_SHELL (user_data);
+
+       gs_shell_show_app (shell, app);
+}
+
 void
 gs_shell_setup (GsShell *shell, GsPluginLoader *plugin_loader, GCancellable *cancellable)
 {
@@ -2375,6 +2385,7 @@ gs_shell_class_init (GsShellClass *klass)
        gtk_widget_class_bind_template_callback (widget_class, gs_shell_back_button_cb);
        gtk_widget_class_bind_template_callback (widget_class, gs_overview_page_button_cb);
        gtk_widget_class_bind_template_callback (widget_class, updates_page_notify_counter_cb);
+       gtk_widget_class_bind_template_callback (widget_class, category_page_app_clicked_cb);
        gtk_widget_class_bind_template_callback (widget_class, search_button_clicked_cb);
        gtk_widget_class_bind_template_callback (widget_class, search_changed_handler);
        gtk_widget_class_bind_template_callback (widget_class, gs_shell_plugin_events_sources_cb);
diff --git a/src/gs-shell.ui b/src/gs-shell.ui
index ac4bc6dd3..a39c3bc4f 100644
--- a/src/gs-shell.ui
+++ b/src/gs-shell.ui
@@ -532,6 +532,7 @@
                         <child>
                           <object class="GsCategoryPage" id="category_page">
                             <property name="visible">True</property>
+                            <signal name="app-clicked" handler="category_page_app_clicked_cb"/>
                           </object>
                           <packing>
                             <property name="name">category</property>


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