[gnome-control-center/wip/feborges/round-and-generate-user-images: 1/3] user-accounts: Round all user images



commit 416c39d6307af657e7dfa77bd1cd3694254be7c9
Author: Felipe Borges <felipeborges gnome org>
Date:   Mon Feb 4 15:27:19 2019 +0000

    user-accounts: Round all user images
    
    This is part of the effort in [0] to standardize the way we display
    user images accross GNOME. Images are now going to be rounded and
    borderless.
    
    See https://gitlab.gnome.org/GNOME/Initiatives/issues/6

 panels/user-accounts/cc-avatar-chooser.c | 17 +++++++++++-----
 panels/user-accounts/cc-user-image.c     |  8 ++++----
 panels/user-accounts/user-utils.c        | 34 ++++++++++++++++++++++++++++++++
 panels/user-accounts/user-utils.h        |  3 ++-
 4 files changed, 52 insertions(+), 10 deletions(-)
---
diff --git a/panels/user-accounts/cc-avatar-chooser.c b/panels/user-accounts/cc-avatar-chooser.c
index 7b568ce6e..9b144a7f0 100644
--- a/panels/user-accounts/cc-avatar-chooser.c
+++ b/panels/user-accounts/cc-avatar-chooser.c
@@ -39,6 +39,7 @@
 
 #include "cc-avatar-chooser.h"
 #include "cc-crop-area.h"
+#include "user-utils.h"
 
 #define ROW_SPAN 5
 #define AVATAR_PIXEL_SIZE 80
@@ -397,18 +398,24 @@ static GtkWidget *
 create_face_widget (gpointer item,
                     gpointer user_data)
 {
+        g_autofree gchar *image_path = NULL;
+        GdkPixbuf *pixbuf = NULL;
         GtkWidget *image;
-        GIcon *icon;
 
-        icon = g_file_icon_new (G_FILE (item));
-        image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
+        image_path = g_file_get_path (G_FILE (item));
+
+        pixbuf = round_image (image_path, AVATAR_PIXEL_SIZE);
+        if (pixbuf == NULL)
+                return NULL;
+
+        image = gtk_image_new_from_pixbuf (pixbuf);
+        g_object_unref (pixbuf);
         gtk_image_set_pixel_size (GTK_IMAGE (image), AVATAR_PIXEL_SIZE);
-        g_object_unref (icon);
 
         gtk_widget_show (image);
 
         g_object_set_data (G_OBJECT (image),
-                           "filename", g_file_get_path (G_FILE (item)));
+                           "filename", image_path);
 
         return image;
 }
diff --git a/panels/user-accounts/cc-user-image.c b/panels/user-accounts/cc-user-image.c
index 79fefc7cb..1bf8f3f1e 100644
--- a/panels/user-accounts/cc-user-image.c
+++ b/panels/user-accounts/cc-user-image.c
@@ -22,6 +22,8 @@
 #include <act/act.h>
 #include <sys/stat.h>
 
+#include "user-utils.h"
+
 struct _CcUserImage {
         GtkImage parent_instance;
 
@@ -63,6 +65,7 @@ check_user_file (const char *filename,
         return TRUE;
 }
 
+
 static cairo_surface_t *
 render_user_icon (ActUser *user,
                   gint     icon_size,
@@ -82,10 +85,7 @@ render_user_icon (ActUser *user,
         if (icon_file) {
                 res = check_user_file (icon_file, MAX_FILE_SIZE);
                 if (res) {
-                        pixbuf = gdk_pixbuf_new_from_file_at_size (icon_file,
-                                                                   icon_size * scale,
-                                                                   icon_size * scale,
-                                                                   NULL);
+                        pixbuf = round_image (icon_file, icon_size * scale);
                 }
                 else {
                         pixbuf = NULL;
diff --git a/panels/user-accounts/user-utils.c b/panels/user-accounts/user-utils.c
index 1aafe82e0..177a88a11 100644
--- a/panels/user-accounts/user-utils.c
+++ b/panels/user-accounts/user-utils.c
@@ -441,3 +441,37 @@ is_valid_username (const gchar *username, gchar **tip)
 
         return valid;
 }
+
+GdkPixbuf *
+round_image (const gchar *icon_file,
+             gint         icon_size)
+{
+        GdkPixbuf *source = NULL;
+        GdkPixbuf *dest = NULL;
+        cairo_surface_t *surface;
+        cairo_t *cr;
+        gint size;
+
+        source = gdk_pixbuf_new_from_file_at_size (icon_file, icon_size, icon_size, NULL);
+        if (source == NULL)
+                return NULL;
+
+        size = gdk_pixbuf_get_width (source);
+        surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size, size);
+        cr = cairo_create (surface);
+
+        /* Clip a circle */
+        cairo_arc (cr, size/2, size/2, size/2, 0, 2 * G_PI);
+        cairo_clip (cr);
+        cairo_new_path (cr);
+
+        gdk_cairo_set_source_pixbuf (cr, source, 0, 0);
+        g_object_unref (source);
+        cairo_paint (cr);
+
+        dest = gdk_pixbuf_get_from_surface (surface, 0, 0, size, size);
+        cairo_surface_destroy (surface);
+        cairo_destroy (cr);
+
+        return dest;
+}
diff --git a/panels/user-accounts/user-utils.h b/panels/user-accounts/user-utils.h
index be31a5776..85b100421 100644
--- a/panels/user-accounts/user-utils.h
+++ b/panels/user-accounts/user-utils.h
@@ -42,5 +42,6 @@ gboolean is_username_used                 (const gchar *username);
 gboolean is_valid_name                    (const gchar *name);
 gboolean is_valid_username                (const gchar *name,
                                            gchar      **tip);
-
+GdkPixbuf *round_image                    (const gchar *icon_file,
+                                           gint         icon_size);
 G_END_DECLS


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