From e9a8a377857fda53ee6f01b559e9f3a56a204d71 Mon Sep 17 00:00:00 2001 From: Clayton Faria Date: Mon, 16 Nov 2026 15:30:58 +0800 Subject: [PATCH 1/3] Load the preview image after it joins its window --- src/PreviewWindow.qml | 7 +++++- tests/folio_tests.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/PreviewWindow.qml b/src/PreviewWindow.qml index 71ed54ea..3f1068c5 100644 --- a/src/PreviewWindow.qml +++ b/src/PreviewWindow.qml @@ -228,6 +228,11 @@ ApplicationWindow { Image { id: still + // Wait until the image belongs to its window before Qt picks + // the loading DPR, avoiding a second decode on attachment. + property bool readyToLoad: false + function startLoading() { readyToLoad = true } + Component.onCompleted: Qt.callLater(startLoading) visible: !flick.animated width: flick.natural.width * flick.zoom height: flick.natural.height * flick.zoom @@ -237,7 +242,7 @@ ApplicationWindow { autoTransform: true smooth: true mipmap: true - source: flick.animated ? "" : previewer.url + source: flick.animated || !readyToLoad ? "" : previewer.url } AnimatedImage { id: moving diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index d4cfcbe5..2c8dfcf2 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6649,6 +6649,64 @@ private slots: QVERIFY(Previewer::markdownOpening(longFence, 600).count('|') <= 600 + 4); } + void previewImageWaitsForItsWindow() + { + QTemporaryDir home; + const QString path = home.filePath("large.png"); + QImage picture(3200, 2400, QImage::Format_RGB32); + picture.fill(Qt::darkCyan); + QVERIFY(picture.save(path)); + Theme theme(home.filePath("theme")); + Previewer preview; + preview.start(path, false, "name"); + QQmlEngine engine; + QStringList warnings; + connect(&engine, &QQmlEngine::warnings, &engine, [&](const QList &errors) { + for (const auto &error : errors) + warnings << error.toString(); + }); + engine.rootContext()->setContextProperty("theme", &theme); + engine.rootContext()->setContextProperty("previewer", &preview); + QQmlComponent component(&engine, QUrl("qrc:/PreviewWindow.qml")); + std::unique_ptr root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + auto *window = qobject_cast(root.get()); + QVERIFY(window); + auto *view = root->property("view").value(); + QVERIFY(view); + auto *image = view->property("source").value(); + QVERIFY(image); + // A local file must not start decoding during component construction, + // before the completed tree has attached it to the window's screen. + QVERIFY(image->property("source").toUrl().isEmpty()); + QVERIFY(QTest::qWaitForWindowExposed(window)); + QTRY_COMPARE(image->property("status").toInt(), 1); + QCOMPARE(image->property("source").toUrl(), QUrl::fromLocalFile(path)); + QCOMPARE(view->property("natural").toSize(), picture.size()); + window->requestActivate(); + QVERIFY(QTest::qWaitForWindowActive(window)); + const double fit = view->property("zoom").toDouble(); + QTest::keyClick(window, Qt::Key_Equal, Qt::ControlModifier); + QVERIFY(view->property("zoom").toDouble() > fit); + QTest::keyClick(window, Qt::Key_0, Qt::ControlModifier); + QCOMPARE(view->property("zoom").toDouble(), fit); + // The same component must still follow another selection. + const QString next = home.filePath("next.png"); + QImage smaller(300, 200, QImage::Format_RGB32); + smaller.fill(Qt::red); + QVERIFY(smaller.save(next)); + preview.show(next); + QTRY_COMPARE(image->property("status").toInt(), 1); + QTRY_COMPARE(view->property("natural").toSize(), smaller.size()); + QCOMPARE(image->property("source").toUrl(), QUrl::fromLocalFile(next)); + // Destruction before the deferred callback must also be safe. + std::unique_ptr discarded(component.create()); + QVERIFY(discarded); + discarded.reset(); + QCoreApplication::processEvents(); + QVERIFY2(warnings.isEmpty(), qPrintable(warnings.join('\n'))); + } + void previewerStepsThroughTheFolder() { QTemporaryDir dir; From a91cbb40c1014e0f88c897c6aaad9ae4b4bd95f5 Mon Sep 17 00:00:00 2001 From: Clayton Faria Date: Mon, 16 Nov 2026 19:35:41 +0800 Subject: [PATCH 2/3] Keep in-flight preview decoding shared across scale reloads --- src/PreviewWindow.qml | 16 +++++---- tests/folio_tests.cpp | 75 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/src/PreviewWindow.qml b/src/PreviewWindow.qml index 3f1068c5..b96e702f 100644 --- a/src/PreviewWindow.qml +++ b/src/PreviewWindow.qml @@ -228,11 +228,15 @@ ApplicationWindow { Image { id: still - // Wait until the image belongs to its window before Qt picks - // the loading DPR, avoiding a second decode on attachment. - property bool readyToLoad: false - function startLoading() { readyToLoad = true } - Component.onCompleted: Qt.callLater(startLoading) + // Share the in-flight decode across scale reloads. The hidden + // child uses the same window/DPR and releases its pixels at Ready. + property Image keeper: Image { + parent: still + visible: false + asynchronous: true + autoTransform: true + source: still.status === Image.Loading ? still.source : "" + } visible: !flick.animated width: flick.natural.width * flick.zoom height: flick.natural.height * flick.zoom @@ -242,7 +246,7 @@ ApplicationWindow { autoTransform: true smooth: true mipmap: true - source: flick.animated || !readyToLoad ? "" : previewer.url + source: flick.animated ? "" : previewer.url } AnimatedImage { id: moving diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 2c8dfcf2..5e13842f 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,31 @@ #include "translator.h" #include "windows.h" +// Lets the real preview use a counted, gated decoder in cache-lifetime tests. +class PreviewProperties : public QQmlPropertyMap { + Q_OBJECT +public: + PreviewProperties() : QQmlPropertyMap(this, nullptr) {} +signals: + void changed(); +}; + +class GatedPreviewImages : public QQuickImageProvider { +public: + GatedPreviewImages() : QQuickImageProvider(QQuickImageProvider::Image) {} + std::atomic reads{0}; + QSemaphore release; + QImage requestImage(const QString &, QSize *size, const QSize &) override { + if (reads.fetch_add(1) == 0) + release.acquire(); + QImage image(1600, 900, QImage::Format_RGB32); + image.fill(Qt::darkCyan); + if (size) + *size = image.size(); + return image; + } +}; + // Puts XDG_DATA_HOME, where the trash and recent files live, in `path` for // as long as the returned guard does. [[nodiscard]] static auto dataHomeAt(const QString &path) @@ -6649,7 +6675,49 @@ private slots: QVERIFY(Previewer::markdownOpening(longFence, 600).count('|') <= 600 + 4); } - void previewImageWaitsForItsWindow() + void previewSharesAnInFlightDecodeAcrossReloads() + { + QTemporaryDir home; + Theme theme(home.filePath("theme")); + PreviewProperties preview; + preview.insert("item", QVariantMap{{"name", "Picture"}, {"path", "/picture.png"}, + {"mime", "image/png"}, {"size", 1}}); + preview.insert("viewer", "image"); + preview.insert("url", "image://gated/picture"); + preview.insert("count", 1); + preview.insert("index", 0); + preview.insert("type", "PNG"); + QQmlEngine engine; + auto *images = new GatedPreviewImages; + engine.addImageProvider("gated", images); + const auto unblock = qScopeGuard([&] { images->release.release(); }); + engine.rootContext()->setContextProperty("theme", &theme); + engine.rootContext()->setContextProperty("previewer", &preview); + QQmlComponent component(&engine, QUrl("qrc:/PreviewWindow.qml")); + std::unique_ptr root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + auto *view = root->property("view").value(); + QVERIFY(view); + auto *image = view->property("source").value(); + QVERIFY(image); + QTRY_COMPARE(images->reads.load(), 1); + // Force Qt to drop and re-request the visible image while its first + // decode is blocked. Unlike changing source, this leaves keeper's URL + // alone, just as a window-DPR reload does. The uncached request is + // cancelled before the reader thread can start it. + QVERIFY(image->setProperty("cache", false)); + QVERIFY(image->setProperty("cache", true)); + images->release.release(); + QTRY_COMPARE(image->property("status").toInt(), 1); + QCOMPARE(images->reads.load(), 1); + auto *keeper = image->property("keeper").value(); + QVERIFY(keeper); + QCOMPARE(keeper->window(), qobject_cast(image)->window()); + QVERIFY(!keeper->isVisible()); + QVERIFY(keeper->property("source").toUrl().isEmpty()); + } + + void previewImageKeepsItsGeometryAndNavigation() { QTemporaryDir home; const QString path = home.filePath("large.png"); @@ -6676,9 +6744,6 @@ private slots: QVERIFY(view); auto *image = view->property("source").value(); QVERIFY(image); - // A local file must not start decoding during component construction, - // before the completed tree has attached it to the window's screen. - QVERIFY(image->property("source").toUrl().isEmpty()); QVERIFY(QTest::qWaitForWindowExposed(window)); QTRY_COMPARE(image->property("status").toInt(), 1); QCOMPARE(image->property("source").toUrl(), QUrl::fromLocalFile(path)); @@ -6699,7 +6764,7 @@ private slots: QTRY_COMPARE(image->property("status").toInt(), 1); QTRY_COMPARE(view->property("natural").toSize(), smaller.size()); QCOMPARE(image->property("source").toUrl(), QUrl::fromLocalFile(next)); - // Destruction before the deferred callback must also be safe. + // Destroying a new view also releases its pending image references. std::unique_ptr discarded(component.create()); QVERIFY(discarded); discarded.reset(); From b72e54ce30610eb3ff03abf4917f6fc1840c9a2c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 18 Nov 2026 15:31:55 +0100 Subject: [PATCH 3/3] Keep only the test that proves a reload shares the decode The fix is nine lines in the preview window; the tests around it had grown to two hundred, with a stand-in previewer class and provider at file scope and a second test of the preview's geometry, zoom and navigation that doesn't touch the fix. Keep the one test that counts the provider's reads across a reload while the first decode is held: two without the keeper, one with it. Its provider and stand-in previewer now live inside it. Co-Authored-By: Claude Opus 5.5 --- tests/folio_tests.cpp | 139 +++++++++++------------------------------- 1 file changed, 37 insertions(+), 102 deletions(-) diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 5e13842f..17209b88 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -60,31 +60,6 @@ #include "translator.h" #include "windows.h" -// Lets the real preview use a counted, gated decoder in cache-lifetime tests. -class PreviewProperties : public QQmlPropertyMap { - Q_OBJECT -public: - PreviewProperties() : QQmlPropertyMap(this, nullptr) {} -signals: - void changed(); -}; - -class GatedPreviewImages : public QQuickImageProvider { -public: - GatedPreviewImages() : QQuickImageProvider(QQuickImageProvider::Image) {} - std::atomic reads{0}; - QSemaphore release; - QImage requestImage(const QString &, QSize *size, const QSize &) override { - if (reads.fetch_add(1) == 0) - release.acquire(); - QImage image(1600, 900, QImage::Format_RGB32); - image.fill(Qt::darkCyan); - if (size) - *size = image.size(); - return image; - } -}; - // Puts XDG_DATA_HOME, where the trash and recent files live, in `path` for // as long as the returned guard does. [[nodiscard]] static auto dataHomeAt(const QString &path) @@ -6677,99 +6652,59 @@ private slots: void previewSharesAnInFlightDecodeAcrossReloads() { + // A window learning its final display scale makes the picture ask + // for its pixels again while the first decode is still running. + // That reload must join the decode in flight, not start a second. + struct GatedImages : QQuickImageProvider { + GatedImages() : QQuickImageProvider(QQuickImageProvider::Image) {} + std::atomic reads{0}; + QSemaphore release; + QImage requestImage(const QString &, QSize *size, const QSize &) override + { + if (reads.fetch_add(1) == 0) + release.acquire(); + QImage image(1600, 900, QImage::Format_RGB32); + image.fill(Qt::darkCyan); + *size = image.size(); + return image; + } + }; QTemporaryDir home; Theme theme(home.filePath("theme")); - PreviewProperties preview; - preview.insert("item", QVariantMap{{"name", "Picture"}, {"path", "/picture.png"}, - {"mime", "image/png"}, {"size", 1}}); - preview.insert("viewer", "image"); - preview.insert("url", "image://gated/picture"); - preview.insert("count", 1); - preview.insert("index", 0); - preview.insert("type", "PNG"); QQmlEngine engine; - auto *images = new GatedPreviewImages; + auto *images = new GatedImages; engine.addImageProvider("gated", images); const auto unblock = qScopeGuard([&] { images->release.release(); }); + QQmlComponent stand(&engine); + stand.setData("import QtQml\n" + "QtObject {\n" + " signal changed\n" + " property var item: ({ name: 'Picture', path: '/picture.png', mime: 'image/png', size: 1 })\n" + " property string viewer: 'image'\n" + " property string url: 'image://gated/picture'\n" + " property int count: 1\n" + " property int index: 0\n" + " property string type: 'PNG'\n" + " property string text\n" + "}\n", + QUrl()); + std::unique_ptr preview(stand.create()); + QVERIFY2(preview, qPrintable(stand.errorString())); engine.rootContext()->setContextProperty("theme", &theme); - engine.rootContext()->setContextProperty("previewer", &preview); + engine.rootContext()->setContextProperty("previewer", preview.get()); QQmlComponent component(&engine, QUrl("qrc:/PreviewWindow.qml")); std::unique_ptr root(component.create()); QVERIFY2(root, qPrintable(component.errorString())); - auto *view = root->property("view").value(); - QVERIFY(view); - auto *image = view->property("source").value(); + auto *image = root->property("view").value()->property("source").value(); QVERIFY(image); QTRY_COMPARE(images->reads.load(), 1); - // Force Qt to drop and re-request the visible image while its first - // decode is blocked. Unlike changing source, this leaves keeper's URL - // alone, just as a window-DPR reload does. The uncached request is - // cancelled before the reader thread can start it. + // Turning the cache off and on drops and re-requests the image as a + // scale change does, while leaving its source alone. QVERIFY(image->setProperty("cache", false)); QVERIFY(image->setProperty("cache", true)); images->release.release(); QTRY_COMPARE(image->property("status").toInt(), 1); QCOMPARE(images->reads.load(), 1); - auto *keeper = image->property("keeper").value(); - QVERIFY(keeper); - QCOMPARE(keeper->window(), qobject_cast(image)->window()); - QVERIFY(!keeper->isVisible()); - QVERIFY(keeper->property("source").toUrl().isEmpty()); - } - - void previewImageKeepsItsGeometryAndNavigation() - { - QTemporaryDir home; - const QString path = home.filePath("large.png"); - QImage picture(3200, 2400, QImage::Format_RGB32); - picture.fill(Qt::darkCyan); - QVERIFY(picture.save(path)); - Theme theme(home.filePath("theme")); - Previewer preview; - preview.start(path, false, "name"); - QQmlEngine engine; - QStringList warnings; - connect(&engine, &QQmlEngine::warnings, &engine, [&](const QList &errors) { - for (const auto &error : errors) - warnings << error.toString(); - }); - engine.rootContext()->setContextProperty("theme", &theme); - engine.rootContext()->setContextProperty("previewer", &preview); - QQmlComponent component(&engine, QUrl("qrc:/PreviewWindow.qml")); - std::unique_ptr root(component.create()); - QVERIFY2(root, qPrintable(component.errorString())); - auto *window = qobject_cast(root.get()); - QVERIFY(window); - auto *view = root->property("view").value(); - QVERIFY(view); - auto *image = view->property("source").value(); - QVERIFY(image); - QVERIFY(QTest::qWaitForWindowExposed(window)); - QTRY_COMPARE(image->property("status").toInt(), 1); - QCOMPARE(image->property("source").toUrl(), QUrl::fromLocalFile(path)); - QCOMPARE(view->property("natural").toSize(), picture.size()); - window->requestActivate(); - QVERIFY(QTest::qWaitForWindowActive(window)); - const double fit = view->property("zoom").toDouble(); - QTest::keyClick(window, Qt::Key_Equal, Qt::ControlModifier); - QVERIFY(view->property("zoom").toDouble() > fit); - QTest::keyClick(window, Qt::Key_0, Qt::ControlModifier); - QCOMPARE(view->property("zoom").toDouble(), fit); - // The same component must still follow another selection. - const QString next = home.filePath("next.png"); - QImage smaller(300, 200, QImage::Format_RGB32); - smaller.fill(Qt::red); - QVERIFY(smaller.save(next)); - preview.show(next); - QTRY_COMPARE(image->property("status").toInt(), 1); - QTRY_COMPARE(view->property("natural").toSize(), smaller.size()); - QCOMPARE(image->property("source").toUrl(), QUrl::fromLocalFile(next)); - // Destroying a new view also releases its pending image references. - std::unique_ptr discarded(component.create()); - QVERIFY(discarded); - discarded.reset(); - QCoreApplication::processEvents(); - QVERIFY2(warnings.isEmpty(), qPrintable(warnings.join('\n'))); } void previewerStepsThroughTheFolder()