From 9f38908bdf658c39efa239b5d340f973999a8738 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Tue, 19 Mar 2024 21:30:46 -0700 Subject: [PATCH] core/intercept: do not intercept non qml files Avoids forcing Images to lazy load which causes unexpected flashes. --- src/core/generation.cpp | 1 + src/core/generation.hpp | 1 + src/core/qsintercept.cpp | 15 +++++++++++++++ src/core/qsintercept.hpp | 6 ++++++ 4 files changed, 23 insertions(+) diff --git a/src/core/generation.cpp b/src/core/generation.cpp index 5d24c5a..de5db17 100644 --- a/src/core/generation.cpp +++ b/src/core/generation.cpp @@ -27,6 +27,7 @@ EngineGeneration::EngineGeneration(QmlScanner scanner) , interceptNetFactory(this->scanner.qmldirIntercepts) { g_generations.insert(&this->engine, this); + this->engine.addUrlInterceptor(&this->urlInterceptor); this->engine.setNetworkAccessManagerFactory(&this->interceptNetFactory); this->engine.setIncubationController(&this->delayedIncubationController); } diff --git a/src/core/generation.hpp b/src/core/generation.hpp index 5c21a95..813190d 100644 --- a/src/core/generation.hpp +++ b/src/core/generation.hpp @@ -29,6 +29,7 @@ public: static EngineGeneration* findObjectGeneration(QObject* object); QmlScanner scanner; + QsUrlInterceptor urlInterceptor; QsInterceptNetworkAccessManagerFactory interceptNetFactory; QQmlEngine engine; ShellRoot* root = nullptr; diff --git a/src/core/qsintercept.cpp b/src/core/qsintercept.cpp index 35b9234..21b6abf 100644 --- a/src/core/qsintercept.cpp +++ b/src/core/qsintercept.cpp @@ -9,11 +9,26 @@ #include #include #include +#include #include #include +#include Q_LOGGING_CATEGORY(logQsIntercept, "quickshell.interceptor", QtWarningMsg); +QUrl QsUrlInterceptor::intercept(const QUrl& url, QQmlAbstractUrlInterceptor::DataType type) { + // Some types such as Image take into account where they are loading from, and force + // asynchronous loading over a network. qsintercept is considered to be over a network. + if (type == QQmlAbstractUrlInterceptor::DataType::UrlString && url.scheme() == "qsintercept") { + auto newUrl = url; + newUrl.setScheme("file"); + qCDebug(logQsIntercept) << "Rewrote intercept" << url << "to" << newUrl; + return newUrl; + } + + return url; +} + QsInterceptDataReply::QsInterceptDataReply(const QString& qmldir, QObject* parent) : QNetworkReply(parent) , content(qmldir.toUtf8()) { diff --git a/src/core/qsintercept.hpp b/src/core/qsintercept.hpp index 7e84da9..d51b78e 100644 --- a/src/core/qsintercept.hpp +++ b/src/core/qsintercept.hpp @@ -5,11 +5,17 @@ #include #include #include +#include #include #include Q_DECLARE_LOGGING_CATEGORY(logQsIntercept); +class QsUrlInterceptor: public QQmlAbstractUrlInterceptor { +public: + QUrl intercept(const QUrl& url, QQmlAbstractUrlInterceptor::DataType type) override; +}; + class QsInterceptDataReply: public QNetworkReply { Q_OBJECT;