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"
5#include <QtCore/qvariant.h>
6#include <QFontDatabase>
7#include <QList>
8#include <qloggingcategory.h>
9#include <qpa/qwindowsysteminterface.h>
10#include <qpalette.h>
11
12#include <emscripten.h>
13#include <emscripten/bind.h>
14#include <emscripten/val.h>
15
16Q_GUI_EXPORT QPalette qt_fusionPalette();
17
18QT_BEGIN_NAMESPACE
19
20Q_STATIC_LOGGING_CATEGORY(lcQpaThemeWasm, "qt.qpa.theme.wasm")
21
22namespace {
23bool matchMedia(std::string mediaQueryString)
24{
25 return emscripten::val::global("window")
26 .call<emscripten::val>("matchMedia", mediaQueryString)["matches"]
27 .as<bool>();
28}
29
31{
32 if (matchMedia(colorSchemePreferenceDark)) {
33 return Qt::ColorScheme::Dark;
34 } else {
35 return Qt::ColorScheme::Light;
36 }
37}
38
40{
41 if (matchMedia(contrastPreferenceMore)) {
42 return Qt::ContrastPreference::HighContrast;
43 } else {
44 return Qt::ContrastPreference::NoPreference;
45 }
46}
47} // namespace
48
49using namespace Qt::StringLiterals;
50
52{
53 m_colorScheme = getColorSchemeFromMedia();
54 qCDebug(lcQpaThemeWasm) << "Initializing Wasm theme. Color scheme: " << m_colorScheme;
55 m_contrastPreference = getContrastPreferenceFromMedia();
56 qCDebug(lcQpaThemeWasm) << "Initializing Wasm theme. Contrast preference: " << m_contrastPreference;
57
58 for (auto family : QFontDatabase::families())
59 if (QFontDatabase::isFixedPitch(family))
60 fixedFont = new QFont(family);
61
62 m_palette = std::make_unique<QPalette>();
63 m_paletteIsDirty = true; // Force update later
64
65 registerCallbacks(
66 { colorSchemePreferenceDark },
67 [this](emscripten::val) { QWasmTheme::onColorSchemeChange(); },
68 m_colorSchemeChangeCallback);
69 registerCallbacks(
70 { contrastPreferenceNoPreference, contrastPreferenceMore, contrastPreferenceLess,
71 contrastPreferenceCustom },
72 [this](emscripten::val) { QWasmTheme::onContrastPreferenceChange(); },
73 m_contrastPreferenceChangeCallbacks);
74}
75
77{
78 if (fixedFont)
79 delete fixedFont;
80}
81
82const QPalette *QWasmTheme::palette(Palette type) const
83{
84 if (type == SystemPalette) {
85 if (m_paletteIsDirty) {
86 m_paletteIsDirty = false;
87 *m_palette = qt_fusionPalette();
88 }
89 return m_palette.get();
90 }
91 return nullptr;
92}
93
95{
96 return m_colorScheme;
97}
98
99void QWasmTheme::requestColorScheme(Qt::ColorScheme scheme)
100{
101 if (m_colorScheme != scheme) {
102 m_paletteIsDirty = true;
103 m_colorScheme = scheme;
104 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
105 }
106}
107
109{
110 return m_contrastPreference;
111}
112
113QVariant QWasmTheme::themeHint(ThemeHint hint) const
114{
115 if (hint == QPlatformTheme::StyleNames)
116 return QVariant(QStringList() << "Fusion"_L1);
117 if (hint == QPlatformTheme::UiEffects)
118 return QVariant(int(HoverEffect));
119 return QPlatformTheme::themeHint(hint);
120}
121
122const QFont *QWasmTheme::font(Font type) const
123{
124 if (type == QPlatformTheme::FixedFont) {
125 return fixedFont;
126 }
127 return nullptr;
128}
129
131{
132 auto colorScheme = getColorSchemeFromMedia();
133 if (m_colorScheme != colorScheme) {
134 qCDebug(lcQpaThemeWasm) << "Color scheme changed to " << colorScheme;
135 m_colorScheme = colorScheme;
136 m_paletteIsDirty = true;
137 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
138 }
139}
140
142{
143 auto contrastPreference = getContrastPreferenceFromMedia();
144 if (m_contrastPreference != contrastPreference) {
145 qCDebug(lcQpaThemeWasm) << "Contrast preference changed to " << contrastPreference;
146 m_contrastPreference = contrastPreference;
147 m_paletteIsDirty = true;
148 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
149 }
150}
151
152
153QT_END_NAMESPACE
void onContrastPreferenceChange()
Qt::ContrastPreference contrastPreference() 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