From a49756ee456c78f0c561877a3cb737c528adf70e Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Wed, 17 Aug 2022 10:42:48 +0800 Subject: [PATCH] registrykey: rename RegistryKey is a better name than Registry. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- .../private/{registry_p.h => registrykey_p.h} | 13 +++-- src/core/CMakeLists.txt | 4 +- src/core/global.cpp | 4 +- src/core/registry_p.h | 1 - src/core/{registry.cpp => registrykey.cpp} | 48 +++++++++++-------- src/core/registrykey_p.h | 1 + src/core/utils_win.cpp | 21 ++++---- 7 files changed, 51 insertions(+), 41 deletions(-) rename include/FramelessHelper/Core/private/{registry_p.h => registrykey_p.h} (83%) delete mode 100644 src/core/registry_p.h rename src/core/{registry.cpp => registrykey.cpp} (73%) create mode 100644 src/core/registrykey_p.h diff --git a/include/FramelessHelper/Core/private/registry_p.h b/include/FramelessHelper/Core/private/registrykey_p.h similarity index 83% rename from include/FramelessHelper/Core/private/registry_p.h rename to include/FramelessHelper/Core/private/registrykey_p.h index a445d85..4743655 100644 --- a/include/FramelessHelper/Core/private/registry_p.h +++ b/include/FramelessHelper/Core/private/registrykey_p.h @@ -34,14 +34,14 @@ QT_END_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE -class FRAMELESSHELPER_CORE_API Registry : public QObject +class FRAMELESSHELPER_CORE_API RegistryKey : public QObject { Q_OBJECT - Q_DISABLE_COPY_MOVE(Registry) + Q_DISABLE_COPY_MOVE(RegistryKey) public: - explicit Registry(const Global::RegistryRootKey root, const QString &key, QObject *parent = nullptr); - ~Registry() override; + explicit RegistryKey(const Global::RegistryRootKey root, const QString &key, QObject *parent = nullptr); + ~RegistryKey() override; Q_NODISCARD Global::RegistryRootKey rootKey() const; Q_NODISCARD QString subKey() const; @@ -54,13 +54,12 @@ private: Global::RegistryRootKey m_rootKey = Global::RegistryRootKey::CurrentUser; QString m_subKey = {}; #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScopedPointer m_registry; + QScopedPointer m_registryKey; #else QScopedPointer m_settings; - bool m_valid = false; #endif }; FRAMELESSHELPER_END_NAMESPACE -Q_DECLARE_METATYPE2(FRAMELESSHELPER_PREPEND_NAMESPACE(Registry)) +Q_DECLARE_METATYPE2(FRAMELESSHELPER_PREPEND_NAMESPACE(RegistryKey)) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 2e021e4..87f8f3d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -86,10 +86,10 @@ if(WIN32) ${INCLUDE_PREFIX}/FramelessHelper_Win ) list(APPEND PRIVATE_HEADERS - ${INCLUDE_PREFIX}/private/registry_p.h + ${INCLUDE_PREFIX}/private/registrykey_p.h ) list(APPEND SOURCES - registry.cpp + registrykey.cpp utils_win.cpp framelesshelper_win.cpp ) diff --git a/src/core/global.cpp b/src/core/global.cpp index 3a1c735..871af57 100644 --- a/src/core/global.cpp +++ b/src/core/global.cpp @@ -37,7 +37,7 @@ #include "chromepalette_p.h" #include "micamaterial_p.h" #ifdef Q_OS_WINDOWS -# include "registry_p.h" +# include "registrykey_p.h" #endif #include @@ -176,7 +176,7 @@ void initialize() qRegisterMetaType(); qRegisterMetaType(); # ifdef Q_OS_WINDOWS - qRegisterMetaType(); + qRegisterMetaType(); # endif #endif } diff --git a/src/core/registry_p.h b/src/core/registry_p.h deleted file mode 100644 index 5a51446..0000000 --- a/src/core/registry_p.h +++ /dev/null @@ -1 +0,0 @@ -#include "../../include/FramelessHelper/Core/private/registry_p.h" diff --git a/src/core/registry.cpp b/src/core/registrykey.cpp similarity index 73% rename from src/core/registry.cpp rename to src/core/registrykey.cpp index bb848bc..9b4c110 100644 --- a/src/core/registry.cpp +++ b/src/core/registrykey.cpp @@ -22,7 +22,7 @@ * SOFTWARE. */ -#include "registry_p.h" +#include "registrykey_p.h" #include #include #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) @@ -33,11 +33,11 @@ FRAMELESSHELPER_BEGIN_NAMESPACE -Q_LOGGING_CATEGORY(lcCoreRegistry, "wangwenx190.framelesshelper.core.registry") -#define INFO qCInfo(lcCoreRegistry) -#define DEBUG qCDebug(lcCoreRegistry) -#define WARNING qCWarning(lcCoreRegistry) -#define CRITICAL qCCritical(lcCoreRegistry) +Q_LOGGING_CATEGORY(lcCoreRegistryKey, "wangwenx190.framelesshelper.core.registrykey") +#define INFO qCInfo(lcCoreRegistryKey) +#define DEBUG qCDebug(lcCoreRegistryKey) +#define WARNING qCWarning(lcCoreRegistryKey) +#define CRITICAL qCCritical(lcCoreRegistryKey) using namespace Global; @@ -69,7 +69,7 @@ static const QString g_strMap[] = { }; static_assert(std::size(g_strMap) == std::size(g_keyMap)); -Registry::Registry(const RegistryRootKey root, const QString &key, QObject *parent) : QObject(parent) +RegistryKey::RegistryKey(const RegistryRootKey root, const QString &key, QObject *parent) : QObject(parent) { Q_ASSERT(!key.isEmpty()); if (key.isEmpty()) { @@ -78,39 +78,43 @@ Registry::Registry(const RegistryRootKey root, const QString &key, QObject *pare m_rootKey = root; m_subKey = key; #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - m_registry.reset(new QWinRegistryKey(g_keyMap[static_cast(m_rootKey)], m_subKey)); + m_registryKey.reset(new QWinRegistryKey(g_keyMap[static_cast(m_rootKey)], m_subKey)); + if (!m_registryKey->isValid()) { + m_registryKey.reset(); + } #else const QString rootKey = g_strMap[static_cast(m_rootKey)]; m_settings.reset(new QSettings(rootKey, QSettings::NativeFormat)); if (m_settings->contains(m_subKey)) { m_settings.reset(new QSettings(rootKey + u'\\' + m_subKey, QSettings::NativeFormat)); - m_valid = true; + } else { + m_settings.reset(); } #endif } -Registry::~Registry() = default; +RegistryKey::~RegistryKey() = default; -RegistryRootKey Registry::rootKey() const +RegistryRootKey RegistryKey::rootKey() const { return m_rootKey; } -QString Registry::subKey() const +QString RegistryKey::subKey() const { return m_subKey; } -bool Registry::isValid() const +bool RegistryKey::isValid() const { #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - return m_registry->isValid(); + return (!m_registryKey.isNull() && m_registryKey->isValid()); #else - return m_valid; + return !m_settings.isNull(); #endif } -QVariant Registry::value(const QString &name) const +QVariant RegistryKey::value(const QString &name) const { Q_ASSERT(!name.isEmpty()); Q_ASSERT(isValid()); @@ -118,11 +122,15 @@ QVariant Registry::value(const QString &name) const return {}; } #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - const QPair dwValue = m_registry->dwordValue(name); - if (dwValue.second) { - return qulonglong(dwValue.first); + const QPair dwVal = m_registryKey->dwordValue(name); + if (dwVal.second) { + return qulonglong(dwVal.first); } - return m_registry->stringValue(name); + const QString strVal = m_registryKey->stringValue(name); + if (!strVal.isEmpty()) { + return strVal; + } + return {}; #else return m_settings->value(name); #endif diff --git a/src/core/registrykey_p.h b/src/core/registrykey_p.h new file mode 100644 index 0000000..066f098 --- /dev/null +++ b/src/core/registrykey_p.h @@ -0,0 +1 @@ +#include "../../include/FramelessHelper/Core/private/registrykey_p.h" diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 9f801c6..1f6db62 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -41,7 +41,7 @@ #include "framelesshelper_windows.h" #include "framelessconfig_p.h" #include "sysapiloader_p.h" -#include "registry_p.h" +#include "registrykey_p.h" #include #include @@ -375,7 +375,7 @@ private: if (code == ERROR_SUCCESS) { return kSuccessMessageText; } -#if 0 +#if 0 // The following code works well, we commented it out just because we want to use as many Qt functionalities as possible. LPWSTR buf = nullptr; if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast(&buf), 0, nullptr) == 0) { @@ -607,7 +607,7 @@ bool Utils::isDwmCompositionEnabled() return true; } const auto resultFromRegistry = []() -> bool { - const Registry registry(RegistryRootKey::CurrentUser, dwmRegistryKey()); + const RegistryKey registry(RegistryRootKey::CurrentUser, dwmRegistryKey()); if (!registry.isValid()) { return false; } @@ -733,7 +733,7 @@ QString Utils::getSystemErrorMessage(const QString &function) QColor Utils::getDwmColorizationColor() { const auto resultFromRegistry = []() -> QColor { - const Registry registry(RegistryRootKey::CurrentUser, dwmRegistryKey()); + const RegistryKey registry(RegistryRootKey::CurrentUser, dwmRegistryKey()); if (!registry.isValid()) { return kDefaultDarkGrayColor; } @@ -761,10 +761,10 @@ DwmColorizationArea Utils::getDwmColorizationArea() if (!isWin10OrGreater) { return DwmColorizationArea::None_; } - const Registry themeRegistry(RegistryRootKey::CurrentUser, personalizeRegistryKey()); + const RegistryKey themeRegistry(RegistryRootKey::CurrentUser, personalizeRegistryKey()); bool themeOk = false; const DWORD themeValue = themeRegistry.isValid() ? themeRegistry.value(qDwmColorKeyName).toULongLong(&themeOk) : 0; - const Registry dwmRegistry(RegistryRootKey::CurrentUser, dwmRegistryKey()); + const RegistryKey dwmRegistry(RegistryRootKey::CurrentUser, dwmRegistryKey()); bool dwmOk = false; const DWORD dwmValue = dwmRegistry.isValid() ? dwmRegistry.value(qDwmColorKeyName).toULongLong(&dwmOk) : 0; const bool theme = (themeOk && (themeValue != 0)); @@ -1573,9 +1573,12 @@ bool Utils::shouldAppsUseDarkMode_windows() } else { WARNING << "Failed to retrieve the platform native interface."; } +#else + // Qt gained the ability to detect the system dark mode setting only since 5.15. + // We should detect it ourself on versions below that. #endif const auto resultFromRegistry = []() -> bool { - const Registry registry(RegistryRootKey::CurrentUser, personalizeRegistryKey()); + const RegistryKey registry(RegistryRootKey::CurrentUser, personalizeRegistryKey()); if (!registry.isValid()) { return false; } @@ -1809,7 +1812,7 @@ QColor Utils::getDwmAccentColor() // so we'd better also do the same thing. // There's no Windows API to get this value, so we can only read it // directly from the registry. - const Registry registry(RegistryRootKey::CurrentUser, dwmRegistryKey()); + const RegistryKey registry(RegistryRootKey::CurrentUser, dwmRegistryKey()); if (!registry.isValid()) { return kDefaultDarkGrayColor; } @@ -1837,7 +1840,7 @@ QString Utils::getWallpaperFilePath() WallpaperAspectStyle Utils::getWallpaperAspectStyle() { static constexpr const auto defaultStyle = WallpaperAspectStyle::Fill; - const Registry registry(RegistryRootKey::CurrentUser, desktopRegistryKey()); + const RegistryKey registry(RegistryRootKey::CurrentUser, desktopRegistryKey()); if (!registry.isValid()) { return defaultStyle; }