[libhandy] avatar: Add the icon-name property



commit cde0693c1e7c035dc74557a27bccb527b6a2c850
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Tue Jul 14 22:10:33 2020 +0200

    avatar: Add the icon-name property
    
    This allows setting a different default icon than
    avatar-default-symbolic.
    
    Fixes https://gitlab.gnome.org/GNOME/libhandy/-/issues/282

 debian/libhandy-1-0.symbols |  2 +
 src/hdy-avatar.c            | 90 ++++++++++++++++++++++++++++++++++++++++++++-
 src/hdy-avatar.h            |  3 ++
 tests/test-avatar.c         | 14 +++++++
 4 files changed, 107 insertions(+), 2 deletions(-)
---
diff --git a/debian/libhandy-1-0.symbols b/debian/libhandy-1-0.symbols
index 9a0a90f3..711d8a15 100644
--- a/debian/libhandy-1-0.symbols
+++ b/debian/libhandy-1-0.symbols
@@ -16,11 +16,13 @@ libhandy-1.so.0 libhandy-1-0 #MINVER#
  hdy_action_row_set_use_underline@LIBHANDY_1_0 0.0.6
  hdy_application_window_get_type@LIBHANDY_1_0 0.80.0
  hdy_application_window_new@LIBHANDY_1_0 0.80.0
+ hdy_avatar_get_icon_name@LIBHANDY_1_0 0.85.0
  hdy_avatar_get_show_initials@LIBHANDY_1_0 0.80.0
  hdy_avatar_get_size@LIBHANDY_1_0 0.80.0
  hdy_avatar_get_text@LIBHANDY_1_0 0.80.0
  hdy_avatar_get_type@LIBHANDY_1_0 0.80.0
  hdy_avatar_new@LIBHANDY_1_0 0.80.0
+ hdy_avatar_set_icon_name@LIBHANDY_1_0 0.85.0
  hdy_avatar_set_image_load_func@LIBHANDY_1_0 0.80.0
  hdy_avatar_set_show_initials@LIBHANDY_1_0 0.80.0
  hdy_avatar_set_size@LIBHANDY_1_0 0.80.0
diff --git a/src/hdy-avatar.c b/src/hdy-avatar.c
index 357f8d03..76fb5ced 100644
--- a/src/hdy-avatar.c
+++ b/src/hdy-avatar.c
@@ -68,6 +68,7 @@ struct _HdyAvatar
 {
   GtkDrawingArea parent_instance;
 
+  gchar *icon_name;
   gchar *text;
   PangoLayout *layout;
   gboolean show_initials;
@@ -84,6 +85,7 @@ G_DEFINE_TYPE (HdyAvatar, hdy_avatar, GTK_TYPE_DRAWING_AREA);
 
 enum {
   PROP_0,
+  PROP_ICON_NAME,
   PROP_TEXT,
   PROP_SHOW_INITIALS,
   PROP_SIZE,
@@ -264,6 +266,10 @@ hdy_avatar_get_property (GObject    *object,
   HdyAvatar *self = HDY_AVATAR (object);
 
   switch (property_id) {
+  case PROP_ICON_NAME:
+    g_value_set_string (value, hdy_avatar_get_icon_name (self));
+    break;
+
   case PROP_TEXT:
     g_value_set_string (value, hdy_avatar_get_text (self));
     break;
@@ -291,6 +297,10 @@ hdy_avatar_set_property (GObject      *object,
   HdyAvatar *self = HDY_AVATAR (object);
 
   switch (property_id) {
+  case PROP_ICON_NAME:
+    hdy_avatar_set_icon_name (self, g_value_get_string (value));
+    break;
+
   case PROP_TEXT:
     hdy_avatar_set_text (self, g_value_get_string (value));
     break;
@@ -314,6 +324,7 @@ hdy_avatar_finalize (GObject *object)
 {
   HdyAvatar *self = HDY_AVATAR (object);
 
+  g_clear_pointer (&self->icon_name, g_free);
   g_clear_pointer (&self->text, g_free);
   g_clear_pointer (&self->round_image, cairo_surface_destroy);
   g_clear_object (&self->layout);
@@ -335,6 +346,7 @@ hdy_avatar_draw (GtkWidget *widget,
   gint size = MIN (width, height);
   gdouble x = (gdouble)(width - size) / 2.0;
   gdouble y = (gdouble)(height - size) / 2.0;
+  const gchar *icon_name;
   gint scale;
   GdkRGBA color;
   g_autoptr (GtkIconInfo) icon = NULL;
@@ -368,15 +380,17 @@ hdy_avatar_draw (GtkWidget *widget,
     return FALSE;
   }
 
+  icon_name = self->icon_name && *self->icon_name != '\0' ?
+    self->icon_name : "avatar-default-symbolic";
   scale = gtk_widget_get_scale_factor (widget);
   icon = gtk_icon_theme_lookup_icon_for_scale (gtk_icon_theme_get_default (),
-                                     "avatar-default-symbolic",
+                                     icon_name,
                                      size / 2, scale,
                                      GTK_ICON_LOOKUP_FORCE_SYMBOLIC);
   gtk_style_context_get_color (context, gtk_style_context_get_state (context), &color);
   pixbuf = gtk_icon_info_load_symbolic (icon, &color, NULL, NULL, NULL, NULL, &error);
   if (error != NULL) {
-    g_critical ("Failed to load avatar-default-symbolic: %s", error->message);
+    g_critical ("Failed to load icon `%s': %s", icon_name, error->message);
 
     return FALSE;
   }
@@ -504,6 +518,26 @@ hdy_avatar_class_init (HdyAvatarClass *klass)
                       "The size of the avatar",
                       -1, INT_MAX, -1,
                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+
+  /**
+   * HdyAvatar:icon-name:
+   *
+   * The name of the icon in the icon theme to use when the icon should be
+   * displayed.
+   * If no name is set, the avatar-default-symbolic icon will be used.
+   * If the name doesn't match a valid icon, it is an error and no icon will be
+   * displayed.
+   * If the icon theme is changed, the image will be updated automatically.
+   *
+   * Since: 1.0
+   */
+  props[PROP_ICON_NAME] =
+    g_param_spec_string ("icon-name",
+                         "Icon name",
+                         "The name of the icon from the icon theme",
+                         NULL,
+                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+
   /**
    * HdyAvatar:text:
    *
@@ -567,6 +601,58 @@ hdy_avatar_new (gint         size,
                        NULL);
 }
 
+/**
+ * hdy_avatar_get_icon_name:
+ * @self: a #HdyAvatar
+ *
+ * Gets the name of the icon in the icon theme to use when the icon should be
+ * displayed.
+ *
+ * Returns: (nullable) (transfer none): the name of the icon from the icon theme.
+ *
+ * Since: 1.0
+ */
+const gchar *
+hdy_avatar_get_icon_name (HdyAvatar *self)
+{
+  g_return_val_if_fail (HDY_IS_AVATAR (self), NULL);
+
+  return self->icon_name;
+}
+
+/**
+ * hdy_avatar_set_icon_name:
+ * @self: a #HdyAvatar
+ * @icon_name: (nullable): the name of the icon from the icon theme
+ *
+ * Sets the name of the icon in the icon theme to use when the icon should be
+ * displayed.
+ * If no name is set, the avatar-default-symbolic icon will be used.
+ * If the name doesn't match a valid icon, it is an error and no icon will be
+ * displayed.
+ * If the icon theme is changed, the image will be updated automatically.
+ *
+ * Since: 1.0
+ */
+void
+hdy_avatar_set_icon_name (HdyAvatar   *self,
+                          const gchar *icon_name)
+{
+  g_return_if_fail (HDY_IS_AVATAR (self));
+
+  if (g_strcmp0 (self->icon_name, icon_name) == 0)
+    return;
+
+  g_clear_pointer (&self->icon_name, g_free);
+  self->icon_name = g_strdup (icon_name);
+
+  if (!self->round_image &&
+      (!self->show_initials || self->layout == NULL))
+    gtk_widget_queue_draw (GTK_WIDGET (self));
+
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ICON_NAME]);
+}
+
 /**
  * hdy_avatar_get_text:
  * @self: a #HdyAvatar
diff --git a/src/hdy-avatar.h b/src/hdy-avatar.h
index b92ef4e5..af7b782e 100644
--- a/src/hdy-avatar.h
+++ b/src/hdy-avatar.h
@@ -37,6 +37,9 @@ typedef GdkPixbuf *(*HdyAvatarImageLoadFunc) (gint     size,
 GtkWidget   *hdy_avatar_new                 (gint                    size,
                                              const gchar            *text,
                                              gboolean                show_initials);
+const gchar *hdy_avatar_get_icon_name       (HdyAvatar              *self);
+void         hdy_avatar_set_icon_name       (HdyAvatar              *self,
+                                             const gchar            *icon_name);
 const gchar *hdy_avatar_get_text            (HdyAvatar              *self);
 void         hdy_avatar_set_text            (HdyAvatar              *self,
                                              const gchar            *text);
diff --git a/tests/test-avatar.c b/tests/test-avatar.c
index 218ab7d2..c1a5fbad 100644
--- a/tests/test-avatar.c
+++ b/tests/test-avatar.c
@@ -7,6 +7,7 @@
 #define HANDY_USE_UNSTABLE_API
 #include <handy.h>
 
+#define TEST_ICON_NAME "avatar-default-symbolic"
 #define TEST_STRING "Mario Rossi"
 #define TEST_SIZE 128
 
@@ -113,6 +114,18 @@ test_hdy_avatar_generate (void)
 }
 
 
+static void
+test_hdy_avatar_icon_name (void)
+{
+  HdyAvatar *avatar = HDY_AVATAR (hdy_avatar_new (128, NULL, TRUE));
+
+  g_assert_null (hdy_avatar_get_icon_name (avatar));
+  hdy_avatar_set_icon_name (avatar, TEST_ICON_NAME);
+  g_assert_cmpstr (hdy_avatar_get_icon_name (avatar), ==, TEST_ICON_NAME);
+
+  g_assert_true (did_draw_something (GTK_WIDGET (avatar)));
+}
+
 static void
 test_hdy_avatar_text (void)
 {
@@ -190,6 +203,7 @@ main (gint argc,
 
   g_test_add_func ("/Handy/Avatar/generate", test_hdy_avatar_generate);
   g_test_add_func ("/Handy/Avatar/custom_image", test_hdy_avatar_custom_image);
+  g_test_add_func ("/Handy/Avatar/icon_name", test_hdy_avatar_icon_name);
   g_test_add_func ("/Handy/Avatar/text", test_hdy_avatar_text);
   g_test_add_func ("/Handy/Avatar/size", test_hdy_avatar_size);
 


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