summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmwindownonclientarea.h
blob: 78c77585a0bb076632fc5f632bba5eade6f6e869 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QWASMWINDOWNONCLIENTAREA_H
#define QWASMWINDOWNONCLIENTAREA_H

#include <QtCore/qrect.h>
#include <QtCore/qtconfigmacros.h>
#include <QtCore/qnamespace.h>

#include <emscripten/val.h>

#include <functional>
#include <memory>
#include <string_view>
#include <vector>

QT_BEGIN_NAMESPACE

namespace qstdweb {
class EventCallback;
}

struct PointerEvent;
class QWindow;
class Resizer;
class TitleBar;
class QWasmWindow;

class NonClientArea
{
public:
    NonClientArea(QWasmWindow *window, emscripten::val containerElement);
    ~NonClientArea();

    void onClientAreaWidthChange(int width);
    void propagateSizeHints();
    TitleBar *titleBar() const { return m_titleBar.get(); }

private:
    void updateResizability();

    emscripten::val m_qtWindowElement;
    std::unique_ptr<Resizer> m_resizer;
    std::unique_ptr<TitleBar> m_titleBar;
};

class WebImageButton
{
public:
    class Callbacks
    {
    public:
        Callbacks();
        Callbacks(std::function<void()> onInteraction, std::function<void()> onClick);
        ~Callbacks();

        Callbacks(const Callbacks &) = delete;
        Callbacks(Callbacks &&);
        Callbacks &operator=(const Callbacks &) = delete;
        Callbacks &operator=(Callbacks &&);

        operator bool() const { return !!m_onInteraction; }

        void onInteraction();
        void onClick();

    private:
        std::function<void()> m_onInteraction;
        std::function<void()> m_onClick;
    };

    WebImageButton();
    ~WebImageButton();

    void setCallbacks(Callbacks callbacks);
    void setImage(std::string_view imageData, std::string_view format);
    void setVisible(bool visible);

    emscripten::val htmlElement() const { return m_containerElement; }
    emscripten::val imageElement() const { return m_imgElement; }

private:
    emscripten::val m_containerElement;
    emscripten::val m_imgElement;

    std::unique_ptr<qstdweb::EventCallback> m_webMouseMoveEventCallback;
    std::unique_ptr<qstdweb::EventCallback> m_webMouseDownEventCallback;
    std::unique_ptr<qstdweb::EventCallback> m_webClickEventCallback;

    Callbacks m_callbacks;
};

struct ResizeConstraints {
    QPoint minShrink;
    QPoint maxGrow;
    int maxGrowTop;
};

class Resizer
{
public:
    class ResizerElement
    {
    public:
        static constexpr const char *cssClassNameForEdges(Qt::Edges edges)
        {
            switch (edges) {
            case Qt::TopEdge | Qt::LeftEdge:;
                return "nw";
            case Qt::TopEdge:
                return "n";
            case Qt::TopEdge | Qt::RightEdge:
                return "ne";
            case Qt::LeftEdge:
                return "w";
            case Qt::RightEdge:
                return "e";
            case Qt::BottomEdge | Qt::LeftEdge:
                return "sw";
            case Qt::BottomEdge:
                return "s";
            case Qt::BottomEdge | Qt::RightEdge:
                return "se";
            default:
                return "";
            }
        }

        ResizerElement(emscripten::val parentElement, Qt::Edges edges, Resizer *resizer);
        ~ResizerElement();
        ResizerElement(const ResizerElement &other) = delete;
        ResizerElement(ResizerElement &&other);
        ResizerElement &operator=(const ResizerElement &other) = delete;
        ResizerElement &operator=(ResizerElement &&other) = delete;

        bool onPointerDown(const PointerEvent &event);
        bool onPointerMove(const PointerEvent &event);
        bool onPointerUp(const PointerEvent &event);

    private:
        emscripten::val m_element;

        int m_capturedPointerId = -1;

        const Qt::Edges m_edges;

        Resizer *m_resizer;

        std::unique_ptr<qstdweb::EventCallback> m_mouseDownEvent;
        std::unique_ptr<qstdweb::EventCallback> m_mouseMoveEvent;
        std::unique_ptr<qstdweb::EventCallback> m_mouseUpEvent;
    };

    using ClickCallback = std::function<void()>;

    Resizer(QWasmWindow *window, emscripten::val parentElement);
    ~Resizer();

    ResizeConstraints getResizeConstraints();

private:
    void onInteraction();
    void startResize(Qt::Edges resizeEdges, const PointerEvent &event);
    void continueResize(const PointerEvent &event);
    void finishResize();

    struct ResizeData
    {
        Qt::Edges edges = Qt::Edges::fromInt(0);
        QPointF originInScreenCoords;
        QPoint minShrink;
        QPoint maxGrow;
        QRect initialBounds;
    };
    std::unique_ptr<ResizeData> m_currentResizeData;

    QWasmWindow *m_window;
    emscripten::val m_windowElement;
    std::vector<std::unique_ptr<ResizerElement>> m_elements;
};

class TitleBar
{
public:
    TitleBar(QWasmWindow *window, emscripten::val parentElement);
    ~TitleBar();

    void setTitle(const QString &title);
    void setRestoreVisible(bool visible);
    void setMaximizeVisible(bool visible);
    void setCloseVisible(bool visible);
    void setIcon(std::string_view imageData, std::string_view format);
    void setWidth(int width);

    QRectF geometry() const;

private:
    bool onPointerDown(const PointerEvent &event);
    bool onPointerMove(const PointerEvent &event);
    bool onPointerUp(const PointerEvent &event);
    bool onDoubleClick();

    QPointF clipPointWithScreen(const QPointF &pointInTitleBarCoords) const;

    QWasmWindow *m_window;

    emscripten::val m_element;
    emscripten::val m_label;

    std::unique_ptr<WebImageButton> m_close;
    std::unique_ptr<WebImageButton> m_maximize;
    std::unique_ptr<WebImageButton> m_restore;
    std::unique_ptr<WebImageButton> m_icon;

    int m_capturedPointerId = -1;
    QPointF m_moveStartPoint;
    QPoint m_moveStartWindowPosition;

    std::unique_ptr<qstdweb::EventCallback> m_mouseDownEvent;
    std::unique_ptr<qstdweb::EventCallback> m_mouseMoveEvent;
    std::unique_ptr<qstdweb::EventCallback> m_mouseUpEvent;
    std::unique_ptr<qstdweb::EventCallback> m_doubleClickEvent;
};

QT_END_NAMESPACE
#endif // QWASMWINDOWNONCLIENTAREA_H