Commit da5bf668 authored by Alexander Mikhaylenko's avatar Alexander Mikhaylenko
Browse files

avatar: Replace draw_to_pixbuf() with draw_to_texture()

Textures implement GIcon now, so we can finally drop pixbufs.

While we're here, remove the size parameter, it didn't work anyway.

See #29
parent 3f4e0d3f
......@@ -274,8 +274,13 @@ The "policy" property has been removed, the behavior is similar to the removed
The `HdyAvatar:loadable-icon` property has been removed along with its getter
and setter. It can be replaced by [property@Adw.Avatar:custom-image].
The `hdy_avatar_draw_to_pixbuf_async()` function has been removed, use the
regular [method@Adw.Avatar.draw_to_pixbuf] instead.
The `hdy_avatar_draw_to_pixbuf()` and `hdy_avatar_draw_to_pixbuf_async()`
functions have been removed, use the newly added
[method@Adw.Avatar.draw_to_texture] instead. [class@Gdk.Texture] implements
`[iface@Gio.Icon], so it should just work for that case.
[method@Adw.Avatar.draw_to_texture] does not have the `size` parameter. Instead,
it uses the avatar's current size, with no replacement.
## Adapt to Stylesheet Changes
......
......@@ -261,17 +261,13 @@ file_chooser_response_cb (AdwDemoWindow *self,
{
if (response_id == GTK_RESPONSE_ACCEPT) {
g_autoptr (GFile) file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (chooser));
g_autoptr (GdkPixbuf) pixbuf =
adw_avatar_draw_to_pixbuf (self->avatar,
adw_avatar_get_size (self->avatar),
gtk_widget_get_scale_factor (GTK_WIDGET (self)));
g_autoptr (GdkTexture) texture =
adw_avatar_draw_to_texture (self->avatar,
gtk_widget_get_scale_factor (GTK_WIDGET (self)));
if (pixbuf != NULL) {
g_autofree char *path = NULL;
g_autofree char *path = g_file_get_path (file);
path = g_file_get_path (file);
gdk_pixbuf_save (pixbuf, path, "png", NULL, NULL);
}
gdk_texture_save_to_png (texture, path);
}
g_object_unref (chooser);
......
......@@ -683,54 +683,41 @@ adw_avatar_set_size (AdwAvatar *self,
}
/**
* adw_avatar_draw_to_pixbuf:
* adw_avatar_draw_to_texture:
* @self: a `AdwAvatar`
* @size: The size of the pixbuf
* @scale_factor: The scale factor
*
* Renders @self into a [class@GdkPixbuf.Pixbuf] at @size and @scale_factor.
* Renders @self into a [class@Gdk.Texture] at @scale_factor.
*
* This can be used to export the fallback avatar.
*
* Returns: (transfer full): the pixbuf
* Returns: (transfer full): the texture
*
* Since: 1.0
*/
GdkPixbuf *
adw_avatar_draw_to_pixbuf (AdwAvatar *self,
int size,
int scale_factor)
GdkTexture *
adw_avatar_draw_to_texture (AdwAvatar *self,
int scale_factor)
{
GtkSnapshot *snapshot;
g_autoptr (GskRenderNode) node = NULL;
g_autoptr (cairo_surface_t) surface = NULL;
g_autoptr (cairo_t) cr = NULL;
graphene_rect_t bounds;
GtkSnapshot *snapshot;
GtkNative *native;
GskRenderer *renderer;
int size;
g_return_val_if_fail (ADW_IS_AVATAR (self), NULL);
g_return_val_if_fail (size > 0, NULL);
g_return_val_if_fail (scale_factor > 0, NULL);
size = self->size * scale_factor;
snapshot = gtk_snapshot_new ();
gtk_snapshot_scale (snapshot, scale_factor, scale_factor);
GTK_WIDGET_GET_CLASS (self)->snapshot (GTK_WIDGET (self), snapshot);
node = gtk_snapshot_free_to_node (snapshot);
gsk_render_node_get_bounds (node, &bounds);
graphene_rect_round_to_pixel (&bounds);
graphene_rect_scale (&bounds, scale_factor, scale_factor, &bounds);
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
bounds.size.width,
bounds.size.height);
cairo_surface_set_device_scale (surface, scale_factor, scale_factor);
cr = cairo_create (surface);
cairo_translate (cr, -bounds.origin.x, -bounds.origin.y);
gsk_render_node_draw (node, cr);
native = gtk_widget_get_native (GTK_WIDGET (self));
renderer = gtk_native_get_renderer (native);
return gdk_pixbuf_get_from_surface (surface, 0, 0,
bounds.size.width,
bounds.size.height);
return gsk_renderer_render_texture (renderer, node, &GRAPHENE_RECT_INIT (0, 0, size, size));
}
......@@ -12,7 +12,6 @@
#include "adw-version.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtk.h>
G_BEGIN_DECLS
......@@ -58,8 +57,7 @@ void adw_avatar_set_size (AdwAvatar *self,
int size);
ADW_AVAILABLE_IN_ALL
GdkPixbuf *adw_avatar_draw_to_pixbuf (AdwAvatar *self,
int size,
int scale_factor) G_GNUC_WARN_UNUSED_RESULT;
GdkTexture *adw_avatar_draw_to_texture (AdwAvatar *self,
int scale_factor) G_GNUC_WARN_UNUSED_RESULT;
G_END_DECLS
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment