diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index 2cce5a3f..19793677 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -431,10 +431,13 @@ void FolderModel::summarize(Entry &entry, bool showHidden) { } } -int FolderModel::itemCount(const QString &folder, bool showHidden) { +int FolderModel::itemCount(const QString &folder, bool showHidden, const std::function &stop) { int count = 0; - for (QDirIterator it(folder, entryFilters(showHidden)); it.hasNext(); it.next()) + for (QDirIterator it(folder, entryFilters(showHidden)); it.hasNext(); it.next()) { + if (stop && stop()) + return -1; ++count; + } return count; } @@ -527,10 +530,11 @@ void FolderModel::countFolders(quint64 generation) { watcher->setFuture(QtConcurrent::run([folders, showHidden, latest, generation]() mutable { // Overtaken (another folder, or a re-read), the counts would be // thrown away, so it stops instead of listing every subfolder. + const auto overtaken = [&] { return *latest != generation; }; for (auto &folder : folders) { - if (*latest != generation) + if (overtaken()) break; - folder.second.itemCount = itemCount(folder.second.path, showHidden); + folder.second.itemCount = itemCount(folder.second.path, showHidden, overtaken); } return folders; })); diff --git a/src/foldermodel.h b/src/foldermodel.h index 681a2c7e..a7fa7c67 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -119,8 +119,8 @@ class FolderModel : public QAbstractListModel { // A folder's item count and first pictures, for the preview window. static void summarize(Entry &entry, bool showHidden); // Only how many items a folder holds, for the folder counts, which have - // no use for its pictures or their order. - static int itemCount(const QString &folder, bool showHidden); + // no use for its pictures or their order. Stopped partway, it's -1 (unknown). + 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 Recents view: a path that isn't a folder, listing recently used files. diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 21e81d07..3fdba20e 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -559,6 +559,9 @@ private slots: // The folder counts just count, the hidden only when they show. QCOMPARE(FolderModel::itemCount(dir.path(), false), 4); QCOMPARE(FolderModel::itemCount(dir.path(), true), 5); + // Stopped partway through, a count is unknown rather than partial. + int checks = 0; + QCOMPARE(FolderModel::itemCount(dir.path(), false, [&] { return ++checks > 1; }), -1); } void folderModelLoadsInTheBackground()