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

View File

@ -21,14 +21,14 @@
#pragma once
#include <gtk/gtk.h>
#include <adwaita.h>
#include <act/act.h>
G_BEGIN_DECLS
#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);
void mct_user_image_set_user (MctUserImage *image,