From b50a2dcd72d758184983d450c41a4ce98d3ffbdd Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Sun, 9 Oct 2022 10:08:49 +0800 Subject: [PATCH] fix Qt5 compilation error And change to lazy initialization for Mica Material. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- .../Core/framelesshelpercore_global.h | 3 ++- include/FramelessHelper/Core/micamaterial.h | 2 +- .../Core/private/micamaterial_p.h | 3 ++- .../Quick/framelesshelperquick_global.h | 2 +- src/core/framelessconfig.cpp | 4 ++- src/core/micamaterial.cpp | 26 ++++++++++++++++--- src/quick/framelesshelperquick_global.cpp | 3 +-- 7 files changed, 33 insertions(+), 10 deletions(-) diff --git a/include/FramelessHelper/Core/framelesshelpercore_global.h b/include/FramelessHelper/Core/framelesshelpercore_global.h index 6f00a72..c9157de 100644 --- a/include/FramelessHelper/Core/framelesshelpercore_global.h +++ b/include/FramelessHelper/Core/framelesshelpercore_global.h @@ -240,7 +240,8 @@ enum class Option WindowUseRoundCorners = 4, CenterWindowBeforeShow = 5, EnableBlurBehindWindow = 6, - ForceNonNativeBackgroundBlur = 7 + ForceNonNativeBackgroundBlur = 7, + DisableLazyInitializationForMicaMaterial = 8 }; Q_ENUM_NS(Option) diff --git a/include/FramelessHelper/Core/micamaterial.h b/include/FramelessHelper/Core/micamaterial.h index fee757c..c04dcea 100644 --- a/include/FramelessHelper/Core/micamaterial.h +++ b/include/FramelessHelper/Core/micamaterial.h @@ -58,7 +58,7 @@ public: void setNoiseOpacity(const qreal value); public Q_SLOTS: - void paint(QPainter *painter, const QSize &size, const QPoint &pos) const; + void paint(QPainter *painter, const QSize &size, const QPoint &pos); Q_NODISCARD static MicaMaterial *attach(QObject *target); Q_SIGNALS: diff --git a/include/FramelessHelper/Core/private/micamaterial_p.h b/include/FramelessHelper/Core/private/micamaterial_p.h index 076f683..49552d0 100644 --- a/include/FramelessHelper/Core/private/micamaterial_p.h +++ b/include/FramelessHelper/Core/private/micamaterial_p.h @@ -48,11 +48,12 @@ public: public Q_SLOTS: void maybeGenerateBlurredWallpaper(const bool force = false); void updateMaterialBrush(); - void paint(QPainter *painter, const QSize &size, const QPoint &pos) const; + void paint(QPainter *painter, const QSize &size, const QPoint &pos); Q_NODISCARD static MicaMaterial *attach(QObject *target); private: void initialize(); + void prepareGraphicsResources(); private: QPointer q_ptr = nullptr; diff --git a/include/FramelessHelper/Quick/framelesshelperquick_global.h b/include/FramelessHelper/Quick/framelesshelperquick_global.h index a8cc6bc..0c8edff 100644 --- a/include/FramelessHelper/Quick/framelesshelperquick_global.h +++ b/include/FramelessHelper/Quick/framelesshelperquick_global.h @@ -196,4 +196,4 @@ FRAMELESSHELPER_QUICK_API void uninitialize(); FRAMELESSHELPER_END_NAMESPACE -Q_DECLARE_METATYPE(FRAMELESSHELPER_PREPEND_NAMESPACE(QuickGlobal)) +Q_DECLARE_METATYPE2(FRAMELESSHELPER_PREPEND_NAMESPACE(QuickGlobal)) diff --git a/src/core/framelessconfig.cpp b/src/core/framelessconfig.cpp index 1b20e91..864e0b5 100644 --- a/src/core/framelessconfig.cpp +++ b/src/core/framelessconfig.cpp @@ -60,7 +60,9 @@ static const struct {FRAMELESSHELPER_BYTEARRAY_LITERAL("FRAMELESSHELPER_ENABLE_BLUR_BEHIND_WINDOW"), FRAMELESSHELPER_BYTEARRAY_LITERAL("Options/EnableBlurBehindWindow")}, {FRAMELESSHELPER_BYTEARRAY_LITERAL("FRAMELESSHELPER_FORCE_NON_NATIVE_BACKGROUND_BLUR"), - FRAMELESSHELPER_BYTEARRAY_LITERAL("Options/ForceNonNativeBackgroundBlur")} + FRAMELESSHELPER_BYTEARRAY_LITERAL("Options/ForceNonNativeBackgroundBlur")}, + {FRAMELESSHELPER_BYTEARRAY_LITERAL("FRAMELESSHELPER_DISABLE_LAZY_INITIALIZATION_FOR_MICA_MATERIAL"), + FRAMELESSHELPER_BYTEARRAY_LITERAL("Options/DisableLazyInitializationForMicaMaterial")} }; static constexpr const auto OptionCount = std::size(OptionsTable); diff --git a/src/core/micamaterial.cpp b/src/core/micamaterial.cpp index 4225ed5..4611e83 100644 --- a/src/core/micamaterial.cpp +++ b/src/core/micamaterial.cpp @@ -26,6 +26,7 @@ #include "micamaterial_p.h" #include "framelessmanager.h" #include "utils.h" +#include "framelessconfig_p.h" #include #include #include @@ -68,6 +69,7 @@ struct MicaMaterialData { QMutex mutex; QPixmap blurredWallpaper = {}; + bool graphicsResourcesReady = false; }; Q_GLOBAL_STATIC(MicaMaterialData, g_micaMaterialData) @@ -572,12 +574,13 @@ void MicaMaterialPrivate::updateMaterialBrush() Q_EMIT q->shouldRedraw(); } -void MicaMaterialPrivate::paint(QPainter *painter, const QSize &size, const QPoint &pos) const +void MicaMaterialPrivate::paint(QPainter *painter, const QSize &size, const QPoint &pos) { Q_ASSERT(painter); if (!painter) { return; } + prepareGraphicsResources(); static constexpr const QPoint originPoint = {0, 0}; painter->save(); painter->setRenderHints(QPainter::Antialiasing | @@ -617,6 +620,20 @@ void MicaMaterialPrivate::initialize() maybeGenerateBlurredWallpaper(true); }); + if (FramelessConfig::instance()->isSet(Option::DisableLazyInitializationForMicaMaterial)) { + prepareGraphicsResources(); + } +} + +void MicaMaterialPrivate::prepareGraphicsResources() +{ + g_micaMaterialData()->mutex.lock(); + if (g_micaMaterialData()->graphicsResourcesReady) { + g_micaMaterialData()->mutex.unlock(); + return; + } + g_micaMaterialData()->graphicsResourcesReady = true; + g_micaMaterialData()->mutex.unlock(); maybeGenerateBlurredWallpaper(); updateMaterialBrush(); } @@ -644,6 +661,7 @@ void MicaMaterial::setTintColor(const QColor &value) if (d->tintColor == value) { return; } + d->prepareGraphicsResources(); d->tintColor = value; d->updateMaterialBrush(); Q_EMIT tintColorChanged(); @@ -661,6 +679,7 @@ void MicaMaterial::setTintOpacity(const qreal value) if (qFuzzyCompare(d->tintOpacity, value)) { return; } + d->prepareGraphicsResources(); d->tintOpacity = value; d->updateMaterialBrush(); Q_EMIT tintOpacityChanged(); @@ -678,14 +697,15 @@ void MicaMaterial::setNoiseOpacity(const qreal value) if (qFuzzyCompare(d->noiseOpacity, value)) { return; } + d->prepareGraphicsResources(); d->noiseOpacity = value; d->updateMaterialBrush(); Q_EMIT noiseOpacityChanged(); } -void MicaMaterial::paint(QPainter *painter, const QSize &size, const QPoint &pos) const +void MicaMaterial::paint(QPainter *painter, const QSize &size, const QPoint &pos) { - Q_D(const MicaMaterial); + Q_D(MicaMaterial); d->paint(painter, size, pos); } diff --git a/src/quick/framelesshelperquick_global.cpp b/src/quick/framelesshelperquick_global.cpp index 6e9bbf4..d220c03 100644 --- a/src/quick/framelesshelperquick_global.cpp +++ b/src/quick/framelesshelperquick_global.cpp @@ -77,9 +77,8 @@ void initialize() qRegisterMetaType(); qRegisterMetaType(); - qRegisterMetaType(); - #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) + qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType();