Add gtk_icon_info_snapshot_with_colors() and use instead of custom code

This commit is contained in:
Alexander Larsson 2020-01-27 16:42:38 +01:00
parent c42977af04
commit 53c542765f
4 changed files with 108 additions and 58 deletions

View File

@ -45,25 +45,22 @@ gtk_css_image_icon_theme_snapshot (GtkCssImage *image,
double height)
{
GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
GdkTexture *texture;
double texture_width, texture_height;
GtkIconInfo *icon_info;
double icon_width, icon_height;
gint size;
gboolean symbolic;
double x, y;
size = floor (MIN (width, height));
if (size <= 0)
return;
if (size == icon_theme->cached_size &&
icon_theme->cached_texture != NULL)
icon_theme->cached_icon != NULL)
{
texture = icon_theme->cached_texture;
symbolic = icon_theme->cached_symbolic;
icon_info = icon_theme->cached_icon;
}
else
{
GtkIconInfo *icon_info;
icon_info = gtk_icon_theme_lookup_icon_for_scale (icon_theme->icon_theme,
icon_theme->name,
size,
@ -77,53 +74,32 @@ gtk_css_image_icon_theme_snapshot (GtkCssImage *image,
g_assert (icon_info != NULL);
symbolic = gtk_icon_info_is_symbolic (icon_info);
texture = GDK_TEXTURE (gtk_icon_info_load_icon (icon_info, NULL));
g_clear_object (&icon_theme->cached_texture);
g_clear_object (&icon_theme->cached_icon);
icon_theme->cached_size = size;
icon_theme->cached_texture = texture;
icon_theme->cached_symbolic = symbolic;
g_object_unref (icon_info);
icon_theme->cached_icon = icon_info;
}
texture_width = (double) gdk_texture_get_width (texture) / icon_theme->scale;
texture_height = (double) gdk_texture_get_height (texture) / icon_theme->scale;
icon_width = (double) MIN (gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (icon_info)), width);
icon_height = (double) MIN (gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (icon_info)), height);
if (symbolic)
x = (width - icon_width) / 2;
y = (height - icon_height) / 2;
if (x != 0 || y != 0)
{
const GdkRGBA *fg = &icon_theme->color;
const GdkRGBA *sc = &icon_theme->success;
const GdkRGBA *wc = &icon_theme->warning;
const GdkRGBA *ec = &icon_theme->error;
graphene_matrix_t matrix;
graphene_vec4_t offset;
graphene_matrix_init_from_float (&matrix,
(float[16]) {
sc->red - fg->red, sc->green - fg->green, sc->blue - fg->blue, 0,
wc->red - fg->red, wc->green - fg->green, wc->blue - fg->blue, 0,
ec->red - fg->red, ec->green - fg->green, ec->blue - fg->blue, 0,
0, 0, 0, fg->alpha
});
graphene_vec4_init (&offset, fg->red, fg->green, fg->blue, 0);
gtk_snapshot_push_color_matrix (snapshot, &matrix, &offset);
gtk_snapshot_save (snapshot);
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (x, y));
}
gtk_snapshot_append_texture (snapshot,
texture,
&GRAPHENE_RECT_INIT(
(width - texture_width) / 2.0,
(height - texture_height) / 2.0,
texture_width,
texture_height
));
if (symbolic)
gtk_snapshot_pop (snapshot);
gtk_icon_info_snapshot_with_colors (icon_info, snapshot,
icon_width,
icon_height,
&icon_theme->color,
&icon_theme->success,
&icon_theme->warning,
&icon_theme->error);
if (x != 0 || y != 0)
gtk_snapshot_restore (snapshot);
}
static guint
@ -201,7 +177,7 @@ gtk_css_image_icon_theme_dispose (GObject *object)
g_free (icon_theme->name);
icon_theme->name = NULL;
g_clear_object (&icon_theme->cached_texture);
g_clear_object (&icon_theme->cached_icon);
G_OBJECT_CLASS (_gtk_css_image_icon_theme_parent_class)->dispose (object);
}
@ -228,6 +204,6 @@ _gtk_css_image_icon_theme_init (GtkCssImageIconTheme *icon_theme)
icon_theme->icon_theme = gtk_icon_theme_get_default ();
icon_theme->scale = 1;
icon_theme->cached_size = -1;
icon_theme->cached_texture = NULL;
icon_theme->cached_icon = NULL;
}

View File

@ -49,7 +49,7 @@ struct _GtkCssImageIconTheme
int cached_size;
gboolean cached_symbolic;
GdkTexture *cached_texture;
GtkIconInfo *cached_icon;
};
struct _GtkCssImageIconThemeClass

View File

@ -3832,14 +3832,9 @@ icon_info_ensure_scale_and_texture__locked (GtkIconInfo *icon_info)
return TRUE;
}
static void
icon_info_paintable_snapshot (GdkPaintable *paintable,
GdkSnapshot *snapshot,
double width,
double height)
static GdkTexture *
icon_info_get_texture (GtkIconInfo *icon_info)
{
GtkIconInfo *icon_info = GTK_ICON_INFO (paintable);
GdkTexture *texture = NULL;
g_mutex_lock (&icon_info->cache_lock);
@ -3852,6 +3847,19 @@ icon_info_paintable_snapshot (GdkPaintable *paintable,
g_mutex_unlock (&icon_info->cache_lock);
return texture;
}
static void
icon_info_paintable_snapshot (GdkPaintable *paintable,
GdkSnapshot *snapshot,
double width,
double height)
{
GtkIconInfo *icon_info = GTK_ICON_INFO (paintable);
GdkTexture *texture;
texture = icon_info_get_texture (icon_info);
if (texture)
{
if (icon_info->desired_scale != 1)
@ -3870,6 +3878,64 @@ icon_info_paintable_snapshot (GdkPaintable *paintable,
}
}
void
gtk_icon_info_snapshot_with_colors (GtkIconInfo *icon_info,
GdkSnapshot *snapshot,
double width,
double height,
const GdkRGBA *foreground_color,
const GdkRGBA *success_color,
const GdkRGBA *warning_color,
const GdkRGBA *error_color)
{
GdkTexture *texture;
texture = icon_info_get_texture (icon_info);
if (texture)
{
gboolean symbolic = gtk_icon_info_is_symbolic (icon_info);
if (icon_info->desired_scale != 1)
{
gtk_snapshot_save (snapshot);
gtk_snapshot_scale (snapshot, 1.0 / icon_info->desired_scale, 1.0 / icon_info->desired_scale);
}
if (symbolic)
{
const GdkRGBA *fg = foreground_color;
const GdkRGBA *sc = success_color;
const GdkRGBA *wc = warning_color;
const GdkRGBA *ec = error_color;
graphene_matrix_t matrix;
graphene_vec4_t offset;
graphene_matrix_init_from_float (&matrix,
(float[16]) {
sc->red - fg->red, sc->green - fg->green, sc->blue - fg->blue, 0,
wc->red - fg->red, wc->green - fg->green, wc->blue - fg->blue, 0,
ec->red - fg->red, ec->green - fg->green, ec->blue - fg->blue, 0,
0, 0, 0, fg->alpha
});
graphene_vec4_init (&offset, fg->red, fg->green, fg->blue, 0);
gtk_snapshot_push_color_matrix (snapshot, &matrix, &offset);
}
gtk_snapshot_append_texture (snapshot, texture,
&GRAPHENE_RECT_INIT (0, 0, width * icon_info->desired_scale, height * icon_info->desired_scale));
if (symbolic)
gtk_snapshot_pop (snapshot);
if (icon_info->desired_scale != 1)
gtk_snapshot_restore (snapshot);
g_object_unref (texture);
}
}
static GdkPaintableFlags
icon_info_paintable_get_flags (GdkPaintable *paintable)
{

View File

@ -213,6 +213,14 @@ GDK_AVAILABLE_IN_ALL
const gchar * gtk_icon_info_get_filename (GtkIconInfo *self);
GDK_AVAILABLE_IN_ALL
gboolean gtk_icon_info_is_symbolic (GtkIconInfo *self);
void gtk_icon_info_snapshot_with_colors (GtkIconInfo *icon_info,
GdkSnapshot *snapshot,
double width,
double height,
const GdkRGBA *foreground_color,
const GdkRGBA *success_color,
const GdkRGBA *warning_color,
const GdkRGBA *error_color);
GDK_AVAILABLE_IN_ALL
GdkPaintable * gtk_icon_info_load_icon (GtkIconInfo *self,
GError **error);