user-image: Port to AdwAvatar

This simultaneously allows us to remove a lot of code, and make
Malcontent more consistent with the rest of the system.

Port MctUserImage to use an AdwAvatar internally. Make it inherit
AdwBin since it has a single child now.
This commit is contained in:
Georges Basile Stavracas Neto 2022-07-08 16:06:39 -03:00
parent d4f219b71e
commit 6cb361efb9
2 changed files with 25 additions and 84 deletions

View File

@ -19,7 +19,7 @@
* - Ondrej Holy <oholy@redhat.com> * - Ondrej Holy <oholy@redhat.com>
*/ */
#include <gtk/gtk.h> #include <adwaita.h>
#include <act/act.h> #include <act/act.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -28,108 +28,49 @@
struct _MctUserImage struct _MctUserImage
{ {
GtkImage parent_instance; AdwBin parent_instance;
AdwAvatar *avatar;
ActUser *user; ActUser *user;
}; };
G_DEFINE_TYPE (MctUserImage, mct_user_image, GTK_TYPE_IMAGE) G_DEFINE_TYPE (MctUserImage, mct_user_image, ADW_TYPE_BIN)
static GdkPixbuf * static GdkTexture *
round_image (GdkPixbuf *pixbuf) render_user_icon_texture (ActUser *user)
{ {
GdkPixbuf *dest = NULL; g_autoptr(GdkTexture) texture = NULL;
cairo_surface_t *surface; g_autoptr(GError) error = NULL;
cairo_t *cr;
gint size;
size = gdk_pixbuf_get_width (pixbuf);
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, pixbuf, 0, 0);
cairo_paint (cr);
dest = gdk_pixbuf_get_from_surface (surface, 0, 0, size, size);
cairo_surface_destroy (surface);
cairo_destroy (cr);
return dest;
}
static cairo_surface_t *
render_user_icon (ActUser *user,
gint icon_size,
gint scale)
{
g_autoptr(GdkPixbuf) source_pixbuf = NULL;
GdkPixbuf *pixbuf = NULL;
GError *error;
const gchar *icon_file; const gchar *icon_file;
cairo_surface_t *surface = NULL;
g_return_val_if_fail (ACT_IS_USER (user), NULL); g_return_val_if_fail (ACT_IS_USER (user), NULL);
g_return_val_if_fail (icon_size > 12, NULL);
icon_file = act_user_get_icon_file (user); icon_file = act_user_get_icon_file (user);
pixbuf = NULL; if (icon_file == NULL)
if (icon_file) return NULL;
texture = gdk_texture_new_from_filename (icon_file, &error);
if (error != NULL)
{ {
source_pixbuf = gdk_pixbuf_new_from_file_at_size (icon_file, g_warning ("Error loading user icon: %s", error->message);
icon_size * scale, return NULL;
icon_size * scale,
NULL);
if (source_pixbuf)
pixbuf = round_image (source_pixbuf);
} }
if (pixbuf != NULL) return g_steal_pointer (&texture);
goto out;
error = NULL;
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
"avatar-default",
icon_size * scale,
GTK_ICON_LOOKUP_FORCE_SIZE,
&error);
if (error)
{
g_warning ("%s", error->message);
g_error_free (error);
}
out:
if (pixbuf != NULL)
{
surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, scale, NULL);
g_object_unref (pixbuf);
}
return surface;
} }
static void static void
render_image (MctUserImage *image) render_image (MctUserImage *image)
{ {
cairo_surface_t *surface; g_autoptr(GdkTexture) texture = NULL;
gint scale, pixel_size;
if (image->user == NULL) if (image->user == NULL)
return; return;
pixel_size = gtk_image_get_pixel_size (GTK_IMAGE (image)); texture = render_user_icon_texture (image->user);
scale = gtk_widget_get_scale_factor (GTK_WIDGET (image)); adw_avatar_set_custom_image (image->avatar, GDK_PAINTABLE (texture));
surface = render_user_icon (image->user, adw_avatar_set_text (image->avatar, act_user_get_real_name (image->user));
pixel_size > 0 ? pixel_size : 48,
scale);
gtk_image_set_from_surface (GTK_IMAGE (image), surface);
cairo_surface_destroy (surface);
} }
void void
@ -163,8 +104,8 @@ mct_user_image_class_init (MctUserImageClass *class)
static void static void
mct_user_image_init (MctUserImage *image) mct_user_image_init (MctUserImage *image)
{ {
g_signal_connect_swapped (image, "notify::scale-factor", G_CALLBACK (render_image), image); image->avatar = ADW_AVATAR (adw_avatar_new (48, NULL, TRUE));
g_signal_connect_swapped (image, "notify::pixel-size", G_CALLBACK (render_image), image); adw_bin_set_child (ADW_BIN (image), GTK_WIDGET (image->avatar));
} }
GtkWidget * GtkWidget *

View File

@ -21,14 +21,14 @@
#pragma once #pragma once
#include <gtk/gtk.h> #include <adwaita.h>
#include <act/act.h> #include <act/act.h>
G_BEGIN_DECLS G_BEGIN_DECLS
#define MCT_TYPE_USER_IMAGE (mct_user_image_get_type ()) #define MCT_TYPE_USER_IMAGE (mct_user_image_get_type ())
G_DECLARE_FINAL_TYPE (MctUserImage, mct_user_image, MCT, USER_IMAGE, GtkImage) G_DECLARE_FINAL_TYPE (MctUserImage, mct_user_image, MCT, USER_IMAGE, AdwBin)
GtkWidget *mct_user_image_new (void); GtkWidget *mct_user_image_new (void);
void mct_user_image_set_user (MctUserImage *image, void mct_user_image_set_user (MctUserImage *image,