Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwasmtheme.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qwasmtheme.h"
6#include <QtCore/qvariant.h>
7#include <QFontDatabase>
8#include <QList>
9#include <qloggingcategory.h>
10#include <qpa/qwindowsysteminterface.h>
11#include <qpalette.h>
12
13#include <emscripten.h>
14#include <emscripten/bind.h>
15#include <emscripten/val.h>
16
17Q_GUI_EXPORT QPalette qt_fusionPalette();
18
19QT_BEGIN_NAMESPACE
20
21Q_STATIC_LOGGING_CATEGORY(lcQpaThemeWasm, "qt.qpa.theme.wasm")
22
23namespace {
24bool matchMedia(std::string mediaQueryString)
25{
26 return emscripten::val::global("window")
27 .call<emscripten::val>("matchMedia", mediaQueryString)["matches"]
28 .as<bool>();
29}
30
32{
33 if (matchMedia(colorSchemePreferenceDark)) {
34 return Qt::ColorScheme::Dark;
35 } else {
36 return Qt::ColorScheme::Light;
37 }
38}
39
41{
42 if (matchMedia(contrastPreferenceMore)) {
43 return Qt::ContrastPreference::HighContrast;
44 } else {
45 return Qt::ContrastPreference::NoPreference;
46 }
47}
48} // namespace
49
50using namespace Qt::StringLiterals;
51
53{
54 m_colorScheme = getColorSchemeFromMedia();
55 qCDebug(lcQpaThemeWasm) << "Initializing Wasm theme. Color scheme: " << m_colorScheme;
56 m_contrastPreference = getContrastPreferenceFromMedia();
57 qCDebug(lcQpaThemeWasm) << "Initializing Wasm theme. Contrast preference: " << m_contrastPreference;
58
59 for (auto family : QFontDatabase::families())
60 if (QFontDatabase::isFixedPitch(family))
61 fixedFont = new QFont(family);
62
63 m_palette = std::make_unique<QPalette>();
64 m_paletteIsDirty = true; // Force update later
65
66 registerCallbacks(
67 { colorSchemePreferenceDark },
68 [this](emscripten::val) { QWasmTheme::onColorSchemeChange(); },
69 m_colorSchemeChangeCallback);
70 registerCallbacks(
71 { contrastPreferenceNoPreference, contrastPreferenceMore, contrastPreferenceLess,
72 contrastPreferenceCustom },
73 [this](emscripten::val) { QWasmTheme::onContrastPreferenceChange(); },
74 m_contrastPreferenceChangeCallbacks);
75}
76
78{
79 if (fixedFont)
80 delete fixedFont;
81}
82
83const QPalette *QWasmTheme::palette(Palette type) const
84{
85 if (type == SystemPalette) {
86 if (m_paletteIsDirty) {
87 m_paletteIsDirty = false;
88 *m_palette = qt_fusionPalette();
89 }
90 return m_palette.get();
91 }
92 return nullptr;
93}
94
96{
97 return m_colorScheme;
98}
99
100void QWasmTheme::requestColorScheme(Qt::ColorScheme scheme)
101{
102 if (m_colorScheme != scheme) {
103 m_paletteIsDirty = true;
104 m_colorScheme = scheme;
105 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
106 }
107}
108
110{
111 return m_contrastPreference;
112}
113
114QVariant QWasmTheme::themeHint(ThemeHint hint) const
115{
116 if (hint == QPlatformTheme::StyleNames)
117 return QVariant(QStringList() << "Fusion"_L1);
118 if (hint == QPlatformTheme::UiEffects)
119 return QVariant(int(HoverEffect));
120 return QPlatformTheme::themeHint(hint);
121}
122
123const QFont *QWasmTheme::font(Font type) const
124{
125 if (type == QPlatformTheme::FixedFont) {
126 return fixedFont;
127 }
128 return nullptr;
129}
130
131bool QWasmTheme::usePlatformNativeDialog(DialogType type) const
132{
133 return (type == DialogType::FileDialog);
134}
135
137{
138 if (type == DialogType::FileDialog)
139 return new QWasmFileDialogHelper();
140 return nullptr;
141}
142
144{
145 auto colorScheme = getColorSchemeFromMedia();
146 if (m_colorScheme != colorScheme) {
147 qCDebug(lcQpaThemeWasm) << "Color scheme changed to " << colorScheme;
148 m_colorScheme = colorScheme;
149 m_paletteIsDirty = true;
150 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
151 }
152}
153
155{
156 auto contrastPreference = getContrastPreferenceFromMedia();
157 if (m_contrastPreference != contrastPreference) {
158 qCDebug(lcQpaThemeWasm) << "Contrast preference changed to " << contrastPreference;
159 m_contrastPreference = contrastPreference;
160 m_paletteIsDirty = true;
161 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
162 }
163}
164
165
166QT_END_NAMESPACE
void onContrastPreferenceChange()
QPlatformDialogHelper * createPlatformDialogHelper(DialogType type) const override
Qt::ContrastPreference contrastPreference() const override
bool usePlatformNativeDialog(DialogType type) const override
const QPalette * palette(Palette type=SystemPalette) const override
Return a color palette for type type.
Qt::ColorScheme colorScheme() const override
void requestColorScheme(Qt::ColorScheme scheme) override
void onColorSchemeChange()
QVariant themeHint(ThemeHint hint) const override
const QFont * font(Font type) const override
Qt::ContrastPreference getContrastPreferenceFromMedia()
Qt::ColorScheme getColorSchemeFromMedia()
bool matchMedia(std::string mediaQueryString)
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)
QDebug Q_GUI_EXPORT & operator<<(QDebug &s, const QVectorPath &path)
constexpr auto contrastPreferenceMore
Definition qwasmtheme.h:27
constexpr auto colorSchemePreferenceDark
Definition qwasmtheme.h:25