From 280142b0ccdf2515b3c9cbd3d48527af0fcb95c7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 17 Nov 2026 22:05:21 +0100 Subject: [PATCH 1/2] Show search results as they're found, with a spinner while it looks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A search went through every folder below before showing anything, so in a big tree the window kept showing the folder searched, with no sign of work, for a long while. It now shows a new search's matches as it finds them, a batch at most every tenth of a second, each put straight into its place in the folder's order, so the list grows without jumping and the cursor goes to the first match as soon as there is one. A new search starts from an empty list rather than the folder searched; the same search run again (the folder changed under it) leaves its matches up and swaps them only if they've changed. While it looks, a spinner turns in the search field and an empty list says Searching… rather than Nothing matches. Searching /usr for gnome- showed 149 of its 258 matches at 250ms, where it showed none until 769ms. A new test checks the matches come in batches while the walk goes on, that they add up to the whole, and how the model shows a new search and the same one again. Co-Authored-By: Claude Opus 5.5 --- src/FolderColumns.qml | 5 +- src/FolderGrid.qml | 7 ++- src/FolderList.qml | 7 ++- src/PathBar.qml | 14 +++++- src/Spinner.qml | 38 +++++++++++++++ src/foldermodel.cpp | 105 ++++++++++++++++++++++++++++++++++++++---- src/foldermodel.h | 13 +++++- src/i18n/en.json | 1 + src/resources.qrc | 1 + tests/folio_tests.cpp | 52 +++++++++++++++++++++ 10 files changed, 225 insertions(+), 18 deletions(-) create mode 100644 src/Spinner.qml diff --git a/src/FolderColumns.qml b/src/FolderColumns.qml index d469bc48..a8049b13 100644 --- a/src/FolderColumns.qml +++ b/src/FolderColumns.qml @@ -208,8 +208,9 @@ Item { Text { x: LayoutMirroring.enabled ? here.width - width - 46 : 46 y: 8 - visible: here.count === 0 && !backend.folder.loading && backend.folder.error.length === 0 - text: backend.folder.query ? qsTr("No matches") : qsTr("No items") + readonly property bool searching: backend.folder.loading && backend.folder.query.length > 0 + visible: here.count === 0 && (!backend.folder.loading || searching) && backend.folder.error.length === 0 + text: searching ? qsTr("Searching…") : backend.folder.query ? qsTr("No matches") : qsTr("No items") color: theme.secondaryForeground } } diff --git a/src/FolderGrid.qml b/src/FolderGrid.qml index 526d6454..7ae0c38f 100644 --- a/src/FolderGrid.qml +++ b/src/FolderGrid.qml @@ -106,8 +106,11 @@ GridView { Text { anchors.centerIn: parent - visible: grid.count === 0 && !backend.folder.loading && backend.folder.error.length === 0 - text: backend.folder.query ? qsTr("Nothing matches “%1”").arg(backend.folder.query) + // A search says it's looking until it finds something, or is done. + readonly property bool searching: backend.folder.loading && backend.folder.query.length > 0 + visible: grid.count === 0 && (!backend.folder.loading || searching) && backend.folder.error.length === 0 + text: searching ? qsTr("Searching…") + : backend.folder.query ? qsTr("Nothing matches “%1”").arg(backend.folder.query) : plugins.views[backend.path] ? plugins.views[backend.path].empty : qsTr("This folder is empty") color: theme.secondaryForeground } diff --git a/src/FolderList.qml b/src/FolderList.qml index ecdcfbd8..2c73e01e 100644 --- a/src/FolderList.qml +++ b/src/FolderList.qml @@ -90,8 +90,11 @@ FileListView { Text { anchors.centerIn: parent - visible: list.count === 0 && !backend.folder.loading && backend.folder.error.length === 0 - text: backend.folder.query ? qsTr("Nothing matches “%1”").arg(backend.folder.query) + // A search says it's looking until it finds something, or is done. + readonly property bool searching: backend.folder.loading && backend.folder.query.length > 0 + visible: list.count === 0 && (!backend.folder.loading || searching) && backend.folder.error.length === 0 + text: searching ? qsTr("Searching…") + : backend.folder.query ? qsTr("Nothing matches “%1”").arg(backend.folder.query) : plugins.views[backend.path] ? plugins.views[backend.path].empty : qsTr("This folder is empty") color: theme.secondaryForeground } diff --git a/src/PathBar.qml b/src/PathBar.qml index bb5f379c..51ed022c 100644 --- a/src/PathBar.qml +++ b/src/PathBar.qml @@ -109,8 +109,18 @@ Rectangle { visible: bar.searching && !bar.editing font.pixelSize: 14 // Room at the end for where it searches; padding isn't mirrored. - leftPadding: LayoutMirroring.enabled ? scope.implicitWidth + 24 : 12 - rightPadding: LayoutMirroring.enabled ? 12 : scope.implicitWidth + 24 + leftPadding: LayoutMirroring.enabled ? scope.implicitWidth + 24 + searchingSpinner.room : 12 + rightPadding: LayoutMirroring.enabled ? 12 : scope.implicitWidth + 24 + searchingSpinner.room + // Turning while the search goes through the folders, beside + // where it searches. + Spinner { + id: searchingSpinner + readonly property int room: visible ? width + 10 : 0 + visible: backend.folder.loading && backend.folder.query.length > 0 + anchors.right: scope.left + anchors.rightMargin: 10 + anchors.verticalCenter: parent.verticalCenter + } // Where it searches, at the end of the field. Unlike a // placeholder, it stays while typing. Plugins' views have // nothing below them. diff --git a/src/Spinner.qml b/src/Spinner.qml new file mode 100644 index 00000000..51ad5cf9 --- /dev/null +++ b/src/Spinner.qml @@ -0,0 +1,38 @@ +import QtQuick +import QtQuick.Shapes + +// A small turning arc, for work under way with no end in sight yet (a +// search going through the folders below). +Item { + id: spinner + property real size: 14 + property color color: theme.secondaryForeground + implicitWidth: size + implicitHeight: size + + Shape { + anchors.fill: parent + preferredRendererType: Shape.CurveRenderer + ShapePath { + strokeColor: spinner.color + strokeWidth: 1.6 + fillColor: "transparent" + capStyle: ShapePath.RoundCap + PathAngleArc { + centerX: spinner.size / 2 + centerY: spinner.size / 2 + radiusX: spinner.size / 2 - 1 + radiusY: spinner.size / 2 - 1 + startAngle: 0 + sweepAngle: 270 + } + } + RotationAnimator on rotation { + running: spinner.visible + from: 0 + to: 360 + duration: 900 + loops: Animation.Infinite + } + } +} diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index b4de493d..c33fe12b 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -384,8 +385,20 @@ FolderModel::Entry FolderModel::entryFor(const QFileInfo &info, const QMimeDatab QVector FolderModel::search(const QString &root, const QString &query, bool showHidden, const QString &sort, const std::function &stop, - int limit, const QLocale &locale, const QStringList &filter) { + int limit, const QLocale &locale, const QStringList &filter, + const std::function &)> &found) { QVector entries; + // What's been found since `found` was last told. + qsizetype told = 0; + QElapsedTimer since; + since.start(); + const auto tell = [&](bool all) { + if (found && entries.size() > told && (all || since.elapsed() >= 100)) { + found(entries.mid(told)); + told = entries.size(); + since.restart(); + } + }; struct stat top; if (::stat(QFile::encodeName(root).constData(), &top) != 0) return entries; @@ -408,8 +421,10 @@ QVector FolderModel::search(const QString &root, const QStri Entry e = entryFor(info, mimes); e.name = base.relativeFilePath(e.path); // Only what fits a file chooser's filter uses up the limit. - if (fits(filter, e)) + if (fits(filter, e)) { entries.push_back(e); + tell(false); + } } // Other filesystems (a mounted drive, another machine, /proc // from /) and symlinked folders aren't walked into. @@ -420,6 +435,7 @@ QVector FolderModel::search(const QString &root, const QStri folders << info.absoluteFilePath(); } } + tell(true); sortEntries(entries, sort, locale); return entries; } @@ -431,10 +447,14 @@ QStringList FolderModel::sortModes() { } void FolderModel::sortEntries(QVector &entries, const QString &sort, const QLocale &locale) { - QCollator collator(locale); - collator.setNumericMode(true); - collator.setCaseSensitivity(Qt::CaseInsensitive); - std::stable_sort(entries.begin(), entries.end(), [&](const Entry &a, const Entry &b) { + std::stable_sort(entries.begin(), entries.end(), before(sort, locale)); +} + +std::function FolderModel::before(const QString &sort, const QLocale &locale) { + auto collator = std::make_shared(locale); + collator->setNumericMode(true); + collator->setCaseSensitivity(Qt::CaseInsensitive); + return [collator, sort](const Entry &a, const Entry &b) { // Folders first in every order, as everywhere else on the desktop. if (a.isDir != b.isDir) return a.isDir; @@ -449,10 +469,10 @@ void FolderModel::sortEntries(QVector &entries, const QString &sort, cons if (a.kind != b.kind) return a.kind < b.kind; if (a.extension != b.extension) - return collator.compare(a.extension, b.extension) < 0; + return collator->compare(a.extension, b.extension) < 0; } - return collator.compare(a.name, b.name) < 0; - }); + return collator->compare(a.name, b.name) < 0; + }; } // Recents are GTK's recently used files (~/.local/share/recently-used.xbel), @@ -560,6 +580,12 @@ void FolderModel::reload() { const QStringList filter = m_filter; const QStringList expanded = expandable() ? QStringList(m_expanded.begin(), m_expanded.end()) : QStringList(); *m_latest = generation; + // A search of a folder shows its matches as they're found. + if (!query.isEmpty() && path != recentPath() && !isPluginView(path) && !isTrash(path)) { + searchHere(generation); + return; + } + m_shownQuery.clear(); const auto latest = m_latest; auto *watcher = new QFutureWatcher, QString, QStringList, QHash>, QHash>>(this); connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation] { @@ -774,6 +800,67 @@ int FolderModel::indexOf(const QString &name) const { return -1; } +void FolderModel::searchHere(quint64 generation) { + const QString path = m_path, query = m_query, sort = m_sort; + const bool showHidden = m_showHidden; + const QStringList filter = m_filter; + const QLocale locale; + const auto latest = m_latest; + // A new search starts empty and fills in as matches are found. The same + // one again (the folder changed under it) leaves its matches up, and + // replaces them only if they've changed once it's done. + const bool fresh = query != m_shownQuery; + m_shownQuery = query; + m_staleClear.stop(); + if (fresh) + clearEntries(); + setError({}); + auto *watcher = new QFutureWatcher>(this); + // Told once, at its first matches, so the cursor goes to the first. + auto announced = std::make_shared(false); + connect(watcher, &QFutureWatcherBase::resultsReadyAt, this, [this, watcher, generation, fresh, announced](int from, int to) { + if (generation != m_generation || !fresh) + return; + for (int i = from; i < to; ++i) + insertFound(watcher->resultAt(i)); + if (!*announced && !m_entries.isEmpty()) { + *announced = true; + emit loaded(); + } + }); + connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, fresh, announced] { + watcher->deleteLater(); + if (generation != m_generation) + return; + if (!fresh) { + QVector all; + for (const QVector &found : watcher->future().results()) + all += found; + sortEntries(all, m_sort); + if (!sameListing(m_entries, all)) + setEntries(all); + } + setLoading(false); + emit loaded(); + countFolders(generation); + }); + watcher->setFuture(QtConcurrent::run(&FolderModel::listingPool(), [path, query, showHidden, sort, filter, locale, latest, generation](QPromise> &promise) { + search(path, query, showHidden, sort, [&] { return *latest != generation; }, 1000, locale, filter, + [&](const QVector &found) { promise.addResult(found); }); + })); +} + +void FolderModel::insertFound(const QVector &found) { + const auto order = before(m_sort); + for (const Entry &e : found) { + const int row = int(std::upper_bound(m_entries.begin(), m_entries.end(), e, order) - m_entries.begin()); + beginInsertRows({}, row, row); + m_entries.insert(row, e); + endInsertRows(); + } + emit countChanged(); +} + bool FolderModel::expandable() const { return !m_path.isEmpty() && !isView(m_path) && !isTrash(m_path) && m_query.isEmpty(); } diff --git a/src/foldermodel.h b/src/foldermodel.h index 80cf9aeb..923d1e7d 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -157,6 +157,8 @@ class FolderModel : public QAbstractListModel { static int itemCount(const QString &folder, bool showHidden, const std::function &stop = {}); static QStringList sortModes(); static void sortEntries(QVector &entries, const QString &sort, const QLocale &locale = QLocale()); + // The order sortEntries() sorts by: whether `a` comes before `b`. + static std::function before(const QString &sort, const QLocale &locale = QLocale()); // The Recents view: a path that isn't a folder, listing recently used files. static QString recentPath() { return QStringLiteral("recent:"); } static QString recentFile(); @@ -184,9 +186,12 @@ class FolderModel : public QAbstractListModel { // Everything under `root` whose name contains `query` (ignoring case), // named by its path from `root`. Stops at `limit` matches, at other // filesystems and at symlinks, and as soon as `stop` returns true. + // `found`, when given, gets the matches as they're found, a batch at a + // time (every tenth of a second at most), in no order. static QVector search(const QString &root, const QString &query, bool showHidden, const QString &sort, const std::function &stop = {}, int limit = 1000, - const QLocale &locale = QLocale(), const QStringList &filter = {}); + const QLocale &locale = QLocale(), const QStringList &filter = {}, + const std::function &)> &found = {}); signals: void pathChanged(); @@ -207,6 +212,12 @@ class FolderModel : public QAbstractListModel { private: void setLoading(bool loading); + // A search of the folder and below, its matches shown as they're found. + void searchHere(quint64 generation); + // Matches just found, each put in its place in the folder's order. + void insertFound(const QVector &found); + // The search whose matches are shown (or being found). + QString m_shownQuery; void setEntries(const QVector &entries); void clearEntries(); QString cacheKey() const; diff --git a/src/i18n/en.json b/src/i18n/en.json index b742a47c..2e833a64 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -353,6 +353,7 @@ "Search %1": "Search %1", "Search %1 and below": "Search %1 and below", "Search this folder and below": "Search this folder and below", + "Searching…": "Searching…", "Select All": "Select All", "Select a range": "Select a range", "Select everything": "Select everything", diff --git a/src/resources.qrc b/src/resources.qrc index 8ecd4571..45fb73d6 100644 --- a/src/resources.qrc +++ b/src/resources.qrc @@ -32,6 +32,7 @@ FormatDialog.qml DriveMenu.qml PlaceMenu.qml + Spinner.qml RenameDialog.qml MountPrompt.qml PreviewWindow.qml diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 3f606369..411e09a8 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -996,6 +997,57 @@ private slots: QVERIFY(model.query().isEmpty()); } + // A search tells what it finds as it goes, rather than all at the end: + // a big tree could keep the window showing nothing for a long while. + void folderSearchShowsMatchesAsTheyreFound() + { + QTemporaryDir dir; + for (int i = 0; i < 40; ++i) { + const QString folder = QString("d%1").arg(i, 2, 10, QLatin1Char('0')); + QDir(dir.path()).mkpath(folder); + write(dir.filePath(folder + "/match-" + folder + ".txt"), "x"); + write(dir.filePath(folder + "/other.txt"), "x"); + } + // A slow walk (each look at the stop check takes a while), so the + // matches come in several batches before it's done. + QStringList told; + int batches = 0; + const auto found = FolderModel::search(dir.path(), "match", false, "name", + [] { QThread::usleep(2000); return false; }, 1000, QLocale(), {}, + [&](const QVector &batch) { + ++batches; + for (const auto &e : batch) + told << e.name; + }); + QVERIFY2(batches > 1, qPrintable(QString::number(batches))); + QStringList all; + for (const auto &e : found) + all << e.name; + QCOMPARE(all.size(), 40); + told.sort(); + QCOMPARE(told, all); + + // The model shows a new search from empty, at once, not the folder it + // searches; and ends with its matches in the folder's order. + FolderModel model; + QSignalSpy loaded(&model, &FolderModel::loaded); + model.setPath(dir.path()); + QVERIFY(loaded.wait(3000)); + QCOMPARE(model.count(), 40); + model.setQuery("match"); + QCOMPARE(model.count(), 0); + QVERIFY(model.loading()); + QTRY_VERIFY(!model.loading()); + QCOMPARE(model.count(), 40); + for (int row = 0; row < 40; ++row) + QCOMPARE(model.get(row).value("name").toString(), all.at(row)); + // The same search again (the folder changed) keeps its matches up. + model.reload(); + QCOMPARE(model.count(), 40); + QTRY_VERIFY(!model.loading()); + QCOMPARE(model.count(), 40); + } + void folderSearchSkipsSymlinkedFolders() { // A link back up the tree would otherwise be walked round and round. From d78f65d54817d0061c94e93831152f790dfecc3e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 17 Nov 2026 22:11:42 +0100 Subject: [PATCH 2/2] Tell a search's matches through quiet stretches, and drop vanished ones from the selection Matches were told only as another was found, so one found early, before a long walk through folders with no more matches, stayed hidden until the search ended. The time is now checked at every entry walked. The same search run again (the folder changed under it) swapped in its new matches but kept selecting those no longer found, so the selection's count was off. It now keeps only what's still listed, as a re-read of a folder does. Co-Authored-By: Claude Opus 5.5 --- src/foldermodel.cpp | 13 ++++++++++--- tests/folio_tests.cpp | 25 +++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index c33fe12b..8cf2f0c8 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -421,11 +421,12 @@ QVector FolderModel::search(const QString &root, const QStri Entry e = entryFor(info, mimes); e.name = base.relativeFilePath(e.path); // Only what fits a file chooser's filter uses up the limit. - if (fits(filter, e)) { + if (fits(filter, e)) entries.push_back(e); - tell(false); - } } + // At every entry, not just at matches: a match found early isn't + // held back by a long stretch of folders with none. + tell(false); // Other filesystems (a mounted drive, another machine, /proc // from /) and symlinked folders aren't walked into. struct stat st; @@ -839,6 +840,12 @@ void FolderModel::searchHere(quint64 generation) { sortEntries(all, m_sort); if (!sameListing(m_entries, all)) setEntries(all); + // What was selected and is no longer found isn't selected. + QSet still; + for (const Entry &e : std::as_const(m_entries)) + if (m_selected.contains(e.path)) + still.insert(e.path); + setSelected(still); } setLoading(false); emit loaded(); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 411e09a8..96a49f26 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -1041,11 +1041,32 @@ private slots: QCOMPARE(model.count(), 40); for (int row = 0; row < 40; ++row) QCOMPARE(model.get(row).value("name").toString(), all.at(row)); - // The same search again (the folder changed) keeps its matches up. + // The same search again (the folder changed) keeps its matches up, + // and what was selected and is gone is no longer selected. + model.selectRows({0, 1}); + QVERIFY(QFile::remove(dir.filePath("d00/match-d00.txt"))); model.reload(); QCOMPARE(model.count(), 40); QTRY_VERIFY(!model.loading()); - QCOMPARE(model.count(), 40); + QCOMPARE(model.count(), 39); + QCOMPARE(model.selectedCount(), 1); + QCOMPARE(model.selectedPaths(), QStringList({dir.filePath("d01/match-d01.txt")})); + + // A match found early is told before the walk ends, though nothing + // else matches after it. + QTemporaryDir quiet; + write(quiet.filePath("early-match.txt"), "x"); + for (int i = 0; i < 30; ++i) { + QDir(quiet.path()).mkpath(QString("f%1").arg(i)); + for (int j = 0; j < 5; ++j) + write(quiet.filePath(QString("f%1/file-%2.txt").arg(i).arg(j)), "x"); + } + int looks = 0, lookedWhenTold = -1; + FolderModel::search(quiet.path(), "match", false, "name", + [&] { ++looks; QThread::usleep(2000); return false; }, 1000, QLocale(), {}, + [&](const QVector &) { if (lookedWhenTold < 0) lookedWhenTold = looks; }); + QVERIFY2(lookedWhenTold > 0 && lookedWhenTold < looks / 2, + qPrintable(QString("told at %1 of %2").arg(lookedWhenTold).arg(looks))); } void folderSearchSkipsSymlinkedFolders()