From 378aa7db120cc624125aab71a2f8c05d1975e4c4 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Wed, 18 Jan 2023 13:43:51 +0800 Subject: [PATCH] fix nested frameless windows & other minor tweaks Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- .../Core/framelesshelpercore_global.h | 17 ++++++++++++----- src/core/framelesshelper_win.cpp | 3 +-- src/core/framelesshelpercore_global.cpp | 4 ++++ src/core/utils_win.cpp | 8 +++----- src/quick/framelessquickhelper.cpp | 6 ++---- src/widgets/framelesswidgetshelper.cpp | 6 ++---- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/include/FramelessHelper/Core/framelesshelpercore_global.h b/include/FramelessHelper/Core/framelesshelpercore_global.h index ae2bd57..71ad5d6 100644 --- a/include/FramelessHelper/Core/framelesshelpercore_global.h +++ b/include/FramelessHelper/Core/framelesshelpercore_global.h @@ -56,10 +56,10 @@ QT_END_NAMESPACE #endif #if defined(Q_OS_WIN) && !defined(Q_OS_WINDOWS) -# define Q_OS_WINDOWS +# define Q_OS_WINDOWS // Since 5.14 #endif -#ifndef Q_DISABLE_COPY_MOVE +#ifndef Q_DISABLE_COPY_MOVE // Since 5.13 # define Q_DISABLE_COPY_MOVE(Class) \ Q_DISABLE_COPY(Class) \ Class(Class &&) = delete; \ @@ -68,8 +68,6 @@ QT_END_NAMESPACE #if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) using QStringView = const QString &; -#else -# include #endif #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0)) @@ -77,6 +75,7 @@ QT_END_NAMESPACE # define Q_NAMESPACE_EXPORT(...) Q_NAMESPACE #endif +// MOC can't handle C++ attributes before 5.15. #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) # define Q_NODISCARD [[nodiscard]] # define Q_MAYBE_UNUSED [[maybe_unused]] @@ -108,7 +107,7 @@ QT_END_NAMESPACE #endif #if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)) - using namespace Qt::StringLiterals; + using namespace Qt::Literals::StringLiterals; #endif #ifndef FRAMELESSHELPER_BYTEARRAY_LITERAL @@ -131,6 +130,14 @@ QT_END_NAMESPACE # endif #endif +#ifndef Q_UNREACHABLE_RETURN // Since 6.5 +# define Q_UNREACHABLE_RETURN(...) \ + do { \ + Q_UNREACHABLE(); \ + return __VA_ARGS__; \ + } while (false) +#endif + #ifndef FRAMELESSHELPER_BYTEARRAY_CONSTANT2 # define FRAMELESSHELPER_BYTEARRAY_CONSTANT2(name, ba) \ [[maybe_unused]] static const auto k##name = FRAMELESSHELPER_BYTEARRAY_LITERAL(ba); diff --git a/src/core/framelesshelper_win.cpp b/src/core/framelesshelper_win.cpp index 87ede19..90e1fb8 100644 --- a/src/core/framelesshelper_win.cpp +++ b/src/core/framelesshelper_win.cpp @@ -206,8 +206,7 @@ Q_GLOBAL_STATIC(Win32Helper, g_win32Helper) if (data.params.isInsideSystemButtons(qtScenePos, &buttonType)) { switch (buttonType) { case SystemButtonType::Unknown: - Q_ASSERT(false); - break; + Q_UNREACHABLE_RETURN(HTNOWHERE); case SystemButtonType::WindowIcon: return HTSYSMENU; case SystemButtonType::Help: diff --git a/src/core/framelesshelpercore_global.cpp b/src/core/framelesshelpercore_global.cpp index c6d8db6..d52a6d7 100644 --- a/src/core/framelesshelpercore_global.cpp +++ b/src/core/framelesshelpercore_global.cpp @@ -197,6 +197,10 @@ void initialize() Utils::fixupDialogsDpiScaling(); #endif + // We need this flag to enable nested frameless windows, however, + // this flag is known to be __NOT__ compatible with QGLWidget. + QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); + #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) // Enable high DPI scaling by default, but only for Qt5 applications, // because this has become the default setting since Qt6 and it can't diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 84fe886..3327e9a 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -1681,8 +1681,7 @@ void Utils::setCornerStyleForWindow(const WId windowId, const WindowCornerStyle case WindowCornerStyle::Round: return _DWMWCP_ROUND; } - Q_ASSERT(false); - return _DWMWCP_DEFAULT; + Q_UNREACHABLE_RETURN(_DWMWCP_DEFAULT); }(); const HRESULT hr = API_CALL_FUNCTION(DwmSetWindowAttribute, hwnd, _DWMWA_WINDOW_CORNER_PREFERENCE, &wcp, sizeof(wcp)); @@ -1739,8 +1738,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, } return BlurMode::Windows_Aero; } - Q_ASSERT(false); // Really should NOT go here. - return mode; + Q_UNREACHABLE_RETURN(BlurMode::Default); }(); if (blurMode == BlurMode::Disable) { bool result = true; @@ -1849,7 +1847,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, } else if (blurMode == BlurMode::Windows_Aero) { policy.State = ACCENT_ENABLE_BLURBEHIND; } else { - Q_ASSERT(false); // Really should NOT go here. + Q_UNREACHABLE_RETURN(false); } WINDOWCOMPOSITIONATTRIBDATA wcad; SecureZeroMemory(&wcad, sizeof(wcad)); diff --git a/src/quick/framelessquickhelper.cpp b/src/quick/framelessquickhelper.cpp index c999d6d..b7f994d 100644 --- a/src/quick/framelessquickhelper.cpp +++ b/src/quick/framelessquickhelper.cpp @@ -275,8 +275,7 @@ void FramelessQuickHelperPrivate::setSystemButton(QQuickItem *item, const QuickG } switch (buttonType) { case QuickGlobal::SystemButtonType::Unknown: - Q_ASSERT(false); - break; + Q_UNREACHABLE_RETURN(static_cast(0)); case QuickGlobal::SystemButtonType::WindowIcon: data->windowIconButton = item; break; @@ -800,8 +799,7 @@ void FramelessQuickHelperPrivate::setSystemButtonState(const QuickGlobal::System QQuickAbstractButton *quickButton = nullptr; switch (button) { case QuickGlobal::SystemButtonType::Unknown: - Q_ASSERT(false); - break; + Q_UNREACHABLE_RETURN(void(0)); case QuickGlobal::SystemButtonType::WindowIcon: if (data.windowIconButton) { if (const auto btn = qobject_cast(data.windowIconButton)) { diff --git a/src/widgets/framelesswidgetshelper.cpp b/src/widgets/framelesswidgetshelper.cpp index bc56ba3..8037aac 100644 --- a/src/widgets/framelesswidgetshelper.cpp +++ b/src/widgets/framelesswidgetshelper.cpp @@ -689,8 +689,7 @@ void FramelessWidgetsHelperPrivate::setSystemButtonState(const SystemButtonType QWidget *widgetButton = nullptr; switch (button) { case SystemButtonType::Unknown: - Q_ASSERT(false); - break; + Q_UNREACHABLE_RETURN(void(0)); case SystemButtonType::WindowIcon: if (data.windowIconButton) { widgetButton = data.windowIconButton; @@ -843,8 +842,7 @@ void FramelessWidgetsHelperPrivate::setSystemButton(QWidget *widget, const Syste } switch (buttonType) { case SystemButtonType::Unknown: - Q_ASSERT(false); - break; + Q_UNREACHABLE_RETURN(void(0)); case SystemButtonType::WindowIcon: data->windowIconButton = widget; break;