From a1b0568630e060fa963e7bb8965fda5a8a4ab71e Mon Sep 17 00:00:00 2001 From: Yuhang Zhao Date: Mon, 13 Nov 2023 11:17:45 +0800 Subject: [PATCH] qmake: remove moc limitations for non apple platforms --- cmake | 2 +- .../Core/private/framelessmanager_p.h | 15 ++ .../Core/private/micamaterial_p.h | 35 +++ src/core/framelessmanager.cpp | 17 -- src/core/micamaterial.cpp | 232 ++++++++---------- src/core/utils_mac.mm | 2 - 6 files changed, 158 insertions(+), 145 deletions(-) diff --git a/cmake b/cmake index 6939b41e..9c52a5c0 160000 --- a/cmake +++ b/cmake @@ -1 +1 @@ -Subproject commit 6939b41eb68450fa06d8d701454031b2328e14c5 +Subproject commit 9c52a5c00401925975019cbefb2805d68b59807c diff --git a/include/FramelessHelper/Core/private/framelessmanager_p.h b/include/FramelessHelper/Core/private/framelessmanager_p.h index 81ba9859..9a9e4ca2 100644 --- a/include/FramelessHelper/Core/private/framelessmanager_p.h +++ b/include/FramelessHelper/Core/private/framelessmanager_p.h @@ -73,4 +73,19 @@ class FRAMELESSHELPER_CORE_API FramelessManagerPrivate : public QObject QTimer wallpaperTimer{}; }; +class InternalEventFilter : public QObject +{ + FRAMELESSHELPER_QT_CLASS(InternalEventFilter) + +public: + explicit InternalEventFilter(const QObject *window, QObject *parent = nullptr); + ~InternalEventFilter() override; + +protected: + Q_NODISCARD bool eventFilter(QObject *object, QEvent *event) override; + +private: + const QObject *m_window = nullptr; +}; + FRAMELESSHELPER_END_NAMESPACE diff --git a/include/FramelessHelper/Core/private/micamaterial_p.h b/include/FramelessHelper/Core/private/micamaterial_p.h index 21592d43..0f5228e0 100644 --- a/include/FramelessHelper/Core/private/micamaterial_p.h +++ b/include/FramelessHelper/Core/private/micamaterial_p.h @@ -26,11 +26,26 @@ #include #include +#ifdef FRAMELESSHELPER_HAS_THREAD +# undef FRAMELESSHELPER_HAS_THREAD +#endif +#if QT_CONFIG(thread) +# define FRAMELESSHELPER_HAS_THREAD 1 +# include +#else // !QT_CONFIG(thread) +# define FRAMELESSHELPER_HAS_THREAD 0 +#endif // QT_CONFIG(thread) #if FRAMELESSHELPER_CONFIG(mica_material) FRAMELESSHELPER_BEGIN_NAMESPACE +#if FRAMELESSHELPER_HAS_THREAD +using FramelessHelperThreadClass = QThread; +#else +using FramelessHelperThreadClass = QObject; +#endif + class MicaMaterial; class FRAMELESSHELPER_CORE_API MicaMaterialPrivate : public QObject { @@ -63,6 +78,26 @@ class FRAMELESSHELPER_CORE_API MicaMaterialPrivate : public QObject QSize wallpaperSize = {}; }; +class WallpaperThread : public FramelessHelperThreadClass +{ + FRAMELESSHELPER_QT_CLASS(WallpaperThread) + +public: + explicit WallpaperThread(QObject *parent = nullptr); + ~WallpaperThread() override; + +Q_SIGNALS: + void imageUpdated(); + +#if FRAMELESSHELPER_HAS_THREAD +protected: + void run() override; +#else +public: + void start(); +#endif +}; + FRAMELESSHELPER_END_NAMESPACE #endif diff --git a/src/core/framelessmanager.cpp b/src/core/framelessmanager.cpp index a871426c..7f8681ae 100644 --- a/src/core/framelessmanager.cpp +++ b/src/core/framelessmanager.cpp @@ -105,21 +105,6 @@ Q_GLOBAL_STATIC(InternalData, g_internalData) } #endif -class InternalEventFilter : public QObject -{ - FRAMELESSHELPER_QT_CLASS(InternalEventFilter) - -public: - explicit InternalEventFilter(const QObject *window, QObject *parent = nullptr); - ~InternalEventFilter() override; - -protected: - Q_NODISCARD bool eventFilter(QObject *object, QEvent *event) override; - -private: - const QObject *m_window = nullptr; -}; - InternalEventFilter::InternalEventFilter(const QObject *window, QObject *parent) : QObject(parent), m_window(window) { Q_ASSERT(m_window); @@ -547,5 +532,3 @@ bool FramelessManager::removeWindow(const QObject *window) } FRAMELESSHELPER_END_NAMESPACE - -#include "framelessmanager.moc" diff --git a/src/core/micamaterial.cpp b/src/core/micamaterial.cpp index f5fadcd8..eafe5abc 100644 --- a/src/core/micamaterial.cpp +++ b/src/core/micamaterial.cpp @@ -27,13 +27,6 @@ #if FRAMELESSHELPER_CONFIG(mica_material) -#if QT_CONFIG(thread) -# define FRAMELESSHELPER_HAS_THREAD - using FramelessHelperThreadClass = QThread; -#else - using FramelessHelperThreadClass = QObject; -#endif - #include "framelessmanager.h" #include "utils.h" #include "framelessconfig_p.h" @@ -42,9 +35,8 @@ #include #include #include -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD # include -# include #endif #include #include @@ -89,7 +81,7 @@ struct ImageData { QPixmap blurredWallpaper = {}; bool graphicsResourcesReady = false; -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD QMutex mutex{}; #endif }; @@ -490,126 +482,118 @@ static inline void expblur(QImage &img, qreal radius, const bool improvedQuality return {x, y, w, h}; } -class WallpaperThread : public FramelessHelperThreadClass +WallpaperThread::WallpaperThread(QObject *parent) : FramelessHelperThreadClass(parent) { - FRAMELESSHELPER_QT_CLASS(WallpaperThread) - -public: - explicit WallpaperThread(QObject *parent = nullptr) : FramelessHelperThreadClass(parent) {} - ~WallpaperThread() override = default; +} -Q_SIGNALS: - void imageUpdated(); +WallpaperThread::~WallpaperThread() = default; -#ifdef FRAMELESSHELPER_HAS_THREAD -protected: - void run() override +#if FRAMELESSHELPER_HAS_THREAD +void WallpaperThread::run() #else -public: - void start() +void WallpaperThread::start() #endif - { - const QString wallpaperFilePath = Utils::getWallpaperFilePath(); - if (wallpaperFilePath.isEmpty()) { - WARNING << "Failed to retrieve the wallpaper file path."; - return; - } - // QImageReader allows us read the image size before we actually loading it, this behavior - // can help us avoid consume too much memory if the image resolution is very large, eg, 4K. - QImageReader reader(wallpaperFilePath); - if (!reader.canRead()) { - WARNING << "Qt can't read the wallpaper file:" << reader.errorString(); - return; - } - const QSize actualSize = reader.size(); - if (actualSize.isEmpty()) { - WARNING << "The wallpaper picture size is invalid."; - return; - } - const QSize correctedSize = (actualSize > kMaximumPictureSize ? kMaximumPictureSize : actualSize); - if (correctedSize != actualSize) { - DEBUG << "The wallpaper picture size is greater than 1920x1080, it will be shrinked to reduce memory consumption."; - reader.setScaledSize(correctedSize); - } - QImage image(correctedSize, kDefaultImageFormat); - if (!reader.read(&image)) { - WARNING << "Failed to read the wallpaper image:" << reader.errorString(); - return; - } - if (image.isNull()) { - WARNING << "The obtained image data is null."; - return; - } - WallpaperAspectStyle aspectStyle = Utils::getWallpaperAspectStyle(); - const QSize wallpaperSize = QGuiApplication::primaryScreen()->size(); - QImage buffer(wallpaperSize, kDefaultImageFormat); +{ + const QString wallpaperFilePath = Utils::getWallpaperFilePath(); + if (wallpaperFilePath.isEmpty()) { + WARNING << "Failed to retrieve the wallpaper file path."; + return; + } + // QImageReader allows us read the image size before we actually loading it, this behavior + // can help us avoid consume too much memory if the image resolution is very large, eg, 4K. + QImageReader reader(wallpaperFilePath); + if (!reader.canRead()) { + WARNING << "Qt can't read the wallpaper file:" << reader.errorString(); + return; + } + const QSize actualSize = reader.size(); + if (actualSize.isEmpty()) { + WARNING << "The wallpaper picture size is invalid."; + return; + } + const QSize correctedSize = (actualSize > kMaximumPictureSize ? kMaximumPictureSize : actualSize); + if (correctedSize != actualSize) { + DEBUG << "The wallpaper picture size is greater than 1920x1080, it will be shrinked to reduce memory consumption."; + reader.setScaledSize(correctedSize); + } + QImage image(correctedSize, kDefaultImageFormat); + if (!reader.read(&image)) { + WARNING << "Failed to read the wallpaper image:" << reader.errorString(); + return; + } + if (image.isNull()) { + WARNING << "The obtained image data is null."; + return; + } + WallpaperAspectStyle aspectStyle = Utils::getWallpaperAspectStyle(); + const QSize wallpaperSize = QGuiApplication::primaryScreen()->size(); + QImage buffer(wallpaperSize, kDefaultImageFormat); #ifdef Q_OS_WINDOWS - if (aspectStyle == WallpaperAspectStyle::Center) { - buffer.fill(kDefaultBlackColor); - } + if (aspectStyle == WallpaperAspectStyle::Center) { + buffer.fill(kDefaultBlackColor); + } #endif - if ((aspectStyle == WallpaperAspectStyle::Stretch) - || (aspectStyle == WallpaperAspectStyle::Fit) - || (aspectStyle == WallpaperAspectStyle::Fill)) { - Qt::AspectRatioMode mode = Qt::KeepAspectRatioByExpanding; - if (aspectStyle == WallpaperAspectStyle::Stretch) { - mode = Qt::IgnoreAspectRatio; - } else if (aspectStyle == WallpaperAspectStyle::Fit) { - mode = Qt::KeepAspectRatio; - } - QSize newSize = image.size(); - newSize.scale(wallpaperSize, mode); - image = image.scaled(newSize); + if ((aspectStyle == WallpaperAspectStyle::Stretch) + || (aspectStyle == WallpaperAspectStyle::Fit) + || (aspectStyle == WallpaperAspectStyle::Fill)) { + Qt::AspectRatioMode mode = Qt::KeepAspectRatioByExpanding; + if (aspectStyle == WallpaperAspectStyle::Stretch) { + mode = Qt::IgnoreAspectRatio; + } else if (aspectStyle == WallpaperAspectStyle::Fit) { + mode = Qt::KeepAspectRatio; } - static constexpr const QPoint desktopOriginPoint = {0, 0}; - const QRect desktopRect = {desktopOriginPoint, wallpaperSize}; - if (aspectStyle == WallpaperAspectStyle::Tile) { - QPainter bufferPainter(&buffer); - // Same as above, we prefer speed than quality here. - bufferPainter.setRenderHint(QPainter::Antialiasing, false); - bufferPainter.setRenderHint(QPainter::TextAntialiasing, false); - bufferPainter.setRenderHint(QPainter::SmoothPixmapTransform, false); - bufferPainter.fillRect(desktopRect, QBrush(image)); - } else { - QPainter bufferPainter(&buffer); - // Same here. - bufferPainter.setRenderHint(QPainter::Antialiasing, false); - bufferPainter.setRenderHint(QPainter::TextAntialiasing, false); - bufferPainter.setRenderHint(QPainter::SmoothPixmapTransform, false); - const QRect rect = alignedRect(Qt::LeftToRight, Qt::AlignCenter, image.size(), desktopRect); - bufferPainter.drawImage(rect.topLeft(), image); - } - { -#ifdef FRAMELESSHELPER_HAS_THREAD - const QMutexLocker locker(&g_imageData()->mutex); + QSize newSize = image.size(); + newSize.scale(wallpaperSize, mode); + image = image.scaled(newSize); + } + static constexpr const QPoint desktopOriginPoint = {0, 0}; + const QRect desktopRect = {desktopOriginPoint, wallpaperSize}; + if (aspectStyle == WallpaperAspectStyle::Tile) { + QPainter bufferPainter(&buffer); + // Same as above, we prefer speed than quality here. + bufferPainter.setRenderHint(QPainter::Antialiasing, false); + bufferPainter.setRenderHint(QPainter::TextAntialiasing, false); + bufferPainter.setRenderHint(QPainter::SmoothPixmapTransform, false); + bufferPainter.fillRect(desktopRect, QBrush(image)); + } else { + QPainter bufferPainter(&buffer); + // Same here. + bufferPainter.setRenderHint(QPainter::Antialiasing, false); + bufferPainter.setRenderHint(QPainter::TextAntialiasing, false); + bufferPainter.setRenderHint(QPainter::SmoothPixmapTransform, false); + const QRect rect = alignedRect(Qt::LeftToRight, Qt::AlignCenter, image.size(), desktopRect); + bufferPainter.drawImage(rect.topLeft(), image); + } + { +#if FRAMELESSHELPER_HAS_THREAD + const QMutexLocker locker(&g_imageData()->mutex); #endif - g_imageData()->blurredWallpaper = QPixmap(wallpaperSize); - g_imageData()->blurredWallpaper.fill(kDefaultTransparentColor); - QPainter painter(&g_imageData()->blurredWallpaper); - // Same here. - painter.setRenderHint(QPainter::Antialiasing, false); - painter.setRenderHint(QPainter::TextAntialiasing, false); - painter.setRenderHint(QPainter::SmoothPixmapTransform, false); + g_imageData()->blurredWallpaper = QPixmap(wallpaperSize); + g_imageData()->blurredWallpaper.fill(kDefaultTransparentColor); + QPainter painter(&g_imageData()->blurredWallpaper); + // Same here. + painter.setRenderHint(QPainter::Antialiasing, false); + painter.setRenderHint(QPainter::TextAntialiasing, false); + painter.setRenderHint(QPainter::SmoothPixmapTransform, false); #if FRAMELESSHELPER_CONFIG(private_qt) - qt_blurImage(&painter, buffer, kDefaultBlurRadius, false, false); + qt_blurImage(&painter, buffer, kDefaultBlurRadius, false, false); #else // !FRAMELESSHELPER_CONFIG(private_qt) - painter.drawImage(desktopOriginPoint, buffer); + painter.drawImage(desktopOriginPoint, buffer); #endif // FRAMELESSHELPER_CONFIG(private_qt) - } - Q_EMIT imageUpdated(); } -}; + Q_EMIT imageUpdated(); +} struct ThreadData { std::unique_ptr thread = nullptr; -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD QMutex mutex{}; #endif }; Q_GLOBAL_STATIC(ThreadData, g_threadData) -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD static inline void threadCleaner() { const QMutexLocker locker(&g_threadData()->mutex); @@ -653,16 +637,16 @@ const MicaMaterialPrivate *MicaMaterialPrivate::get(const MicaMaterial *q) void MicaMaterialPrivate::maybeGenerateBlurredWallpaper(const bool force) { -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.lock(); #endif if (!g_imageData()->blurredWallpaper.isNull() && !force) { -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.unlock(); #endif return; } -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.unlock(); const QMutexLocker locker(&g_threadData()->mutex); if (g_threadData()->thread->isRunning()) { @@ -713,12 +697,12 @@ void MicaMaterialPrivate::forceRebuildWallpaper() void MicaMaterialPrivate::initialize() { -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_threadData()->mutex.lock(); #endif if (!g_threadData()->thread) { g_threadData()->thread = std::make_unique(); -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD qAddPostRoutine(threadCleaner); #endif } @@ -728,7 +712,7 @@ void MicaMaterialPrivate::initialize() Q_EMIT q->shouldRedraw(); } }); -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_threadData()->mutex.unlock(); #endif @@ -758,17 +742,17 @@ void MicaMaterialPrivate::initialize() void MicaMaterialPrivate::prepareGraphicsResources() { -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.lock(); #endif if (g_imageData()->graphicsResourcesReady) { -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.unlock(); #endif return; } g_imageData()->graphicsResourcesReady = true; -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.unlock(); #endif maybeGenerateBlurredWallpaper(); @@ -951,11 +935,11 @@ void MicaMaterial::paint(QPainter *painter, const QRect &rect, const bool active painter->setRenderHint(QPainter::SmoothPixmapTransform, false); if (active) { const QRect intersectedRect = wallpaperRect.intersected(mappedRect); -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.lock(); #endif painter->drawPixmap(originPoint, g_imageData()->blurredWallpaper, intersectedRect); -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.unlock(); #endif if (intersectedRect != mappedRect) { @@ -964,7 +948,7 @@ void MicaMaterial::paint(QPainter *painter, const QRect &rect, const bool active const QRect outerRect = { intersectedRect.topRight() + xOffset, QSize{ mappedRect.width() - intersectedRect.width(), intersectedRect.height() } }; const QPoint outerRectOriginPoint = originPoint + QPoint{ intersectedRect.width(), 0 } + xOffset; const QRect mappedOuterRect = d->mapToWallpaper(outerRect); -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD const QMutexLocker locker(&g_imageData()->mutex); #endif painter->drawPixmap(outerRectOriginPoint, g_imageData()->blurredWallpaper, mappedOuterRect); @@ -973,11 +957,11 @@ void MicaMaterial::paint(QPainter *painter, const QRect &rect, const bool active const QRect outerRectBottom = { intersectedRect.bottomLeft() + yOffset, QSize{ intersectedRect.width(), mappedRect.height() - intersectedRect.height() } }; const QPoint outerRectBottomOriginPoint = originPoint + QPoint{ 0, intersectedRect.height() } + yOffset; const QRect mappedOuterRectBottom = d->mapToWallpaper(outerRectBottom); -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.lock(); #endif painter->drawPixmap(outerRectBottomOriginPoint, g_imageData()->blurredWallpaper, mappedOuterRectBottom); -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD g_imageData()->mutex.unlock(); #endif if (mappedRect.x() + mappedRect.width() > wallpaperRect.width()) { @@ -987,7 +971,7 @@ void MicaMaterial::paint(QPainter *painter, const QRect &rect, const bool active const QRect outerRectCorner = { intersectedRect.bottomRight() + xOffset + yOffset, QSize{ outerRectRight.width(), outerRectBottom.height() } }; const QPoint outerRectCornerOriginPoint = originPoint + QPoint{ intersectedRect.width(), intersectedRect.height() } + xOffset + yOffset; const QRect mappedOuterRectCorner = d->mapToWallpaper(outerRectCorner); -#ifdef FRAMELESSHELPER_HAS_THREAD +#if FRAMELESSHELPER_HAS_THREAD const QMutexLocker locker(&g_imageData()->mutex); #endif painter->drawPixmap(outerRectRightOriginPoint, g_imageData()->blurredWallpaper, mappedOuterRectRight); @@ -1012,6 +996,4 @@ void MicaMaterial::paint(QPainter *painter, const QRect &rect, const bool active FRAMELESSHELPER_END_NAMESPACE -#include "micamaterial.moc" - #endif diff --git a/src/core/utils_mac.mm b/src/core/utils_mac.mm index 379f8d03..a17c5b89 100644 --- a/src/core/utils_mac.mm +++ b/src/core/utils_mac.mm @@ -253,7 +253,6 @@ explicit NSWindowProxy(QWindow *qtWindow, NSWindow *macWindow, QObject *parent = } public Q_SLOTS: - void replaceImplementations() { Method method = class_getInstanceMethod(windowClass, @selector(setStyleMask:)); @@ -515,7 +514,6 @@ static void sendEvent(id obj, SEL sel, NSEvent *event) }; using MacUtilsData = QHash; - Q_GLOBAL_STATIC(MacUtilsData, g_macUtilsData); [[nodiscard]] static inline NSWindow *mac_getNSWindow(const WId windowId)