summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmtheme.h
blob: 4eaad874c76c5281288f2f16417aad91e83653d9 (plain)
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
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QWASMTHEME_H
#define QWASMTHEME_H

#include <qpa/qplatformtheme.h>
#include <QtGui/QFont>

#include <memory>

#include <private/qstdweb_p.h>

QT_BEGIN_NAMESPACE

class QWasmEventTranslator;
class QWasmFontDatabase;
class QWasmWindow;
class QWasmEventDispatcher;
class QWasmScreen;
class QWasmCompositor;
class QWasmBackingStore;

// this reflects @media/prefers-contrast
constexpr auto colorSchemePreferenceDark =  "(prefers-color-scheme: dark)";
constexpr auto contrastPreferenceNoPreference =  "(prefers-contrast: no-preference)";
constexpr auto contrastPreferenceMore =  "(prefers-contrast: more)";
constexpr auto contrastPreferenceLess =  "(prefers-contrast: less)";
constexpr auto contrastPreferenceCustom =  "(prefers-contrast: custom)";

template <typename MediaName, typename CallbackFn, typename Container>
void registerCallbacks(std::initializer_list<MediaName> mediaNames, CallbackFn callback, Container &callbacksContainer)
{
    emscripten::val window = emscripten::val::global("window");
    if (!window.isUndefined()) {
        for (auto &&mediaName : mediaNames) {
            auto media = window.call<emscripten::val>("matchMedia", emscripten::val(mediaName));
            if constexpr (std::is_same_v<Container, std::vector<QWasmEventHandler>>) {
                callbacksContainer.emplace_back(media, "change", callback);
            } else {
                Q_ASSERT(mediaNames.size() == 1);
                callbacksContainer = QWasmEventHandler(media, "change", callback);
            }
        }
    }
}

class QWasmTheme : public QPlatformTheme
{
public:
    QWasmTheme();
    ~QWasmTheme();

    const QPalette *palette(Palette type = SystemPalette) const override;
    Qt::ColorScheme colorScheme() const override;
    void requestColorScheme(Qt::ColorScheme scheme) override;
    Qt::ContrastPreference contrastPreference() const override;
    QVariant themeHint(ThemeHint hint) const override;
    const QFont *font(Font type) const override;
    QFont *fixedFont = nullptr;

    void onColorSchemeChange();
    void onContrastPreferenceChange();

private:
    Qt::ColorScheme m_colorScheme = Qt::ColorScheme::Unknown;
    QWasmEventHandler m_colorSchemeChangeCallback;
    std::unique_ptr<QPalette> m_palette;
    mutable bool m_paletteIsDirty = false;
    Qt::ContrastPreference m_contrastPreference;
    std::vector<QWasmEventHandler> m_contrastPreferenceChangeCallbacks;
};

QT_END_NAMESPACE

#endif // QWASMTHEME_H