diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index a9394f9d0..95f962555 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -407,6 +407,7 @@ static void update_cell_renderer_attributes (GtkFileChooserDefault *impl); static void browse_files_center_selected_row (GtkFileChooserDefault *impl); static void view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode); +static void load_set_model (GtkFileChooserDefault *impl); static void visible_range_invalidate (GtkFileChooserDefault *impl); static gboolean visible_range_get (GtkFileChooserDefault *impl, gint *first, @@ -2423,6 +2424,12 @@ new_folder_button_clicked (GtkButton *button, /* Prevent button from being clicked twice */ gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE); + /* While a folder loads, the icon view does not have the model yet. The + * new row must be editable in the view, otherwise editing never ends and + * the model stays on hold. */ + if (impl->current_model != GTK_TREE_MODEL (impl->browse_files_model)) + load_set_model (impl); + _gtk_file_system_model_add_editable (impl->browse_files_model, &iter); path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter); @@ -5604,11 +5611,33 @@ view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode) else g_assert_not_reached (); + /* A folder that is still loading is shown incrementally only in the + * list view (see set_list_model()). */ + if (impl->load_state == LOAD_LOADING && + impl->operation_mode == OPERATION_MODE_BROWSE && + impl->browse_files_model != NULL && + view_mode == VIEW_MODE_ICON && + impl->current_model == GTK_TREE_MODEL (impl->browse_files_model)) + { + _gtk_file_system_model_set_incremental (impl->browse_files_model, FALSE); + impl->current_model = NULL; + } + /* Set model and selection */ current_view_set_file_model (impl, impl->current_model); current_view_set_select_multiple (impl, impl->select_multiple); copy_old_selection_to_current_view (impl, old_view_mode); + if (impl->load_state == LOAD_LOADING && + impl->operation_mode == OPERATION_MODE_BROWSE && + impl->browse_files_model != NULL && + view_mode == VIEW_MODE_LIST && + impl->current_model != GTK_TREE_MODEL (impl->browse_files_model)) + { + _gtk_file_system_model_set_incremental (impl->browse_files_model, TRUE); + load_set_model (impl); + } + /* Destroy the old view */ if (view_mode == VIEW_MODE_ICON) { @@ -7895,8 +7924,11 @@ pending_select_files_process (GtkFileChooserDefault *impl) * that case, the chooser's selection should be what the caller expects, * as the user can't see that something else got selected. See bug #165264. */ + /* The list can be used while the folder loads; keep any selection + * the user made in the meantime. */ if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && - gtk_widget_get_mapped (GTK_WIDGET (impl))) + gtk_widget_get_mapped (GTK_WIDGET (impl)) && + current_selection_count_selected_rows (impl) == 0) browse_files_select_first_row (impl); } @@ -7934,7 +7966,11 @@ browse_files_model_finished_loading_cb (GtkFileSystemModel *model, show_error_on_reading_current_folder (impl, error); if (impl->load_state == LOAD_LOADING) - load_set_model (impl); + { + /* The list view already shows the model. */ + if (impl->current_model != GTK_TREE_MODEL (impl->browse_files_model)) + load_set_model (impl); + } else { /* We can't g_assert_not_reached(), as something other than us may have @@ -8464,6 +8500,15 @@ set_list_model (GtkFileChooserDefault *impl, install_list_model_filter (impl); + /* The list shows files while the folder is still loading. GtkIconView + * inserts rows in O(n) each, so it only gets the model once loading is + * done (see browse_files_model_finished_loading_cb). */ + if (impl->view_mode == VIEW_MODE_LIST) + { + _gtk_file_system_model_set_incremental (impl->browse_files_model, TRUE); + load_set_model (impl); + } + profile_end ("end", NULL); return TRUE; diff --git a/gtk/gtkfilesystemmodel.c b/gtk/gtkfilesystemmodel.c index 2e958492e..d9029695f 100644 --- a/gtk/gtkfilesystemmodel.c +++ b/gtk/gtkfilesystemmodel.c @@ -59,6 +59,13 @@ /* random number that everyone else seems to use, too */ #define FILES_PER_QUERY 100 #define MODEL_DRAIN_SLICE_US 2000 +/* Committing announces rows, and every slice that announces rows makes the + * view redraw before the next slice runs. Longer slices (still well below + * one frame) mean far fewer redraws while a large folder is merged. */ +#define MODEL_COMMIT_SLICE_US 8000 +/* Read ahead up to this many entries so the enumerator thread never idles + * while the main thread integrates a batch. */ +#define MODEL_MAX_READ_AHEAD (2 * FILES_PER_QUERY) #define MODEL_DRAIN_CLOCK_GRANULARITY 4 #define MODEL_FINALIZE_CLOCK_GRANULARITY 8 @@ -71,14 +78,27 @@ #define MODEL_MAX_SNIFFS 2 #define MODEL_SNIFFED_ATTRIBUTE "gtk-fsmodel::sniffed" +/* Loading phases (model->finalize_phase). While a folder loads, new + * entries collect in an unsorted tail of model->order. From time to time + * the tail is "committed": sorted (MODEL_PHASE_SORT) and merged into the + * sorted, visible part (MODEL_PHASE_MERGE), announcing new rows in order. + * Both run in short main loop slices. MODEL_PHASE_REPLAY applies the + * changes that arrived while loading and emits ::finished-loading. */ enum { - MODEL_FINALIZE_NONE, - MODEL_FINALIZE_MERGE, - MODEL_FINALIZE_VISIBLE, - MODEL_FINALIZE_REPLAY + MODEL_PHASE_IDLE, + MODEL_PHASE_SORT, + MODEL_PHASE_MERGE, + MODEL_PHASE_REPLAY }; +/* When committing incrementally, the first rows appear after this delay; + * later commits come at growing intervals so large folders are not + * merged over and over. */ +#define MODEL_FIRST_COMMIT_US (100 * 1000) +#define MODEL_COMMIT_INTERVAL_US (250 * 1000) +#define MODEL_MAX_COMMIT_INTERVAL_US (1000 * 1000) + #define VALUE_PAGE_SHIFT 6 #define VALUE_PAGE_SIZE (1u << VALUE_PAGE_SHIFT) #define VALUE_PAGE_MASK (VALUE_PAGE_SIZE - 1u) @@ -98,6 +118,7 @@ struct _FileModelNode guint visible :1; guint filtered_out :1; guint held_add :1; + guint merged_epoch :1; /* see model->merge_epoch */ }; typedef struct @@ -141,6 +162,22 @@ struct _GtkFileSystemModel guint finalize_phase; GError * load_error; + /* Incremental loading (see MODEL_PHASE_*) */ + guint committed; /* order[0, committed) is sorted and filtered */ + GArray * tail; /* commit in progress: the new nodes */ + GArray * merge_order; /* merge in progress: new model->order */ + GArray * merge_vis; /* merge in progress: new visible rows */ + guint merge_oi; /* next old node to merge */ + guint merge_ti; /* next tail node to merge */ + guint merge_ovi; /* old visible rows already merged */ + gint64 load_start_time; + gint64 next_commit_time; + gint64 commit_interval; + guint commit_timer; + guint merge_epoch :1; + guint in_merge_step :1; + guint incremental :1; + GQueue * pending_monitor_changes; GHashTable * pending_monitor_queries; @@ -200,11 +237,16 @@ struct _GtkFileSystemModelClass static void gtk_file_system_model_got_files (GObject *object, GAsyncResult *res, gpointer data); -static gboolean gtk_file_system_model_drain (gpointer data); +static gboolean gtk_file_system_model_work (gpointer data); +static void gtk_file_system_model_request_next_files (GtkFileSystemModel *model); +static void gtk_file_system_model_apply_monitor_change (GtkFileSystemModel *model, + GFile *file, + GFileMonitorEvent type); static void gtk_file_system_model_schedule_work (GtkFileSystemModel *model); -static gboolean gtk_file_system_model_finalize_load (gpointer data); -static void gtk_file_system_model_begin_finalize_load (GtkFileSystemModel *model, - GError *error); +static void gtk_file_system_model_enumeration_finished (GtkFileSystemModel *model, + const GError *error); +static void gtk_file_system_model_complete_merge (GtkFileSystemModel *model); +static void gtk_file_system_model_refilter_all (GtkFileSystemModel *model); static void gtk_file_system_model_apply_monitor_change (GtkFileSystemModel *model, GFile *file, GFileMonitorEvent type); @@ -308,23 +350,79 @@ visible_find_order_pos (GtkFileSystemModel *model, return lo; } +/* While a commit merges new nodes into the visible rows, the rows are + * split between two arrays: model->merge_vis holds the already merged + * prefix (in new order), model->visible_order[merge_ovi..] the old rows + * not merged yet. Every query below sees exactly the rows announced so + * far, so listeners stay consistent between merge slices. */ +#define MODEL_MERGING(model) ((model)->merge_vis != NULL) + +static guint +model_n_visible (GtkFileSystemModel *model) +{ + if (MODEL_MERGING (model)) + return model->merge_vis->len + (model->visible_order->len - model->merge_ovi); + + return model->visible_order->len; +} + +static guint +model_nth_visible (GtkFileSystemModel *model, + guint n) +{ + if (MODEL_MERGING (model)) + { + if (n < model->merge_vis->len) + return g_array_index (model->merge_vis, guint, n); + + n = n - model->merge_vis->len + model->merge_ovi; + } + + if (n >= model->visible_order->len) + return G_MAXUINT; + + return g_array_index (model->visible_order, guint, n); +} + +/* Row of a visible node. */ +static guint +node_visible_row (GtkFileSystemModel *model, + FileModelNode *node) +{ + if (MODEL_MERGING (model) && node->merged_epoch != model->merge_epoch) + return node->visible_pos - model->merge_ovi + model->merge_vis->len; + + return node->visible_pos; +} + static GtkTreePath * tree_path_new_from_node (GtkFileSystemModel *model, guint id) { FileModelNode *node = get_node (model, id); - guint row = G_MAXUINT; + guint row = 0; if (node->visible) - row = node->visible_pos; + row = node_visible_row (model, node); + else if (MODEL_MERGING (model) && model->in_merge_step) + { + /* Asked from a handler of a row we are announcing: there is no + * meaningful row for an invisible node yet. */ + row = model_n_visible (model) > 0 ? model_n_visible (model) - 1 : 0; + } else if (id != 0) { - guint pos = visible_find_order_pos (model, node->order_pos, TRUE); + guint pos; + + if (MODEL_MERGING (model)) + gtk_file_system_model_complete_merge (model); + + /* Invisible nodes report the closest preceding visible row. */ + pos = visible_find_order_pos (model, node->order_pos, TRUE); if (pos > 0) row = pos - 1; } - g_assert (row != G_MAXUINT); return gtk_tree_path_new_from_indices (row, -1); } @@ -528,6 +626,9 @@ node_set_visible_and_filtered_out (GtkFileSystemModel *model, { FileModelNode *node = get_node (model, id); + if (MODEL_MERGING (model)) + gtk_file_system_model_complete_merge (model); + if (node->filtered_out != filtered_out) { node->filtered_out = filtered_out; @@ -843,10 +944,13 @@ gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model, g_return_val_if_fail (n >= 0, FALSE); - if (parent != NULL || (guint) n >= model->visible_order->len) + if (parent != NULL) + return FALSE; + + id = model_nth_visible (model, n); + if (id == G_MAXUINT) return FALSE; - id = g_array_index (model->visible_order, guint, n); ITER_INIT_FROM_INDEX (model, iter, id); return TRUE; } @@ -905,24 +1009,32 @@ gtk_file_system_model_iter_next (GtkTreeModel *tree_model, { GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model); FileModelNode *node; - guint pos; + guint pos, id; g_return_val_if_fail (ITER_IS_VALID (model, iter), FALSE); node = get_node (model, ITER_INDEX (iter)); if (node->visible) - pos = node->visible_pos + 1; + pos = node_visible_row (model, node) + 1; else if (ITER_INDEX (iter) != 0) - pos = visible_find_order_pos (model, node->order_pos, TRUE); + { + if (MODEL_MERGING (model)) + { + if (model->in_merge_step) + return FALSE; + gtk_file_system_model_complete_merge (model); + } + pos = visible_find_order_pos (model, node->order_pos, TRUE); + } else return FALSE; - if (pos >= model->visible_order->len) + id = model_nth_visible (model, pos); + if (id == G_MAXUINT) return FALSE; - ITER_INIT_FROM_INDEX (model, iter, - g_array_index (model->visible_order, guint, pos)); + ITER_INIT_FROM_INDEX (model, iter, id); return TRUE; } @@ -950,7 +1062,7 @@ gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model, if (iter) return 0; - return model->visible_order->len; + return model_n_visible (model); } static gboolean @@ -1110,6 +1222,7 @@ gtk_file_system_model_sort (GtkFileSystemModel *model) if (model->directory_loading || model->editing_hold) { model->resort_pending = TRUE; + gtk_file_system_model_schedule_work (model); return; } @@ -1336,10 +1449,21 @@ pending_monitor_change_free (gpointer data) g_free (change); } +/* ---- Incremental loading ---------------------------------------------- */ + +static gboolean +deadline_passed (gint64 deadline, + guint *counter, + guint granularity) +{ + return (++(*counter) % granularity) == 0 && g_get_monotonic_time () >= deadline; +} + +/* Bottom-up merge sort of model->tail, resumable between main loop slices. */ static void gtk_file_system_model_prepare_merge (GtkFileSystemModel *model) { - guint n = model->order->len; + guint n = model->tail->len; model->sort_mid = MIN (model->sort_left + model->sort_width, n); model->sort_right = MIN (model->sort_left + 2 * model->sort_width, n); @@ -1349,273 +1473,513 @@ gtk_file_system_model_prepare_merge (GtkFileSystemModel *model) } static void -gtk_file_system_model_prepare_visible (GtkFileSystemModel *model) +gtk_file_system_model_merge_init (GtkFileSystemModel *model) { FileModelNode *editable = get_node (model, 0); - g_array_set_size (model->visible_order, 0); + model->finalize_phase = MODEL_PHASE_MERGE; + model->merge_order = g_array_sized_new (FALSE, FALSE, sizeof (guint), model->order->len); + model->merge_vis = g_array_sized_new (FALSE, FALSE, sizeof (guint), + model->visible_order->len + model->tail->len); + model->merge_oi = 0; + model->merge_ti = 0; + model->merge_ovi = 0; + model->merge_epoch = !model->merge_epoch; + + /* The editable row, if any, stays first. */ if (editable->visible) { guint id = 0; + g_assert (g_array_index (model->visible_order, guint, 0) == 0); + g_array_append_val (model->merge_vis, id); editable->visible_pos = 0; - g_array_append_val (model->visible_order, id); + editable->merged_epoch = model->merge_epoch; + model->merge_ovi = 1; } - - model->sort_left = 0; - model->finalize_phase = MODEL_FINALIZE_VISIBLE; - model->refilter_pending = FALSE; } static void -gtk_file_system_model_restart_initial_sort (GtkFileSystemModel *model) +gtk_file_system_model_apply_pending_resort_refilter (GtkFileSystemModel *model) { SortData data; - guint n = model->order->len; + guint i; - model->resort_pending = FALSE; + /* Only the committed part is visible; the tail is sorted and filtered + * when it is committed. */ + if (model->resort_pending) + { + model->resort_pending = FALSE; + if (model->committed > 1 && sort_data_init (&data, model)) + { + _gtk_sort_array (model->order->data, model->committed, sizeof (guint), + compare_order_element, &data); + order_update_positions_from (model, 0); + visible_order_rebuild_after_sort (model); + } + } - if (sort_data_init (&data, model)) + if (model->refilter_pending) + { + model->refilter_pending = FALSE; + for (i = 0; i < model->committed; i++) + node_compute_visibility_and_filters (model, g_array_index (model->order, guint, i)); + } +} + +static void +gtk_file_system_model_start_commit (GtkFileSystemModel *model) +{ + SortData data; + guint n; + + g_assert (model->finalize_phase == MODEL_PHASE_IDLE); + + gtk_file_system_model_apply_pending_resort_refilter (model); + + n = model->order->len - model->committed; + model->tail = g_array_sized_new (FALSE, FALSE, sizeof (guint), n); + g_array_append_vals (model->tail, + &g_array_index (model->order, guint, model->committed), n); + + if (n > 1 && sort_data_init (&data, model)) { if (model->sort_scratch == NULL) - model->sort_scratch = - g_array_sized_new (FALSE, FALSE, sizeof (guint), n); + model->sort_scratch = g_array_sized_new (FALSE, FALSE, sizeof (guint), n); g_array_set_size (model->sort_scratch, n); model->sort_width = 1; model->sort_left = 0; gtk_file_system_model_prepare_merge (model); - model->finalize_phase = MODEL_FINALIZE_MERGE; + model->finalize_phase = MODEL_PHASE_SORT; } else - gtk_file_system_model_prepare_visible (model); + gtk_file_system_model_merge_init (model); +} + +/* Returns TRUE once the tail is sorted. */ +static gboolean +gtk_file_system_model_commit_sort_step (GtkFileSystemModel *model, + gint64 deadline) +{ + SortData data; + guint processed = 0; + guint n = model->tail->len; + + if (!sort_data_init (&data, model)) + { + gtk_file_system_model_merge_init (model); + return TRUE; + } + + while (TRUE) + { + while (model->sort_k < model->sort_right) + { + guint id; + + if (model->sort_i < model->sort_mid && + (model->sort_j >= model->sort_right || + compare_order_element (&g_array_index (model->tail, guint, model->sort_i), + &g_array_index (model->tail, guint, model->sort_j), + &data) <= 0)) + id = g_array_index (model->tail, guint, model->sort_i++); + else + id = g_array_index (model->tail, guint, model->sort_j++); + + g_array_index (model->sort_scratch, guint, model->sort_k++) = id; + + if (deadline_passed (deadline, &processed, MODEL_FINALIZE_CLOCK_GRANULARITY)) + return FALSE; + } + + model->sort_left = model->sort_right; + if (model->sort_left < n) + { + gtk_file_system_model_prepare_merge (model); + continue; + } + + /* Pass complete: the scratch buffer becomes the tail. */ + { + GArray *tmp = model->tail; + + model->tail = model->sort_scratch; + model->sort_scratch = tmp; + } + + if (model->sort_width >= n / 2 + n % 2) + break; + + model->sort_width *= 2; + model->sort_left = 0; + gtk_file_system_model_prepare_merge (model); + } + + g_clear_pointer (&model->sort_scratch, g_array_unref); + gtk_file_system_model_merge_init (model); + return TRUE; +} + +/* Merges the sorted tail into the committed nodes, announcing each new + * visible row at its final position. Returns TRUE when done. */ +static gboolean +gtk_file_system_model_commit_merge_step (GtkFileSystemModel *model, + gint64 deadline) +{ + SortData data; + gboolean have_sort; + guint processed = 0; + + have_sort = sort_data_init (&data, model); + + while (model->merge_oi < model->committed || model->merge_ti < model->tail->len) + { + gboolean from_tail; + FileModelNode *node; + guint id; + + if (model->merge_ti >= model->tail->len) + from_tail = FALSE; + else if (model->merge_oi >= model->committed || !have_sort) + from_tail = model->merge_oi >= model->committed; + else + from_tail = compare_order_element (&g_array_index (model->tail, guint, model->merge_ti), + &g_array_index (model->order, guint, model->merge_oi), + &data) < 0; + + id = from_tail ? g_array_index (model->tail, guint, model->merge_ti++) + : g_array_index (model->order, guint, model->merge_oi++); + node = get_node (model, id); + + g_array_append_val (model->merge_order, id); + node->order_pos = model->merge_order->len - 1; + node->merged_epoch = model->merge_epoch; + + if (!from_tail) + { + if (node->visible) + { + g_assert (g_array_index (model->visible_order, guint, model->merge_ovi) == id); + model->merge_ovi++; + g_array_append_val (model->merge_vis, id); + node->visible_pos = model->merge_vis->len - 1; + } + } + else + { + node->filtered_out = node_should_be_filtered_out (model, id); + node->visible = node_should_be_visible (model, id, node->filtered_out); + + if (node->visible) + { + GtkTreePath *path; + GtkTreeIter iter; + + g_array_append_val (model->merge_vis, id); + node->visible_pos = model->merge_vis->len - 1; + + path = gtk_tree_path_new_from_indices (node->visible_pos, -1); + ITER_INIT_FROM_INDEX (model, &iter, id); + model->in_merge_step = TRUE; + gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter); + model->in_merge_step = FALSE; + gtk_tree_path_free (path); + } + } + + if (deadline_passed (deadline, &processed, MODEL_FINALIZE_CLOCK_GRANULARITY)) + return FALSE; + } + + g_array_unref (model->order); + model->order = model->merge_order; + model->merge_order = NULL; + + g_array_unref (model->visible_order); + model->visible_order = model->merge_vis; + model->merge_vis = NULL; + + g_clear_pointer (&model->tail, g_array_unref); + model->committed = model->order->len; + model->finalize_phase = MODEL_PHASE_IDLE; + + model->next_commit_time = g_get_monotonic_time () + model->commit_interval; + model->commit_interval = MIN (model->commit_interval * 2, MODEL_MAX_COMMIT_INTERVAL_US); + + return TRUE; +} + +/* Finishes a commit in progress right away. Used before operations that + * need a single, consistent visible array. */ +static void +gtk_file_system_model_complete_merge (GtkFileSystemModel *model) +{ + g_return_if_fail (!model->in_merge_step); + + if (model->finalize_phase == MODEL_PHASE_SORT) + gtk_file_system_model_commit_sort_step (model, G_MAXINT64); + if (model->finalize_phase == MODEL_PHASE_MERGE) + gtk_file_system_model_commit_merge_step (model, G_MAXINT64); +} + +static gboolean +gtk_file_system_model_commit_timeout (gpointer data) +{ + GtkFileSystemModel *model = data; + + model->commit_timer = 0; + gtk_file_system_model_schedule_work (model); + + return FALSE; +} + +static gboolean +gtk_file_system_model_commit_due (GtkFileSystemModel *model) +{ + return model->incremental && + model->order->len > model->committed && + g_get_monotonic_time () >= model->next_commit_time; +} + +/* Waiting for the enumerator: make sure queued entries still show up. */ +static void +gtk_file_system_model_arm_commit_timer (GtkFileSystemModel *model) +{ + gint64 delay; + + if (!model->incremental || model->commit_timer != 0 || + model->order->len == model->committed) + return; + + delay = (model->next_commit_time - g_get_monotonic_time ()) / 1000; + model->commit_timer = + gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE, + (guint) CLAMP (delay, 1, MODEL_MAX_COMMIT_INTERVAL_US / 1000), + gtk_file_system_model_commit_timeout, + model, NULL); } static void gtk_file_system_model_schedule_work (GtkFileSystemModel *model) { - GSourceFunc func; - if (model->drain_source != 0 || model->editing_hold) return; - if (model->pending_files != NULL) - func = gtk_file_system_model_drain; - else if (model->finalize_phase != MODEL_FINALIZE_NONE) - func = gtk_file_system_model_finalize_load; - else + if (model->pending_files == NULL && + model->finalize_phase == MODEL_PHASE_IDLE && + !(model->directory_loading && + (model->enumeration_done || model->order->len > model->committed || + model->resort_pending || model->refilter_pending))) return; model->drain_source = gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE, - func, + gtk_file_system_model_work, g_object_ref (model), g_object_unref); } +/* Called when the enumerator reports the end of the folder or an error. */ static void -gtk_file_system_model_begin_finalize_load (GtkFileSystemModel *model, - GError *error) +gtk_file_system_model_enumeration_finished (GtkFileSystemModel *model, + const GError *error) { - g_assert (model->directory_loading); - g_assert (model->finalize_phase == MODEL_FINALIZE_NONE); - g_assert (model->pending_files == NULL); + model->enumeration_done = TRUE; + g_clear_error (&model->enumeration_error); + if (error) + model->enumeration_error = g_error_copy (error); - g_clear_error (&model->load_error); - model->load_error = error ? g_error_copy (error) : NULL; - - gtk_file_system_model_restart_initial_sort (model); gtk_file_system_model_schedule_work (model); } +/* Integrates queued directory entries until @deadline. New nodes go to + * the unsorted tail of model->order (see add_file()). */ static gboolean -gtk_file_system_model_finalize_load (gpointer data) +gtk_file_system_model_drain_some (GtkFileSystemModel *model, + gint64 deadline) { - GtkFileSystemModel *model = data; - gint64 deadline = g_get_monotonic_time () + MODEL_DRAIN_SLICE_US; guint processed = 0; - if (model->editing_hold && model->directory_loading) + while (model->pending_files != NULL) + { + GList *link = model->pending_files; + GFileInfo *info = link->data; + const char *name; + + model->pending_files = + g_list_delete_link (model->pending_files, link); + if (model->pending_files == NULL) + model->pending_files_tail = NULL; + model->n_pending_files--; + + /* Keep the enumerator busy while this batch is integrated: on a + * high-latency share the next batch then arrives while the main + * thread is still working, instead of only being requested once + * the queue ran dry. */ + if (model->n_pending_files < MODEL_MAX_READ_AHEAD) + gtk_file_system_model_request_next_files (model); + + name = g_file_info_get_name (info); + if (name != NULL) + { + GFile *file = g_file_get_child (model->dir, name); + add_file (model, file, info); + g_object_unref (file); + } + + g_object_unref (info); + + if (deadline_passed (deadline, &processed, MODEL_DRAIN_CLOCK_GRANULARITY)) + return FALSE; + } + + return TRUE; +} + +/* Applies what arrived while loading and announces the end of loading. + * Returns TRUE when done. */ +static gboolean +gtk_file_system_model_replay_step (GtkFileSystemModel *model, + gint64 deadline) +{ + guint processed = 0; + + while (model->sniffed_during_load != NULL) + { + GFile *file = model->sniffed_during_load->data; + guint sniffed_id = node_get_for_file (model, file); + + model->sniffed_during_load = + g_slist_delete_link (model->sniffed_during_load, + model->sniffed_during_load); + if (sniffed_id != 0) + node_compute_visibility_and_filters (model, sniffed_id); + g_object_unref (file); + } + + while (model->pending_monitor_changes != NULL && + !g_queue_is_empty (model->pending_monitor_changes)) + { + PendingMonitorChange *change; + + change = g_queue_pop_head (model->pending_monitor_changes); + + gtk_file_system_model_apply_monitor_change (model, + change->file, + change->type); + pending_monitor_change_free (change); + + if (deadline_passed (deadline, &processed, MODEL_DRAIN_CLOCK_GRANULARITY)) + return FALSE; + } + + model->finalize_phase = MODEL_PHASE_IDLE; + + g_signal_emit (model, + file_system_model_signals[FINISHED_LOADING], + 0, model->load_error); + g_clear_error (&model->load_error); + + return TRUE; +} + +static gboolean +gtk_file_system_model_work (gpointer data) +{ + GtkFileSystemModel *model = data; + gint64 now = g_get_monotonic_time (); + gint64 deadline = now + MODEL_DRAIN_SLICE_US; + gint64 commit_deadline = now + MODEL_COMMIT_SLICE_US; + + if (model->editing_hold) { model->drain_source = 0; return FALSE; } - if (model->resort_pending) - gtk_file_system_model_restart_initial_sort (model); - while (TRUE) { - guint n = model->order->len; - - if (model->finalize_phase == MODEL_FINALIZE_MERGE) + switch (model->finalize_phase) { - SortData data; + case MODEL_PHASE_SORT: + if (!gtk_file_system_model_commit_sort_step (model, commit_deadline)) + return TRUE; + continue; - if (!sort_data_init (&data, model)) - { - gtk_file_system_model_prepare_visible (model); - continue; - } + case MODEL_PHASE_MERGE: + if (!gtk_file_system_model_commit_merge_step (model, commit_deadline)) + return TRUE; + continue; - while (model->sort_k < model->sort_right) - { - guint id; + case MODEL_PHASE_REPLAY: + if (!gtk_file_system_model_replay_step (model, deadline)) + return TRUE; + model->drain_source = 0; + return FALSE; - if (model->sort_i < model->sort_mid && - (model->sort_j >= model->sort_right || - compare_order_element - (&g_array_index (model->order, guint, model->sort_i), - &g_array_index (model->order, guint, model->sort_j), - &data) <= 0)) - id = g_array_index (model->order, guint, model->sort_i++); - else - id = g_array_index (model->order, guint, model->sort_j++); + default: + break; + } - g_array_index (model->sort_scratch, guint, model->sort_k++) = id; + if (!model->directory_loading) + break; - if ((++processed % MODEL_FINALIZE_CLOCK_GRANULARITY) == 0 && - g_get_monotonic_time () >= deadline) - return TRUE; - } + if (g_get_monotonic_time () >= deadline) + return TRUE; - model->sort_left = model->sort_right; - if (model->sort_left < n) - { - gtk_file_system_model_prepare_merge (model); - continue; - } - - /* Make the completed pass canonical and reuse the old buffer. */ - { - GArray *tmp = model->order; - - model->order = model->sort_scratch; - model->sort_scratch = tmp; - } - - if (model->resort_pending) - { - gtk_file_system_model_restart_initial_sort (model); - continue; - } - - if (model->sort_width >= n / 2 + n % 2) - { - gtk_file_system_model_prepare_visible (model); - continue; - } - - model->sort_width *= 2; - model->sort_left = 0; - gtk_file_system_model_prepare_merge (model); + if (gtk_file_system_model_commit_due (model)) + { + gtk_file_system_model_start_commit (model); continue; } - if (model->finalize_phase == MODEL_FINALIZE_VISIBLE) + if (model->pending_files != NULL) { - if (model->refilter_pending) - gtk_file_system_model_prepare_visible (model); - - while (model->sort_left < n) - { - guint pos = model->sort_left++; - guint id = g_array_index (model->order, guint, pos); - FileModelNode *node = get_node (model, id); - gboolean filtered_out; - gboolean visible; - - node->order_pos = pos; - filtered_out = node_should_be_filtered_out (model, id); - visible = node_should_be_visible (model, id, filtered_out); - - node->filtered_out = filtered_out; - node->visible = visible; - - if (visible) - { - node->visible_pos = model->visible_order->len; - g_array_append_val (model->visible_order, id); - } - - if ((++processed % MODEL_FINALIZE_CLOCK_GRANULARITY) == 0 && - g_get_monotonic_time () >= deadline) - return TRUE; - } - - if (model->resort_pending) - { - gtk_file_system_model_restart_initial_sort (model); - continue; - } - - if (model->refilter_pending) - { - gtk_file_system_model_prepare_visible (model); - continue; - } - - model->directory_loading = FALSE; - model->refilter_pending = FALSE; - model->resort_pending = FALSE; - g_clear_pointer (&model->sort_scratch, g_array_unref); - - model->finalize_phase = MODEL_FINALIZE_REPLAY; - if (g_get_monotonic_time () >= deadline) + if (!gtk_file_system_model_drain_some (model, deadline)) return TRUE; continue; } - if (model->finalize_phase == MODEL_FINALIZE_REPLAY) + if (model->enumeration_done) { - while (model->sniffed_during_load != NULL) + if (model->order->len > model->committed) { - GFile *file = model->sniffed_during_load->data; - guint sniffed_id = node_get_for_file (model, file); - - model->sniffed_during_load = - g_slist_delete_link (model->sniffed_during_load, - model->sniffed_during_load); - if (sniffed_id != 0) - node_compute_visibility_and_filters (model, sniffed_id); - g_object_unref (file); + gtk_file_system_model_start_commit (model); + continue; } - while (model->pending_monitor_changes != NULL && - !g_queue_is_empty (model->pending_monitor_changes)) + /* Everything is committed: loading is done. */ + model->directory_loading = FALSE; + if (model->commit_timer != 0) { - PendingMonitorChange *change; - - change = g_queue_pop_head (model->pending_monitor_changes); - - gtk_file_system_model_apply_monitor_change (model, - change->file, - change->type); - pending_monitor_change_free (change); - - if ((++processed % MODEL_DRAIN_CLOCK_GRANULARITY) == 0 && - g_get_monotonic_time () >= deadline) - return TRUE; + g_source_remove (model->commit_timer); + model->commit_timer = 0; } + if (model->resort_pending) + gtk_file_system_model_sort (model); + if (model->refilter_pending) + gtk_file_system_model_refilter_all (model); - model->finalize_phase = MODEL_FINALIZE_NONE; - model->drain_source = 0; - - g_signal_emit (model, - file_system_model_signals[FINISHED_LOADING], - 0, model->load_error); g_clear_error (&model->load_error); - - return FALSE; + model->load_error = model->enumeration_error; + model->enumeration_error = NULL; + model->finalize_phase = MODEL_PHASE_REPLAY; + continue; } - g_assert_not_reached (); + /* Waiting for the enumerator. Apply a sort or filter change to the + * rows already shown, and show queued entries when they are due. */ + if (model->resort_pending || model->refilter_pending) + gtk_file_system_model_apply_pending_resort_refilter (model); + gtk_file_system_model_arm_commit_timer (model); + break; } + + model->drain_source = 0; + return FALSE; } - - - G_DEFINE_TYPE_WITH_CODE (GtkFileSystemModel, _gtk_file_system_model, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL, gtk_file_system_model_iface_init) @@ -1641,6 +2005,12 @@ gtk_file_system_model_dispose (GObject *object) model->resort_source = 0; } + if (model->commit_timer != 0) + { + g_source_remove (model->commit_timer); + model->commit_timer = 0; + } + g_cancellable_cancel (model->cancellable); g_clear_pointer (&model->pending_monitor_queries, g_hash_table_destroy); @@ -1676,6 +2046,9 @@ gtk_file_system_model_finalize (GObject *object) g_clear_pointer (&model->files, g_array_unref); g_clear_pointer (&model->sort_scratch, g_array_unref); + g_clear_pointer (&model->tail, g_array_unref); + g_clear_pointer (&model->merge_order, g_array_unref); + g_clear_pointer (&model->merge_vis, g_array_unref); g_clear_error (&model->load_error); if (model->pending_monitor_changes) g_queue_free_full (model->pending_monitor_changes, @@ -1770,76 +2143,6 @@ gtk_file_system_model_request_next_files (GtkFileSystemModel *model) model); } -static gboolean -gtk_file_system_model_drain (gpointer data) -{ - GtkFileSystemModel *model = data; - gint64 deadline; - guint processed = 0; - - if (model->editing_hold) - { - model->drain_source = 0; - return FALSE; - } - - deadline = g_get_monotonic_time () + MODEL_DRAIN_SLICE_US; - - while (model->pending_files != NULL) - { - GList *link = model->pending_files; - GFileInfo *info = link->data; - const char *name; - - model->pending_files = - g_list_delete_link (model->pending_files, link); - if (model->pending_files == NULL) - model->pending_files_tail = NULL; - model->n_pending_files--; - - /* Keep the enumerator busy while this batch is integrated: on a - * high-latency share the next batch then arrives while the main - * thread is still working, instead of only being requested once - * the queue ran dry. */ - if (model->n_pending_files < FILES_PER_QUERY) - gtk_file_system_model_request_next_files (model); - - name = g_file_info_get_name (info); - if (name != NULL) - { - GFile *file = g_file_get_child (model->dir, name); - add_file (model, file, info); - g_object_unref (file); - } - - g_object_unref (info); - processed++; - - if ((processed % MODEL_DRAIN_CLOCK_GRANULARITY) == 0 && - g_get_monotonic_time () >= deadline) - break; - } - - if (model->pending_files != NULL) - return TRUE; - - model->drain_source = 0; - - /* All batches are in and integrated: finish loading. */ - if (model->enumeration_done && model->finalize_phase == MODEL_FINALIZE_NONE && - model->directory_loading) - { - GError *error = model->enumeration_error; - - model->enumeration_error = NULL; - gtk_file_system_model_begin_finalize_load (model, error); - g_clear_error (&error); - } - else - gtk_file_system_model_request_next_files (model); - - return FALSE; -} static void gtk_file_system_model_got_files (GObject *object, @@ -1870,8 +2173,8 @@ gtk_file_system_model_got_files (GObject *object, model->pending_files_tail = tail; model->n_pending_files += g_list_length (files); - /* Read ahead by one batch at most, so memory stays bounded. */ - if (model->n_pending_files < FILES_PER_QUERY) + /* Read ahead, but keep memory bounded. */ + if (model->n_pending_files < MODEL_MAX_READ_AHEAD) gtk_file_system_model_request_next_files (model); gtk_file_system_model_schedule_work (model); @@ -1894,11 +2197,8 @@ gtk_file_system_model_got_files (GObject *object, g_clear_object (&model->dir_enumerator); - /* Batches that are still queued must be integrated first. */ - if (model->pending_files == NULL) - gtk_file_system_model_begin_finalize_load (model, error); - else if (error) - model->enumeration_error = g_error_copy (error); + /* Queued batches are integrated before loading finishes. */ + gtk_file_system_model_enumeration_finished (model, error); gdk_threads_leave (); } @@ -2081,7 +2381,7 @@ gtk_file_system_model_got_enumerator (GObject *object, if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { gdk_threads_enter (); - gtk_file_system_model_begin_finalize_load (model, error); + gtk_file_system_model_enumeration_finished (model, error); gdk_threads_leave (); } @@ -2160,6 +2460,9 @@ gtk_file_system_model_set_directory (GtkFileSystemModel *model, model->dir = g_object_ref (dir); model->attributes = g_strdup (attributes); model->directory_loading = TRUE; + model->load_start_time = g_get_monotonic_time (); + model->next_commit_time = model->load_start_time + MODEL_FIRST_COMMIT_US; + model->commit_interval = MODEL_COMMIT_INTERVAL_US; g_file_enumerate_children_async (model->dir, attributes, @@ -2270,6 +2573,7 @@ gtk_file_system_model_refilter_all (GtkFileSystemModel *model) if (model->directory_loading || model->editing_hold) { model->refilter_pending = TRUE; + gtk_file_system_model_schedule_work (model); return; } @@ -2377,6 +2681,26 @@ _gtk_file_system_model_set_filter_folders (GtkFileSystemModel *model, } } +/** + * _gtk_file_system_model_set_incremental: + * @model: a #GtkFileSystemModel + * @incremental: whether to show files while the folder is still loading + * + * If %TRUE, rows appear while the folder loads: entries are sorted and + * merged into the model at growing intervals (first after 100 ms, then up + * to once per second). Otherwise all rows appear at the end. Views + * whose row insertion is expensive should not use incremental mode. + **/ +void +_gtk_file_system_model_set_incremental (GtkFileSystemModel *model, + gboolean incremental) +{ + g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model)); + + model->incremental = incremental != FALSE; + gtk_file_system_model_schedule_work (model); +} + /** * _gtk_file_system_model_get_cancellable: * @model: the model @@ -2708,6 +3032,9 @@ remove_file (GtkFileSystemModel *model, if (id == 0) return; + if (MODEL_MERGING (model)) + gtk_file_system_model_complete_merge (model); + node = get_node (model, id); was_visible = node->visible; @@ -2879,6 +3206,8 @@ _gtk_file_system_model_clear_cache (GtkFileSystemModel *model, } /* Emit at most one change signal for each visible row, in row order. */ + if (MODEL_MERGING (model)) + gtk_file_system_model_complete_merge (model); for (i = 0; i < model->visible_order->len; i++) { guint id = g_array_index (model->visible_order, guint, i); @@ -2975,6 +3304,9 @@ _gtk_file_system_model_add_editable (GtkFileSystemModel *model, g_return_if_fail (iter != NULL); g_return_if_fail (!get_node (model, 0)->visible); + /* A commit in progress must not be paused half way. */ + gtk_file_system_model_complete_merge (model); + node_set_visible_and_filtered_out (model, 0, TRUE, FALSE); ITER_INIT_FROM_INDEX (model, iter, 0); diff --git a/gtk/gtkfilesystemmodel.h b/gtk/gtkfilesystemmodel.h index 4bdda0c23..6ea413901 100644 --- a/gtk/gtkfilesystemmodel.h +++ b/gtk/gtkfilesystemmodel.h @@ -53,6 +53,8 @@ GtkFileSystemModel *_gtk_file_system_model_new_for_directory(GFile * guint n_columns, ...); GCancellable * _gtk_file_system_model_get_cancellable (GtkFileSystemModel *model); +void _gtk_file_system_model_set_incremental (GtkFileSystemModel *model, + gboolean incremental); gboolean _gtk_file_system_model_iter_is_visible (GtkFileSystemModel *model, GtkTreeIter *iter); gboolean _gtk_file_system_model_iter_is_filtered_out (GtkFileSystemModel *model,