From 8b6dd746eb234b99b4dab24950eded93a087134a Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Mon, 6 Feb 2023 15:08:19 +0800 Subject: [PATCH] refactor Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- .../Core/framelesshelpercore_global.h | 93 ------------- .../Core/private/scopeguard_p.h | 101 ++++++++++++++ .../Core/private/versionnumber_p.h | 129 ++++++++++++++++++ qmake/core.pri | 4 +- src/core/CMakeLists.txt | 2 + src/core/framelesshelpercore_global.cpp | 13 +- src/core/scopeguard_p.h | 1 + src/core/utils_win.cpp | 26 ++-- src/core/versionnumber_p.h | 1 + 9 files changed, 254 insertions(+), 116 deletions(-) create mode 100644 include/FramelessHelper/Core/private/scopeguard_p.h create mode 100644 include/FramelessHelper/Core/private/versionnumber_p.h create mode 100644 src/core/scopeguard_p.h create mode 100644 src/core/versionnumber_p.h diff --git a/include/FramelessHelper/Core/framelesshelpercore_global.h b/include/FramelessHelper/Core/framelesshelpercore_global.h index 3517690..7ab6718 100644 --- a/include/FramelessHelper/Core/framelesshelpercore_global.h +++ b/include/FramelessHelper/Core/framelesshelpercore_global.h @@ -410,65 +410,6 @@ enum class WindowCornerStyle }; Q_ENUM_NS(WindowCornerStyle) -struct VersionNumber -{ - int major = 0; - int minor = 0; - int patch = 0; - int tweak = 0; - - [[nodiscard]] friend constexpr bool operator==(const VersionNumber &lhs, const VersionNumber &rhs) noexcept - { - return ((lhs.major == rhs.major) && (lhs.minor == rhs.minor) && (lhs.patch == rhs.patch) && (lhs.tweak == rhs.tweak)); - } - - [[nodiscard]] friend constexpr bool operator!=(const VersionNumber &lhs, const VersionNumber &rhs) noexcept - { - return !operator==(lhs, rhs); - } - - [[nodiscard]] friend constexpr bool operator>(const VersionNumber &lhs, const VersionNumber &rhs) noexcept - { - if (operator==(lhs, rhs)) { - return false; - } - if (lhs.major > rhs.major) { - return true; - } - if (lhs.major < rhs.major) { - return false; - } - if (lhs.minor > rhs.minor) { - return true; - } - if (lhs.minor < rhs.minor) { - return false; - } - if (lhs.patch > rhs.patch) { - return true; - } - if (lhs.patch < rhs.patch) { - return false; - } - return (lhs.tweak > rhs.tweak); - } - - [[nodiscard]] friend constexpr bool operator<(const VersionNumber &lhs, const VersionNumber &rhs) noexcept - { - return (operator!=(lhs, rhs) && !operator>(lhs, rhs)); - } - - [[nodiscard]] friend constexpr bool operator>=(const VersionNumber &lhs, const VersionNumber &rhs) noexcept - { - return (operator>(lhs, rhs) || operator==(lhs, rhs)); - } - - [[nodiscard]] friend constexpr bool operator<=(const VersionNumber &lhs, const VersionNumber &rhs) noexcept - { - return (operator<(lhs, rhs) || operator==(lhs, rhs)); - } -}; - struct VersionInfo { int version = 0; @@ -486,39 +427,6 @@ struct Dpi quint32 y = 0; }; -#ifdef Q_OS_WINDOWS -[[maybe_unused]] inline constexpr const VersionNumber WindowsVersions[] = -{ - { 5, 0, 2195}, // Windows 2000 - { 5, 1, 2600}, // Windows XP - { 5, 2, 3790}, // Windows XP x64 Edition or Windows Server 2003 - { 6, 0, 6000}, // Windows Vista - { 6, 0, 6001}, // Windows Vista with Service Pack 1 or Windows Server 2008 - { 6, 0, 6002}, // Windows Vista with Service Pack 2 - { 6, 1, 7600}, // Windows 7 or Windows Server 2008 R2 - { 6, 1, 7601}, // Windows 7 with Service Pack 1 or Windows Server 2008 R2 with Service Pack 1 - { 6, 2, 9200}, // Windows 8 or Windows Server 2012 - { 6, 3, 9200}, // Windows 8.1 or Windows Server 2012 R2 - { 6, 3, 9600}, // Windows 8.1 with Update 1 - {10, 0, 10240}, // Windows 10 Version 1507 (TH1) - {10, 0, 10586}, // Windows 10 Version 1511 (November Update) (TH2) - {10, 0, 14393}, // Windows 10 Version 1607 (Anniversary Update) (RS1) or Windows Server 2016 - {10, 0, 15063}, // Windows 10 Version 1703 (Creators Update) (RS2) - {10, 0, 16299}, // Windows 10 Version 1709 (Fall Creators Update) (RS3) - {10, 0, 17134}, // Windows 10 Version 1803 (April 2018 Update) (RS4) - {10, 0, 17763}, // Windows 10 Version 1809 (October 2018 Update) (RS5) or Windows Server 2019 - {10, 0, 18362}, // Windows 10 Version 1903 (May 2019 Update) (19H1) - {10, 0, 18363}, // Windows 10 Version 1909 (November 2019 Update) (19H2) - {10, 0, 19041}, // Windows 10 Version 2004 (May 2020 Update) (20H1) - {10, 0, 19042}, // Windows 10 Version 20H2 (October 2020 Update) (20H2) - {10, 0, 19043}, // Windows 10 Version 21H1 (May 2021 Update) (21H1) - {10, 0, 19044}, // Windows 10 Version 21H2 (November 2021 Update) (21H2) - {10, 0, 19045}, // Windows 10 Version 22H2 (October 2022 Update) (22H2) - {10, 0, 22000}, // Windows 11 Version 21H2 (21H2) - {10, 0, 22621} // Windows 11 Version 22H2 (October 2022 Update) (22H2) -}; -#endif // Q_OS_WINDOWS - } // namespace Global namespace FramelessHelper::Core @@ -534,7 +442,6 @@ FRAMELESSHELPER_END_NAMESPACE #ifndef QT_NO_DEBUG_STREAM QT_BEGIN_NAMESPACE -FRAMELESSHELPER_CORE_API QDebug operator<<(QDebug, const FRAMELESSHELPER_PREPEND_NAMESPACE(Global)::VersionNumber &); FRAMELESSHELPER_CORE_API QDebug operator<<(QDebug, const FRAMELESSHELPER_PREPEND_NAMESPACE(Global)::VersionInfo &); FRAMELESSHELPER_CORE_API QDebug operator<<(QDebug, const FRAMELESSHELPER_PREPEND_NAMESPACE(Global)::Dpi &); QT_END_NAMESPACE diff --git a/include/FramelessHelper/Core/private/scopeguard_p.h b/include/FramelessHelper/Core/private/scopeguard_p.h new file mode 100644 index 0000000..5591436 --- /dev/null +++ b/include/FramelessHelper/Core/private/scopeguard_p.h @@ -0,0 +1,101 @@ +/* + * MIT License + * + * Copyright (C) 2021-2023 by wangwenx190 (Yuhang Zhao) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +#include + +#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) +# include +#else // (QT_VERSION < QT_VERSION_CHECK(5, 12, 0)) + +FRAMELESSHELPER_BEGIN_NAMESPACE + +using ScopeGuardCallback = std::function; + +class [[nodiscard]] ScopeGuard +{ + Q_DISABLE_COPY(ScopeGuard) + +public: + ScopeGuard() + { + } + + explicit ScopeGuard(const ScopeGuardCallback &cb) + { + // For the ctor, we require a non-null function. + Q_ASSERT(cb); + if (!cb) { + return; + } + m_callback = cb; + } + + explicit ScopeGuard(ScopeGuardCallback &&cb) + { + // For the ctor, we require a non-null function. + Q_ASSERT(cb); + if (!cb) { + return; + } + m_callback = std::move(cb); + } + + ScopeGuard(ScopeGuard &&other) : m_callback(std::move(other.m_callback)) + { + } + + ~ScopeGuard() + { + if (m_callback) { + m_callback(); + } + } + + [[nodiscard]] ScopeGuardCallback callback() const + { + return m_callback; + } + + void setCallback(const ScopeGuardCallback &cb) + { + // But we allow null functions here. + m_callback = cb; + } + +private: + ScopeGuardCallback m_callback = nullptr; +}; + +[[nodiscard]] inline ScopeGuard make_guard(ScopeGuardCallback &&cb) +{ + return ScopeGuard(cb); +} + +#define qScopeGuard make_guard + +FRAMELESSHELPER_END_NAMESPACE + +#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) diff --git a/include/FramelessHelper/Core/private/versionnumber_p.h b/include/FramelessHelper/Core/private/versionnumber_p.h new file mode 100644 index 0000000..a6509b0 --- /dev/null +++ b/include/FramelessHelper/Core/private/versionnumber_p.h @@ -0,0 +1,129 @@ +/* + * MIT License + * + * Copyright (C) 2021-2023 by wangwenx190 (Yuhang Zhao) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +#include + +FRAMELESSHELPER_BEGIN_NAMESPACE + +struct VersionNumber +{ + int Major = 0; + int Minor = 0; + int Patch = 0; + int Tweak = 0; + + [[nodiscard]] friend constexpr bool operator==(const VersionNumber &lhs, const VersionNumber &rhs) noexcept + { + return ((lhs.Major == rhs.Major) && (lhs.Minor == rhs.Minor) && (lhs.Patch == rhs.Patch) && (lhs.Tweak == rhs.Tweak)); + } + + [[nodiscard]] friend constexpr bool operator!=(const VersionNumber &lhs, const VersionNumber &rhs) noexcept + { + return !operator==(lhs, rhs); + } + + [[nodiscard]] friend constexpr bool operator>(const VersionNumber &lhs, const VersionNumber &rhs) noexcept + { + if (operator==(lhs, rhs)) { + return false; + } + if (lhs.Major > rhs.Major) { + return true; + } + if (lhs.Major < rhs.Major) { + return false; + } + if (lhs.Minor > rhs.Minor) { + return true; + } + if (lhs.Minor < rhs.Minor) { + return false; + } + if (lhs.Patch > rhs.Patch) { + return true; + } + if (lhs.Patch < rhs.Patch) { + return false; + } + return (lhs.Tweak > rhs.Tweak); + } + + [[nodiscard]] friend constexpr bool operator<(const VersionNumber &lhs, const VersionNumber &rhs) noexcept + { + return (operator!=(lhs, rhs) && !operator>(lhs, rhs)); + } + + [[nodiscard]] friend constexpr bool operator>=(const VersionNumber &lhs, const VersionNumber &rhs) noexcept + { + return (operator>(lhs, rhs) || operator==(lhs, rhs)); + } + + [[nodiscard]] friend constexpr bool operator<=(const VersionNumber &lhs, const VersionNumber &rhs) noexcept + { + return (operator<(lhs, rhs) || operator==(lhs, rhs)); + } +}; + +#ifdef Q_OS_WINDOWS +[[maybe_unused]] inline constexpr const VersionNumber WindowsVersions[] = +{ + { 5, 0, 2195}, // Windows 2000 + { 5, 1, 2600}, // Windows XP + { 5, 2, 3790}, // Windows XP x64 Edition or Windows Server 2003 + { 6, 0, 6000}, // Windows Vista + { 6, 0, 6001}, // Windows Vista with Service Pack 1 or Windows Server 2008 + { 6, 0, 6002}, // Windows Vista with Service Pack 2 + { 6, 1, 7600}, // Windows 7 or Windows Server 2008 R2 + { 6, 1, 7601}, // Windows 7 with Service Pack 1 or Windows Server 2008 R2 with Service Pack 1 + { 6, 2, 9200}, // Windows 8 or Windows Server 2012 + { 6, 3, 9200}, // Windows 8.1 or Windows Server 2012 R2 + { 6, 3, 9600}, // Windows 8.1 with Update 1 + {10, 0, 10240}, // Windows 10 Version 1507 (TH1) + {10, 0, 10586}, // Windows 10 Version 1511 (November Update) (TH2) + {10, 0, 14393}, // Windows 10 Version 1607 (Anniversary Update) (RS1) or Windows Server 2016 + {10, 0, 15063}, // Windows 10 Version 1703 (Creators Update) (RS2) + {10, 0, 16299}, // Windows 10 Version 1709 (Fall Creators Update) (RS3) + {10, 0, 17134}, // Windows 10 Version 1803 (April 2018 Update) (RS4) + {10, 0, 17763}, // Windows 10 Version 1809 (October 2018 Update) (RS5) or Windows Server 2019 + {10, 0, 18362}, // Windows 10 Version 1903 (May 2019 Update) (19H1) + {10, 0, 18363}, // Windows 10 Version 1909 (November 2019 Update) (19H2) + {10, 0, 19041}, // Windows 10 Version 2004 (May 2020 Update) (20H1) + {10, 0, 19042}, // Windows 10 Version 20H2 (October 2020 Update) (20H2) + {10, 0, 19043}, // Windows 10 Version 21H1 (May 2021 Update) (21H1) + {10, 0, 19044}, // Windows 10 Version 21H2 (November 2021 Update) (21H2) + {10, 0, 19045}, // Windows 10 Version 22H2 (October 2022 Update) (22H2) + {10, 0, 22000}, // Windows 11 Version 21H2 (21H2) + {10, 0, 22621} // Windows 11 Version 22H2 (October 2022 Update) (22H2) +}; +#endif // Q_OS_WINDOWS + +FRAMELESSHELPER_END_NAMESPACE + +#ifndef QT_NO_DEBUG_STREAM +QT_BEGIN_NAMESPACE +FRAMELESSHELPER_CORE_API QDebug operator<<(QDebug, const FRAMELESSHELPER_PREPEND_NAMESPACE(VersionNumber) &); +QT_END_NAMESPACE +#endif // QT_NO_DEBUG_STREAM diff --git a/qmake/core.pri b/qmake/core.pri index 7518dbb..34245dd 100644 --- a/qmake/core.pri +++ b/qmake/core.pri @@ -39,7 +39,9 @@ HEADERS += \ $$CORE_PRIV_INC_DIR/micamaterial_p.h \ $$CORE_PRIV_INC_DIR/sysapiloader_p.h \ $$CORE_PRIV_INC_DIR/windowborderpainter_p.h \ - $$CORE_PRIV_INC_DIR/framelesshelpercore_global_p.h + $$CORE_PRIV_INC_DIR/framelesshelpercore_global_p.h \ + $$CORE_PRIV_INC_DIR/versionnumber_p.h \ + $$CORE_PRIV_INC_DIR/scopeguard_p.h SOURCES += \ $$CORE_SRC_DIR/chromepalette.cpp \ diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 0932ca2..cb46041 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -84,6 +84,8 @@ set(PRIVATE_HEADERS ${INCLUDE_PREFIX}/private/micamaterial_p.h ${INCLUDE_PREFIX}/private/windowborderpainter_p.h ${INCLUDE_PREFIX}/private/framelesshelpercore_global_p.h + ${INCLUDE_PREFIX}/private/versionnumber_p.h + ${INCLUDE_PREFIX}/private/scopeguard_p.h ) set(SOURCES diff --git a/src/core/framelesshelpercore_global.cpp b/src/core/framelesshelpercore_global.cpp index 7478eb0..ef179b1 100644 --- a/src/core/framelesshelpercore_global.cpp +++ b/src/core/framelesshelpercore_global.cpp @@ -24,6 +24,7 @@ #include "framelesshelpercore_global.h" #include "framelesshelpercore_global_p.h" +#include "versionnumber_p.h" #include "utils.h" #include #include @@ -56,14 +57,14 @@ #ifndef QT_NO_DEBUG_STREAM QT_BEGIN_NAMESPACE -QDebug operator<<(QDebug d, const FRAMELESSHELPER_PREPEND_NAMESPACE(Global)::VersionNumber &ver) +QDebug operator<<(QDebug d, const FRAMELESSHELPER_PREPEND_NAMESPACE(VersionNumber) &ver) { const QDebugStateSaver saver(d); d.nospace().noquote() << "VersionNumber(" - << ver.major << ", " - << ver.minor << ", " - << ver.patch << ", " - << ver.tweak << ')'; + << ver.Major << ", " + << ver.Minor << ", " + << ver.Patch << ", " + << ver.Tweak << ')'; return d; } @@ -72,7 +73,7 @@ QDebug operator<<(QDebug d, const FRAMELESSHELPER_PREPEND_NAMESPACE(Global)::Ver const QDebugStateSaver saver(d); int major = 0, minor = 0, patch = 0, tweak = 0; FRAMELESSHELPER_EXTRACT_VERSION(ver.version, major, minor, patch, tweak) - const auto ver_num = FRAMELESSHELPER_PREPEND_NAMESPACE(Global)::VersionNumber{major, minor, patch, tweak}; + const auto ver_num = FRAMELESSHELPER_PREPEND_NAMESPACE(VersionNumber){major, minor, patch, tweak}; d.nospace().noquote() << "VersionInfo(" << "version number: " << ver_num << ", " << "version string: " << ver.version_str << ", " diff --git a/src/core/scopeguard_p.h b/src/core/scopeguard_p.h new file mode 100644 index 0000000..13bd8a3 --- /dev/null +++ b/src/core/scopeguard_p.h @@ -0,0 +1 @@ +#include "../../include/FramelessHelper/Core/private/scopeguard_p.h" diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index fef20ed..9661ec7 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -30,6 +30,8 @@ #include "registrykey_p.h" #include "winverhelper_p.h" #include "framelesshelpercore_global_p.h" +#include "versionnumber_p.h" +#include "scopeguard_p.h" #include #include #include @@ -283,9 +285,9 @@ struct SYSTEM_METRIC OSVERSIONINFOEXW osvi; SecureZeroMemory(&osvi, sizeof(osvi)); osvi.dwOSVersionInfoSize = sizeof(osvi); - osvi.dwMajorVersion = targetOsVer.major; - osvi.dwMinorVersion = targetOsVer.minor; - osvi.dwBuildNumber = targetOsVer.patch; + osvi.dwMajorVersion = targetOsVer.Major; + osvi.dwMinorVersion = targetOsVer.Minor; + osvi.dwBuildNumber = targetOsVer.Patch; DWORDLONG dwlConditionMask = 0; const auto op = VER_GREATER_EQUAL; VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, op); @@ -2361,20 +2363,12 @@ void Utils::bringWindowToFront(const WId windowId) return; } // And also don't forget to disconnect from it. - // Ideally we would want to use qScopeGuard to do this, but sadly it was introduced - // in Qt 5.12 and we still want to support some old Qt versions. - const struct __Cleanup { - const DWORD idAttach = 0; - const DWORD idAttachTo = 0; - __Cleanup(const DWORD attach, const DWORD attachTo) - : idAttach(attach), idAttachTo(attachTo) {} - ~__Cleanup() { - if (AttachThreadInput(idAttach, idAttachTo, FALSE) == FALSE) { - WARNING << getSystemErrorMessage(kAttachThreadInput); - } + volatile const auto cleanup = qScopeGuard([windowThreadProcessId, currentThreadId]() -> void { + if (AttachThreadInput(windowThreadProcessId, currentThreadId, FALSE) == FALSE) { + WARNING << getSystemErrorMessage(kAttachThreadInput); } - } __cleanup(windowThreadProcessId, currentThreadId); - Q_UNUSED(__cleanup); + }); + Q_UNUSED(cleanup); // Make our window be the first one in the Z order. if (BringWindowToTop(hwnd) == FALSE) { WARNING << getSystemErrorMessage(kBringWindowToTop); diff --git a/src/core/versionnumber_p.h b/src/core/versionnumber_p.h new file mode 100644 index 0000000..5281dd9 --- /dev/null +++ b/src/core/versionnumber_p.h @@ -0,0 +1 @@ +#include "../../include/FramelessHelper/Core/private/versionnumber_p.h"