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 <qpa/qwindowsysteminterface.h>
9#include <qpalette.h>
10
11#include <private/qstdweb_p.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::ColorScheme QWasmTheme::s_autoColorScheme = Qt::ColorScheme::Unknown;
20bool QWasmTheme::s_autoPaletteIsDirty = false;
21
22QT_BEGIN_NAMESPACE
23
24using namespace Qt::StringLiterals;
25
27{
28 if (emscripten::val::global("window").call<emscripten::val>(
29 "matchMedia",
30 std::string("(prefers-color-scheme:dark)"))["matches"].as<bool>())
31 s_autoColorScheme = Qt::ColorScheme::Dark;
32 else
33 s_autoColorScheme = Qt::ColorScheme::Light;
34
35 for (auto family : QFontDatabase::families())
36 if (QFontDatabase::isFixedPitch(family))
37 fixedFont = new QFont(family);
38
39 m_palette = std::make_unique<QPalette>();
40 m_paletteIsDirty = true; // Force update later
41
42 const auto callback = [=](emscripten::val event) { QWasmTheme::onColorSchemeChange(event); };
43 const emscripten::val window = emscripten::val::global("window");
44 if (!window.isUndefined()) {
45 const emscripten::val matchMedia = window.call<emscripten::val>("matchMedia", emscripten::val("(prefers-color-scheme: dark)"));
46 if (!matchMedia.isUndefined()) {
47 static auto changeEvent =
48 std::make_unique<qstdweb::EventCallback>(matchMedia, "change", callback);
49 }
50 }
51}
52
54{
55 if (fixedFont)
56 delete fixedFont;
57}
58
59const QPalette *QWasmTheme::palette(Palette type) const
60{
61 if (type == SystemPalette) {
62 if (m_paletteIsDirty || s_autoPaletteIsDirty) {
63 m_paletteIsDirty = false;
64 s_autoPaletteIsDirty = false;
65 *m_palette = qt_fusionPalette();
66 }
67 return m_palette.get();
68 }
69 return nullptr;
70}
71
73{
74 if (m_colorScheme != Qt::ColorScheme::Unknown)
75 return m_colorScheme;
76 return s_autoColorScheme;
77}
78
79void QWasmTheme::requestColorScheme(Qt::ColorScheme scheme)
80{
81 if (m_colorScheme != scheme) {
82 m_paletteIsDirty = true;
83 m_colorScheme = scheme;
84 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
85 }
86}
87
88QVariant QWasmTheme::themeHint(ThemeHint hint) const
89{
90 if (hint == QPlatformTheme::StyleNames)
91 return QVariant(QStringList() << "Fusion"_L1);
92 if (hint == QPlatformTheme::UiEffects)
93 return QVariant(int(HoverEffect));
94 return QPlatformTheme::themeHint(hint);
95}
96
97const QFont *QWasmTheme::font(Font type) const
98{
99 if (type == QPlatformTheme::FixedFont) {
100 return fixedFont;
101 }
102 return nullptr;
103}
104
105void QWasmTheme::onColorSchemeChange(emscripten::val event)
106{
107 const emscripten::val matches = event["matches"];
108 if (!matches.isUndefined()) {
109 const auto oldAutoColorScheme = s_autoColorScheme;
110 if (matches.as<int>())
111 s_autoColorScheme = Qt::ColorScheme::Dark;
112 else
113 s_autoColorScheme = Qt::ColorScheme::Light;
114
115 if (oldAutoColorScheme != s_autoColorScheme) {
116 s_autoPaletteIsDirty = true;
117 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
118 }
119 }
120}
121
122
123QT_END_NAMESPACE
const QPalette * palette(Palette type=SystemPalette) const override
Qt::ColorScheme colorScheme() const override
void requestColorScheme(Qt::ColorScheme scheme) override
QVariant themeHint(ThemeHint hint) const override
const QFont * font(Font type) const override
QDebug Q_GUI_EXPORT & operator<<(QDebug &s, const QVectorPath &path)