From 15256c0bbb177884625fb19059a09649df0d53a7 Mon Sep 17 00:00:00 2001 From: Kirill Kostin <15322854+k-kostin@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:25:56 +0300 Subject: [PATCH 1/2] Prototype list row reuse with pooled-state cleanup --- src/FileRow.qml | 35 +++++++++++++++++++++++++++++++---- src/FolderList.qml | 9 +++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/FileRow.qml b/src/FileRow.qml index a8dab284..4afbcb6f 100644 --- a/src/FileRow.qml +++ b/src/FileRow.qml @@ -27,6 +27,31 @@ Item { property bool detailed: false property bool chevron: false + // Model roles change on reuse; drag assets belong to the old file. + property bool pooled: false + property int dragGeneration: 0 + function clearDragAssets() { + ++dragGeneration + Drag.imageSource = "" + Drag.mimeData = ({}) + } + onPathChanged: clearDragAssets() + ListView.onPooled: { + pooled = true + clearDragAssets() + } + ListView.onReused: pooled = false + + function captureDragPicture() { + const target = row + const generation = ++dragGeneration + const path = row.path + return grabToImage(result => { + if (target && !target.pooled && target.path === path && target.dragGeneration === generation) + target.Drag.imageSource = result.url + }) + } + signal clicked(int modifiers) signal activated() @@ -50,7 +75,7 @@ Item { DropArea { id: folderDrop anchors.fill: parent - enabled: row.isDir + enabled: !row.pooled && row.isDir keys: ["text/uri-list"] onEntered: (drag) => { drag.accepted = fileOps.canDrop(drag.urls, row.path) } onDropped: (drop) => { fileOps.dropInto(drop.urls, row.path); drop.accept(Qt.MoveAction) } @@ -90,7 +115,7 @@ Item { } asynchronous: true fillMode: Image.PreserveAspectCrop - source: row.kind === "image" || row.kind === "video" ? Format.thumb(row.path, row.mime, 128, row.modified) : "" + source: !row.pooled && (row.kind === "image" || row.kind === "video") ? Format.thumb(row.path, row.mime, 128, row.modified) : "" visible: status === Image.Ready } Rectangle { @@ -152,10 +177,12 @@ Item { HoverHandler { id: hover - onHoveredChanged: if (hovered) row.grabToImage(result => row.Drag.imageSource = result.url) + enabled: !row.pooled + onHoveredChanged: if (hovered) row.captureDragPicture() } TapHandler { id: tap + enabled: !row.pooled onTapped: row.clicked(tap.point.modifiers) onDoubleTapped: row.activated() } @@ -168,7 +195,7 @@ Item { Drag.hotSpot.x: LayoutMirroring.enabled ? row.width : 0 DragHandler { target: null - enabled: row.kind !== "item" + enabled: !row.pooled && row.kind !== "item" // Dragging a selected item takes the whole selection along. The list // is built as the drag starts, not again on every change of selection. onActiveChanged: { diff --git a/src/FolderList.qml b/src/FolderList.qml index 8e93a633..12046a19 100644 --- a/src/FolderList.qml +++ b/src/FolderList.qml @@ -41,6 +41,10 @@ ListView { onTapped: (point) => list.contextMenu(selecting.menuAt(point), backend.path, point.scenePosition) } + // Never recycle an active drag into another file. While dragging, + // disposal follows the existing non-reusing behavior. + property Item dragSource: null + reuseItems: dragSource === null clip: true focus: true keyNavigationEnabled: true @@ -54,6 +58,11 @@ ListView { ScrollBar.vertical: FolioScrollBar { } delegate: FileRow { + id: entry + Drag.onActiveChanged: { + if (Drag.active) list.dragSource = entry + else if (list.dragSource === entry) list.dragSource = null + } width: list.width detailed: true current: selected From 98628c27af2bcd2264e0e259fdff4314bd19e0f0 Mon Sep 17 00:00:00 2001 From: Kirill Kostin <15322854+k-kostin@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:22:29 +0300 Subject: [PATCH 2/2] Reset tap history on row reuse and cover interaction lifetimes --- CMakeLists.txt | 5 + src/FileRow.qml | 15 ++- tests/rowreuse_tests.cpp | 273 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 tests/rowreuse_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b9cd2268..b88a7e41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,3 +37,8 @@ add_test(NAME folio_tests COMMAND folio_tests) set_tests_properties(folio_tests PROPERTIES ENVIRONMENT QT_QPA_PLATFORM=offscreen) # The bundled plugins, which the tests run against stubs. target_compile_definitions(folio_tests PRIVATE FOLIO_PLUGINS="${CMAKE_SOURCE_DIR}/plugins") + +add_executable(rowreuse_tests tests/rowreuse_tests.cpp) +target_link_libraries(rowreuse_tests PRIVATE folio_core Qt6::Test) +add_test(NAME rowreuse_tests COMMAND rowreuse_tests) +set_tests_properties(rowreuse_tests PROPERTIES ENVIRONMENT QT_QPA_PLATFORM=offscreen) diff --git a/src/FileRow.qml b/src/FileRow.qml index 4afbcb6f..f25a86e3 100644 --- a/src/FileRow.qml +++ b/src/FileRow.qml @@ -180,11 +180,16 @@ Item { enabled: !row.pooled onHoveredChanged: if (hovered) row.captureDragPicture() } - TapHandler { - id: tap - enabled: !row.pooled - onTapped: row.clicked(tap.point.modifiers) - onDoubleTapped: row.activated() + // A TapHandler remembers the previous tap even when disabled. A reused + // row must not join clicks on two different files into a double click. + Loader { + active: !row.pooled + sourceComponent: TapHandler { + id: tap + parent: row + onTapped: row.clicked(tap.point.modifiers) + onDoubleTapped: row.activated() + } } // Like the tiles: dragged, a row is a file for the sidebar or another app. diff --git a/tests/rowreuse_tests.cpp b/tests/rowreuse_tests.cpp new file mode 100644 index 00000000..1d91b81a --- /dev/null +++ b/tests/rowreuse_tests.cpp @@ -0,0 +1,273 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "backend.h" +#include "fileops.h" +#include "theme.h" +#include "thumbnails.h" +#include "translator.h" + +static QString sources() { return QFileInfo(QFINDTESTDATA("../src/Format.js")).absolutePath(); } +static void writeFile(const QString &path, const QByteArray &bytes = "fixture") { + QFile file(path); + if (!file.open(QIODevice::WriteOnly) || file.write(bytes) != bytes.size()) qFatal("Cannot write fixture"); +} + +// Only opening another program is fenced. The folder, selection and file +// operations exercised by the QML are the real ones. +class TestBackend : public QObject { + Q_OBJECT + Q_PROPERTY(FolderModel *folder READ folder CONSTANT) + Q_PROPERTY(QString path READ path NOTIFY pathChanged) +public: + explicit TestBackend(const QString &home) : backend(home) { + connect(&backend, &Backend::pathChanged, this, &TestBackend::pathChanged); + } + FolderModel *folder() { return backend.folder(); } + QString path() const { return backend.path(); } + QStringList opened; + Q_INVOKABLE void open(const QString &path) { opened << path; } + Q_INVOKABLE void navigate(const QString &path) { backend.navigate(path); } + Q_INVOKABLE void goUp() { backend.goUp(); } +signals: + void pathChanged(); +private: + Backend backend; +}; + +struct LocaleGuard { + QLocale before; + ~LocaleGuard() { QLocale::setDefault(before); } +}; + +class ListFixture { +public: + LocaleGuard locale; + QTemporaryDir home; + TestBackend backend{home.path()}; + Theme theme{home.filePath("theme")}; + FileOps ops; + Translator translator{home.filePath("language"), sources() + "/i18n"}; + std::unique_ptr plugins{QQmlPropertyMap::create()}; + Thumbnails thumbnails{home.filePath("cache"), {home.filePath("none")}}; + QQmlEngine engine; + std::unique_ptr root; + QQuickItem *list = nullptr; + QString error; + + ListFixture() { + QDir().mkpath(folder()); + for (int i = 0; i < 400; ++i) writeFile(folder() + QString("/file%1.txt").arg(i, 3, 10, QChar('0'))); + plugins->insert("views", QVariantMap{}); + auto *context = engine.rootContext(); + context->setContextProperty("backend", &backend); + context->setContextProperty("theme", &theme); + context->setContextProperty("fileOps", &ops); + context->setContextProperty("translator", &translator); + context->setContextProperty("plugins", plugins.get()); + engine.addImageProvider("thumb", new ThumbnailProvider(&thumbnails)); + } + ~ListFixture() { + root.reset(); + QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); + QThreadPool::globalInstance()->waitForDone(); + } + QString folder() const { return home.filePath("folder"); } + QQuickWindow *window() const { return qobject_cast(root.get()); } + bool show(bool mirrored = false) { + backend.navigate(folder()); + if (!QTest::qWaitFor([&] { return !backend.folder()->loading(); })) return false; + QQmlComponent component(&engine); + component.setData("import QtQuick\nWindow { width: 900; height: 480; visible: true\n" + " Item { anchors.fill: parent; LayoutMirroring.enabled: " + QByteArray(mirrored ? "true" : "false") + + "; LayoutMirroring.childrenInherit: true; FolderList { objectName: 'list'; anchors.fill: parent } } }", + QUrl::fromLocalFile(sources() + "/RowReuseTest.qml")); + root.reset(component.create()); + error = component.errorString(); + if (!root) return false; + list = root->findChild("list"); + return list && QTest::qWaitForWindowExposed(window()) && QTest::qWaitFor([&] { return item(4) != nullptr; }); + } + QQuickItem *item(int index) const { + QQuickItem *found = nullptr; + QMetaObject::invokeMethod(list, "itemAtIndex", Q_RETURN_ARG(QQuickItem *, found), Q_ARG(int, index)); + return found; + } + void move(int index) { + QMetaObject::invokeMethod(list, "positionViewAtIndex", Q_ARG(int, index), Q_ARG(int, 0)); + QMetaObject::invokeMethod(list, "forceLayout"); + } + QVariant eval(QObject *object, const QString &code) { + QQmlExpression expression(qmlContext(object), object, code); + QVariant result = expression.evaluate(); + if (expression.hasError()) error = expression.error().toString(); + return result; + } + QPoint point(QQuickItem *item) const { + return item->mapToItem(window()->contentItem(), QPointF(100, item->height() / 2)).toPoint(); + } +}; + +static void clickAt(QQuickWindow *window, const QPoint &at, ulong timestamp) { + const QPointF global = window->mapToGlobal(at); + QMouseEvent press(QEvent::MouseButtonPress, at, global, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); + press.setTimestamp(timestamp); + QGuiApplication::sendEvent(window, &press); + QMouseEvent release(QEvent::MouseButtonRelease, at, global, Qt::LeftButton, Qt::NoButton, Qt::NoModifier); + release.setTimestamp(timestamp + 1); + QGuiApplication::sendEvent(window, &release); +} + +class RowReuseTests : public QObject { + Q_OBJECT +private slots: + void reusesRowsWithoutOldDragAssets_data() { + QTest::addColumn("mirrored"); + QTest::newRow("ltr") << false; + QTest::newRow("rtl") << true; + } + void reusesRowsWithoutOldDragAssets() { + QFETCH(bool, mirrored); + ListFixture f; + QVERIFY2(f.show(mirrored), qPrintable(f.error)); + QPointer row = f.item(4); + QVERIFY(row); + const QString original = row->property("path").toString(); + f.eval(row, "Drag.imageSource = 'file:///old-drag.png'; Drag.mimeData = {'text/uri-list':'file:///old-file.txt'}"); + f.move(120); f.move(240); + QTRY_VERIFY(row && row->property("path").toString() != original); + QCOMPARE(f.eval(row, "String(Drag.imageSource)").toString(), QString()); + QVERIFY(f.eval(row, "Object.keys(Drag.mimeData).length === 0").toBool()); + QVERIFY(f.error.isEmpty()); + } + + void doesNotTurnTheFirstClickOnAReusedFileIntoADoubleClick() { + ListFixture f; + QVERIFY2(f.show(), qPrintable(f.error)); + QPointer row = f.item(4); + const QString original = row->property("path").toString(); + const QPoint at = f.point(row); + clickAt(f.window(), at, 1000); + QCOMPARE(f.backend.folder()->selectedPaths(), QStringList{original}); + // Another row becomes current, making the first one reusable. + clickAt(f.window(), f.point(f.item(0)), 1040); + f.move(120); f.move(240); + QTRY_VERIFY(row && row->property("path").toString() != original); + f.move(row->property("index").toInt() - 4); + QTRY_COMPARE(f.point(row), at); + const QString reused = row->property("path").toString(); + clickAt(f.window(), at, 1100); + QCOMPARE(f.backend.folder()->selectedPaths(), QStringList{reused}); + QVERIFY2(f.backend.opened.isEmpty(), "First click on the new file activated it"); + clickAt(f.window(), at, 1150); + QCOMPARE(f.backend.opened, QStringList{reused}); + } + + void releasingAfterScrollDoesNotClickAnotherFile() { + ListFixture f; + QVERIFY2(f.show(), qPrintable(f.error)); + const QPoint at = f.point(f.item(4)); + const QStringList selected = f.backend.folder()->selectedPaths(); + QTest::mousePress(f.window(), Qt::LeftButton, Qt::NoModifier, at); + f.move(120); f.move(240); + QTest::mouseRelease(f.window(), Qt::LeftButton, Qt::NoModifier, at); + QCOMPARE(f.backend.folder()->selectedPaths(), selected); + QVERIFY(f.backend.opened.isEmpty()); + } + + void dragPausesReuseUntilItEnds() { + ListFixture f; + QVERIFY2(f.show(), qPrintable(f.error)); + QPointer row = f.item(4); + const QString path = row->property("path").toString(); + f.eval(row, "Drag.dragType = Drag.Internal; Drag.active = true"); + QVERIFY(!f.list->property("reuseItems").toBool()); + f.move(120); f.move(240); + QVERIFY(!row || row->property("path").toString() == path); + QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); + if (row) f.eval(row, "Drag.active = false"); + QTRY_VERIFY(f.list->property("reuseItems").toBool()); + QVERIFY2(f.error.isEmpty(), qPrintable(f.error)); + } + + void delayedPicturesRespectReuseAndDestruction() { + ListFixture f; + QQmlComponent component(&f.engine); + component.setData(R"(import QtQuick +Window { + id: host + width: 900; height: 480; visible: true + property var callbacks: [] + function take() { return callbacks.shift() } + FileRow { + objectName: "row"; width: 900 + index: 0; name: "a"; path: "/a"; isDir: false; kind: "text"; mime: "text/plain" + extension: "TXT"; size: 1; modified: new Date(0); itemCount: -1; icon: ""; selected: false + function grabToImage(callback) { host.callbacks.push(callback); return true } + function pool() { ListView.pooled() } + function reuse() { ListView.reused() } + } +})", QUrl::fromLocalFile(sources() + "/DelayedRowTest.qml")); + std::unique_ptr root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + QPointer row = root->findChild("row"); + QVERIFY(row); + QVERIFY(f.eval(row, "captureDragPicture()").toBool()); + QVariant callback; + QVERIFY(QMetaObject::invokeMethod(root.get(), "take", Q_RETURN_ARG(QVariant, callback))); + QJSValue old = callback.value(); + QVERIFY(old.isCallable()); + f.eval(row, "pool()"); row->setProperty("path", "/b"); f.eval(row, "reuse()"); + f.eval(row, "pool()"); row->setProperty("path", "/a"); f.eval(row, "reuse()"); + QJSValue picture = f.engine.newObject(); picture.setProperty("url", "file:///late.png"); + QVERIFY(!old.call({picture}).isError()); + QCOMPARE(f.eval(row, "String(Drag.imageSource)").toString(), QString()); + // The current request still succeeds: rejecting stale ones must not + // disable drag pictures altogether. + QVERIFY(f.eval(row, "captureDragPicture()").toBool()); + QVERIFY(QMetaObject::invokeMethod(root.get(), "take", Q_RETURN_ARG(QVariant, callback))); + QJSValue current = callback.value(); + QVERIFY(current.isCallable()); + QVERIFY(!current.call({picture}).isError()); + QCOMPARE(f.eval(row, "String(Drag.imageSource)").toString(), QString("file:///late.png")); + QVERIFY(f.eval(row, "captureDragPicture()").toBool()); + QVERIFY(QMetaObject::invokeMethod(root.get(), "take", Q_RETURN_ARG(QVariant, callback))); + QJSValue pending = callback.value(); + QVERIFY(pending.isCallable()); + root.reset(); + QVERIFY(row.isNull()); + QVERIFY(!pending.call({picture}).isError()); + QVERIFY2(f.error.isEmpty(), qPrintable(f.error)); + } + + void closesWithRealGrabPending() { + ListFixture f; + QVERIFY2(f.show(), qPrintable(f.error)); + QPointer row = f.item(4); + QVERIFY(f.eval(row, "captureDragPicture()").toBool()); + f.root.reset(); + QVERIFY(row.isNull()); + QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); + QCoreApplication::processEvents(); + } +}; + +QTEST_MAIN(RowReuseTests) +#include "rowreuse_tests.moc"