From 130d174dc29ee7201f14a0fadeec265a3813d113 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Thu, 21 Apr 2022 21:13:16 +0800 Subject: [PATCH] misc: improve old Qt version support Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/openglwidget/main.cpp | 1 + .../Core/framelesshelpercore_global.h | 15 ++- .../Quick/framelesshelperquick_global.h | 91 +++++++++++++++++++ .../Quick/framelessquickutils.h | 2 +- .../Quick/framelessquickwindow.h | 4 +- src/core/framelesshelper_qt.cpp | 11 ++- src/core/framelesswindowsmanager.cpp | 18 +--- src/core/qwinregistry.cpp | 41 ++++----- src/core/qwinregistry_p.h | 12 +-- src/core/utils_win.cpp | 2 +- src/quick/CMakeLists.txt | 13 ++- src/quick/framelessquickhelper.cpp | 26 +++++- src/quick/framelessquickutils.cpp | 6 +- src/quick/framelessquickwindow.cpp | 54 ++++++----- src/quick/framelessquickwindow_p.h | 4 +- src/quick/quickstandardclosebutton.cpp | 12 +-- src/quick/quickstandardmaximizebutton.cpp | 15 +-- src/quick/quickstandardminimizebutton.cpp | 12 +-- 18 files changed, 214 insertions(+), 125 deletions(-) diff --git a/examples/openglwidget/main.cpp b/examples/openglwidget/main.cpp index 65eb40d..6777860 100644 --- a/examples/openglwidget/main.cpp +++ b/examples/openglwidget/main.cpp @@ -48,6 +48,7 @@ ** ****************************************************************************/ +#include #include #include #include diff --git a/include/FramelessHelper/Core/framelesshelpercore_global.h b/include/FramelessHelper/Core/framelesshelpercore_global.h index 962d759..b803672 100644 --- a/include/FramelessHelper/Core/framelesshelpercore_global.h +++ b/include/FramelessHelper/Core/framelesshelpercore_global.h @@ -192,9 +192,8 @@ FRAMELESSHELPER_STRING_CONSTANT2(UsePureQtImplKeyPath, "Options/UsePureQtImpleme FRAMELESSHELPER_STRING_CONSTANT2(ForceHideFrameBorderKeyPath, "Options/ForceHideFrameBorder") FRAMELESSHELPER_STRING_CONSTANT2(ForceShowFrameBorderKeyPath, "Options/ForceShowFrameBorder") -enum class Option : int +enum class Option { - Default = 0x00000000, // Default placeholder, have no effect. ForceHideWindowFrameBorder = 0x00000001, // Windows only, force hide the window frame border even on Windows 10 and onwards. ForceShowWindowFrameBorder = 0x00000002, // Windows only, force show the window frame border even on Windows 7 (~ 8.1). DontDrawTopWindowFrameBorder = 0x00000004, // Windows only, don't draw the top window frame border even if the window frame border is visible. @@ -224,7 +223,7 @@ Q_DECLARE_FLAGS(Options, Option) Q_FLAG_NS(Options) Q_DECLARE_OPERATORS_FOR_FLAGS(Options) -enum class SystemTheme : int +enum class SystemTheme { Unknown = -1, Light = 0, @@ -233,7 +232,7 @@ enum class SystemTheme : int }; Q_ENUM_NS(SystemTheme) -enum class SystemButtonType : int +enum class SystemButtonType { Unknown = -1, WindowIcon = 0, @@ -245,7 +244,7 @@ enum class SystemButtonType : int }; Q_ENUM_NS(SystemButtonType) -enum class ResourceType : int +enum class ResourceType { Image = 0, Pixmap = 1, @@ -253,7 +252,7 @@ enum class ResourceType : int }; Q_ENUM_NS(ResourceType) -enum class DwmColorizationArea : int +enum class DwmColorizationArea { None_ = 0, // Avoid name conflicts with X11 headers. StartMenu_TaskBar_ActionCenter = 1, @@ -262,7 +261,7 @@ enum class DwmColorizationArea : int }; Q_ENUM_NS(DwmColorizationArea) -enum class Anchor : int +enum class Anchor { Top = 0, Bottom = 1, @@ -274,7 +273,7 @@ enum class Anchor : int }; Q_ENUM_NS(Anchor) -enum class ButtonState : int +enum class ButtonState { Unspecified = -1, Hovered = 0, diff --git a/include/FramelessHelper/Quick/framelesshelperquick_global.h b/include/FramelessHelper/Quick/framelesshelperquick_global.h index 02b1e23..799600b 100644 --- a/include/FramelessHelper/Quick/framelesshelperquick_global.h +++ b/include/FramelessHelper/Quick/framelesshelperquick_global.h @@ -25,6 +25,9 @@ #pragma once #include +#if __has_include() +#include +#endif #ifndef FRAMELESSHELPER_QUICK_API # ifdef FRAMELESSHELPER_QUICK_STATIC @@ -38,6 +41,94 @@ # endif #endif +#ifndef FRAMELESSHELPER_QUICK_ENUM_VALUE +# define FRAMELESSHELPER_QUICK_ENUM_VALUE(Enum, Value) \ + Value = static_cast(FRAMELESSHELPER_PREPEND_NAMESPACE(Global)::Enum::Value), +#endif + +#ifndef FRAMELESSHELPER_ENUM_CORE_TO_QUICK +# define FRAMELESSHELPER_ENUM_CORE_TO_QUICK(Enum, Value) \ + static_cast(static_cast(Value)) +#endif + +#ifndef FRAMELESSHELPER_ENUM_QUICK_TO_CORE +# define FRAMELESSHELPER_ENUM_QUICK_TO_CORE(Enum, Value) \ + static_cast(static_cast(Value)) +#endif + FRAMELESSHELPER_BEGIN_NAMESPACE + [[maybe_unused]] static constexpr const char FRAMELESSHELPER_QUICK_URI[] = "org.wangwenx190.FramelessHelper"; + +struct FRAMELESSHELPER_QUICK_API QuickGlobal +{ + enum class SystemTheme + { + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemTheme, Unknown) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemTheme, Light) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemTheme, Dark) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemTheme, HighContrast) + }; + Q_ENUM(SystemTheme) + + enum class SystemButtonType + { + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, Unknown) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, WindowIcon) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, Help) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, Minimize) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, Maximize) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, Restore) + FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, Close) + }; + Q_ENUM(SystemButtonType) + + enum class ResourceType + { + FRAMELESSHELPER_QUICK_ENUM_VALUE(ResourceType, Image) + FRAMELESSHELPER_QUICK_ENUM_VALUE(ResourceType, Pixmap) + FRAMELESSHELPER_QUICK_ENUM_VALUE(ResourceType, Icon) + }; + Q_ENUM(ResourceType) + + enum class DwmColorizationArea + { + FRAMELESSHELPER_QUICK_ENUM_VALUE(DwmColorizationArea, None_) // Avoid name conflicts with X11 headers. + FRAMELESSHELPER_QUICK_ENUM_VALUE(DwmColorizationArea, StartMenu_TaskBar_ActionCenter) + FRAMELESSHELPER_QUICK_ENUM_VALUE(DwmColorizationArea, TitleBar_WindowBorder) + FRAMELESSHELPER_QUICK_ENUM_VALUE(DwmColorizationArea, All) + }; + Q_ENUM(DwmColorizationArea) + + enum class Anchor + { + FRAMELESSHELPER_QUICK_ENUM_VALUE(Anchor, Top) + FRAMELESSHELPER_QUICK_ENUM_VALUE(Anchor, Bottom) + FRAMELESSHELPER_QUICK_ENUM_VALUE(Anchor, Left) + FRAMELESSHELPER_QUICK_ENUM_VALUE(Anchor, Right) + FRAMELESSHELPER_QUICK_ENUM_VALUE(Anchor, HorizontalCenter) + FRAMELESSHELPER_QUICK_ENUM_VALUE(Anchor, VerticalCenter) + FRAMELESSHELPER_QUICK_ENUM_VALUE(Anchor, Center) + }; + Q_ENUM(Anchor) + + enum class ButtonState + { + FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Unspecified) + FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Hovered) + FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Pressed) + }; + Q_ENUM(ButtonState) + +private: + Q_GADGET +#ifdef QML_NAMED_ELEMENT + QML_NAMED_ELEMENT(FramelessHelper) +#endif +#ifdef QML_UNCREATABLE + QML_UNCREATABLE("The FramelessHelper namespace is not creatable, you can only use it to access it's enums.") +#endif + Q_DISABLE_COPY_MOVE(QuickGlobal) +}; + FRAMELESSHELPER_END_NAMESPACE diff --git a/include/FramelessHelper/Quick/framelessquickutils.h b/include/FramelessHelper/Quick/framelessquickutils.h index be4bff8..9618ee9 100644 --- a/include/FramelessHelper/Quick/framelessquickutils.h +++ b/include/FramelessHelper/Quick/framelessquickutils.h @@ -78,7 +78,7 @@ public: Q_NODISCARD static QColor defaultSystemCloseButtonBackgroundColor(); Q_NODISCARD Q_INVOKABLE static QColor getSystemButtonBackgroundColor( - const Global::SystemButtonType button, const Global::ButtonState state); + const QuickGlobal::SystemButtonType button, const QuickGlobal::ButtonState state); Q_SIGNALS: void darkModeEnabledChanged(); diff --git a/include/FramelessHelper/Quick/framelessquickwindow.h b/include/FramelessHelper/Quick/framelessquickwindow.h index 63c4bff..b71f411 100644 --- a/include/FramelessHelper/Quick/framelessquickwindow.h +++ b/include/FramelessHelper/Quick/framelessquickwindow.h @@ -74,7 +74,7 @@ public Q_SLOTS: void setHitTestVisible(QQuickItem *item); void moveToDesktopCenter(); void bringToFront(); - void snapToTopBorder(QQuickItem *item, const Global::Anchor itemAnchor, const Global::Anchor topBorderAnchor); + void snapToTopBorder(QQuickItem *item, const QuickGlobal::Anchor itemAnchor, const QuickGlobal::Anchor topBorderAnchor); Q_SIGNALS: void hiddenChanged(); @@ -84,7 +84,7 @@ Q_SIGNALS: void fullScreenChanged(); void fixedSizeChanged(); void frameBorderColorChanged(); - void systemButtonStateChanged(const Global::SystemButtonType, const Global::ButtonState); + void systemButtonStateChanged(const QuickGlobal::SystemButtonType, const QuickGlobal::ButtonState); private: QScopedPointer d_ptr; diff --git a/src/core/framelesshelper_qt.cpp b/src/core/framelesshelper_qt.cpp index 53f8bbd..021d2cb 100644 --- a/src/core/framelesshelper_qt.cpp +++ b/src/core/framelesshelper_qt.cpp @@ -39,6 +39,7 @@ struct QtHelperData UserSettings settings = {}; SystemParameters params = {}; FramelessHelperQt *eventFilter = nullptr; + bool cursorShapeChanged = false; }; struct QtHelper @@ -133,9 +134,17 @@ bool FramelessHelperQt::eventFilter(QObject *object, QEvent *event) } const Qt::CursorShape cs = Utils::calculateCursorShape(window, scenePos); if (cs == Qt::ArrowCursor) { - window->unsetCursor(); + if (data.cursorShapeChanged) { + window->unsetCursor(); + QMutexLocker locker(&g_qtHelper()->mutex); + g_qtHelper()->data[windowId].cursorShapeChanged = false; + } else { + return false; + } } else { window->setCursor(cs); + QMutexLocker locker(&g_qtHelper()->mutex); + g_qtHelper()->data[windowId].cursorShapeChanged = true; } } break; case QEvent::MouseButtonPress: { diff --git a/src/core/framelesswindowsmanager.cpp b/src/core/framelesswindowsmanager.cpp index 16f649e..881a43f 100644 --- a/src/core/framelesswindowsmanager.cpp +++ b/src/core/framelesswindowsmanager.cpp @@ -97,7 +97,7 @@ bool FramelessWindowsManagerPrivate::usePureQtImplementation() if (qEnvironmentVariableIntValue(kUsePureQtImplFlag) != 0) { return true; } - const QString iniFilePath = QCoreApplication::applicationDirPath() + u'/' + kConfigFileName; + const QString iniFilePath = QCoreApplication::applicationDirPath() + QChar(u'/') + kConfigFileName; QSettings settings(iniFilePath, QSettings::IniFormat); return settings.value(kUsePureQtImplKeyPath, false).toBool(); }(); @@ -300,8 +300,6 @@ void FramelessHelper::Core::initialize(const Options options) QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round); } #endif - // Mainly for Qt Quick applications, but won't bring any harm to Qt Widgets - // applications either. qRegisterMetaType