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// Qt-Security score:significant reason:default
4
5#include "qwasmtheme.h"
8#include <QtCore/qvariant.h>
9#include <QFontDatabase>
10#include <QList>
11#include <qloggingcategory.h>
12#include <qpa/qwindowsysteminterface.h>
13#include <qpalette.h>
14
15#include <emscripten.h>
16#include <emscripten/bind.h>
17#include <emscripten/val.h>
18
19Q_GUI_EXPORT QPalette qt_fusionPalette();
20
21QT_BEGIN_NAMESPACE
22
23Q_STATIC_LOGGING_CATEGORY(lcQpaThemeWasm, "qt.qpa.theme.wasm")
24
25namespace {
26bool matchMedia(std::string mediaQueryString)
27{
28 return emscripten::val::global("window")
29 .call<emscripten::val>("matchMedia", mediaQueryString)["matches"]
30 .as<bool>();
31}
32
34{
35 if (matchMedia(colorSchemePreferenceDark)) {
36 return Qt::ColorScheme::Dark;
37 } else {
38 return Qt::ColorScheme::Light;
39 }
40}
41
43{
44 if (matchMedia(contrastPreferenceMore)) {
45 return Qt::ContrastPreference::HighContrast;
46 } else {
47 return Qt::ContrastPreference::NoPreference;
48 }
49}
50
52{
53 if (matchMedia(reducedMotionPreferenceReduce))
54 return Qt::MotionPreference::ReducedMotion;
55 return Qt::MotionPreference::NoPreference;
56}
57} // namespace
58
59using namespace Qt::StringLiterals;
60
62{
63 m_colorScheme = getColorSchemeFromMedia();
64 qCDebug(lcQpaThemeWasm) << "Initializing Wasm theme. Color scheme: " << m_colorScheme;
65 m_contrastPreference = getContrastPreferenceFromMedia();
66 qCDebug(lcQpaThemeWasm) << "Initializing Wasm theme. Contrast preference: " << m_contrastPreference;
67 m_motionPreference = getMotionPreferenceFromMedia();
68 qCDebug(lcQpaThemeWasm) << "Initializing Wasm theme. Motion preference: " << m_motionPreference;
69
70 for (auto family : QFontDatabase::families())
71 if (QFontDatabase::isFixedPitch(family))
72 fixedFont = new QFont(family);
73
74 m_palette = std::make_unique<QPalette>();
75 m_paletteIsDirty = true; // Force update later
76
77 registerCallbacks(
78 { colorSchemePreferenceDark },
79 [this](emscripten::val) { QWasmTheme::onColorSchemeChange(); },
80 m_colorSchemeChangeCallback);
81 registerCallbacks(
82 { contrastPreferenceNoPreference, contrastPreferenceMore, contrastPreferenceLess,
83 contrastPreferenceCustom },
84 [this](emscripten::val) { QWasmTheme::onContrastPreferenceChange(); },
85 m_contrastPreferenceChangeCallbacks);
86 registerCallbacks(
87 { reducedMotionPreferenceReduce },
88 [this](emscripten::val) { QWasmTheme::onMotionPreferenceChange(); },
89 m_motionPreferenceChangeCallback);
90}
91
93{
94 if (fixedFont)
95 delete fixedFont;
96}
97
98const QPalette *QWasmTheme::palette(Palette type) const
99{
100 if (type == SystemPalette) {
101 if (m_paletteIsDirty) {
102 m_paletteIsDirty = false;
103 *m_palette = qt_fusionPalette();
104 }
105 return m_palette.get();
106 }
107 return nullptr;
108}
109
111{
112 return m_colorScheme;
113}
114
115void QWasmTheme::requestColorScheme(Qt::ColorScheme scheme)
116{
117 if (m_colorScheme != scheme) {
118 m_paletteIsDirty = true;
119 m_colorScheme = scheme;
120 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
121 }
122}
123
125{
126 return m_contrastPreference;
127}
128
130{
131 return m_motionPreference;
132}
133
134QVariant QWasmTheme::themeHint(ThemeHint hint) const
135{
136 if (hint == QPlatformTheme::StyleNames)
137 return QVariant(QStringList() << "Fusion"_L1);
138 if (hint == QPlatformTheme::UiEffects)
139 return QVariant(int(HoverEffect));
140 if (hint == QPlatformTheme::MnemonicsEnabled)
141 return platform() != Platform::MacOS;
142 return QPlatformTheme::themeHint(hint);
143}
144
145const QFont *QWasmTheme::font(Font type) const
146{
147 if (type == QPlatformTheme::FixedFont) {
148 return fixedFont;
149 }
150 return nullptr;
151}
152
153bool QWasmTheme::usePlatformNativeDialog(DialogType type) const
154{
155 return (type == DialogType::FileDialog);
156}
157
159{
160 if (type == DialogType::FileDialog)
161 return new QWasmFileDialogHelper();
162 return nullptr;
163}
164
166{
167 auto colorScheme = getColorSchemeFromMedia();
168 if (m_colorScheme != colorScheme) {
169 qCDebug(lcQpaThemeWasm) << "Color scheme changed to " << colorScheme;
170 m_colorScheme = colorScheme;
171 m_paletteIsDirty = true;
172 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
173 }
174}
175
177{
178 auto contrastPreference = getContrastPreferenceFromMedia();
179 if (m_contrastPreference != contrastPreference) {
180 qCDebug(lcQpaThemeWasm) << "Contrast preference changed to " << contrastPreference;
181 m_contrastPreference = contrastPreference;
182 m_paletteIsDirty = true;
183 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
184 }
185}
186
188{
189 auto motionPreference = getMotionPreferenceFromMedia();
190 if (m_motionPreference != motionPreference) {
191 qCDebug(lcQpaThemeWasm) << "Motion preference changed to " << motionPreference;
192 m_motionPreference = motionPreference;
193 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
194 }
195}
196
197QT_END_NAMESPACE
void onContrastPreferenceChange()
QPlatformDialogHelper * createPlatformDialogHelper(DialogType type) const override
Qt::ContrastPreference contrastPreference() const override
void onMotionPreferenceChange()
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
Qt::MotionPreference motionPreference() const override
const QFont * font(Font type) const override
Qt::ContrastPreference getContrastPreferenceFromMedia()
Qt::ColorScheme getColorSchemeFromMedia()
Qt::MotionPreference getMotionPreferenceFromMedia()
bool matchMedia(std::string mediaQueryString)
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)
QDebug Q_GUI_EXPORT & operator<<(QDebug &s, const QVectorPath &path)
QT_BEGIN_NAMESPACE Platform platform()
constexpr auto contrastPreferenceMore
Definition qwasmtheme.h:28
constexpr auto colorSchemePreferenceDark
Definition qwasmtheme.h:26