diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 10e303dcf..f481d8cf1 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -444,6 +444,12 @@ static void icon_view_scale_queue_cache_clear (GtkFileChooserDefault *impl); static gboolean get_visible_range (GtkTreePath **start, GtkTreePath **end, GtkFileChooserDefault *impl); +static void icon_view_visible_range_schedule (GtkFileChooserDefault *impl); +static void icon_view_visible_range_size_allocate_cb (GtkWidget *widget, + GtkAllocation *allocation, + GtkFileChooserDefault *impl); +static void icon_view_visible_range_adjustment_changed_cb (GtkAdjustment *adjustment, + GtkFileChooserDefault *impl); static void location_button_toggled_cb (GtkToggleButton *toggle, GtkFileChooserDefault *impl); @@ -1042,8 +1048,6 @@ typedef struct gboolean failed; } ThumbnailJob; -static gboolean thumbnail_cache_path_is_sufficient (const gchar *path, - gint icon_size); static gboolean thumbnail_debug_enabled (void); static void thumbnail_debug_log_file (const gchar *where, @@ -1205,13 +1209,6 @@ thumbnail_cache_max_dimension_for_path (const gchar *path) } -static gboolean -thumbnail_cache_path_is_sufficient (const gchar *path, - gint icon_size) -{ - return thumbnail_cache_max_dimension_for_path (path) >= icon_size; -} - static gboolean thumbnail_debug_enabled (void) { @@ -1836,6 +1833,12 @@ gtk_file_chooser_default_finalize (GObject *object) icon_view_scale_remove_timeout (impl); + if (impl->icon_view_visible_range_idle_id) + { + g_source_remove (impl->icon_view_visible_range_idle_id); + impl->icon_view_visible_range_idle_id = 0; + } + thumbnail_queue_clear (impl); thumbnail_standard_icon_cache_clear (impl); @@ -6939,7 +6942,6 @@ show_preview_panel_toggled_cb (GtkCheckMenuItem *item, } - /* Callback used when the "Show Thumbnails" menu item is toggled */ static void show_thumbnails_toggled_cb (GtkCheckMenuItem *item, @@ -8338,11 +8340,6 @@ file_list_query_tooltip_cb (GtkWidget *widget, } - - - - - static void file_icon_view_tooltip_cache_clear (GtkFileChooserDefault *impl) { @@ -8864,6 +8861,8 @@ create_browse_files_icon_view (GtkFileChooserDefault *impl) gtk_widget_add_events (impl->browse_files_icon_view, GDK_SCROLL_MASK); g_signal_connect (impl->browse_files_icon_view, "scroll-event", G_CALLBACK (browse_files_icon_view_scroll_event_cb), impl); + g_signal_connect (impl->browse_files_icon_view, "size-allocate", + G_CALLBACK (icon_view_visible_range_size_allocate_cb), impl); g_object_set (impl->browse_files_icon_view, "has-tooltip", TRUE, NULL); g_signal_connect (impl->browse_files_icon_view, "query-tooltip", @@ -8946,6 +8945,12 @@ create_file_list (GtkFileChooserDefault *impl) /* Scrolled window */ swin = gtk_scrolled_window_new (NULL, NULL); impl->browse_files_scrolled_window = swin; + g_signal_connect (gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (swin)), + "value-changed", + G_CALLBACK (icon_view_visible_range_adjustment_changed_cb), impl); + g_signal_connect (gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (swin)), + "value-changed", + G_CALLBACK (icon_view_visible_range_adjustment_changed_cb), impl); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin), @@ -9675,85 +9680,154 @@ icon_view_scale_queue_visible_after_layout_clear (GtkFileChooserDefault *impl) (GDestroyNotify) g_object_unref); } +static gboolean +file_list_get_visible_indices (GtkFileChooserDefault *impl, + gint *first, + gint *last) +{ + GtkTreePath *start, *end; + gint *start_indices, *end_indices; + gboolean valid; + + if (!impl->current_model || !GTK_IS_FILE_SYSTEM_MODEL (impl->current_model) || + !get_visible_range (&start, &end, impl)) + return FALSE; + + start_indices = gtk_tree_path_get_indices (start); + end_indices = gtk_tree_path_get_indices (end); + valid = start_indices && end_indices && end_indices[0] >= start_indices[0]; + + if (valid) + { + *first = start_indices[0]; + *last = end_indices[0]; + } + + gtk_tree_path_free (start); + gtk_tree_path_free (end); + return valid; +} + +#define FILE_LIST_VISIBLE_OVERSCAN(first, last) \ + CLAMP (MAX (1, (last) - (first) + 1) * 2, 16, 64) + +static void +file_list_clear_cache_range (GtkFileChooserDefault *impl, + gint first, + gint last, + gint column) +{ + GtkTreeModel *model = impl->current_model; + GtkTreePath *path; + GtkTreeIter iter; + gint i; + + first = MAX (0, first); + path = gtk_tree_path_new_from_indices (first, -1); + + for (i = first; i <= last; i++) + { + if (!gtk_tree_model_get_iter (model, &iter, path)) + break; + + _gtk_file_system_model_clear_cache_for_iter (GTK_FILE_SYSTEM_MODEL (model), + &iter, column); + gtk_tree_path_next (path); + } + + gtk_tree_path_free (path); +} + static void icon_view_scale_clear_visible_model_cache (GtkFileChooserDefault *impl, gboolean include_overscan) { - GtkTreeModel *tree_model; - GtkTreePath *start, *end, *path; - GtkTreeIter iter; - gint *start_indices; - gint *end_indices; - gint first; - gint last; - gint i; + gint first, last; - if (!impl->current_model || !GTK_IS_FILE_SYSTEM_MODEL (impl->current_model)) - return; - - /* - * A full _gtk_file_system_model_clear_cache() is O(total files). During - * icon-view zooming we refresh only the visible range. - * - * When shrinking icons, the new layout can make rows visible that were just - * outside the old visible range. Only in that case do we add a small margin. - */ - if (!get_visible_range (&start, &end, impl)) + if (!file_list_get_visible_indices (impl, &first, &last)) { - _gtk_file_system_model_clear_cache (GTK_FILE_SYSTEM_MODEL (impl->current_model), - MODEL_COL_ICON_PIXBUF); - return; - } - - start_indices = gtk_tree_path_get_indices (start); - end_indices = gtk_tree_path_get_indices (end); - - if (!start_indices || !end_indices) - { - gtk_tree_path_free (start); - gtk_tree_path_free (end); - _gtk_file_system_model_clear_cache (GTK_FILE_SYSTEM_MODEL (impl->current_model), - MODEL_COL_ICON_PIXBUF); + if (impl->current_model && GTK_IS_FILE_SYSTEM_MODEL (impl->current_model)) + _gtk_file_system_model_clear_cache (GTK_FILE_SYSTEM_MODEL (impl->current_model), + MODEL_COL_ICON_PIXBUF); return; } if (include_overscan) { - gint overscan; + gint overscan = + MIN (impl->icon_view_scale_visible_overscan > 0 + ? impl->icon_view_scale_visible_overscan : 16, + ICON_VIEW_SCALE_MAX_VISIBLE_OVERSCAN); - overscan = impl->icon_view_scale_visible_overscan; - if (overscan <= 0) - overscan = 16; - - overscan = MIN (overscan, ICON_VIEW_SCALE_MAX_VISIBLE_OVERSCAN); - - first = MAX (0, start_indices[0] - overscan); - last = end_indices[0] + overscan; - } - else - { - first = start_indices[0]; - last = end_indices[0]; + first -= overscan; + last += overscan; } - tree_model = impl->current_model; - path = gtk_tree_path_new_from_indices (first, -1); + file_list_clear_cache_range (impl, first, last, MODEL_COL_ICON_PIXBUF); +} - for (i = first; i <= last; i++) - { - if (!gtk_tree_model_get_iter (tree_model, &iter, path)) - break; +/* Cache the GtkIconView viewport; per-cell visible-range scans are O(n^2). */ +static gboolean +icon_view_visible_range_refresh_cb (gpointer data) +{ + GtkFileChooserDefault *impl = data; + gint first, last; + gint overscan; - _gtk_file_system_model_clear_cache_for_iter (GTK_FILE_SYSTEM_MODEL (tree_model), - &iter, - MODEL_COL_ICON_PIXBUF); + impl->icon_view_visible_range_idle_id = 0; + impl->icon_view_visible_range_valid = FALSE; - gtk_tree_path_next (path); - } + if (!VIEW_MODE_USES_ICON_VIEW (impl->view_mode) || + !impl->browse_files_icon_view || + !file_list_get_visible_indices (impl, &first, &last)) + return FALSE; - gtk_tree_path_free (path); - gtk_tree_path_free (start); - gtk_tree_path_free (end); + impl->icon_view_visible_first = first; + impl->icon_view_visible_last = last; + impl->icon_view_visible_range_valid = TRUE; + + overscan = FILE_LIST_VISIBLE_OVERSCAN (first, last); + file_list_clear_cache_range + (impl, first - overscan, last + overscan, + impl->view_mode == VIEW_MODE_COMPACT + ? MODEL_COL_LIST_PIXBUF : MODEL_COL_ICON_PIXBUF); + gtk_widget_queue_draw (impl->browse_files_icon_view); + + return FALSE; +} + +static void +icon_view_visible_range_schedule (GtkFileChooserDefault *impl) +{ + impl->icon_view_visible_range_valid = FALSE; + + if (impl->icon_view_visible_range_idle_id || + !VIEW_MODE_USES_ICON_VIEW (impl->view_mode) || + !impl->browse_files_icon_view || + !impl->current_model) + return; + + /* Run after GtkIconView's layout and coalesce repeated requests. */ + impl->icon_view_visible_range_idle_id = + gdk_threads_add_idle_full (G_PRIORITY_LOW, + icon_view_visible_range_refresh_cb, + impl, + NULL); +} + +static void +icon_view_visible_range_size_allocate_cb (GtkWidget *widget, + GtkAllocation *allocation, + GtkFileChooserDefault *impl) +{ + icon_view_visible_range_schedule (impl); +} + +static void +icon_view_visible_range_adjustment_changed_cb (GtkAdjustment *adjustment, + GtkFileChooserDefault *impl) +{ + icon_view_visible_range_schedule (impl); } static gboolean @@ -12361,13 +12435,12 @@ file_system_model_set (GtkFileSystemModel *model, if (info) { GtkTreeModel *tree_model; - GtkTreePath *path, *start, *end; + GtkTreePath *path; GtkTreeIter iter; gboolean file_near_visible; gint path_index; gint start_index; gint end_index; - gint visible_items; gint overscan_items; /* not loading icon view's icon in the list view */ @@ -12379,7 +12452,23 @@ file_system_model_set (GtkFileSystemModel *model, return FALSE; file_near_visible = FALSE; - if (get_visible_range (&start, &end, impl)) + start_index = -1; + end_index = -1; + + if (impl->view_mode == VIEW_MODE_LIST) + file_list_get_visible_indices (impl, &start_index, &end_index); + else if (impl->icon_view_visible_range_valid) + { + start_index = impl->icon_view_visible_first; + end_index = impl->icon_view_visible_last; + } + else + { + /* Wait for valid GtkIconView geometry. */ + icon_view_visible_range_schedule (impl); + } + + if (start_index >= 0 && end_index >= start_index) { if (!_gtk_file_system_model_get_iter_for_file (model, &iter, @@ -12388,18 +12477,12 @@ file_system_model_set (GtkFileSystemModel *model, path = gtk_tree_model_get_path (tree_model, &iter); path_index = gtk_tree_path_get_indices (path)[0]; - start_index = gtk_tree_path_get_indices (start)[0]; - end_index = gtk_tree_path_get_indices (end)[0]; - - visible_items = MAX (1, end_index - start_index + 1); - overscan_items = CLAMP (visible_items * 2, 16, 64); + overscan_items = FILE_LIST_VISIBLE_OVERSCAN (start_index, end_index); file_near_visible = (path_index >= start_index - overscan_items && - path_index <= end_index + overscan_items); + path_index <= end_index + overscan_items); gtk_tree_path_free (path); - gtk_tree_path_free (start); - gtk_tree_path_free (end); } /* #1 use standard icon if it is loaded */ @@ -16322,6 +16405,8 @@ current_view_set_file_model (GtkFileChooserDefault *impl, GtkTreeModel *model) * can become visible before that timeout fires. Clear the newly visible model * immediately so it cannot show pixbufs cached for an older icon size. */ + icon_view_visible_range_schedule (impl); + if (impl->view_mode == VIEW_MODE_ICON && impl->icon_view_scale_timeout_id && GTK_IS_FILE_SYSTEM_MODEL (impl->current_model)) diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h index 00f94a209..01e15dc40 100644 --- a/gtk/gtkfilechooserprivate.h +++ b/gtk/gtkfilechooserprivate.h @@ -267,6 +267,12 @@ struct _GtkFileChooserDefault guint icon_view_scale_after_layout_id; gint pending_icon_view_icon_size; gint icon_view_scale_visible_overscan; + + /* Cached viewport avoids an O(n) visible-range scan per icon cell. */ + guint icon_view_visible_range_idle_id; + gint icon_view_visible_first, icon_view_visible_last; + guint icon_view_visible_range_valid : 1; + ViewMode view_mode; /* Visible-only thumbnail generation state. Thumbnail workers are