Skip to content

Commit 10b2021

Browse files
vkarehraveit65
authored andcommitted
Convert launcher icons to cairo surfaces
This improves support for HiDPI by loading properly scaled surfaces for launcher and drawer icons. It also Fixes the Show Desktop wncklet to show a surface icon. Other wncklets have their icons determined by libwnck, so they remain as pixbufs. Fixes mate-desktop/mate-desktop#314
1 parent f775e89 commit 10b2021

File tree

10 files changed

+187
-134
lines changed

10 files changed

+187
-134
lines changed

‎applets/clock/clock-location-tile.c‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,14 @@ weather_info_setup_tooltip (WeatherInfo *info, ClockLocation *location, GtkToolt
584584
const gchar *sys_timezone;
585585
time_t sunrise_time, sunset_time;
586586
gchar *sunrise_str, *sunset_str;
587+
gint icon_scale;
587588

588-
icon_name = weather_info_get_icon_name (info);
589589
theme = gtk_icon_theme_get_default ();
590-
pixbuf = gtk_icon_theme_load_icon (theme, icon_name, 48,
591-
GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
590+
icon_name = weather_info_get_icon_name (info);
591+
icon_scale = gdk_window_get_scale_factor (gdk_get_default_root_window ());
592+
593+
pixbuf = gtk_icon_theme_load_icon_for_scale (theme, icon_name, 48, icon_scale,
594+
GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
592595
if (pixbuf)
593596
gtk_tooltip_set_icon (tooltip, pixbuf);
594597

@@ -654,7 +657,7 @@ weather_info_setup_tooltip (WeatherInfo *info, ClockLocation *location, GtkToolt
654657
static gboolean
655658
weather_tooltip (GtkWidget *widget,
656659
gint x,
657-
gint y,
660+
gint y,
658661
gboolean keyboard_mode,
659662
GtkTooltip *tooltip,
660663
gpointer data)
@@ -681,20 +684,23 @@ update_weather_icon (ClockLocation *loc, WeatherInfo *info, gpointer data)
681684
{
682685
ClockLocationTile *tile = data;
683686
ClockLocationTilePrivate *priv = PRIVATE (tile);
684-
GdkPixbuf *pixbuf = NULL;
687+
cairo_surface_t *surface = NULL;
685688
GtkIconTheme *theme = NULL;
686689
const gchar *icon_name;
690+
gint icon_scale;
687691

688692
if (!info || !weather_info_is_valid (info))
689693
return;
690694

695+
theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (priv->weather_icon)));
691696
icon_name = weather_info_get_icon_name (info);
692-
theme = gtk_icon_theme_get_default ();
693-
pixbuf = gtk_icon_theme_load_icon (theme, icon_name, 16,
694-
GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
697+
icon_scale = gtk_widget_get_scale_factor (GTK_WIDGET (priv->weather_icon));
698+
699+
surface = gtk_icon_theme_load_surface (theme, icon_name, 16, icon_scale,
700+
NULL, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
695701

696-
if (pixbuf) {
697-
gtk_image_set_from_pixbuf (GTK_IMAGE (priv->weather_icon), pixbuf);
702+
if (surface) {
703+
gtk_image_set_from_surface (GTK_IMAGE (priv->weather_icon), surface);
698704
gtk_widget_set_margin_end (priv->weather_icon, 6);
699705
}
700706
}

‎applets/clock/clock.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,8 @@ location_weather_updated_cb (ClockLocation *location,
19751975
const gchar *icon_name;
19761976
const gchar *temp;
19771977
GtkIconTheme *theme;
1978-
GdkPixbuf *pixbuf;
1978+
cairo_surface_t *surface;
1979+
gint icon_size, icon_scale;
19791980

19801981
if (!info || !weather_info_is_valid (info))
19811982
return;
@@ -1984,15 +1985,23 @@ location_weather_updated_cb (ClockLocation *location,
19841985
return;
19851986

19861987
icon_name = weather_info_get_icon_name (info);
1987-
/* FIXME: mmh, screen please? Also, don't hardcode to 16 */
1988-
theme = gtk_icon_theme_get_default ();
1989-
pixbuf = gtk_icon_theme_load_icon (theme, icon_name, 16,
1990-
GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
1988+
if (icon_name == NULL)
1989+
return;
1990+
1991+
theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (cd->applet)));
1992+
1993+
icon_size = mate_panel_applet_get_size (MATE_PANEL_APPLET (cd->applet));
1994+
icon_scale = gtk_widget_get_scale_factor (GTK_WIDGET (cd->applet));
1995+
1996+
surface = gtk_icon_theme_load_surface (theme, icon_name, icon_size, icon_scale,
1997+
NULL, GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
19911998

19921999
temp = weather_info_get_temp_summary (info);
19932000

1994-
gtk_image_set_from_pixbuf (GTK_IMAGE (cd->panel_weather_icon), pixbuf);
2001+
gtk_image_set_from_surface (GTK_IMAGE (cd->panel_weather_icon), surface);
19952002
gtk_label_set_text (GTK_LABEL (cd->panel_temperature_label), temp);
2003+
2004+
cairo_surface_destroy (surface);
19962005
}
19972006

19982007
static void

‎applets/wncklet/showdesktop.c‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ static void update_icon(ShowDesktopData* sdd)
134134
GtkStateFlags state;
135135
GtkBorder padding;
136136
int width, height;
137-
GdkPixbuf* icon;
138-
GdkPixbuf* scaled;
139-
int icon_size;
137+
cairo_surface_t* icon;
138+
cairo_surface_t* scaled;
139+
int icon_size, icon_scale;
140140
GError* error;
141141
int thickness = 0;
142142

@@ -156,17 +156,24 @@ static void update_icon(ShowDesktopData* sdd)
156156
break;
157157
}
158158

159-
icon_size = sdd->size - thickness;
159+
icon_scale = gtk_widget_get_scale_factor (sdd->button);
160+
icon_size = sdd->size * icon_scale - thickness;
160161

161162
if (icon_size < 22)
162163
icon_size = 16;
163-
else if (icon_size < 32)
164+
else if (icon_size < 24)
164165
icon_size = 22;
166+
else if (icon_size < 32)
167+
icon_size = 24;
165168
else if (icon_size < 48)
166169
icon_size = 32;
170+
else if (icon_size < 64)
171+
icon_size = 48;
172+
else if (icon_size < 128)
173+
icon_size = 64;
167174

168175
error = NULL;
169-
icon = gtk_icon_theme_load_icon (sdd->icon_theme, SHOW_DESKTOP_ICON, icon_size, 0, &error);
176+
icon = gtk_icon_theme_load_surface (sdd->icon_theme, SHOW_DESKTOP_ICON, icon_size, icon_scale, NULL, 0, &error);
170177

171178
if (icon == NULL)
172179
{
@@ -182,37 +189,45 @@ static void update_icon(ShowDesktopData* sdd)
182189
return;
183190
}
184191

185-
width = gdk_pixbuf_get_width(icon);
186-
height = gdk_pixbuf_get_height(icon);
192+
width = cairo_image_surface_get_width (icon);
193+
height = cairo_image_surface_get_height (icon);
187194

188195
scaled = NULL;
189196

190197
/* Make it fit on the given panel */
191198
switch (sdd->orient)
192199
{
193200
case GTK_ORIENTATION_HORIZONTAL:
194-
width = (icon_size * width) / height;
195-
height = icon_size;
201+
width = (icon_size / icon_scale * width) / height;
202+
height = icon_size / icon_scale;
196203
break;
197204
case GTK_ORIENTATION_VERTICAL:
198-
height = (icon_size * height) / width;
199-
width = icon_size;
205+
height = (icon_size / icon_scale * height) / width;
206+
width = icon_size / icon_scale;
200207
break;
201208
}
202209

203-
scaled = gdk_pixbuf_scale_simple(icon, width, height, GDK_INTERP_BILINEAR);
210+
scaled = cairo_surface_create_similar (icon,
211+
cairo_surface_get_content (icon),
212+
width,
213+
height);
204214

205215
if (scaled != NULL)
206216
{
207-
gtk_image_set_from_pixbuf(GTK_IMAGE(sdd->image), scaled);
208-
g_object_unref(scaled);
217+
cairo_t *cr;
218+
cr = cairo_create (scaled);
219+
cairo_scale (cr, (double) width / icon_size, (double) height / icon_size);
220+
cairo_set_source_surface (cr, icon, 0, 0);
221+
cairo_paint (cr);
222+
gtk_image_set_from_surface (GTK_IMAGE(sdd->image), scaled);
223+
cairo_surface_destroy (scaled);
209224
}
210225
else
211226
{
212-
gtk_image_set_from_pixbuf (GTK_IMAGE (sdd->image), icon);
227+
gtk_image_set_from_surface (GTK_IMAGE (sdd->image), icon);
213228
}
214229

215-
g_object_unref (icon);
230+
cairo_surface_destroy (icon);
216231
}
217232

218233
static const GtkActionEntry show_desktop_menu_actions[] = {

0 commit comments

Comments
 (0)