[libdazzle] suggestion: add get_icon() vfunc



commit aa97e96b90ba9c3dfd519724519ea37063d7dba0
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jul 13 17:26:34 2018 -0700

    suggestion: add get_icon() vfunc
    
    This is useful in situations where the GIcon needs to be determined at
    runtime. This doesn't necessarily solve the issue we need with Epiphany,
    but it is closer than what is available currently.

 src/suggestions/dzl-suggestion-row.c |  6 ++---
 src/suggestions/dzl-suggestion.c     | 47 ++++++++++++++++++++++++++++++++++++
 src/suggestions/dzl-suggestion.h     |  6 +++--
 3 files changed, 54 insertions(+), 5 deletions(-)
---
diff --git a/src/suggestions/dzl-suggestion-row.c b/src/suggestions/dzl-suggestion-row.c
index e1d96fd..a623395 100644
--- a/src/suggestions/dzl-suggestion-row.c
+++ b/src/suggestions/dzl-suggestion-row.c
@@ -61,14 +61,14 @@ static void
 dzl_suggestion_row_connect (DzlSuggestionRow *self)
 {
   DzlSuggestionRowPrivate *priv = dzl_suggestion_row_get_instance_private (self);
-  const gchar *icon_name;
+  g_autoptr(GIcon) icon = NULL;
   const gchar *subtitle;
 
   g_return_if_fail (DZL_IS_SUGGESTION_ROW (self));
   g_return_if_fail (priv->suggestion != NULL);
 
-  icon_name = dzl_suggestion_get_icon_name (priv->suggestion);
-  g_object_set (priv->image, "icon-name", icon_name, NULL);
+  icon = dzl_suggestion_get_icon (priv->suggestion);
+  gtk_image_set_from_gicon (priv->image, icon, GTK_ICON_SIZE_MENU);
 
   gtk_label_set_label (priv->title, dzl_suggestion_get_title (priv->suggestion));
 
diff --git a/src/suggestions/dzl-suggestion.c b/src/suggestions/dzl-suggestion.c
index eb99ca2..6d9b949 100644
--- a/src/suggestions/dzl-suggestion.c
+++ b/src/suggestions/dzl-suggestion.c
@@ -31,11 +31,14 @@ typedef struct
 
   /* interned string */
   const gchar *icon_name;
+
+  GIcon *icon;
 } DzlSuggestionPrivate;
 
 enum {
   PROP_0,
   PROP_ICON_NAME,
+  PROP_ICON,
   PROP_ID,
   PROP_SUBTITLE,
   PROP_TITLE,
@@ -53,6 +56,19 @@ G_DEFINE_TYPE_WITH_PRIVATE (DzlSuggestion, dzl_suggestion, G_TYPE_OBJECT)
 static GParamSpec *properties [N_PROPS];
 static guint signals [N_SIGNALS];
 
+static GIcon *
+dzl_suggestion_real_get_icon (DzlSuggestion *self)
+{
+  DzlSuggestionPrivate *priv = dzl_suggestion_get_instance_private (self);
+
+  g_assert (DZL_IS_SUGGESTION (self));
+
+  if (priv->icon_name != NULL)
+    return g_icon_new_for_string (priv->icon_name, NULL);
+
+  return NULL;
+}
+
 static void
 dzl_suggestion_finalize (GObject *object)
 {
@@ -86,6 +102,10 @@ dzl_suggestion_get_property (GObject    *object,
       g_value_set_static_string (value, dzl_suggestion_get_icon_name (self));
       break;
 
+    case PROP_ICON:
+      g_value_take_object (value, dzl_suggestion_get_icon (self));
+      break;
+
     case PROP_TITLE:
       g_value_set_string (value, dzl_suggestion_get_title (self));
       break;
@@ -139,6 +159,8 @@ dzl_suggestion_class_init (DzlSuggestionClass *klass)
   object_class->get_property = dzl_suggestion_get_property;
   object_class->set_property = dzl_suggestion_set_property;
 
+  klass->get_icon = dzl_suggestion_real_get_icon;
+
   properties [PROP_ID] =
     g_param_spec_string ("id",
                          "Id",
@@ -146,6 +168,13 @@ dzl_suggestion_class_init (DzlSuggestionClass *klass)
                          NULL,
                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_ICON] =
+    g_param_spec_object ("icon",
+                         "Icon",
+                         "The GIcon for the suggestion",
+                         G_TYPE_ICON,
+                         (G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
   properties [PROP_ICON_NAME] =
     g_param_spec_string ("icon-name",
                          "Icon Name",
@@ -356,3 +385,21 @@ dzl_suggestion_replace_typed_text (DzlSuggestion *self,
 
   return ret;
 }
+
+/**
+ * dzl_suggestion_get_icon:
+ * @self: a #DzlSuggestion
+ *
+ * Gets the icon for the suggestion, if any.
+ *
+ * Returns: (transfer full) (nullable): a #GIcon or %NULL
+ *
+ * Since: 3.30
+ */
+GIcon *
+dzl_suggestion_get_icon (DzlSuggestion *self)
+{
+  g_return_val_if_fail (DZL_IS_SUGGESTION (self), NULL);
+
+  return DZL_SUGGESTION_GET_CLASS (self)->get_icon (self);
+}
diff --git a/src/suggestions/dzl-suggestion.h b/src/suggestions/dzl-suggestion.h
index 78d16a3..5fc141b 100644
--- a/src/suggestions/dzl-suggestion.h
+++ b/src/suggestions/dzl-suggestion.h
@@ -20,7 +20,7 @@
 #ifndef DZL_SUGGESTION_H
 #define DZL_SUGGESTION_H
 
-#include <glib-object.h>
+#include <gio/gio.h>
 
 #include "dzl-version-macros.h"
 
@@ -39,8 +39,8 @@ struct _DzlSuggestionClass
                                 const gchar   *typed_text);
   gchar *(*replace_typed_text) (DzlSuggestion *self,
                                 const gchar   *typed_text);
+  GIcon *(*get_icon)           (DzlSuggestion *self);
 
-  gpointer _reserved1;
   gpointer _reserved2;
   gpointer _reserved3;
   gpointer _reserved4;
@@ -74,6 +74,8 @@ gchar         *dzl_suggestion_suggest_suffix     (DzlSuggestion *self,
 DZL_AVAILABLE_IN_ALL
 gchar         *dzl_suggestion_replace_typed_text (DzlSuggestion *self,
                                                   const gchar   *typed_text);
+DZL_AVAILABLE_IN_3_30
+GIcon         *dzl_suggestion_get_icon           (DzlSuggestion *self);
 
 G_END_DECLS
 


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