filechooser: query icons only for the rows on screen
This commit is contained in:
parent
6cf67e8b7c
commit
89850b3602
4 changed files with 245 additions and 74 deletions
|
|
@ -406,6 +406,10 @@ static void remove_bookmark_button_clicked_cb (GtkButton *button,
|
|||
static void update_cell_renderer_attributes (GtkFileChooserDefault *impl);
|
||||
|
||||
static void load_set_model (GtkFileChooserDefault *impl);
|
||||
static gboolean icons_queue_update (GtkWidget *view,
|
||||
GdkEventExpose *event,
|
||||
GtkFileChooserDefault *impl);
|
||||
static void icon_queries_cancel_all (GtkFileChooserDefault *impl);
|
||||
static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
|
||||
|
||||
static void view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode);
|
||||
|
|
@ -4973,6 +4977,8 @@ create_browse_files_tree_view (GtkFileChooserDefault *impl)
|
|||
|
||||
g_signal_connect (selection, "changed",
|
||||
G_CALLBACK (list_selection_changed), impl);
|
||||
g_signal_connect (impl->browse_files_tree_view, "expose-event",
|
||||
G_CALLBACK (icons_queue_update), impl);
|
||||
|
||||
/* Keep the column order in sync with update_cell_renderer_attributes() */
|
||||
|
||||
|
|
@ -5179,6 +5185,8 @@ create_browse_files_icon_view (GtkFileChooserDefault *impl)
|
|||
G_CALLBACK (browse_files_key_press_event_cb), impl);
|
||||
g_signal_connect (impl->browse_files_icon_view, "selection-changed",
|
||||
G_CALLBACK (list_selection_changed), impl);
|
||||
g_signal_connect (impl->browse_files_icon_view, "expose-event",
|
||||
G_CALLBACK (icons_queue_update), impl);
|
||||
g_signal_connect (impl->browse_files_icon_view, "popup-menu",
|
||||
G_CALLBACK (list_popup_menu_cb), impl);
|
||||
g_signal_connect (impl->browse_files_icon_view, "button-press-event",
|
||||
|
|
@ -7089,6 +7097,8 @@ cancel_all_operations (GtkFileChooserDefault *impl)
|
|||
impl->shortcuts_activate_iter_cancellable = NULL;
|
||||
}
|
||||
|
||||
icon_queries_cancel_all (impl);
|
||||
|
||||
search_stop_searching (impl, TRUE);
|
||||
recent_stop_loading (impl);
|
||||
}
|
||||
|
|
@ -7967,6 +7977,11 @@ stop_loading_and_clear_list_model (GtkFileChooserDefault *impl,
|
|||
|
||||
if (impl->browse_files_model)
|
||||
{
|
||||
/* Icon queries keep the model alive; drop them so it is disposed
|
||||
* and stops loading now, and do not hear from it any more. */
|
||||
icon_queries_cancel_all (impl);
|
||||
g_signal_handlers_disconnect_by_data (impl->browse_files_model, impl);
|
||||
|
||||
if (impl->current_model == GTK_TREE_MODEL (impl->browse_files_model))
|
||||
impl->current_model = NULL;
|
||||
g_object_unref (impl->browse_files_model);
|
||||
|
|
@ -8105,44 +8120,250 @@ copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
|
|||
g_file_info_set_attribute (to, attribute, type, value);
|
||||
}
|
||||
|
||||
static void
|
||||
file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
|
||||
/* Icons and thumbnail paths are queried in the background, only for the
|
||||
* rows on screen: after the view was drawn, icons_update() looks at the
|
||||
* visible rows once, queries those without an icon from top to bottom and
|
||||
* cancels the queries for rows that were scrolled away, which matters on
|
||||
* slow shares where each query takes a while. */
|
||||
#define ICON_QUERIES_MAX_RUNNING 16
|
||||
#define ICON_QUERIED_ATTRIBUTE "filechooser::icon_queried"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
|
||||
GtkFileChooserDefault *impl; /* owns a reference */
|
||||
GtkFileSystemModel *model; /* owns a reference; NULL once cancelled */
|
||||
GFile *file;
|
||||
GCancellable *cancellable;
|
||||
} IconQuery;
|
||||
|
||||
static void
|
||||
icon_query_free (IconQuery *query)
|
||||
{
|
||||
if (query->model)
|
||||
g_object_unref (query->model);
|
||||
g_object_unref (query->cancellable);
|
||||
g_object_unref (query->file);
|
||||
g_object_unref (query->impl);
|
||||
g_slice_free (IconQuery, query);
|
||||
}
|
||||
|
||||
/* Cancels @query; its row may be queried again when it is shown. */
|
||||
static void
|
||||
icon_query_cancel (IconQuery *query)
|
||||
{
|
||||
GtkTreeIter iter;
|
||||
|
||||
if (query->model == NULL)
|
||||
return;
|
||||
|
||||
if (_gtk_file_system_model_get_iter_for_file (query->model, &iter, query->file))
|
||||
{
|
||||
GFileInfo *info = _gtk_file_system_model_get_info (query->model, &iter);
|
||||
|
||||
if (info)
|
||||
g_file_info_remove_attribute (info, ICON_QUERIED_ATTRIBUTE);
|
||||
}
|
||||
|
||||
g_cancellable_cancel (query->cancellable);
|
||||
g_clear_object (&query->model);
|
||||
}
|
||||
|
||||
static void icon_queries_start (GtkFileChooserDefault *impl);
|
||||
|
||||
static void
|
||||
icon_query_done (GObject *object,
|
||||
GAsyncResult *res,
|
||||
gpointer data)
|
||||
{
|
||||
IconQuery *query = data;
|
||||
GtkFileChooserDefault *impl = query->impl;
|
||||
GFile *file = G_FILE (object);
|
||||
GFileInfo *queried, *info;
|
||||
GFileInfo *queried;
|
||||
GtkTreeIter iter;
|
||||
|
||||
queried = g_file_query_info_finish (file, res, NULL);
|
||||
if (queried == NULL)
|
||||
return;
|
||||
|
||||
GDK_THREADS_ENTER ();
|
||||
|
||||
/* now we know model is valid */
|
||||
impl->icon_queries_running = g_list_remove (impl->icon_queries_running, query);
|
||||
|
||||
/* file was deleted */
|
||||
if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
|
||||
/* Failed queries are not repeated; the file may have been removed. */
|
||||
if (queried && query->model &&
|
||||
_gtk_file_system_model_get_iter_for_file (query->model, &iter, file))
|
||||
{
|
||||
g_object_unref (queried);
|
||||
GDK_THREADS_LEAVE ();
|
||||
return;
|
||||
GFileInfo *info = g_file_info_dup (_gtk_file_system_model_get_info (query->model, &iter));
|
||||
|
||||
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
|
||||
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
|
||||
copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
|
||||
|
||||
_gtk_file_system_model_update_file (query->model, file, info);
|
||||
|
||||
g_object_unref (info);
|
||||
}
|
||||
|
||||
info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
|
||||
if (queried)
|
||||
g_object_unref (queried);
|
||||
|
||||
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
|
||||
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
|
||||
copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
|
||||
|
||||
_gtk_file_system_model_update_file (model, file, info);
|
||||
|
||||
g_object_unref (info);
|
||||
g_object_unref (queried);
|
||||
/* The query may hold the last reference to @impl. */
|
||||
icon_queries_start (impl);
|
||||
icon_query_free (query);
|
||||
|
||||
GDK_THREADS_LEAVE ();
|
||||
}
|
||||
|
||||
static void
|
||||
icon_queries_start (GtkFileChooserDefault *impl)
|
||||
{
|
||||
while (g_list_length (impl->icon_queries_running) < ICON_QUERIES_MAX_RUNNING &&
|
||||
!g_queue_is_empty (&impl->icon_queries_waiting))
|
||||
{
|
||||
IconQuery *query = g_queue_pop_head (&impl->icon_queries_waiting);
|
||||
|
||||
impl->icon_queries_running = g_list_prepend (impl->icon_queries_running, query);
|
||||
g_file_query_info_async (query->file,
|
||||
G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
|
||||
G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
|
||||
G_FILE_ATTRIBUTE_STANDARD_ICON,
|
||||
G_FILE_QUERY_INFO_NONE,
|
||||
G_PRIORITY_DEFAULT,
|
||||
query->cancellable,
|
||||
icon_query_done,
|
||||
query);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
icon_queries_cancel_all (GtkFileChooserDefault *impl)
|
||||
{
|
||||
GList *l;
|
||||
|
||||
if (impl->icons_update_id)
|
||||
{
|
||||
g_source_remove (impl->icons_update_id);
|
||||
impl->icons_update_id = 0;
|
||||
}
|
||||
|
||||
while (!g_queue_is_empty (&impl->icon_queries_waiting))
|
||||
{
|
||||
IconQuery *query = g_queue_pop_head (&impl->icon_queries_waiting);
|
||||
|
||||
icon_query_cancel (query);
|
||||
icon_query_free (query);
|
||||
}
|
||||
|
||||
/* Running queries free themselves when they complete. */
|
||||
for (l = impl->icon_queries_running; l; l = l->next)
|
||||
icon_query_cancel (l->data);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
icons_update_cb (gpointer data)
|
||||
{
|
||||
GtkFileChooserDefault *impl = data;
|
||||
GtkFileSystemModel *model;
|
||||
GtkTreePath *start, *end;
|
||||
gint first = 0, last = -1, margin = 0;
|
||||
GList *l;
|
||||
gint row;
|
||||
|
||||
GDK_THREADS_ENTER ();
|
||||
|
||||
impl->icons_update_id = 0;
|
||||
|
||||
if (!GTK_IS_FILE_SYSTEM_MODEL (impl->current_model))
|
||||
{
|
||||
icon_queries_cancel_all (impl);
|
||||
goto out;
|
||||
}
|
||||
|
||||
model = GTK_FILE_SYSTEM_MODEL (impl->current_model);
|
||||
|
||||
if (get_visible_range (&start, &end, impl))
|
||||
{
|
||||
/* Like GTK 3, also query the rows just outside the view. */
|
||||
first = MAX (0, gtk_tree_path_get_indices (start)[0] - 1);
|
||||
last = gtk_tree_path_get_indices (end)[0] + 1;
|
||||
margin = last - first + 1;
|
||||
gtk_tree_path_free (start);
|
||||
gtk_tree_path_free (end);
|
||||
}
|
||||
|
||||
/* The waiting queries are replaced by the rows on screen now. */
|
||||
while (!g_queue_is_empty (&impl->icon_queries_waiting))
|
||||
{
|
||||
IconQuery *query = g_queue_pop_head (&impl->icon_queries_waiting);
|
||||
|
||||
icon_query_cancel (query);
|
||||
icon_query_free (query);
|
||||
}
|
||||
|
||||
/* Running ones may finish if their row is still within a page. */
|
||||
for (l = impl->icon_queries_running; l; l = l->next)
|
||||
{
|
||||
IconQuery *query = l->data;
|
||||
GtkTreeIter iter;
|
||||
|
||||
if (query->model != model ||
|
||||
!_gtk_file_system_model_get_iter_for_file (model, &iter, query->file))
|
||||
icon_query_cancel (query);
|
||||
else
|
||||
{
|
||||
GtkTreePath *path = gtk_tree_model_get_path (impl->current_model, &iter);
|
||||
|
||||
row = gtk_tree_path_get_indices (path)[0];
|
||||
gtk_tree_path_free (path);
|
||||
if (row < first - margin || row > last + margin)
|
||||
icon_query_cancel (query);
|
||||
}
|
||||
}
|
||||
|
||||
for (row = first; row <= last; row++)
|
||||
{
|
||||
GtkTreeIter iter;
|
||||
GFileInfo *info;
|
||||
IconQuery *query;
|
||||
|
||||
if (!gtk_tree_model_iter_nth_child (impl->current_model, &iter, NULL, row))
|
||||
break;
|
||||
|
||||
info = _gtk_file_system_model_get_info (model, &iter);
|
||||
if (info == NULL ||
|
||||
g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_ICON) ||
|
||||
g_file_info_has_attribute (info, ICON_QUERIED_ATTRIBUTE))
|
||||
continue;
|
||||
|
||||
g_file_info_set_attribute_boolean (info, ICON_QUERIED_ATTRIBUTE, TRUE);
|
||||
|
||||
query = g_slice_new (IconQuery);
|
||||
query->impl = g_object_ref (impl);
|
||||
query->model = g_object_ref (model);
|
||||
query->file = g_object_ref (_gtk_file_system_model_get_file (model, &iter));
|
||||
query->cancellable = g_cancellable_new ();
|
||||
g_queue_push_tail (&impl->icon_queries_waiting, query);
|
||||
}
|
||||
|
||||
icon_queries_start (impl);
|
||||
|
||||
out:
|
||||
GDK_THREADS_LEAVE ();
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Called when the file view is drawn: whatever changed what is on screen
|
||||
* (scrolling, resizing, new rows, another model), it ends with a redraw. */
|
||||
static gboolean
|
||||
icons_queue_update (GtkWidget *view,
|
||||
GdkEventExpose *event,
|
||||
GtkFileChooserDefault *impl)
|
||||
{
|
||||
if (impl->icons_update_id == 0)
|
||||
impl->icons_update_id = gdk_threads_add_idle (icons_update_cb, impl);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
get_visible_range (GtkTreePath **start, GtkTreePath **end,
|
||||
GtkFileChooserDefault *impl)
|
||||
|
|
@ -8216,9 +8437,6 @@ file_system_model_set (GtkFileSystemModel *model,
|
|||
if (info)
|
||||
{
|
||||
GtkTreeModel *tree_model;
|
||||
GtkTreePath *path, *start, *end;
|
||||
GtkTreeIter iter;
|
||||
gboolean file_visible;
|
||||
|
||||
/* not loading icon view's icon in the list view */
|
||||
if (column == MODEL_COL_ICON_PIXBUF && impl->view_mode == VIEW_MODE_LIST)
|
||||
|
|
@ -8252,39 +8470,7 @@ file_system_model_set (GtkFileSystemModel *model,
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
if (!get_visible_range (&start, &end, impl))
|
||||
return FALSE;
|
||||
|
||||
if (!_gtk_file_system_model_get_iter_for_file (model,
|
||||
&iter,
|
||||
file))
|
||||
g_assert_not_reached ();
|
||||
|
||||
path = gtk_tree_model_get_path (tree_model, &iter);
|
||||
file_visible = (gtk_tree_path_compare (start, path) != 1 &&
|
||||
gtk_tree_path_compare (path, end) != 1);
|
||||
|
||||
gtk_tree_path_free (path);
|
||||
gtk_tree_path_free (start);
|
||||
gtk_tree_path_free (end);
|
||||
|
||||
if (file_visible)
|
||||
{
|
||||
/* #2 start loading standard icon (callback will be handled by #1) */
|
||||
if (!g_file_info_has_attribute (info, "filechooser::icon_queried"))
|
||||
{
|
||||
g_file_info_set_attribute_boolean (info, "filechooser::icon_queried", TRUE);
|
||||
g_file_query_info_async (file,
|
||||
G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
|
||||
G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
|
||||
G_FILE_ATTRIBUTE_STANDARD_ICON,
|
||||
G_FILE_QUERY_INFO_NONE,
|
||||
G_PRIORITY_DEFAULT,
|
||||
_gtk_file_system_model_get_cancellable (model),
|
||||
file_system_model_got_thumbnail,
|
||||
model);
|
||||
}
|
||||
}
|
||||
/* Not known yet: icons_update() queries it once the row is shown. */
|
||||
return FALSE;
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -234,6 +234,11 @@ struct _GtkFileChooserDefault
|
|||
guint icon_view_scale_visible_timeout_id;
|
||||
guint icon_view_scale_after_layout_id;
|
||||
gint pending_icon_view_icon_size;
|
||||
|
||||
/* Icon queries for the rows on screen (see icons_update_cb()) */
|
||||
GQueue icon_queries_waiting;
|
||||
GList *icon_queries_running;
|
||||
guint icons_update_id;
|
||||
gint icon_view_scale_visible_overscan;
|
||||
ViewMode view_mode;
|
||||
|
||||
|
|
|
|||
|
|
@ -2750,25 +2750,6 @@ _gtk_file_system_model_set_filter_folders (GtkFileSystemModel *model,
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* _gtk_file_system_model_get_cancellable:
|
||||
* @model: the model
|
||||
*
|
||||
* Gets the cancellable used by the @model. This is the cancellable used
|
||||
* internally by the @model that will be cancelled when @model is
|
||||
* disposed. So you can use it for operations that should be cancelled
|
||||
* when the model goes away.
|
||||
*
|
||||
* Returns: The cancellable used by @model
|
||||
**/
|
||||
GCancellable *
|
||||
_gtk_file_system_model_get_cancellable (GtkFileSystemModel *model)
|
||||
{
|
||||
g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
|
||||
|
||||
return model->cancellable;
|
||||
}
|
||||
|
||||
/**
|
||||
* _gtk_file_system_model_iter_is_visible:
|
||||
* @model: the model
|
||||
|
|
|
|||
|
|
@ -52,7 +52,6 @@ GtkFileSystemModel *_gtk_file_system_model_new_for_directory(GFile *
|
|||
gpointer get_data,
|
||||
guint n_columns,
|
||||
...);
|
||||
GCancellable * _gtk_file_system_model_get_cancellable (GtkFileSystemModel *model);
|
||||
gboolean _gtk_file_system_model_iter_is_visible (GtkFileSystemModel *model,
|
||||
GtkTreeIter *iter);
|
||||
gboolean _gtk_file_system_model_iter_is_filtered_out (GtkFileSystemModel *model,
|
||||
|
|
|
|||
Loading…
Reference in a new issue