From 5701a667e98ac0efa7fa8dbcfd952cd02f2a3c07 Mon Sep 17 00:00:00 2001 From: Kirill Kostin <15322854+k-kostin@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:08:56 +0300 Subject: [PATCH 1/2] Keep drag-picture grabs alive during render callbacks --- src/FileRow.qml | 12 +++- src/FileTile.qml | 12 +++- src/dragpicture.cpp | 22 ++++++ src/dragpicture.h | 28 ++++++++ src/main.cpp | 2 + tests/folio_tests.cpp | 152 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 222 insertions(+), 6 deletions(-) create mode 100644 src/dragpicture.cpp create mode 100644 src/dragpicture.h diff --git a/src/FileRow.qml b/src/FileRow.qml index 26031f46..13c2d209 100644 --- a/src/FileRow.qml +++ b/src/FileRow.qml @@ -1,4 +1,5 @@ import QtQuick +import Folio 1.0 import QtQuick.Layouts import QtQuick.Effects import "Format.js" as Format @@ -187,11 +188,16 @@ Item { } } - HoverHandler { - id: hover + DragPicture { + id: dragPicture // Cleared first: Qt (6.12) reloads a new picture at the size of the // one before, which for a grab only warns ("Ignoring sourceSize"). - onHoveredChanged: if (hovered) row.grabToImage(result => { row.Drag.imageSource = ""; row.Drag.imageSource = result.url }) + onReady: { row.Drag.imageSource = ""; row.Drag.imageSource = url } + } + + HoverHandler { + id: hover + onHoveredChanged: if (hovered) dragPicture.capture(row) } TapHandler { id: tap diff --git a/src/FileTile.qml b/src/FileTile.qml index 4be99d2d..bca8c28d 100644 --- a/src/FileTile.qml +++ b/src/FileTile.qml @@ -1,4 +1,5 @@ import QtQuick +import Folio 1.0 import QtQuick.Shapes import QtQuick.Effects import "Format.js" as Format @@ -119,13 +120,18 @@ Item { onDropped: (drop) => { fileOps.dropInto(drop.urls, tile.path); drop.accept(Qt.MoveAction) } } + DragPicture { + id: dragPicture + // Cleared first: Qt (6.12) reloads a new picture at the size of the + // one before, which for a grab only warns ("Ignoring sourceSize"). + onReady: { tile.Drag.imageSource = ""; tile.Drag.imageSource = url } + } + HoverHandler { id: hover // The picture that follows the pointer while dragging; taken ahead // of time because a drag can't wait for it. - // Cleared first: Qt (6.12) reloads a new picture at the size of the - // one before, which for a grab only warns ("Ignoring sourceSize"). - onHoveredChanged: if (hovered) art.grabToImage(result => { tile.Drag.imageSource = ""; tile.Drag.imageSource = result.url }) + onHoveredChanged: if (hovered) dragPicture.capture(art) } // Dragging hands the file to whatever it lands on as a URI list: the diff --git a/src/dragpicture.cpp b/src/dragpicture.cpp new file mode 100644 index 00000000..8a25b199 --- /dev/null +++ b/src/dragpicture.cpp @@ -0,0 +1,22 @@ +#include "dragpicture.h" + +#include + +bool DragPicture::capture(QQuickItem *item) { + if (!item) + return false; + auto grab = item->grabToImage(); + if (!grab) + return false; + m_source = item; + m_grab = grab; + connect(grab.data(), &QQuickItemGrabResult::ready, this, [this, request = grab.data()] { + if (m_grab.data() == request && m_source) + emit ready(); + }); + return true; +} + +QUrl DragPicture::url() const { + return m_grab ? m_grab->url() : QUrl(); +} diff --git a/src/dragpicture.h b/src/dragpicture.h new file mode 100644 index 00000000..8824984e --- /dev/null +++ b/src/dragpicture.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include +#include +#include + +class QQuickItem; + +// A drag's picture, taken ahead of time. The C++ grab API keeps a render +// callback's result alive even if its item disappears while it is running. +class DragPicture : public QObject { + Q_OBJECT + Q_PROPERTY(QUrl url READ url NOTIFY ready) + +public: + explicit DragPicture(QObject *parent = nullptr) : QObject(parent) {} + Q_INVOKABLE bool capture(QQuickItem *item); + QUrl url() const; + +signals: + void ready(); + +private: + QPointer m_source; + QSharedPointer m_grab; +}; diff --git a/src/main.cpp b/src/main.cpp index 914e4ce4..00b4e033 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ #include #include "chooser.h" +#include "dragpicture.h" #include "filemanager1.h" #include "foldermodel.h" #include "places.h" @@ -146,6 +147,7 @@ int main(int argc, char *argv[]) { engine.addImageProvider("plugin-icon", new PluginIconProvider); // Creatable too: the column view lists each folder on the way here. qmlRegisterType("Folio", 1, 0, "FolderModel"); + qmlRegisterType("Folio", 1, 0, "DragPicture"); engine.rootContext()->setContextProperty("translator", &translator); engine.rootContext()->setContextProperty("theme", &theme); engine.rootContext()->setContextProperty("places", &places); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 1de00c72..ae0c58b4 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -50,6 +50,7 @@ #include "backend.h" #include "chooser.h" #include "disks.h" +#include "dragpicture.h" #include "fileops.h" #include "foldermodel.h" #include "mounter.h" @@ -549,11 +550,162 @@ class FolioTests : public QObject { private slots: void initTestCase() { + qmlRegisterType("Folio", 1, 0, "DragPicture"); // No test ever reaches the real udisks, which would change real disks. fakeDisks({}); Backend::setSettingsFile(settings.filePath("folio.conf")); } + void dragPicturesKeepTheLatestImage() + { + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData(R"( + import QtQuick + import Folio 1.0 + Window { + width: 160; height: 64; visible: true + DragPicture { id: picture; objectName: "picture" } + Rectangle { objectName: "red"; width: 24; height: 24; color: "red" } + Rectangle { objectName: "green"; x: 32; width: 24; height: 24; color: "lime" } + Image { objectName: "preview"; x: 96; width: 24; height: 24; source: picture.url } + } + )", QUrl("file:///DragPictureTest.qml")); + std::unique_ptr root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + auto *window = qobject_cast(root.get()); + QVERIFY(QTest::qWaitForWindowExposed(window)); + auto *picture = root->findChild("picture"); + auto *red = root->findChild("red"); + auto *green = root->findChild("green"); + auto *preview = root->findChild("preview"); + QVERIFY(picture && red && green && preview); + QSignalSpy ready(picture, &DragPicture::ready); + const auto shown = [&] { + // grabWindow returns physical pixels, including at high DPI. + const auto frame = window->grabWindow(); + const QPointF at = preview->mapToScene(QPointF(12, 12)) * window->devicePixelRatio(); + return frame.isNull() ? QColor() : frame.pixelColor(at.toPoint()); + }; + QVERIFY(picture->capture(red)); + QTRY_COMPARE(ready.count(), 1); + const QUrl first = picture->url(); + QVERIFY(!first.isEmpty()); + QTRY_COMPARE(shown(), QColor(Qt::red)); + // The URL must stay usable after QML's garbage collection, until + // another capture replaces it, rather than referring to freed data. + engine.collectGarbage(); + window->update(); + QTRY_COMPARE(shown(), QColor(Qt::red)); + ready.clear(); + QVERIFY(picture->capture(red)); + QVERIFY(picture->capture(green)); + QTRY_COMPARE(ready.count(), 1); + QVERIFY(picture->url() != first); + QTRY_COMPARE(shown(), QColor(Qt::green)); + QVERIFY(!picture->capture(nullptr)); + QCOMPARE(ready.count(), 1); + QTRY_COMPARE(shown(), QColor(Qt::green)); + } + + void dragPicturesAllowPendingItemsAndWindowsToDisappear() + { + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData(R"( + import QtQuick + import Folio 1.0 + Window { + width: 64; height: 64; visible: true + DragPicture { objectName: "picture" } + Rectangle { objectName: "source"; width: 24; height: 24; color: "red" } + } + )", QUrl("file:///PendingDragPictureTest.qml")); + // Destroy a source while its snapshot is queued, then the whole + // window with one queued. No reply may reach a discarded delegate. + for (int i = 0; i < 12; ++i) { + std::unique_ptr root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + auto *window = qobject_cast(root.get()); + QVERIFY(QTest::qWaitForWindowExposed(window)); + auto *picture = root->findChild("picture"); + auto *source = root->findChild("source"); + QVERIFY(picture && source); + QSignalSpy ready(picture, &DragPicture::ready); + QVERIFY(picture->capture(source)); + if (i % 2 == 0) { + delete source; + // A rendered frame is the boundary for the pending grab. + QSignalSpy frame(window, &QQuickWindow::frameSwapped); + window->update(); + QTRY_VERIFY(!frame.isEmpty()); + QCOMPARE(ready.count(), 0); + } else { + QPointer closing(picture); + root.reset(); + QVERIFY(closing.isNull()); + QCoreApplication::processEvents(); + } + } + } + + void fileDelegatesPrepareDragPictures_data() + { + QTest::addColumn("delegate"); + QTest::newRow("list-and-columns") << QString("FileRow"); + QTest::newRow("grid") << QString("FileTile"); + } + + void fileDelegatesPrepareDragPictures() + { + QFETCH(QString, delegate); + QTemporaryDir home; + Backend backend(home.path()); + Theme theme(home.filePath("theme")); + FileOps fileOps; + Translator translator(home.filePath("language"), sourceFolder() + "/i18n"); + Thumbnails thumbnails(home.filePath("cache"), {home.filePath("none")}); + QQmlEngine engine; + engine.rootContext()->setContextProperty("backend", &backend); + engine.rootContext()->setContextProperty("theme", &theme); + engine.rootContext()->setContextProperty("fileOps", &fileOps); + engine.rootContext()->setContextProperty("translator", &translator); + engine.addImageProvider("thumb", new ThumbnailProvider(&thumbnails)); + QQmlComponent component(&engine); + component.setData(QString(R"( + import QtQuick + Window { + width: 640; height: 240; visible: true + %1 { + id: entry; objectName: "entry"; width: 300; y: 20; height: %3 + index: 0; name: "notes.txt"; path: "/notes.txt"; isDir: false + kind: "text"; mime: "text/plain"; extension: "TXT" + size: 1; modified: new Date(0); itemCount: -1; icon: ""; selected: false + %2 + } + function pictureUrl() { return String(entry.Drag.imageSource) } + } + )").arg(delegate, delegate == "FileRow" ? "depth: 0; expanded: false" : "", + delegate == "FileRow" ? "32" : "160").toUtf8(), + QUrl::fromLocalFile(sourceFolder() + "/DelegateDragPictureTest.qml")); + std::unique_ptr root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + auto *window = qobject_cast(root.get()); + QVERIFY(QTest::qWaitForWindowExposed(window)); + auto *entry = root->findChild("entry"); + QVERIFY(entry); + // Move out and back in: the real hover handler must precompute the + // picture and hand its still-live URL to Qt's drag machinery. + QTest::mouseMove(window, QPoint(600, 220)); + QTest::mouseMove(window, entry->mapToScene(QPointF(100, 16)).toPoint()); + QVariant url; + QTRY_VERIFY((QMetaObject::invokeMethod(root.get(), "pictureUrl", Q_RETURN_ARG(QVariant, url)), + !url.toString().isEmpty())); + auto *picture = entry->findChild(); + QVERIFY(picture); + QCOMPARE(QUrl(url.toString()), picture->url()); + } + void foldersRememberTheirOrderAndPassItDown() { QTemporaryDir home; From 7f56654ce19c54101c480b7a2beb6c1adcec818e Mon Sep 17 00:00:00 2001 From: Kirill Kostin <15322854+k-kostin@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:13:28 +0300 Subject: [PATCH 2/2] Retain pending grabs through completion or window teardown --- src/dragpicture.cpp | 7 +++++++ tests/folio_tests.cpp | 22 ++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/dragpicture.cpp b/src/dragpicture.cpp index 8a25b199..3fea9373 100644 --- a/src/dragpicture.cpp +++ b/src/dragpicture.cpp @@ -1,6 +1,7 @@ #include "dragpicture.h" #include +#include bool DragPicture::capture(QQuickItem *item) { if (!item) @@ -8,6 +9,12 @@ bool DragPicture::capture(QQuickItem *item) { auto grab = item->grabToImage(); if (!grab) return false; + // A replaced or discarded delegate can let go between Qt's setup and + // render callbacks. Keep that frame's work alive until it has completed; + // queue the release after ready returns. The window also releases it on + // teardown, after its render thread has stopped. + connect(grab.data(), &QQuickItemGrabResult::ready, item->window(), [grab] {}, + Qt::ConnectionType(Qt::QueuedConnection | Qt::SingleShotConnection)); m_source = item; m_grab = grab; connect(grab.data(), &QQuickItemGrabResult::ready, this, [this, request = grab.data()] { diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index ae0c58b4..9b0d683c 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -569,6 +569,7 @@ private slots: Rectangle { objectName: "red"; width: 24; height: 24; color: "red" } Rectangle { objectName: "green"; x: 32; width: 24; height: 24; color: "lime" } Image { objectName: "preview"; x: 96; width: 24; height: 24; source: picture.url } + Image { objectName: "later"; x: 128; width: 24; height: 24 } } )", QUrl("file:///DragPictureTest.qml")); std::unique_ptr root(component.create()); @@ -579,33 +580,38 @@ private slots: auto *red = root->findChild("red"); auto *green = root->findChild("green"); auto *preview = root->findChild("preview"); - QVERIFY(picture && red && green && preview); + auto *later = root->findChild("later"); + QVERIFY(picture && red && green && preview && later); QSignalSpy ready(picture, &DragPicture::ready); - const auto shown = [&] { + const auto shown = [&](QQuickItem *image) { // grabWindow returns physical pixels, including at high DPI. const auto frame = window->grabWindow(); - const QPointF at = preview->mapToScene(QPointF(12, 12)) * window->devicePixelRatio(); + const QPointF at = image->mapToScene(QPointF(12, 12)) * window->devicePixelRatio(); return frame.isNull() ? QColor() : frame.pixelColor(at.toPoint()); }; QVERIFY(picture->capture(red)); QTRY_COMPARE(ready.count(), 1); const QUrl first = picture->url(); QVERIFY(!first.isEmpty()); - QTRY_COMPARE(shown(), QColor(Qt::red)); + QTRY_COMPARE(shown(preview), QColor(Qt::red)); // The URL must stay usable after QML's garbage collection, until // another capture replaces it, rather than referring to freed data. + // Release the first consumer's pixels before collecting, then load + // the saved URL in an initially empty Image. + QVERIFY(preview->setProperty("source", QUrl())); engine.collectGarbage(); - window->update(); - QTRY_COMPARE(shown(), QColor(Qt::red)); + QVERIFY(later->setProperty("source", first)); + QTRY_COMPARE(shown(later), QColor(Qt::red)); + QVERIFY(later->setProperty("source", QUrl())); ready.clear(); QVERIFY(picture->capture(red)); QVERIFY(picture->capture(green)); QTRY_COMPARE(ready.count(), 1); QVERIFY(picture->url() != first); - QTRY_COMPARE(shown(), QColor(Qt::green)); + QTRY_COMPARE(shown(preview), QColor(Qt::green)); QVERIFY(!picture->capture(nullptr)); QCOMPARE(ready.count(), 1); - QTRY_COMPARE(shown(), QColor(Qt::green)); + QTRY_COMPARE(shown(preview), QColor(Qt::green)); } void dragPicturesAllowPendingItemsAndWindowsToDisappear()