[gnome-software] Break out the app tiles as a sepate widget



commit b39294d78d7207bf02963cd376e60f3d98e4bcd7
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Sep 7 12:52:30 2013 -0400

    Break out the app tiles as a sepate widget
    
    One more template conversion done.

 src/Makefile.am                  |    2 +
 src/app-tile.ui                  |   65 +++++++++++++++++
 src/gnome-software.gresource.xml |    1 +
 src/gs-app-tile.c                |  143 ++++++++++++++++++++++++++++++++++++++
 src/gs-app-tile.h                |   64 +++++++++++++++++
 src/gs-shell-category.c          |   50 ++------------
 6 files changed, 281 insertions(+), 44 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index e510bb6..63b34f1 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -45,6 +45,8 @@ gnome_software_SOURCES =                              \
        gs-feature-tile.h                               \
        gs-category-tile.c                              \
        gs-category-tile.h                              \
+       gs-app-tile.c                                   \
+       gs-app-tile.h                                   \
        gs-box.h                                        \
        gs-box.c                                        \
        gs-plugin.c                                     \
diff --git a/src/app-tile.ui b/src/app-tile.ui
new file mode 100644
index 0000000..f3a0295
--- /dev/null
+++ b/src/app-tile.ui
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.10 -->
+  <template class="GsAppTile" parent="GtkBin">
+    <property name="visible">True</property>
+    <child>
+      <object class="GtkButton" id="button">
+        <property name="visible">True</property>
+        <property name="hexpand">True</property>
+        <style>
+          <class name="view"/>
+          <class name="tile"/>
+        </style>
+        <child>
+          <object class="GtkGrid" id="grid">
+            <property name="visible">True</property>
+            <property name="margin">12</property>
+            <property name="row-spacing">6</property>
+            <property name="column-spacing">6</property>
+            <child>
+              <object class="GtkImage" id="image">
+                <property name="visible">True</property>
+              </object>
+              <packing>
+                <property name="left-attach">0</property>
+                <property name="top-attach">0</property>
+                <property name="width">1</property>
+                <property name="height">2</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="name">
+                <property name="visible">True</property>
+                <property name="ellipsize">end</property>
+                <property name="xalign">0.0</property>
+                <attributes>
+                  <attribute name="weight" value="bold"/>
+                </attributes>
+              </object>
+              <packing>
+                <property name="left-attach">1</property>
+                <property name="top-attach">0</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="summary">
+                <property name="visible">True</property>
+                <property name="ellipsize">end</property>
+                <property name="xalign">0.0</property>
+              </object>
+              <packing>
+                <property name="left-attach">1</property>
+                <property name="top-attach">1</property>
+                <property name="width">1</property>
+                <property name="height">1</property>
+              </packing>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/gnome-software.gresource.xml b/src/gnome-software.gresource.xml
index 97ffd37..139d1bc 100644
--- a/src/gnome-software.gresource.xml
+++ b/src/gnome-software.gresource.xml
@@ -6,6 +6,7 @@
   <file preprocess="xml-stripblanks">popular-tile.ui</file>
   <file preprocess="xml-stripblanks">feature-tile.ui</file>
   <file preprocess="xml-stripblanks">category-tile.ui</file>
+  <file preprocess="xml-stripblanks">app-tile.ui</file>
   <file preprocess="xml-stripblanks">app-widget.ui</file>
   <file>gtk-style.css</file>
   <file>shadow.png</file>
diff --git a/src/gs-app-tile.c b/src/gs-app-tile.c
new file mode 100644
index 0000000..f6b0135
--- /dev/null
+++ b/src/gs-app-tile.c
@@ -0,0 +1,143 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include "gs-app-tile.h"
+
+struct _GsAppTilePrivate
+{
+       GsApp           *app;
+       GtkWidget       *button;
+       GtkWidget       *image;
+       GtkWidget       *name;
+       GtkWidget       *summary;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GsAppTile, gs_app_tile, GTK_TYPE_BIN)
+
+enum {
+       SIGNAL_CLICKED,
+       SIGNAL_LAST
+};
+
+static guint signals [SIGNAL_LAST] = { 0 };
+
+GsApp *
+gs_app_tile_get_app (GsAppTile *tile)
+{
+        GsAppTilePrivate *priv;
+
+       g_return_val_if_fail (GS_IS_APP_TILE (tile), NULL);
+
+        priv = gs_app_tile_get_instance_private (tile);
+       return priv->app;
+}
+
+void
+gs_app_tile_set_app (GsAppTile *tile, GsApp *app)
+{
+        GsAppTilePrivate *priv;
+        const gchar *summary;
+
+       g_return_if_fail (GS_IS_APP_TILE (tile));
+       g_return_if_fail (GS_IS_APP (app));
+
+        priv = gs_app_tile_get_instance_private (tile);
+
+        g_clear_object (&priv->app);
+       priv->app = g_object_ref (app);
+
+        gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), gs_app_get_pixbuf (app));
+        gtk_label_set_label (GTK_LABEL (priv->name), gs_app_get_name (app));
+        summary = gs_app_get_summary (app);
+        gtk_label_set_label (GTK_LABEL (priv->summary), summary);
+        gtk_widget_set_visible (priv->summary, summary && summary[0]);
+}
+
+static void
+gs_app_tile_destroy (GtkWidget *widget)
+{
+       GsAppTile *tile = GS_APP_TILE (widget);
+       GsAppTilePrivate *priv;
+
+        priv = gs_app_tile_get_instance_private (tile);
+
+       g_clear_object (&priv->app);
+
+       GTK_WIDGET_CLASS (gs_app_tile_parent_class)->destroy (widget);
+}
+
+static void
+button_clicked (GsAppTile *tile)
+{
+        g_signal_emit (tile, signals[SIGNAL_CLICKED], 0);
+}
+
+static void
+gs_app_tile_init (GsAppTile *tile)
+{
+        GsAppTilePrivate *priv;
+
+        gtk_widget_set_has_window (GTK_WIDGET (tile), FALSE);
+        gtk_widget_init_template (GTK_WIDGET (tile));
+        priv = gs_app_tile_get_instance_private (tile);
+        g_signal_connect_swapped (priv->button, "clicked",
+                                  G_CALLBACK (button_clicked), tile);
+}
+
+static void
+gs_app_tile_class_init (GsAppTileClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+       widget_class->destroy = gs_app_tile_destroy;
+
+       signals [SIGNAL_CLICKED] =
+               g_signal_new ("clicked",
+                             G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (GsAppTileClass, clicked),
+                             NULL, NULL, g_cclosure_marshal_VOID__VOID,
+                             G_TYPE_NONE, 0);
+
+        gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/software/app-tile.ui");
+
+        gtk_widget_class_bind_template_child_private (widget_class, GsAppTile, button);
+        gtk_widget_class_bind_template_child_private (widget_class, GsAppTile, image);
+        gtk_widget_class_bind_template_child_private (widget_class, GsAppTile, name);
+        gtk_widget_class_bind_template_child_private (widget_class, GsAppTile, summary);
+}
+
+GtkWidget *
+gs_app_tile_new (GsApp *cat)
+{
+        GsAppTile *tile;
+
+        tile = g_object_new (GS_TYPE_APP_TILE, NULL);
+        gs_app_tile_set_app (tile, cat);
+
+        return GTK_WIDGET (tile);
+}
+
diff --git a/src/gs-app-tile.h b/src/gs-app-tile.h
new file mode 100644
index 0000000..aaff60e
--- /dev/null
+++ b/src/gs-app-tile.h
@@ -0,0 +1,64 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef GS_APP_TILE_H
+#define GS_APP_TILE_H
+
+#include <gtk/gtk.h>
+
+#include "gs-app.h"
+
+#define GS_TYPE_APP_TILE               (gs_app_tile_get_type())
+#define GS_APP_TILE(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj), GS_TYPE_APP_TILE, GsAppTile))
+#define GS_APP_TILE_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST((cls), GS_TYPE_APP_TILE, GsAppTileClass))
+#define GS_IS_APP_TILE(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj), GS_TYPE_APP_TILE))
+#define GS_IS_APP_TILE_CLASS(cls)      (G_TYPE_CHECK_CLASS_TYPE((cls), GS_TYPE_APP_TILE))
+#define GS_APP_TILE_GET_CLASS(obj)     (G_TYPE_INSTANCE_GET_CLASS((obj), GS_TYPE_APP_TILE, GsAppTileClass))
+
+G_BEGIN_DECLS
+
+typedef struct _GsAppTile              GsAppTile;
+typedef struct _GsAppTileClass         GsAppTileClass;
+typedef struct _GsAppTilePrivate       GsAppTilePrivate;
+
+struct _GsAppTile
+{
+       GtkBin                   parent;
+       GsAppTilePrivate        *priv;
+};
+
+struct _GsAppTileClass
+{
+       GtkBinClass              parent_class;
+
+       void                    (*clicked)      (GsAppTile      *tile);
+};
+
+GType           gs_app_tile_get_type   (void);
+GtkWidget       *gs_app_tile_new       (GsApp          *app);
+GsApp           *gs_app_tile_get_app    (GsAppTile     *tile);
+void            gs_app_tile_set_app    (GsAppTile      *tile,
+                                        GsApp          *cat);
+
+G_END_DECLS
+
+#endif /* GS_APP_TILE_H */
+
diff --git a/src/gs-shell-category.c b/src/gs-shell-category.c
index 46afa1d..7dd35af 100644
--- a/src/gs-shell-category.c
+++ b/src/gs-shell-category.c
@@ -25,6 +25,7 @@
 #include <glib/gi18n.h>
 
 #include "gs-utils.h"
+#include "gs-app-tile.h"
 #include "gs-shell-category.h"
 
 struct GsShellCategoryPrivate {
@@ -57,56 +58,15 @@ gs_shell_category_refresh (GsShellCategory *shell)
 }
 
 static void
-app_tile_clicked (GtkButton *button, gpointer data)
+app_tile_clicked (GsAppTile *tile, gpointer data)
 {
         GsShellCategory *shell = GS_SHELL_CATEGORY (data);
         GsApp *app;
 
-        app = g_object_get_data (G_OBJECT (button), "app");
+        app = gs_app_tile_get_app (tile);
         gs_shell_show_app (shell->priv->shell, app);
 }
 
-static GtkWidget *
-create_app_tile (GsShellCategory *shell, GsApp *app)
-{
-        GtkWidget *button, *label;
-        GtkWidget *image, *grid;
-        const gchar *tmp;
-        PangoAttrList *attrs;
-
-        button = gtk_button_new ();
-        gtk_widget_set_hexpand (button, TRUE);
-        gtk_style_context_add_class (gtk_widget_get_style_context (button), "view");
-        gtk_style_context_add_class (gtk_widget_get_style_context (button), "tile");
-        grid = gtk_grid_new ();
-        gtk_container_add (GTK_CONTAINER (button), grid);
-        g_object_set (grid, "margin", 12, "row-spacing", 6, "column-spacing", 6, NULL);
-        image = gtk_image_new_from_pixbuf (gs_app_get_pixbuf (app));
-        gtk_grid_attach (GTK_GRID (grid), image, 0, 0, 1, 2);
-        label = gtk_label_new (gs_app_get_name (app));
-        gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
-        attrs = pango_attr_list_new ();
-        pango_attr_list_insert (attrs, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
-        gtk_label_set_attributes (GTK_LABEL (label), attrs);
-        pango_attr_list_unref (attrs);
-        g_object_set (label, "xalign", 0, NULL);
-        gtk_grid_attach (GTK_GRID (grid), label, 1, 0, 1, 1);
-        tmp = gs_app_get_summary (app);
-        if (tmp != NULL && tmp[0] != '\0') {
-                label = gtk_label_new (tmp);
-                g_object_set (label, "xalign", 0, NULL);
-                gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
-                gtk_grid_attach (GTK_GRID (grid), label, 1, 1, 1, 1);
-        }
-
-        gtk_widget_show_all (button);
-        g_object_set_data_full (G_OBJECT (button), "app", g_object_ref (app), g_object_unref);
-        g_signal_connect (button, "clicked",
-                          G_CALLBACK (app_tile_clicked), shell);
-
-        return button;
-}
-
 /**
  * gs_shell_category_get_apps_cb:
  **/
@@ -142,7 +102,9 @@ gs_shell_category_get_apps_cb (GObject *source_object,
 
         for (l = list, i = 0; l != NULL; l = l->next, i++) {
                 app = GS_APP (l->data);
-                tile = create_app_tile (shell, app);
+                tile = gs_app_tile_new (app);
+                g_signal_connect (tile, "clicked",
+                                  G_CALLBACK (app_tile_clicked), shell);
                 gtk_grid_attach (GTK_GRID (grid), tile, (i % 2), i / 2, 1, 1);
         }
 


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