filechooser: show files while a folder is still loading

The file list stayed empty until the whole folder had been read, sorted
and filtered.  On a high-latency NFS share that can take many seconds
(20000 files at 1 ms per call: 24 s) with nothing to see.

GtkFileSystemModel now commits loaded entries incrementally when asked
to (_gtk_file_system_model_set_incremental):

- New entries collect in an unsorted tail.  The first commit happens
  after 100 ms, later ones at growing intervals up to once per second.
- A commit sorts the tail (resumable bottom-up merge sort) and merges it
  into the committed, sorted part in one O(n) pass, announcing new rows
  with row-inserted in ascending order.  Both steps run in short main
  loop slices; between slices, every GtkTreeModel query answers from the
  merged prefix plus the not yet merged old rows, so views stay
  consistent.  Operations that need one array (editable row, removals,
  cache clearing) finish a running merge first.
- Sorting or filtering changes during loading are applied to the rows
  already shown; up to two batches are read ahead.
- The old loading code was the same sort without the merge, so a folder
  that is not loaded incrementally takes the same path with a single
  final commit.

The file chooser uses this for the list view and attaches the model
right away.  GtkIconView inserts rows in O(n), so it keeps getting the
model when loading is done (switching views during loading follows
this).  This also fixes "Create Folder" during loading: it used to put
the model on hold while the view had no model, so editing never started
and the folder never finished loading.  The view now gets the model
first.  A selection made during loading is no longer replaced by the
first row when loading finishes.

Invisible nodes without a preceding visible row now report row 0 instead
of hitting an assertion.

Measured (list view): 20000 files with 1 ms emulated latency: first rows
after 0.13 s instead of 24 s; 100000 local files: first rows after
0.14 s, complete after 2.4 s, longest main loop stall 18 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F6aiMGWu4tXHcuSSC9DHi2
This commit is contained in:
Daemonratte 2026-10-13 12:48:09 +02:00
commit d5175e2f26
3 changed files with 658 additions and 279 deletions

View file

@ -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;

File diff suppressed because it is too large Load diff

View file

@ -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,