Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/FolderColumns.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/FolderGrid.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions src/FolderList.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 12 additions & 2 deletions src/PathBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions src/Spinner.qml
Original file line number Diff line number Diff line change
@@ -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
}
}
}
110 changes: 102 additions & 8 deletions src/foldermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QCoreApplication>
#include <QDir>
#include <QDirIterator>
#include <QElapsedTimer>
#include <QXmlStreamReader>
#include <QUrl>
#include <QStandardPaths>
Expand Down Expand Up @@ -384,8 +385,20 @@ FolderModel::Entry FolderModel::entryFor(const QFileInfo &info, const QMimeDatab

QVector<FolderModel::Entry> FolderModel::search(const QString &root, const QString &query, bool showHidden,
const QString &sort, const std::function<bool()> &stop,
int limit, const QLocale &locale, const QStringList &filter) {
int limit, const QLocale &locale, const QStringList &filter,
const std::function<void(const QVector<Entry> &)> &found) {
QVector<Entry> 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;
Expand All @@ -411,6 +424,9 @@ QVector<FolderModel::Entry> FolderModel::search(const QString &root, const QStri
if (fits(filter, e))
entries.push_back(e);
}
// 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;
Expand All @@ -420,6 +436,7 @@ QVector<FolderModel::Entry> FolderModel::search(const QString &root, const QStri
folders << info.absoluteFilePath();
}
}
tell(true);
sortEntries(entries, sort, locale);
return entries;
}
Expand All @@ -431,10 +448,14 @@ QStringList FolderModel::sortModes() {
}

void FolderModel::sortEntries(QVector<Entry> &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<bool(const FolderModel::Entry &, const FolderModel::Entry &)> FolderModel::before(const QString &sort, const QLocale &locale) {
auto collator = std::make_shared<QCollator>(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;
Expand All @@ -449,10 +470,10 @@ void FolderModel::sortEntries(QVector<Entry> &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),
Expand Down Expand Up @@ -560,6 +581,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<std::tuple<QVector<Entry>, QString, QStringList, QHash<QString, QVector<Entry>>, QHash<QString, QString>>>(this);
connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation] {
Expand Down Expand Up @@ -774,6 +801,73 @@ 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<QVector<Entry>>(this);
// Told once, at its first matches, so the cursor goes to the first.
auto announced = std::make_shared<bool>(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<Entry> all;
for (const QVector<Entry> &found : watcher->future().results())
all += found;
sortEntries(all, m_sort);
if (!sameListing(m_entries, all))
setEntries(all);
// What was selected and is no longer found isn't selected.
QSet<QString> still;
for (const Entry &e : std::as_const(m_entries))
if (m_selected.contains(e.path))
still.insert(e.path);
setSelected(still);
}
setLoading(false);
Comment thread
dhh marked this conversation as resolved.
Resolved
emit loaded();
countFolders(generation);
});
watcher->setFuture(QtConcurrent::run(&FolderModel::listingPool(), [path, query, showHidden, sort, filter, locale, latest, generation](QPromise<QVector<Entry>> &promise) {
search(path, query, showHidden, sort, [&] { return *latest != generation; }, 1000, locale, filter,
[&](const QVector<Entry> &found) { promise.addResult(found); });
}));
}

void FolderModel::insertFound(const QVector<Entry> &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();
}
Expand Down
13 changes: 12 additions & 1 deletion src/foldermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class FolderModel : public QAbstractListModel {
static int itemCount(const QString &folder, bool showHidden, const std::function<bool()> &stop = {});
static QStringList sortModes();
static void sortEntries(QVector<Entry> &entries, const QString &sort, const QLocale &locale = QLocale());
// The order sortEntries() sorts by: whether `a` comes before `b`.
static std::function<bool(const Entry &, const Entry &)> 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();
Expand Down Expand Up @@ -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<Entry> search(const QString &root, const QString &query, bool showHidden, const QString &sort,
const std::function<bool()> &stop = {}, int limit = 1000,
const QLocale &locale = QLocale(), const QStringList &filter = {});
const QLocale &locale = QLocale(), const QStringList &filter = {},
const std::function<void(const QVector<Entry> &)> &found = {});

signals:
void pathChanged();
Expand All @@ -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<Entry> &found);
// The search whose matches are shown (or being found).
QString m_shownQuery;
void setEntries(const QVector<Entry> &entries);
void clearEntries();
QString cacheKey() const;
Expand Down
1 change: 1 addition & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<file>FormatDialog.qml</file>
<file>DriveMenu.qml</file>
<file>PlaceMenu.qml</file>
<file>Spinner.qml</file>
<file>RenameDialog.qml</file>
<file>MountPrompt.qml</file>
<file>PreviewWindow.qml</file>
Expand Down
Loading
Loading