1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qgnometheme_p.h"
#include <qpa/qplatformdialoghelper.h>
#include <qpa/qplatformfontdatabase.h>
#if QT_CONFIG(dbus)
#include "qdbuslistener_p.h"
#include <private/qdbustrayicon_p.h>
#include <private/qdbustrayicon_p.h>
#include <private/qdbusplatformmenu_p.h>
#include <private/qdbusmenubar_p.h>
#include <QtDBus/QDBusMessage>
#include <QtDBus/QDBusPendingCall>
#include <QtDBus/QDBusReply>
#endif
#include <qpa/qwindowsysteminterface.h>
QT_BEGIN_NAMESPACE
#if QT_CONFIG(dbus)
Q_STATIC_LOGGING_CATEGORY(lcQpaThemeGnome, "qt.qpa.theme.gnome")
#endif
/*!
\class QGnomeTheme
\brief QGnomeTheme is a theme implementation for the Gnome desktop.
\since 5.0
\internal
\ingroup qpa
*/
const char *QGnomeTheme::name = "gnome";
QGnomeThemePrivate::QGnomeThemePrivate()
{
#if QT_CONFIG(dbus)
static constexpr QLatin1String appearanceNamespace("org.freedesktop.appearance");
static constexpr QLatin1String colorSchemeKey("color-scheme");
static constexpr QLatin1String contrastKey("contrast");
QDBusConnection dbus = QDBusConnection::sessionBus();
if (dbus.isConnected()) {
// ReadAll appears to omit the contrast setting on Ubuntu.
QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"),
QLatin1String("/org/freedesktop/portal/desktop"),
QLatin1String("org.freedesktop.portal.Settings"),
QLatin1String("ReadOne"));
message << appearanceNamespace << colorSchemeKey;
QDBusReply<QVariant> reply = dbus.call(message);
if (Q_LIKELY(reply.isValid())) {
uint xdgColorSchemeValue = reply.value().toUInt();
switch (xdgColorSchemeValue) {
case 1:
m_colorScheme = Qt::ColorScheme::Dark;
QWindowSystemInterface::handleThemeChange();
break;
case 2:
m_colorScheme = Qt::ColorScheme::Light;
QWindowSystemInterface::handleThemeChange();
break;
default:
break;
}
}
message.setArguments({});
message << appearanceNamespace << contrastKey;
pendingCallWatcher = std::make_unique<QDBusPendingCallWatcher>(dbus.asyncCall(message));
QObject::connect(pendingCallWatcher.get(), &QDBusPendingCallWatcher::finished, pendingCallWatcher.get(), [this](QDBusPendingCallWatcher *watcher) {
if (!watcher->isError()) {
QDBusPendingReply<QVariant> reply = *watcher;
if (Q_LIKELY(reply.isValid()))
updateHighContrast(static_cast<Qt::ContrastPreference>(reply.value().toUInt()));
}
initDbus();
});
} else {
qCWarning(lcQpaThemeGnome) << "dbus connection failed. Last error: " << dbus.lastError();
}
#endif // QT_CONFIG(dbus)
}
QGnomeThemePrivate::~QGnomeThemePrivate()
{
if (systemFont)
delete systemFont;
if (fixedFont)
delete fixedFont;
}
void QGnomeThemePrivate::configureFonts(const QString >kFontName) const
{
Q_ASSERT(!systemFont);
const int split = gtkFontName.lastIndexOf(QChar::Space);
float size = QStringView{gtkFontName}.mid(split + 1).toFloat();
QString fontName = gtkFontName.left(split);
systemFont = new QFont(fontName, size);
fixedFont = new QFont(QLatin1StringView(QGenericUnixTheme::defaultFixedFontNameC), systemFont->pointSize());
fixedFont->setStyleHint(QFont::TypeWriter);
qCDebug(lcQpaFonts) << "default fonts: system" << systemFont << "fixed" << fixedFont;
}
#if QT_CONFIG(dbus)
bool QGnomeThemePrivate::initDbus()
{
dbus.reset(new QDBusListener());
Q_ASSERT(dbus);
// Wrap slot in a lambda to avoid inheriting QGnomeThemePrivate from QObject
auto wrapper = [this](QDBusListener::Provider provider,
QDBusListener::Setting setting,
const QVariant &value) {
if (provider != QDBusListener::Provider::Gnome
&& provider != QDBusListener::Provider::Gtk) {
return;
}
switch (setting) {
case QDBusListener::Setting::Theme:
updateColorScheme(value.toString());
break;
case QDBusListener::Setting::Contrast:
updateHighContrast(static_cast<Qt::ContrastPreference>(value.toUInt()));
break;
default:
break;
}
};
return QObject::connect(dbus.get(), &QDBusListener::settingChanged, dbus.get(), wrapper);
}
void QGnomeThemePrivate::updateColorScheme(const QString &themeName)
{
const auto oldColorScheme = m_colorScheme;
if (themeName.contains(QLatin1StringView("light"), Qt::CaseInsensitive)) {
m_colorScheme = Qt::ColorScheme::Light;
} else if (themeName.contains(QLatin1StringView("dark"), Qt::CaseInsensitive)) {
m_colorScheme = Qt::ColorScheme::Dark;
} else {
m_colorScheme = Qt::ColorScheme::Unknown;
}
if (oldColorScheme != m_colorScheme)
QWindowSystemInterface::handleThemeChange();
}
void QGnomeThemePrivate::updateHighContrast(Qt::ContrastPreference contrast)
{
if (m_contrast == contrast)
return;
m_contrast = contrast;
QWindowSystemInterface::handleThemeChange();
}
#endif // QT_CONFIG(dbus)
QGnomeTheme::QGnomeTheme()
: QGenericUnixTheme(new QGnomeThemePrivate())
{
}
QVariant QGnomeTheme::themeHint(QPlatformTheme::ThemeHint hint) const
{
switch (hint) {
case QPlatformTheme::DialogButtonBoxButtonsHaveIcons:
return QVariant(true);
case QPlatformTheme::DialogButtonBoxLayout:
return QVariant(QPlatformDialogHelper::GnomeLayout);
case QPlatformTheme::SystemIconThemeName:
return QVariant(QStringLiteral("Adwaita"));
case QPlatformTheme::SystemIconFallbackThemeName:
return QVariant(QStringLiteral("gnome"));
case QPlatformTheme::IconThemeSearchPaths:
return QVariant(xdgIconThemePaths());
case QPlatformTheme::IconPixmapSizes:
return QVariant::fromValue(availableXdgFileIconSizes());
case QPlatformTheme::StyleNames: {
QStringList styleNames;
styleNames << QStringLiteral("Fusion") << QStringLiteral("windows");
return QVariant(styleNames);
}
case QPlatformTheme::KeyboardScheme:
return QVariant(int(GnomeKeyboardScheme));
case QPlatformTheme::PasswordMaskCharacter:
return QVariant(QChar(0x2022));
case QPlatformTheme::UiEffects:
return QVariant(int(HoverEffect));
case QPlatformTheme::ButtonPressKeys:
return QVariant::fromValue(
QList<Qt::Key>({ Qt::Key_Space, Qt::Key_Return, Qt::Key_Enter, Qt::Key_Select }));
case QPlatformTheme::PreselectFirstFileInDirectory:
return true;
case QPlatformTheme::MouseCursorTheme:
return QVariant(mouseCursorTheme());
case QPlatformTheme::MouseCursorSize:
return QVariant(mouseCursorSize());
case QPlatformTheme::PreferFileIconFromTheme:
return true;
default:
break;
}
return QPlatformTheme::themeHint(hint);
}
QIcon QGnomeTheme::fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions) const
{
#if QT_CONFIG(mimetype)
return xdgFileIcon(fileInfo);
#else
Q_UNUSED(fileInfo);
return QIcon();
#endif
}
const QFont *QGnomeTheme::font(Font type) const
{
Q_D(const QGnomeTheme);
if (!d->systemFont)
d->configureFonts(gtkFontName());
switch (type) {
case QPlatformTheme::SystemFont:
return d->systemFont;
case QPlatformTheme::FixedFont:
return d->fixedFont;
default:
return nullptr;
}
}
QString QGnomeTheme::gtkFontName() const
{
return QStringLiteral("%1 %2").arg(QLatin1StringView(defaultSystemFontNameC))
.arg(defaultSystemFontSize);
}
#if QT_CONFIG(dbus)
QPlatformMenuBar *QGnomeTheme::createPlatformMenuBar() const
{
if (isDBusGlobalMenuAvailable())
return new QDBusMenuBar();
return nullptr;
}
Qt::ColorScheme QGnomeTheme::colorScheme() const
{
return d_func()->m_colorScheme;
}
Qt::ContrastPreference QGnomeTheme::contrastPreference() const
{
return d_func()->m_contrast;
}
#endif
#if QT_CONFIG(dbus) && QT_CONFIG(systemtrayicon)
QPlatformSystemTrayIcon *QGnomeTheme::createPlatformSystemTrayIcon() const
{
if (shouldUseDBusTray())
return new QDBusTrayIcon();
return nullptr;
}
#endif
QString QGnomeTheme::standardButtonText(int button) const
{
switch (button) {
case QPlatformDialogHelper::Ok:
return QCoreApplication::translate("QGnomeTheme", "&OK");
case QPlatformDialogHelper::Save:
return QCoreApplication::translate("QGnomeTheme", "&Save");
case QPlatformDialogHelper::Cancel:
return QCoreApplication::translate("QGnomeTheme", "&Cancel");
case QPlatformDialogHelper::Close:
return QCoreApplication::translate("QGnomeTheme", "&Close");
case QPlatformDialogHelper::Discard:
return QCoreApplication::translate("QGnomeTheme", "Close without Saving");
default:
break;
}
return QPlatformTheme::standardButtonText(button);
}
QT_END_NAMESPACE
|