Commit bdc3343a authored by Alexander Larsson's avatar Alexander Larsson
Browse files

File chooser: Convert icons to using cairo_pattern_t

This means we draw faster, and automatically handle hidpi icons.
parent bcde0f38
......@@ -1321,7 +1321,7 @@ change_icon_theme_get_info_cb (GCancellable *cancellable,
gpointer user_data)
{
gboolean cancelled = g_cancellable_is_cancelled (cancellable);
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
struct ChangeIconThemeData *data = user_data;
if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
......@@ -1333,15 +1333,15 @@ change_icon_theme_get_info_cb (GCancellable *cancellable,
if (cancelled || error)
goto out;
pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
pattern = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
if (pixbuf)
if (pattern)
{
gint width = 0;
GtkTreeIter iter;
GtkTreePath *path;
width = MAX (width, gdk_pixbuf_get_width (pixbuf));
width = MAX (width, data->button->priv->icon_size);
path = gtk_tree_row_reference_get_path (data->row_ref);
if (path)
......@@ -1350,14 +1350,14 @@ change_icon_theme_get_info_cb (GCancellable *cancellable,
gtk_tree_path_free (path);
gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
ICON_COLUMN, pixbuf,
ICON_COLUMN, pattern,
-1);
g_object_set (data->button->priv->icon_cell,
"width", width,
NULL);
}
g_object_unref (pixbuf);
cairo_pattern_destroy (pattern);
}
out:
......@@ -1398,7 +1398,7 @@ change_icon_theme (GtkFileChooserButton *button)
do
{
GdkPixbuf *pixbuf = NULL;
cairo_pattern_t *pattern = NULL;
gchar type;
gpointer data;
......@@ -1435,7 +1435,7 @@ change_icon_theme (GtkFileChooserButton *button)
info);
button->priv->change_icon_theme_cancellables =
g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
pixbuf = NULL;
pattern = NULL;
}
else
/* Don't call get_info for remote paths to avoid latency and
......@@ -1443,31 +1443,34 @@ change_icon_theme (GtkFileChooserButton *button)
* If we switch to a better bookmarks file format (XBEL), we
* should use mime info to get a better icon.
*/
pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
priv->icon_size, 0, NULL);
pattern = gtk_icon_theme_load_pattern (theme, "folder-remote",
priv->icon_size,
gtk_widget_get_scale_factor (GTK_WIDGET (button)),
gtk_widget_get_window (GTK_WIDGET (button)),
0, NULL);
}
break;
case ROW_TYPE_VOLUME:
if (data)
pixbuf = _gtk_file_system_volume_render_icon (data,
GTK_WIDGET (button),
priv->icon_size,
NULL);
pattern = _gtk_file_system_volume_render_icon (data,
GTK_WIDGET (button),
priv->icon_size,
NULL);
break;
default:
continue;
break;
}
if (pixbuf)
width = MAX (width, gdk_pixbuf_get_width (pixbuf));
if (pattern)
width = MAX (width, priv->icon_size);
gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
ICON_COLUMN, pixbuf,
ICON_COLUMN, pattern,
-1);
if (pixbuf)
g_object_unref (pixbuf);
if (pattern)
cairo_pattern_destroy (pattern);
}
while (gtk_tree_model_iter_next (priv->model, &iter));
......@@ -1526,7 +1529,7 @@ set_info_get_info_cb (GCancellable *cancellable,
gpointer callback_data)
{
gboolean cancelled = g_cancellable_is_cancelled (cancellable);
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
GtkTreePath *path;
GtkTreeIter iter;
GCancellable *model_cancellable = NULL;
......@@ -1560,7 +1563,7 @@ set_info_get_info_cb (GCancellable *cancellable,
/* There was an error, leave the fallback name in there */
goto out;
pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
pattern = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
if (!data->label)
data->label = g_strdup (g_file_info_get_display_name (info));
......@@ -1568,13 +1571,13 @@ set_info_get_info_cb (GCancellable *cancellable,
is_folder = _gtk_file_info_consider_as_directory (info);
gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
ICON_COLUMN, pixbuf,
ICON_COLUMN, pattern,
DISPLAY_NAME_COLUMN, data->label,
IS_FOLDER_COLUMN, is_folder,
-1);
if (pixbuf)
g_object_unref (pixbuf);
if (pattern)
cairo_pattern_destroy (pattern);
out:
g_object_unref (data->button);
......@@ -1716,7 +1719,7 @@ model_add_special_get_info_cb (GCancellable *cancellable,
gboolean cancelled = g_cancellable_is_cancelled (cancellable);
GtkTreeIter iter;
GtkTreePath *path;
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
GCancellable *model_cancellable = NULL;
struct ChangeIconThemeData *data = user_data;
gchar *name;
......@@ -1746,14 +1749,14 @@ model_add_special_get_info_cb (GCancellable *cancellable,
if (cancelled || error)
goto out;
pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
pattern = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
if (pixbuf)
if (pattern)
{
gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
ICON_COLUMN, pixbuf,
ICON_COLUMN, pattern,
-1);
g_object_unref (pixbuf);
cairo_pattern_destroy (pattern);
}
gtk_tree_model_get (data->button->priv->model, &iter,
......@@ -1881,7 +1884,7 @@ model_add_volumes (GtkFileChooserButton *button,
{
GtkFileSystemVolume *volume;
GtkTreeIter iter;
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
gchar *display_name;
volume = l->data;
......@@ -1906,23 +1909,23 @@ model_add_volumes (GtkFileChooserButton *button,
}
}
pixbuf = _gtk_file_system_volume_render_icon (volume,
GTK_WIDGET (button),
button->priv->icon_size,
NULL);
pattern = _gtk_file_system_volume_render_icon (volume,
GTK_WIDGET (button),
button->priv->icon_size,
NULL);
display_name = _gtk_file_system_volume_get_display_name (volume);
gtk_list_store_insert (store, &iter, pos);
gtk_list_store_set (store, &iter,
ICON_COLUMN, pixbuf,
ICON_COLUMN, pattern,
DISPLAY_NAME_COLUMN, display_name,
TYPE_COLUMN, ROW_TYPE_VOLUME,
DATA_COLUMN, _gtk_file_system_volume_ref (volume),
IS_FOLDER_COLUMN, TRUE,
-1);
if (pixbuf)
g_object_unref (pixbuf);
if (pattern)
cairo_pattern_destroy (pattern);
g_free (display_name);
button->priv->n_volumes++;
......@@ -1971,7 +1974,7 @@ model_add_bookmarks (GtkFileChooserButton *button,
{
gchar *label;
GtkIconTheme *icon_theme;
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
if (local_only)
continue;
......@@ -1986,12 +1989,15 @@ model_add_bookmarks (GtkFileChooserButton *button,
label = _gtk_file_chooser_label_for_file (file);
icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
button->priv->icon_size, 0, NULL);
pattern = gtk_icon_theme_load_pattern (icon_theme, "folder-remote",
button->priv->icon_size,
gtk_widget_get_scale_factor (GTK_WIDGET (button)),
gtk_widget_get_window (GTK_WIDGET (button)),
0, NULL);
gtk_list_store_insert (store, &iter, pos);
gtk_list_store_set (store, &iter,
ICON_COLUMN, pixbuf,
ICON_COLUMN, pattern,
DISPLAY_NAME_COLUMN, label,
TYPE_COLUMN, ROW_TYPE_BOOKMARK,
DATA_COLUMN, g_object_ref (file),
......@@ -1999,7 +2005,8 @@ model_add_bookmarks (GtkFileChooserButton *button,
-1);
g_free (label);
g_object_unref (pixbuf);
if (pattern)
cairo_pattern_destroy (pattern);
}
button->priv->n_bookmarks++;
......@@ -2077,7 +2084,7 @@ model_update_current_folder (GtkFileChooserButton *button,
{
gchar *label;
GtkIconTheme *icon_theme;
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
/* Don't call get_info for remote paths to avoid latency and
* auth dialogs.
......@@ -2091,14 +2098,20 @@ model_update_current_folder (GtkFileChooserButton *button,
icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
if (g_file_is_native (file))
pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
button->priv->icon_size, 0, NULL);
pattern = gtk_icon_theme_load_pattern (icon_theme, "folder",
button->priv->icon_size,
gtk_widget_get_scale_factor (GTK_WIDGET (button)),
gtk_widget_get_window (GTK_WIDGET (button)),
0, NULL);
else
pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
button->priv->icon_size, 0, NULL);
pattern = gtk_icon_theme_load_pattern (icon_theme, "folder-remote",
button->priv->icon_size,
gtk_widget_get_scale_factor (GTK_WIDGET (button)),
gtk_widget_get_window (GTK_WIDGET (button)),
0, NULL);
gtk_list_store_set (store, &iter,
ICON_COLUMN, pixbuf,
ICON_COLUMN, pattern,
DISPLAY_NAME_COLUMN, label,
TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
DATA_COLUMN, g_object_ref (file),
......@@ -2106,7 +2119,8 @@ model_update_current_folder (GtkFileChooserButton *button,
-1);
g_free (label);
g_object_unref (pixbuf);
if (pattern)
cairo_pattern_destroy (pattern);
}
}
......@@ -2442,7 +2456,7 @@ update_label_get_info_cb (GCancellable *cancellable,
gpointer data)
{
gboolean cancelled = g_cancellable_is_cancelled (cancellable);
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
GtkFileChooserButton *button = data;
GtkFileChooserButtonPrivate *priv = button->priv;
......@@ -2456,11 +2470,11 @@ update_label_get_info_cb (GCancellable *cancellable,
gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
pattern = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
if (pixbuf)
g_object_unref (pixbuf);
gtk_image_set_from_pattern (GTK_IMAGE (priv->image), pattern);
if (pattern)
cairo_pattern_destroy (pattern);
out:
emit_selection_changed_if_changing_selection (button);
......@@ -2500,16 +2514,16 @@ update_label_and_image (GtkFileChooserButton *button)
base_file = _gtk_file_system_volume_get_root (volume);
if (base_file && g_file_equal (base_file, file))
{
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
label_text = _gtk_file_system_volume_get_display_name (volume);
pixbuf = _gtk_file_system_volume_render_icon (volume,
GTK_WIDGET (button),
priv->icon_size,
NULL);
gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
if (pixbuf)
g_object_unref (pixbuf);
pattern = _gtk_file_system_volume_render_icon (volume,
GTK_WIDGET (button),
priv->icon_size,
NULL);
gtk_image_set_from_pattern (GTK_IMAGE (priv->image), pattern);
if (pattern)
cairo_pattern_destroy (pattern);
}
if (base_file)
......@@ -2534,15 +2548,18 @@ update_label_and_image (GtkFileChooserButton *button)
}
else
{
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
label_text = _gtk_bookmarks_manager_get_bookmark_label (button->priv->bookmarks_manager, file);
pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
"text-x-generic",
priv->icon_size, 0, NULL);
gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
if (pixbuf)
g_object_unref (pixbuf);
pattern = gtk_icon_theme_load_pattern (get_icon_theme (GTK_WIDGET (priv->image)),
"text-x-generic",
priv->icon_size,
gtk_widget_get_scale_factor (GTK_WIDGET (button)),
gtk_widget_get_window (GTK_WIDGET (button)),
0, NULL);
gtk_image_set_from_pattern (GTK_IMAGE (priv->image), pattern);
if (pattern)
cairo_pattern_destroy (pattern);
done_changing_selection = TRUE;
}
......@@ -2566,7 +2583,7 @@ out:
else
{
gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
gtk_image_set_from_pattern (GTK_IMAGE (priv->image), NULL);
}
if (done_changing_selection)
......
......@@ -84,7 +84,7 @@
<child>
<object class="GtkCellRendererPixbuf" id="icon_cell"/>
<attributes>
<attribute name="pixbuf">0</attribute>
<attribute name="pattern">0</attribute>
</attributes>
</child>
<child>
......@@ -104,7 +104,7 @@
<object class="GtkListStore" id="model">
<columns>
<!-- column-name icon -->
<column type="GdkPixbuf"/>
<column type="CairoPattern"/>
<!-- column-name display-name -->
<column type="gchararray"/>
<!-- column-name type -->
......
......@@ -76,6 +76,7 @@
#include "gtkorientable.h"
#include "gtkintl.h"
#include <cairo-gobject.h>
#include <errno.h>
#include <string.h>
#include <time.h>
......@@ -364,7 +365,7 @@ enum {
MODEL_COL_NAME_COLLATED,
MODEL_COL_IS_FOLDER,
MODEL_COL_IS_SENSITIVE,
MODEL_COL_PIXBUF,
MODEL_COL_PATTERN,
MODEL_COL_SIZE_TEXT,
MODEL_COL_MTIME_TEXT,
MODEL_COL_ELLIPSIZE,
......@@ -381,7 +382,7 @@ enum {
G_TYPE_STRING, /* MODEL_COL_NAME_COLLATED */ \
G_TYPE_BOOLEAN, /* MODEL_COL_IS_FOLDER */ \
G_TYPE_BOOLEAN, /* MODEL_COL_IS_SENSITIVE */ \
GDK_TYPE_PIXBUF, /* MODEL_COL_PIXBUF */ \
CAIRO_GOBJECT_TYPE_PATTERN, /* MODEL_COL_PATTERN */ \
G_TYPE_STRING, /* MODEL_COL_SIZE_TEXT */ \
G_TYPE_STRING, /* MODEL_COL_MTIME_TEXT */ \
PANGO_TYPE_ELLIPSIZE_MODE /* MODEL_COL_ELLIPSIZE */
......@@ -3132,7 +3133,7 @@ change_icon_theme (GtkFileChooserDefault *impl)
set_icon_cell_renderer_fixed_size (impl);
if (priv->browse_files_model)
_gtk_file_system_model_clear_cache (priv->browse_files_model, MODEL_COL_PIXBUF);
_gtk_file_system_model_clear_cache (priv->browse_files_model, MODEL_COL_PATTERN);
gtk_widget_queue_resize (priv->browse_files_tree_view);
profile_end ("end", NULL);
......@@ -4152,12 +4153,12 @@ file_system_model_set (GtkFileSystemModel *model,
else
g_value_set_boolean (value, TRUE);
break;
case MODEL_COL_PIXBUF:
case MODEL_COL_PATTERN:
if (info)
{
if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON))
{
g_value_take_object (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), priv->icon_size));
g_value_take_boxed (value, _gtk_file_info_render_icon (info, GTK_WIDGET (impl), priv->icon_size));
}
else
{
......@@ -7069,7 +7070,7 @@ update_cell_renderer_attributes (GtkFileChooserDefault *impl)
if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
{
gtk_tree_view_column_set_attributes (column, renderer,
"pixbuf", MODEL_COL_PIXBUF,
"pattern", MODEL_COL_PATTERN,
NULL);
}
else
......
......@@ -709,42 +709,44 @@ _gtk_file_system_volume_get_root (GtkFileSystemVolume *volume)
return file;
}
static GdkPixbuf *
get_pixbuf_from_gicon (GIcon *icon,
GtkWidget *widget,
gint icon_size,
GError **error)
static cairo_pattern_t *
get_pattern_from_gicon (GIcon *icon,
GtkWidget *widget,
gint icon_size,
GError **error)
{
GdkScreen *screen;
GtkIconTheme *icon_theme;
GtkIconInfo *icon_info;
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
screen = gtk_widget_get_screen (GTK_WIDGET (widget));
icon_theme = gtk_icon_theme_get_for_screen (screen);
icon_info = gtk_icon_theme_lookup_by_gicon (icon_theme,
icon,
icon_size,
GTK_ICON_LOOKUP_USE_BUILTIN);
icon_info = gtk_icon_theme_lookup_by_gicon_for_scale (icon_theme,
icon,
icon_size,
gtk_widget_get_scale_factor (widget),
GTK_ICON_LOOKUP_USE_BUILTIN);
if (!icon_info)
return NULL;
pixbuf = gtk_icon_info_load_icon (icon_info, error);
pattern = gtk_icon_info_load_pattern (icon_info,
gtk_widget_get_window (widget), error);
g_object_unref (icon_info);
return pixbuf;
return pattern;
}
GdkPixbuf *
cairo_pattern_t *
_gtk_file_system_volume_render_icon (GtkFileSystemVolume *volume,
GtkWidget *widget,
gint icon_size,
GError **error)
{
GIcon *icon = NULL;
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern;
DEBUG ("volume_get_icon_name");
......@@ -760,11 +762,11 @@ _gtk_file_system_volume_render_icon (GtkFileSystemVolume *volume,
if (!icon)
return NULL;
pixbuf = get_pixbuf_from_gicon (icon, widget, icon_size, error);
pattern = get_pattern_from_gicon (icon, widget, icon_size, error);
g_object_unref (icon);
return pixbuf;
return pattern;
}
GtkFileSystemVolume *
......@@ -795,39 +797,55 @@ _gtk_file_system_volume_unref (GtkFileSystemVolume *volume)
}
/* GFileInfo helper functions */
GdkPixbuf *
cairo_pattern_t *
_gtk_file_info_render_icon (GFileInfo *info,
GtkWidget *widget,
gint icon_size)
GtkWidget *widget,
gint icon_size)
{
GIcon *icon;
GdkPixbuf *pixbuf = NULL;
GdkPixbuf *pixbuf;
cairo_pattern_t *pattern = NULL;
const gchar *thumbnail_path;
cairo_surface_t *surface;
cairo_matrix_t matrix;
int scale;
thumbnail_path = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
if (thumbnail_path)
pixbuf = gdk_pixbuf_new_from_file_at_size (thumbnail_path,
icon_size, icon_size,
NULL);
{
scale = gtk_widget_get_scale_factor (widget);
pixbuf = gdk_pixbuf_new_from_file_at_size (thumbnail_path,
icon_size*scale, icon_size*scale,
NULL);
surface = gdk_cairo_pixbuf_to_surface (pixbuf,
gtk_widget_get_window (widget));
g_object_unref (pixbuf);
pattern = cairo_pattern_create_for_surface (surface);
cairo_surface_destroy (surface);
cairo_matrix_init_scale (&matrix, scale, scale);
cairo_pattern_set_matrix (pattern, &matrix);
}
if (!pixbuf)
if (!pattern)
{
icon = g_file_info_get_icon (info);
if (icon)
pixbuf = get_pixbuf_from_gicon (icon, widget, icon_size, NULL);
pattern = get_pattern_from_gicon (icon, widget, icon_size, NULL);
if (!pixbuf)
if (!pattern)
{
/* Use general fallback for all files without icon */
icon = g_themed_icon_new ("text-x-generic");
pixbuf = get_pixbuf_from_gicon (icon, widget, icon_size, NULL);
pattern = get_pattern_from_gicon (icon, widget, icon_size, NULL);
g_object_unref (icon);
}
}
return pixbuf;
return pattern;
}
gboolean
......
......@@ -93,7 +93,7 @@ GtkFileSystemVolume * _gtk_file_system_get_volume_for_file (GtkFileSystem
gchar * _gtk_file_system_volume_get_display_name (GtkFileSystemVolume *volume);
gboolean _gtk_file_system_volume_is_mounted (GtkFileSystemVolume *volume);
GFile * _gtk_file_system_volume_get_root (GtkFileSystemVolume *volume);
GdkPixbuf * _gtk_file_system_volume_render_icon (GtkFileSystemVolume *volume,
cairo_pattern_t * _gtk_file_system_volume_render_icon (GtkFileSystemVolume *volume,
GtkWidget *widget,
gint icon_size,
GError **error);
......@@ -102,9 +102,9 @@ GtkFileSystemVolume *_gtk_file_system_volume_ref (GtkFileSystemVol
void _gtk_file_system_volume_unref (GtkFileSystemVolume *volume);
/* GFileInfo helper functions */
GdkPixbuf * _gtk_file_info_render_icon (GFileInfo *info,
GtkWidget *widget,
gint icon_size);
cairo_pattern_t * _gtk_file_info_render_icon (GFileInfo *info,
GtkWidget *widget,
gint icon_size);
gboolean _gtk_file_info_consider_as_directory (GFileInfo *info);
......
......@@ -95,6 +95,8 @@ void _gtk_icon_helper_set_icon_name (GtkIconHelper *self,
void _gtk_icon_helper_set_stock_id (GtkIconHelper *self,
const gchar *stock_id,
GtkIconSize icon_size);
void _gtk_icon_helper_set_pattern (GtkIconHelper *self,
cairo_pattern_t *pattern);
void _gtk_icon_helper_set_icon_size (GtkIconHelper *self,
GtkIconSize icon_size);
......@@ -112,6 +114,7 @@ GdkPixbuf *_gtk_icon_helper_peek_pixbuf (GtkIconHelper *self);
GIcon *_gtk_icon_helper_peek_gicon (GtkIconHelper *self);
GtkIconSet *_gtk_icon_helper_peek_icon_set (GtkIconHelper *self);
GdkPixbufAnimation *_gtk_icon_helper_peek_animation (GtkIconHelper *self);