icons: on a screen of a higher scale, icons of the icon theme and stock icons come with a picture of that many more pixels, which GDK draws sharp; also insensitive and prelight

This commit is contained in:
Daemonratte 2026-10-17 18:50:32 +02:00
commit 15f80d7bfa
No known key found for this signature in database
4 changed files with 348 additions and 31 deletions

View file

@ -36,6 +36,7 @@
#include "gtkstock.h"
#include "gtkwidget.h"
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
#include "gtkalias.h"
@ -1395,6 +1396,64 @@ ensure_filename_pixbuf (GtkIconSet *icon_set,
return TRUE;
}
gint _gtk_loading_hires;
/* HiDPI: @big becomes the picture GDK draws for @pixbuf on drawables of
* @scale (see _gdk_pixbuf_get_hires()), made to fit exactly; one smaller
* than that would be blurry, its own pixels as blocks are sharper */
/* HiDPI: the largest scale of the screen's monitors, for which icons
* get a picture of more pixels */
gint
_gtk_screen_get_hires_scale (GdkScreen *screen)
{
gint i, n, scale = 1;
n = gdk_screen_get_n_monitors (screen);
for (i = 0; i < n; i++)
scale = MAX (scale, gdk_screen_get_monitor_scale_factor (screen, i));
return scale;
}
void
_gtk_pixbuf_attach_hires (GdkPixbuf *pixbuf,
GdkPixbuf *big,
gint scale)
{
gint width, height;
GdkPixbuf *fit;
gchar key[32];
if (scale <= 1 || !pixbuf || !big || big == pixbuf)
return;
width = gdk_pixbuf_get_width (pixbuf) * scale;
height = gdk_pixbuf_get_height (pixbuf) * scale;
if (gdk_pixbuf_get_width (big) < width || gdk_pixbuf_get_height (big) < height ||
gdk_pixbuf_get_colorspace (big) != gdk_pixbuf_get_colorspace (pixbuf) ||
gdk_pixbuf_get_bits_per_sample (big) != 8)
return;
if (gdk_pixbuf_get_has_alpha (pixbuf) && !gdk_pixbuf_get_has_alpha (big))
big = gdk_pixbuf_add_alpha (big, FALSE, 0, 0, 0);
else if (gdk_pixbuf_get_has_alpha (pixbuf) != gdk_pixbuf_get_has_alpha (big))
return;
else
g_object_ref (big);
if (gdk_pixbuf_get_width (big) == width && gdk_pixbuf_get_height (big) == height)
fit = g_object_ref (big);
else
fit = gdk_pixbuf_scale_simple (big, width, height, GDK_INTERP_HYPER);
g_object_unref (big);
if (fit)
{
g_snprintf (key, sizeof key, "gdk-pixbuf-hires-%d", scale);
g_object_set_data_full (G_OBJECT (pixbuf), g_intern_string (key), fit, g_object_unref);
}
}
static GdkPixbuf *
render_icon_name_pixbuf (GtkIconSource *icon_source,
GtkStyle *style,
@ -1630,6 +1689,109 @@ render_fallback_image (GtkStyle *style,
detail);
}
/* the icon size of @width by @height pixels, registered for the pictures
* of a higher scale */
static GtkIconSize
hires_icon_size (gint width,
gint height)
{
gchar *name = g_strdup_printf ("gtk-hires-%dx%d", width, height);
GtkIconSize size = gtk_icon_size_from_name (name);
if (size == GTK_ICON_SIZE_INVALID)
size = gtk_icon_size_register (name, width, height);
g_free (name);
return size;
}
/* HiDPI: the icon of the set as well in as many more pixels as the
* scale of the screen, attached to @icon, where a source has them (a
* themed or built-in icon, or a picture big enough) */
static void
attach_icon_set_hires (GtkIconSet *icon_set,
GtkStyle *style,
GtkTextDirection direction,
GtkStateType state,
GtkIconSize size,
GtkWidget *widget,
const char *detail,
GdkPixbuf *icon)
{
GdkScreen *screen;
GtkSettings *settings;
GtkIconSource *source;
GtkIconSize big_size;
GdkPixbuf *big;
gint scale, width, height;
if (_gtk_loading_hires || !icon)
return;
/* given by the rendering of the source already */
{
gint i;
for (i = 2; i <= 4; i++)
{
gchar key[32];
g_snprintf (key, sizeof key, "gdk-pixbuf-hires-%d", i);
if (g_object_get_data (G_OBJECT (icon), key))
return;
}
}
if (widget && gtk_widget_has_screen (widget))
screen = gtk_widget_get_screen (widget);
else if (style && style->colormap)
screen = gdk_colormap_get_screen (style->colormap);
else
screen = gdk_screen_get_default ();
if (!screen || (scale = _gtk_screen_get_hires_scale (screen)) <= 1)
return;
settings = gtk_settings_get_for_screen (screen);
if (size == (GtkIconSize) -1 ||
!gtk_icon_size_lookup_for_settings (settings, size, &width, &height) ||
width * scale > 512 || height * scale > 512)
return;
big_size = hires_icon_size (width * scale, height * scale);
/* only a picture of the same source: a theme giving its own picture
* for some sizes keeps it (not the icon theme's, which may differ) */
source = find_best_matching_source (icon_set, direction, state, big_size, NULL);
if (!source ||
source != find_best_matching_source (icon_set, direction, state, size, NULL))
return;
if (source->type == GTK_ICON_SOURCE_FILENAME && !ensure_filename_pixbuf (icon_set, source))
return;
if ((source->type == GTK_ICON_SOURCE_FILENAME || source->type == GTK_ICON_SOURCE_PIXBUF) &&
(gdk_pixbuf_get_width (source->filename_pixbuf ? source->filename_pixbuf
: source->source.pixbuf) < width * scale))
return;
if ((source->type == GTK_ICON_SOURCE_ICON_NAME ||
source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME) &&
!_gtk_icon_theme_same_icon_at_sizes (gtk_icon_theme_get_for_screen (screen),
source->source.icon_name,
MIN (width, height), MIN (width, height) * scale,
source->direction != GTK_TEXT_DIR_NONE
? GTK_ICON_LOOKUP_USE_BUILTIN : 0))
return;
_gtk_loading_hires++;
big = find_and_render_icon_source (icon_set, style, direction, state, big_size,
widget, detail);
_gtk_loading_hires--;
if (big)
{
_gtk_pixbuf_attach_hires (icon, big, scale);
g_object_unref (big);
}
}
/**
* gtk_icon_set_render_icon:
* @icon_set: a #GtkIconSet
@ -1689,6 +1851,8 @@ gtk_icon_set_render_icon (GtkIconSet *icon_set,
if (icon == NULL)
icon = render_fallback_image (style, direction, state, size, widget, detail);
else
attach_icon_set_hires (icon_set, style, direction, state, size, widget, detail, icon);
if (detail == NULL)
add_to_cache (icon_set, style, direction, state, size, icon);

View file

@ -1532,37 +1532,8 @@ gtk_icon_theme_error_quark (void)
return g_quark_from_static_string ("gtk-icon-theme-error-quark");
}
/**
* gtk_icon_theme_load_icon:
* @icon_theme: a #GtkIconTheme
* @icon_name: the name of the icon to lookup
* @size: the desired icon size. The resulting icon may not be
* exactly this size; see gtk_icon_info_load_icon().
* @flags: flags modifying the behavior of the icon lookup
* @error: (allow-none): Location to store error information on failure, or %NULL.
*
* Looks up an icon in an icon theme, scales it to the given size
* and renders it into a pixbuf. This is a convenience function;
* if more details about the icon are needed, use
* gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
*
* Note that you probably want to listen for icon theme changes and
* update the icon. This is usually done by connecting to the
* GtkWidget::style-set signal. If for some reason you do not want to
* update the icon when the icon theme changes, you should consider
* using gdk_pixbuf_copy() to make a private copy of the pixbuf
* returned by this function. Otherwise GTK+ may need to keep the old
* icon theme loaded, which would be a waste of memory.
*
* Return value: (transfer full): the rendered icon; this may be a newly
* created icon or a new reference to an internal icon, so you must not modify
* the icon. Use g_object_unref() to release your reference to the
* icon. %NULL if the icon isn't found.
*
* Since: 2.4
**/
GdkPixbuf *
gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
static GdkPixbuf *
load_icon_unscaled (GtkIconTheme *icon_theme,
const gchar *icon_name,
gint size,
GtkIconLookupFlags flags,
@ -1641,6 +1612,132 @@ gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
return pixbuf;
}
/* HiDPI: on a screen of a higher scale the icon carries one of that
* many more pixels (_gtk_pixbuf_attach_hires()), which GDK draws then */
/**
* gtk_icon_theme_load_icon:
* @icon_theme: a #GtkIconTheme
* @icon_name: the name of the icon to lookup
* @size: the desired icon size. The resulting icon may not be
* exactly this size; see gtk_icon_info_load_icon().
* @flags: flags modifying the behavior of the icon lookup
* @error: (allow-none): Location to store error information on failure, or %NULL.
*
* Looks up an icon in an icon theme, scales it to the given size
* and renders it into a pixbuf. This is a convenience function;
* if more details about the icon are needed, use
* gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
*
* Note that you probably want to listen for icon theme changes and
* update the icon. This is usually done by connecting to the
* GtkWidget::style-set signal. If for some reason you do not want to
* update the icon when the icon theme changes, you should consider
* using gdk_pixbuf_copy() to make a private copy of the pixbuf
* returned by this function. Otherwise GTK+ may need to keep the old
* icon theme loaded, which would be a waste of memory.
*
* Return value: (transfer full): the rendered icon; this may be a newly
* created icon or a new reference to an internal icon, so you must not modify
* the icon. Use g_object_unref() to release your reference to the
* icon. %NULL if the icon isn't found.
*
* Since: 2.4
**/
/* HiDPI: whether the icon of @icon_name looked up at @size and at
* @big_size is the same picture: from the same theme, of the same file
* name, or both built into GTK+ (a theme may have one icon at some
* sizes, another one, or none, at others) */
static gchar *
icon_info_theme_dir (const gchar *filename)
{
gchar *a = g_path_get_dirname (filename), *b, *c;
/* <theme>/<size>/<context>/<file> */
b = g_path_get_dirname (a);
c = g_path_get_dirname (b);
g_free (a);
g_free (b);
return c;
}
gboolean
_gtk_icon_theme_same_icon_at_sizes (GtkIconTheme *icon_theme,
const gchar *icon_name,
gint size,
gint big_size,
GtkIconLookupFlags flags)
{
GtkIconInfo *small, *big;
gboolean same = FALSE;
small = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, flags);
big = gtk_icon_theme_lookup_icon (icon_theme, icon_name, big_size, flags);
if (small && big)
{
const gchar *sf = gtk_icon_info_get_filename (small);
const gchar *bf = gtk_icon_info_get_filename (big);
if (!sf && !bf)
same = gtk_icon_info_get_builtin_pixbuf (small) != NULL &&
gtk_icon_info_get_builtin_pixbuf (big) != NULL;
else if (sf && bf)
{
gchar *sd = icon_info_theme_dir (sf), *bd = icon_info_theme_dir (bf);
gchar *sb = g_path_get_basename (sf), *bb = g_path_get_basename (bf);
gchar *dot;
if ((dot = strrchr (sb, '.')))
*dot = '\0';
if ((dot = strrchr (bb, '.')))
*dot = '\0';
same = strcmp (sd, bd) == 0 && strcmp (sb, bb) == 0;
g_free (sd);
g_free (bd);
g_free (sb);
g_free (bb);
}
}
if (small)
gtk_icon_info_free (small);
if (big)
gtk_icon_info_free (big);
return same;
}
GdkPixbuf *
gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
const gchar *icon_name,
gint size,
GtkIconLookupFlags flags,
GError **error)
{
GdkPixbuf *pixbuf, *big;
gint scale;
pixbuf = load_icon_unscaled (icon_theme, icon_name, size, flags, error);
if (pixbuf && !_gtk_loading_hires && icon_theme->priv->screen && size > 0 &&
(scale = _gtk_screen_get_hires_scale (icon_theme->priv->screen)) > 1 &&
size * scale <= 512 &&
_gtk_icon_theme_same_icon_at_sizes (icon_theme, icon_name, size, size * scale, flags))
{
_gtk_loading_hires++;
big = load_icon_unscaled (icon_theme, icon_name, size * scale, flags, NULL);
_gtk_loading_hires--;
if (big)
{
_gtk_pixbuf_attach_hires (pixbuf, big, scale);
g_object_unref (big);
}
}
return pixbuf;
}
/**
* gtk_icon_theme_has_icon:
* @icon_theme: a #GtkIconTheme

View file

@ -28,6 +28,7 @@
#define __GTK_PRIVATE_H__
#include <gtk/gtkwidget.h>
#include <gtk/gtkicontheme.h>
G_BEGIN_DECLS
@ -187,6 +188,18 @@ gboolean _gtk_fnmatch (const char *pattern,
gboolean _gtk_button_event_triggers_context_menu (GdkEventButton *event);
/* while the pictures of a higher scale are made: none of them get one */
extern gint _gtk_loading_hires;
gint _gtk_screen_get_hires_scale (GdkScreen *screen);
gboolean _gtk_icon_theme_same_icon_at_sizes (GtkIconTheme *icon_theme,
const gchar *icon_name,
gint size,
gint big_size,
GtkIconLookupFlags flags);
void _gtk_pixbuf_attach_hires (GdkPixbuf *pixbuf,
GdkPixbuf *big,
gint scale);
gboolean _gtk_scroll_event_get_steps (GdkEventScroll *event,
gdouble *steps_x,
gdouble *steps_y,

View file

@ -42,6 +42,7 @@
#include "gtkiconfactory.h"
#include "gtksettings.h" /* _gtk_settings_parse_convert() */
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtkalias.h"
#define LIGHTNESS_MULT 1.3
@ -2201,6 +2202,48 @@ gtk_style_render_icon (GtkStyle *style,
g_return_val_if_fail (pixbuf != NULL, NULL);
/* HiDPI: a picture of a higher scale that the source pixbuf carries
* gets the same treatment (insensitive, prelight), where the icon
* keeps its size */
if (!_gtk_loading_hires &&
gtk_icon_source_get_pixbuf (source) &&
gdk_pixbuf_get_width (pixbuf) == gdk_pixbuf_get_width (gtk_icon_source_get_pixbuf (source)) &&
gdk_pixbuf_get_height (pixbuf) == gdk_pixbuf_get_height (gtk_icon_source_get_pixbuf (source)))
{
gint scale;
for (scale = 2; scale <= 4; scale++)
{
gchar key[32];
GdkPixbuf *hires, *rendered;
GtkIconSource *hires_source;
g_snprintf (key, sizeof key, "gdk-pixbuf-hires-%d", scale);
hires = g_object_get_data (G_OBJECT (gtk_icon_source_get_pixbuf (source)), key);
if (!hires)
continue;
if (pixbuf == gtk_icon_source_get_pixbuf (source))
break; /* the same pixbuf, it has it already */
hires_source = gtk_icon_source_copy (source);
gtk_icon_source_set_pixbuf (hires_source, hires);
gtk_icon_source_set_size_wildcarded (hires_source, FALSE);
_gtk_loading_hires++;
rendered = GTK_STYLE_GET_CLASS (style)->render_icon (style, hires_source, direction,
state, (GtkIconSize) -1,
widget, detail);
_gtk_loading_hires--;
gtk_icon_source_free (hires_source);
if (rendered)
{
_gtk_pixbuf_attach_hires (pixbuf, rendered, scale);
g_object_unref (rendered);
}
break;
}
}
return pixbuf;
}