minor refactor

This commit is contained in:
Yuhang Zhao 2023-04-11 17:09:58 +08:00
parent 99417bf916
commit 3b8571401f
6 changed files with 180 additions and 176 deletions

View File

@ -259,145 +259,147 @@ Q_NAMESPACE_EXPORT(FRAMELESSHELPER_CORE_API)
[[maybe_unused]] inline const QByteArray kSysMenuDisableRestoreVar [[maybe_unused]] inline const QByteArray kSysMenuDisableRestoreVar
= FRAMELESSHELPER_BYTEARRAY_LITERAL("FRAMELESSHELPER_SYSTEM_MENU_DISABLE_RESTORE"); = FRAMELESSHELPER_BYTEARRAY_LITERAL("FRAMELESSHELPER_SYSTEM_MENU_DISABLE_RESTORE");
enum class Option enum class Option : quint8
{ {
UseCrossPlatformQtImplementation = 0, UseCrossPlatformQtImplementation,
ForceHideWindowFrameBorder = 1, ForceHideWindowFrameBorder,
ForceShowWindowFrameBorder = 2, ForceShowWindowFrameBorder,
DisableWindowsSnapLayout = 3, DisableWindowsSnapLayout,
WindowUseRoundCorners = 4, WindowUseRoundCorners,
CenterWindowBeforeShow = 5, CenterWindowBeforeShow,
EnableBlurBehindWindow = 6, EnableBlurBehindWindow,
ForceNonNativeBackgroundBlur = 7, ForceNonNativeBackgroundBlur,
DisableLazyInitializationForMicaMaterial = 8, DisableLazyInitializationForMicaMaterial,
ForceNativeBackgroundBlur = 9 ForceNativeBackgroundBlur
}; };
Q_ENUM_NS(Option) Q_ENUM_NS(Option)
enum class SystemTheme enum class SystemTheme : quint8
{ {
Unknown = -1, Unknown,
Light = 0, Light,
Dark = 1, Dark,
HighContrast = 2 HighContrast
}; };
Q_ENUM_NS(SystemTheme) Q_ENUM_NS(SystemTheme)
enum class SystemButtonType enum class SystemButtonType : quint8
{ {
Unknown = -1, Unknown,
WindowIcon = 0, WindowIcon,
Help = 1, Help,
Minimize = 2, Minimize,
Maximize = 3, Maximize,
Restore = 4, Restore,
Close = 5 Close
}; };
Q_ENUM_NS(SystemButtonType) Q_ENUM_NS(SystemButtonType)
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
enum class DwmColorizationArea enum class DwmColorizationArea : quint8
{ {
None = 0, None,
StartMenu_TaskBar_ActionCenter = 1, StartMenu_TaskBar_ActionCenter,
TitleBar_WindowBorder = 2, TitleBar_WindowBorder,
All = 3 All
}; };
Q_ENUM_NS(DwmColorizationArea) Q_ENUM_NS(DwmColorizationArea)
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS
enum class ButtonState enum class ButtonState : quint8
{ {
Unspecified = -1, Normal,
Hovered = 0, Hovered,
Pressed = 1, Pressed,
Clicked = 2 Released
}; };
Q_ENUM_NS(ButtonState) Q_ENUM_NS(ButtonState)
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
enum class WindowsVersion enum class WindowsVersion : quint8
{ {
_2000 = 0, _2000,
_XP = 1, _XP,
_XP_64 = 2, _XP_64,
_Vista,
_Vista_SP1,
_Vista_SP2,
_7,
_7_SP1,
_8,
_8_1,
_8_1_Update1,
_10_1507,
_10_1511,
_10_1607,
_10_1703,
_10_1709,
_10_1803,
_10_1809,
_10_1903,
_10_1909,
_10_2004,
_10_20H2,
_10_21H1,
_10_21H2,
_10_22H2,
_11_21H2,
_11_22H2,
_WS_03 = _XP_64, // Windows Server 2003 _WS_03 = _XP_64, // Windows Server 2003
_Vista = 3,
_Vista_SP1 = 4,
_Vista_SP2 = 5,
_7 = 6,
_7_SP1 = 7,
_8 = 8,
_8_1 = 9,
_8_1_Update1 = 10,
_10_1507 = 11,
_10_1511 = 12,
_10_1607 = 13,
_10_1703 = 14,
_10_1709 = 15,
_10_1803 = 16,
_10_1809 = 17,
_10_1903 = 18,
_10_1909 = 19,
_10_2004 = 20,
_10_20H2 = 21,
_10_21H1 = 22,
_10_21H2 = 23,
_10_22H2 = 24,
_10 = _10_1507, _10 = _10_1507,
_11_21H2 = 25,
_11_22H2 = 26,
_11 = _11_21H2, _11 = _11_21H2,
Latest = _11_22H2 Latest = _11_22H2
}; };
Q_ENUM_NS(WindowsVersion) Q_ENUM_NS(WindowsVersion)
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS
enum class BlurMode enum class BlurMode : quint8
{ {
Disable = 0, // Do not enable blur behind window Disable, // Do not enable blur behind window
Default = 1, // Use platform default blur mode Default, // Use platform default blur mode
Windows_Aero = 2, // Windows only, use the traditional DWM blur Windows_Aero, // Windows only, use the traditional DWM blur
Windows_Acrylic = 3, // Windows only, use the Acrylic blur Windows_Acrylic, // Windows only, use the Acrylic blur
Windows_Mica = 4, // Windows only, use the Mica material Windows_Mica, // Windows only, use the Mica material
Windows_MicaAlt = 5 // Windows only, use the Mica Alt material Windows_MicaAlt // Windows only, use the Mica Alt material
}; };
Q_ENUM_NS(BlurMode) Q_ENUM_NS(BlurMode)
enum class WallpaperAspectStyle enum class WallpaperAspectStyle : quint8
{ {
Fill = 0, // Keep aspect ratio to fill, expand/crop if necessary. Fill, // Keep aspect ratio to fill, expand/crop if necessary.
Fit = 1, // Keep aspect ratio to fill, but don't expand/crop. Fit, // Keep aspect ratio to fill, but don't expand/crop.
Stretch = 2, // Ignore aspect ratio to fill. Stretch, // Ignore aspect ratio to fill.
Tile = 3, Tile,
Center = 4, Center,
Span = 5 // ??? Span // ???
}; };
Q_ENUM_NS(WallpaperAspectStyle) Q_ENUM_NS(WallpaperAspectStyle)
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
enum class RegistryRootKey enum class RegistryRootKey : quint8
{ {
ClassesRoot = 0, ClassesRoot,
CurrentUser = 1, CurrentUser,
LocalMachine = 2, LocalMachine,
Users = 3, Users,
PerformanceData = 4, PerformanceData,
CurrentConfig = 5, CurrentConfig,
DynData = 6, DynData,
CurrentUserLocalSettings = 7, CurrentUserLocalSettings,
PerformanceText = 8, PerformanceText,
PerformanceNlsText = 9 PerformanceNlsText
}; };
Q_ENUM_NS(RegistryRootKey) Q_ENUM_NS(RegistryRootKey)
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS
enum class WindowEdge : quint32 enum class WindowEdge : quint8
{ {
Left = 0x00000001, Left = 1 << 0,
Top = 0x00000002, Top = 1 << 1,
Right = 0x00000004, Right = 1 << 2,
Bottom = 0x00000008 Bottom = 1 << 3
}; };
Q_ENUM_NS(WindowEdge) Q_ENUM_NS(WindowEdge)
Q_DECLARE_FLAGS(WindowEdges, WindowEdge) Q_DECLARE_FLAGS(WindowEdges, WindowEdge)
@ -405,23 +407,23 @@ Q_FLAG_NS(WindowEdges)
Q_DECLARE_OPERATORS_FOR_FLAGS(WindowEdges) Q_DECLARE_OPERATORS_FOR_FLAGS(WindowEdges)
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
enum class DpiAwareness enum class DpiAwareness : quint8
{ {
Unknown = -1, Unknown,
Unaware = 0, Unaware,
System = 1, System,
PerMonitor = 2, PerMonitor,
PerMonitorVersion2 = 3, PerMonitorVersion2,
Unaware_GdiScaled = 4 Unaware_GdiScaled
}; };
Q_ENUM_NS(DpiAwareness) Q_ENUM_NS(DpiAwareness)
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS
enum class WindowCornerStyle enum class WindowCornerStyle : quint8
{ {
Default = 0, Default,
Square = 1, Square,
Round = 2 Round
}; };
Q_ENUM_NS(WindowCornerStyle) Q_ENUM_NS(WindowCornerStyle)

View File

@ -90,7 +90,7 @@ public:
explicit QuickGlobal(QObject *parent = nullptr); explicit QuickGlobal(QObject *parent = nullptr);
~QuickGlobal() override; ~QuickGlobal() override;
enum class SystemTheme enum class SystemTheme : quint8
{ {
FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemTheme, Unknown) FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemTheme, Unknown)
FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemTheme, Light) FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemTheme, Light)
@ -99,7 +99,7 @@ public:
}; };
Q_ENUM(SystemTheme) Q_ENUM(SystemTheme)
enum class SystemButtonType enum class SystemButtonType : quint8
{ {
FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, Unknown) FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, Unknown)
FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, WindowIcon) FRAMELESSHELPER_QUICK_ENUM_VALUE(SystemButtonType, WindowIcon)
@ -111,16 +111,16 @@ public:
}; };
Q_ENUM(SystemButtonType) Q_ENUM(SystemButtonType)
enum class ButtonState enum class ButtonState : quint8
{ {
FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Unspecified) FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Normal)
FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Hovered) FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Hovered)
FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Pressed) FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Pressed)
FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Clicked) FRAMELESSHELPER_QUICK_ENUM_VALUE(ButtonState, Released)
}; };
Q_ENUM(ButtonState) Q_ENUM(ButtonState)
enum class BlurMode enum class BlurMode : quint8
{ {
FRAMELESSHELPER_QUICK_ENUM_VALUE(BlurMode, Disable) FRAMELESSHELPER_QUICK_ENUM_VALUE(BlurMode, Disable)
FRAMELESSHELPER_QUICK_ENUM_VALUE(BlurMode, Default) FRAMELESSHELPER_QUICK_ENUM_VALUE(BlurMode, Default)
@ -131,7 +131,7 @@ public:
}; };
Q_ENUM(BlurMode) Q_ENUM(BlurMode)
enum class WindowEdge : quint32 enum class WindowEdge : quint8
{ {
FRAMELESSHELPER_QUICK_ENUM_VALUE(WindowEdge, Left) FRAMELESSHELPER_QUICK_ENUM_VALUE(WindowEdge, Left)
FRAMELESSHELPER_QUICK_ENUM_VALUE(WindowEdge, Top) FRAMELESSHELPER_QUICK_ENUM_VALUE(WindowEdge, Top)

View File

@ -159,7 +159,7 @@ Q_GLOBAL_STATIC(Win32Helper, g_win32Helper)
const bool isMouseEvent = (((uMsg >= WM_MOUSEFIRST) && (uMsg <= WM_MOUSELAST)) || const bool isMouseEvent = (((uMsg >= WM_MOUSEFIRST) && (uMsg <= WM_MOUSELAST)) ||
((uMsg >= WM_NCMOUSEMOVE) && (uMsg <= WM_NCXBUTTONDBLCLK))); ((uMsg >= WM_NCMOUSEMOVE) && (uMsg <= WM_NCXBUTTONDBLCLK)));
const auto releaseButtons = [&data](const std::optional<SystemButtonType> exclude) -> void { const auto releaseButtons = [&data](const std::optional<SystemButtonType> exclude) -> void {
static constexpr const auto defaultButtonState = ButtonState::Unspecified; static constexpr const auto defaultButtonState = ButtonState::Normal;
const SystemButtonType button = exclude.value_or(SystemButtonType::Unknown); const SystemButtonType button = exclude.value_or(SystemButtonType::Unknown);
if (button != SystemButtonType::WindowIcon) { if (button != SystemButtonType::WindowIcon) {
data.params.setSystemButtonState(SystemButtonType::WindowIcon, defaultButtonState); data.params.setSystemButtonState(SystemButtonType::WindowIcon, defaultButtonState);
@ -190,7 +190,7 @@ Q_GLOBAL_STATIC(Win32Helper, g_win32Helper)
}; };
const auto clickButton = [&releaseButtons, &data](const SystemButtonType button) -> void { const auto clickButton = [&releaseButtons, &data](const SystemButtonType button) -> void {
releaseButtons(button); releaseButtons(button);
data.params.setSystemButtonState(button, ButtonState::Clicked); data.params.setSystemButtonState(button, ButtonState::Released);
}; };
switch (uMsg) { switch (uMsg) {
case WM_NCHITTEST: { case WM_NCHITTEST: {

View File

@ -265,7 +265,7 @@ bool Utils::isThemeChangeEvent(const QEvent * const event)
QColor Utils::calculateSystemButtonBackgroundColor(const SystemButtonType button, const ButtonState state) QColor Utils::calculateSystemButtonBackgroundColor(const SystemButtonType button, const ButtonState state)
{ {
if (state == ButtonState::Unspecified) { if (state == ButtonState::Normal) {
return kDefaultTransparentColor; return kDefaultTransparentColor;
} }
const bool isClose = (button == SystemButtonType::Close); const bool isClose = (button == SystemButtonType::Close);

View File

@ -866,14 +866,16 @@ void FramelessQuickHelperPrivate::setSystemButtonState(const QuickGlobal::System
} }
break; break;
} }
if (quickButton) { if (!quickButton) {
return;
}
const auto updateButtonState = [state](QQuickAbstractButton *btn) -> void { const auto updateButtonState = [state](QQuickAbstractButton *btn) -> void {
Q_ASSERT(btn); Q_ASSERT(btn);
if (!btn) { if (!btn) {
return; return;
} }
switch (state) { switch (state) {
case QuickGlobal::ButtonState::Unspecified: { case QuickGlobal::ButtonState::Normal: {
btn->setPressed(false); btn->setPressed(false);
btn->setHovered(false); btn->setHovered(false);
} break; } break;
@ -885,7 +887,7 @@ void FramelessQuickHelperPrivate::setSystemButtonState(const QuickGlobal::System
btn->setHovered(true); btn->setHovered(true);
btn->setPressed(true); btn->setPressed(true);
} break; } break;
case QuickGlobal::ButtonState::Clicked: { case QuickGlobal::ButtonState::Released: {
// Clicked: pressed --> released, so behave like hovered. // Clicked: pressed --> released, so behave like hovered.
btn->setPressed(false); btn->setPressed(false);
btn->setHovered(true); btn->setHovered(true);
@ -894,7 +896,6 @@ void FramelessQuickHelperPrivate::setSystemButtonState(const QuickGlobal::System
} }
}; };
updateButtonState(quickButton); updateButtonState(quickButton);
}
#endif // FRAMELESSHELPER_QUICK_NO_PRIVATE #endif // FRAMELESSHELPER_QUICK_NO_PRIVATE
} }

View File

@ -745,14 +745,16 @@ void FramelessWidgetsHelperPrivate::setSystemButtonState(const SystemButtonType
} }
break; break;
} }
if (widgetButton) { if (!widgetButton) {
return;
}
const auto updateButtonState = [state](QWidget *btn) -> void { const auto updateButtonState = [state](QWidget *btn) -> void {
Q_ASSERT(btn); Q_ASSERT(btn);
if (!btn) { if (!btn) {
return; return;
} }
switch (state) { switch (state) {
case ButtonState::Unspecified: { case ButtonState::Normal: {
QMetaObject::invokeMethod(btn, "setPressed", Q_ARG(bool, false)); QMetaObject::invokeMethod(btn, "setPressed", Q_ARG(bool, false));
QMetaObject::invokeMethod(btn, "setHovered", Q_ARG(bool, false)); QMetaObject::invokeMethod(btn, "setHovered", Q_ARG(bool, false));
} break; } break;
@ -764,7 +766,7 @@ void FramelessWidgetsHelperPrivate::setSystemButtonState(const SystemButtonType
QMetaObject::invokeMethod(btn, "setHovered", Q_ARG(bool, true)); QMetaObject::invokeMethod(btn, "setHovered", Q_ARG(bool, true));
QMetaObject::invokeMethod(btn, "setPressed", Q_ARG(bool, true)); QMetaObject::invokeMethod(btn, "setPressed", Q_ARG(bool, true));
} break; } break;
case ButtonState::Clicked: { case ButtonState::Released: {
// Clicked: pressed --> released, so behave like hovered. // Clicked: pressed --> released, so behave like hovered.
QMetaObject::invokeMethod(btn, "setPressed", Q_ARG(bool, false)); QMetaObject::invokeMethod(btn, "setPressed", Q_ARG(bool, false));
QMetaObject::invokeMethod(btn, "setHovered", Q_ARG(bool, true)); QMetaObject::invokeMethod(btn, "setHovered", Q_ARG(bool, true));
@ -782,7 +784,6 @@ void FramelessWidgetsHelperPrivate::setSystemButtonState(const SystemButtonType
} }
} }
} }
}
void FramelessWidgetsHelperPrivate::moveWindowToDesktopCenter() void FramelessWidgetsHelperPrivate::moveWindowToDesktopCenter()
{ {