Skip to content

Commit 6eb2efe

Browse files
committed
Add WinRegKey support
1 parent 58ef035 commit 6eb2efe

File tree

7 files changed

+111
-16
lines changed

7 files changed

+111
-16
lines changed

examples/shared/widgetframe/windowbar_p.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace QWK {
4545
}
4646

4747
private:
48-
Q_DISABLE_COPY_MOVE(WindowBarPrivate)
48+
Q_DISABLE_COPY(WindowBarPrivate)
4949
};
5050

5151
}

src/core/qwindowkit_windows.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,65 @@ namespace QWK {
2222

2323
}
2424

25+
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
26+
WindowsRegistryKey::WindowsRegistryKey(HKEY parentHandle, QStringView subKey,
27+
REGSAM permissions, REGSAM access) {
28+
if (::RegOpenKeyExW(parentHandle, reinterpret_cast<const wchar_t *>(subKey.utf16()), 0,
29+
permissions | access, &m_key) != ERROR_SUCCESS) {
30+
m_key = nullptr;
31+
}
32+
}
33+
34+
WindowsRegistryKey::~WindowsRegistryKey() {
35+
close();
36+
}
37+
38+
void WindowsRegistryKey::close() {
39+
if (isValid()) {
40+
::RegCloseKey(m_key);
41+
m_key = nullptr;
42+
}
43+
}
44+
45+
QString WindowsRegistryKey::stringValue(QStringView subKey) const {
46+
QString result;
47+
if (!isValid())
48+
return result;
49+
DWORD type;
50+
DWORD size;
51+
auto subKeyC = reinterpret_cast<const wchar_t *>(subKey.utf16());
52+
if (::RegQueryValueExW(m_key, subKeyC, nullptr, &type, nullptr, &size) != ERROR_SUCCESS ||
53+
(type != REG_SZ && type != REG_EXPAND_SZ) || size <= 2) {
54+
return result;
55+
}
56+
// Reserve more for rare cases where trailing '\0' are missing in registry.
57+
// Rely on 0-termination since strings of size 256 padded with 0 have been
58+
// observed (QTBUG-84455).
59+
size += 2;
60+
QVarLengthArray<unsigned char> buffer(static_cast<int>(size));
61+
std::fill(buffer.data(), buffer.data() + size, 0u);
62+
if (::RegQueryValueExW(m_key, subKeyC, nullptr, &type, buffer.data(), &size) ==
63+
ERROR_SUCCESS)
64+
result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(buffer.constData()));
65+
return result;
66+
}
67+
68+
QPair<DWORD, bool> WindowsRegistryKey::dwordValue(QStringView subKey) const {
69+
if (!isValid())
70+
return qMakePair(0, false);
71+
DWORD type;
72+
auto subKeyC = reinterpret_cast<const wchar_t *>(subKey.utf16());
73+
if (::RegQueryValueExW(m_key, subKeyC, nullptr, &type, nullptr, nullptr) != ERROR_SUCCESS ||
74+
type != REG_DWORD) {
75+
return qMakePair(0, false);
76+
}
77+
DWORD value = 0;
78+
DWORD size = sizeof(value);
79+
const bool ok =
80+
::RegQueryValueExW(m_key, subKeyC, nullptr, nullptr,
81+
reinterpret_cast<unsigned char *>(&value), &size) == ERROR_SUCCESS;
82+
return qMakePair(value, ok);
83+
}
84+
#endif
85+
2586
}

src/core/qwindowkit_windows.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include <QtCore/qt_windows.h>
55
#include <QtCore/qglobal.h>
66

7+
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
8+
# include <QtCore/private/qwinregistry_p.h>
9+
#endif
10+
711
#include <QWKCore/qwkglobal.h>
812

913
#ifndef GET_X_LPARAM
@@ -105,6 +109,36 @@ namespace QWK {
105109

106110
}
107111

112+
//
113+
// Registry Helpers
114+
//
115+
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
116+
class QWK_CORE_EXPORT WindowsRegistryKey {
117+
public:
118+
WindowsRegistryKey(HKEY parentHandle, QStringView subKey, REGSAM permissions = KEY_READ,
119+
REGSAM access = 0);
120+
121+
~WindowsRegistryKey();
122+
123+
inline bool isValid() const;
124+
125+
void close();
126+
QString stringValue(QStringView subKey) const;
127+
QPair<DWORD, bool> dwordValue(QStringView subKey) const;
128+
129+
private:
130+
HKEY m_key;
131+
132+
Q_DISABLE_COPY(WindowsRegistryKey)
133+
};
134+
135+
inline bool WindowsRegistryKey::isValid() const {
136+
return m_key != nullptr;
137+
}
138+
#else
139+
using WindowsRegistryKey = QWinRegistryKey;
140+
#endif
141+
108142
//
109143
// Version Helpers
110144
//

src/core/shared/qwkwindowsextra_p.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
#include <timeapi.h>
1616

1717
#include <QWKCore/qwindowkit_windows.h>
18-
1918
#include <QtCore/private/qsystemlibrary_p.h>
20-
#include <QtCore/private/qwinregistry_p.h>
2119

2220
#include <QtGui/QGuiApplication>
2321
#include <QtGui/QStyleHints>
@@ -132,12 +130,14 @@ namespace QWK {
132130
};
133131
using PWINDOWCOMPOSITIONATTRIBDATA = WINDOWCOMPOSITIONATTRIBDATA *;
134132

135-
enum PREFERRED_APP_MODE
136-
{
137-
PAM_DEFAULT = 0, // Default behavior on systems before Win10 1809. It indicates the application doesn't support dark mode at all.
138-
PAM_AUTO = 1, // Available since Win10 1809, let system decide whether to enable dark mode or not.
139-
PAM_DARK = 2, // Available since Win10 1903, force dark mode regardless of the system theme.
140-
PAM_LIGHT = 3, // Available since Win10 1903, force light mode regardless of the system theme.
133+
enum PREFERRED_APP_MODE {
134+
PAM_DEFAULT = 0, // Default behavior on systems before Win10 1809. It indicates the
135+
// application doesn't support dark mode at all.
136+
PAM_AUTO =
137+
1, // Available since Win10 1809, let system decide whether to enable dark mode or not.
138+
PAM_DARK = 2, // Available since Win10 1903, force dark mode regardless of the system theme.
139+
PAM_LIGHT =
140+
3, // Available since Win10 1903, force light mode regardless of the system theme.
141141
PAM_MAX = 4
142142
};
143143

@@ -230,7 +230,7 @@ namespace QWK {
230230

231231
~DynamicApis() = default;
232232

233-
Q_DISABLE_COPY_MOVE(DynamicApis)
233+
Q_DISABLE_COPY(DynamicApis)
234234
};
235235

236236
}
@@ -337,7 +337,7 @@ namespace QWK {
337337
}
338338

339339
static inline bool isWindowFrameBorderColorized() {
340-
QWinRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)");
340+
WindowsRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)");
341341
if (!registry.isValid()) {
342342
return false;
343343
}
@@ -359,7 +359,7 @@ namespace QWK {
359359
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
360360
return QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark;
361361
#else
362-
QWinRegistryKey registry(
362+
WindowsRegistryKey registry(
363363
HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)");
364364
if (!registry.isValid()) {
365365
return false;
@@ -390,7 +390,7 @@ namespace QWK {
390390
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
391391
return QGuiApplication::palette().color(QPalette::Accent);
392392
#else
393-
QWinRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)");
393+
WindowsRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)");
394394
if (!registry.isValid()) {
395395
return {};
396396
}

src/core/windowagentbase_p.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace QWK {
3737
static WindowContextFactoryMethod windowContextFactoryMethod;
3838

3939
private:
40-
Q_DISABLE_COPY_MOVE(WindowAgentBasePrivate)
40+
Q_DISABLE_COPY(WindowAgentBasePrivate)
4141
};
4242

4343
}

src/quick/quickwindowagent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace QWK {
3131
Q_INVOKABLE void setHitTestVisible(const QQuickItem *item, bool visible = true);
3232

3333
#ifdef Q_OS_MAC
34-
// The system button area APIs are experimental, may be changed in the future.
34+
// The system button area APIs are experimental, very likely to change in the future.
3535
Q_INVOKABLE QQuickItem *systemButtonArea() const;
3636
Q_INVOKABLE void setSystemButtonArea(QQuickItem *item);
3737

src/widgets/widgetwindowagent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace QWK {
2727
void setSystemButton(SystemButton button, QWidget *w);
2828

2929
#ifdef Q_OS_MAC
30-
// The system button area APIs are experimental, may be changed in the future.
30+
// The system button area APIs are experimental, very likely to change in the future.
3131
QWidget *systemButtonArea() const;
3232
void setSystemButtonArea(QWidget *widget);
3333

0 commit comments

Comments
 (0)