From bdbfc96eb0e687a7640dc12ad6e64963b6ac17cc Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Sat, 17 Jul 2021 15:35:55 +0800 Subject: [PATCH] Minor tweaks Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- CMakeLists.txt | 1 + framelesshelper_win32.cpp | 2 +- framelessquickhelper.h | 12 ++++++------ framelesswindowsmanager.h | 10 +++++----- utilities.h | 39 +++++++++++++++++++-------------------- utilities_win32.cpp | 22 ++++++---------------- 6 files changed, 38 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b5a69b..14ff718 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,7 @@ if(WIN32) endif() target_link_libraries(${PROJECT_NAME} PRIVATE + Qt${QT_VERSION_MAJOR}::CorePrivate Qt${QT_VERSION_MAJOR}::GuiPrivate ) diff --git a/framelesshelper_win32.cpp b/framelesshelper_win32.cpp index 16bb5c1..3ea3f19 100644 --- a/framelesshelper_win32.cpp +++ b/framelesshelper_win32.cpp @@ -74,7 +74,7 @@ #define GET_Y_LPARAM(lp) ((int) (short) HIWORD(lp)) #endif -static inline bool shouldHaveWindowFrame() +[[nodiscard]] static inline bool shouldHaveWindowFrame() { if (Utilities::shouldUseNativeTitleBar()) { // We have to use the original window frame unconditionally if we diff --git a/framelessquickhelper.h b/framelessquickhelper.h index 6245fe7..80b9b47 100644 --- a/framelessquickhelper.h +++ b/framelessquickhelper.h @@ -44,24 +44,24 @@ public: explicit FramelessQuickHelper(QQuickItem *parent = nullptr); ~FramelessQuickHelper() override = default; - int borderWidth() const; + [[nodiscard]] int borderWidth() const; void setBorderWidth(const int val); - int borderHeight() const; + [[nodiscard]] int borderHeight() const; void setBorderHeight(const int val); - int titleBarHeight() const; + [[nodiscard]] int titleBarHeight() const; void setTitleBarHeight(const int val); - bool resizable() const; + [[nodiscard]] bool resizable() const; void setResizable(const bool val); - QColor nativeFrameColor() const; + [[nodiscard]] QColor nativeFrameColor() const; public Q_SLOTS: void removeWindowFrame(); void bringBackWindowFrame(); - bool isWindowFrameless() const; + [[nodiscard]] bool isWindowFrameless() const; void setHitTestVisibleInChrome(QQuickItem *item, const bool visible); Q_SIGNALS: diff --git a/framelesswindowsmanager.h b/framelesswindowsmanager.h index 0e1e712..a8c5135 100644 --- a/framelesswindowsmanager.h +++ b/framelesswindowsmanager.h @@ -35,15 +35,15 @@ namespace FramelessWindowsManager { FRAMELESSHELPER_API void addWindow(QWindow *window); FRAMELESSHELPER_API void removeWindow(QWindow *window); -FRAMELESSHELPER_API bool isWindowFrameless(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API bool isWindowFrameless(const QWindow *window); FRAMELESSHELPER_API void setHitTestVisibleInChrome(QWindow *window, QObject *object, const bool value = true); -FRAMELESSHELPER_API int getBorderWidth(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API int getBorderWidth(const QWindow *window); FRAMELESSHELPER_API void setBorderWidth(QWindow *window, const int value); -FRAMELESSHELPER_API int getBorderHeight(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API int getBorderHeight(const QWindow *window); FRAMELESSHELPER_API void setBorderHeight(QWindow *window, const int value); -FRAMELESSHELPER_API int getTitleBarHeight(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API int getTitleBarHeight(const QWindow *window); FRAMELESSHELPER_API void setTitleBarHeight(QWindow *window, const int value); -FRAMELESSHELPER_API bool getResizable(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API bool getResizable(const QWindow *window); FRAMELESSHELPER_API void setResizable(QWindow *window, const bool value = true); } diff --git a/utilities.h b/utilities.h index 81a0c78..4340933 100644 --- a/utilities.h +++ b/utilities.h @@ -38,32 +38,31 @@ enum class SystemMetric }; // Common -FRAMELESSHELPER_API int getSystemMetric(const QWindow *window, const SystemMetric metric, const bool dpiAware, const bool forceSystemValue = false); -FRAMELESSHELPER_API bool isLightThemeEnabled(); -FRAMELESSHELPER_API bool isDarkThemeEnabled(); -FRAMELESSHELPER_API QWindow *findWindow(const WId winId); -FRAMELESSHELPER_API bool shouldUseNativeTitleBar(); -FRAMELESSHELPER_API bool isWindowFixedSize(const QWindow *window); -FRAMELESSHELPER_API QPointF getGlobalMousePosition(const QWindow *window); -FRAMELESSHELPER_API bool isHitTestVisibleInChrome(const QWindow *window); -FRAMELESSHELPER_API QColor getNativeWindowFrameColor(const bool isActive = true); -FRAMELESSHELPER_API QPointF mapOriginPointToWindow(const QObject *object); +[[nodiscard]] FRAMELESSHELPER_API int getSystemMetric(const QWindow *window, const SystemMetric metric, const bool dpiAware, const bool forceSystemValue = false); +[[nodiscard]] FRAMELESSHELPER_API bool isLightThemeEnabled(); +[[nodiscard]] FRAMELESSHELPER_API bool isDarkThemeEnabled(); +[[nodiscard]] FRAMELESSHELPER_API QWindow *findWindow(const WId winId); +[[nodiscard]] FRAMELESSHELPER_API bool shouldUseNativeTitleBar(); +[[nodiscard]] FRAMELESSHELPER_API bool isWindowFixedSize(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API QPointF getGlobalMousePosition(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API bool isHitTestVisibleInChrome(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API QColor getNativeWindowFrameColor(const bool isActive = true); +[[nodiscard]] FRAMELESSHELPER_API QPointF mapOriginPointToWindow(const QObject *object); #ifdef Q_OS_WINDOWS // Windows specific -FRAMELESSHELPER_API bool isWin7OrGreater(); -FRAMELESSHELPER_API bool isWin8OrGreater(); -FRAMELESSHELPER_API bool isWin8Point1OrGreater(); -FRAMELESSHELPER_API bool isWin10OrGreater(); -FRAMELESSHELPER_API bool isWin10OrGreater(const int subVer); -FRAMELESSHELPER_API bool isDwmBlurAvailable(); -FRAMELESSHELPER_API bool isColorizationEnabled(); -FRAMELESSHELPER_API QColor getColorizationColor(); +[[nodiscard]] FRAMELESSHELPER_API bool isWin8OrGreater(); +[[nodiscard]] FRAMELESSHELPER_API bool isWin8Point1OrGreater(); +[[nodiscard]] FRAMELESSHELPER_API bool isWin10OrGreater(); +[[nodiscard]] FRAMELESSHELPER_API bool isWin10OrGreater(const int subVer); +[[nodiscard]] FRAMELESSHELPER_API bool isDwmBlurAvailable(); +[[nodiscard]] FRAMELESSHELPER_API bool isColorizationEnabled(); +[[nodiscard]] FRAMELESSHELPER_API QColor getColorizationColor(); FRAMELESSHELPER_API void triggerFrameChange(const QWindow *window); FRAMELESSHELPER_API void updateFrameMargins(const QWindow *window, const bool reset); FRAMELESSHELPER_API void updateQtFrameMargins(QWindow *window, const bool enable); -FRAMELESSHELPER_API quint32 getWindowDpi(const QWindow *window); -FRAMELESSHELPER_API QMargins getWindowNativeFrameMargins(const QWindow *window); +[[nodiscard]] quint32 getWindowDpi(const QWindow *window); +[[nodiscard]] FRAMELESSHELPER_API QMargins getWindowNativeFrameMargins(const QWindow *window); FRAMELESSHELPER_API void displaySystemMenu(const QWindow *window, const QPointF &pos = {}); #endif diff --git a/utilities_win32.cpp b/utilities_win32.cpp index 6b750ab..c20dee7 100644 --- a/utilities_win32.cpp +++ b/utilities_win32.cpp @@ -32,7 +32,7 @@ #include "utilities.h" #include -#include +#include #include #include #include @@ -92,8 +92,6 @@ struct Win32Data using GetSystemMetricsForDpiPtr = int(WINAPI *)(int, UINT); using AdjustWindowRectExForDpiPtr = BOOL(WINAPI *)(LPRECT, DWORD, BOOL, DWORD, UINT); - - ShouldAppsUseDarkModePtr ShouldAppsUseDarkModePFN = nullptr; ShouldSystemUseDarkModePtr ShouldSystemUseDarkModePFN = nullptr; @@ -112,18 +110,18 @@ struct Win32Data void load() { - QLibrary User32Dll(QStringLiteral("User32")); + QSystemLibrary User32Dll(QStringLiteral("User32.dll")); GetDpiForWindowPFN = reinterpret_cast(User32Dll.resolve("GetDpiForWindow")); GetDpiForSystemPFN = reinterpret_cast(User32Dll.resolve("GetDpiForSystem")); GetSystemMetricsForDpiPFN = reinterpret_cast(User32Dll.resolve("GetSystemMetricsForDpi")); AdjustWindowRectExForDpiPFN = reinterpret_cast(User32Dll.resolve("AdjustWindowRectExForDpi")); GetSystemDpiForProcessPFN = reinterpret_cast(User32Dll.resolve("GetSystemDpiForProcess")); - QLibrary UxThemeDll(QStringLiteral("UxTheme")); + QSystemLibrary UxThemeDll(QStringLiteral("UxTheme.dll")); ShouldAppsUseDarkModePFN = reinterpret_cast(UxThemeDll.resolve(MAKEINTRESOURCEA(132))); ShouldSystemUseDarkModePFN = reinterpret_cast(UxThemeDll.resolve(MAKEINTRESOURCEA(138))); - QLibrary SHCoreDll(QStringLiteral("SHCore")); + QSystemLibrary SHCoreDll(QStringLiteral("SHCore.dll")); GetDpiForMonitorPFN = reinterpret_cast(SHCoreDll.resolve("GetDpiForMonitor")); GetProcessDpiAwarenessPFN = reinterpret_cast(SHCoreDll.resolve("GetProcessDpiAwareness")); } @@ -133,6 +131,7 @@ Q_GLOBAL_STATIC(Win32Data, win32Data) bool Utilities::isDwmBlurAvailable() { + // DWM Composition is always enabled and can't be disabled since Windows 8. if (isWin8OrGreater()) { return true; } @@ -141,7 +140,7 @@ bool Utilities::isDwmBlurAvailable() qWarning() << "DwmIsCompositionEnabled failed."; return false; } - return isWin7OrGreater() && (enabled != FALSE); + return (enabled != FALSE); } int Utilities::getSystemMetric(const QWindow *window, const SystemMetric metric, const bool dpiAware, const bool forceSystemValue) @@ -415,15 +414,6 @@ void Utilities::updateQtFrameMargins(QWindow *window, const bool enable) #endif } -bool Utilities::isWin7OrGreater() -{ -#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)) - return QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows7; -#else - return QSysInfo::WindowsVersion >= QSysInfo::WV_WINDOWS7; -#endif -} - bool Utilities::isWin8OrGreater() { #if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))