summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasminputcontext.cpp
blob: 96d0c272c04a924fad74bc3960d860643aa73050 (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <emscripten/bind.h>

#include "qwasminputcontext.h"
#include "qwasmintegration.h"
#include <QRectF>
#include <qpa/qplatforminputcontext.h>
#include "qwasmeventtranslator.h"
#include "qwasmscreen.h"
#include <qguiapplication.h>
#include <qwindow.h>
#include <QKeySequence>
#include <qpa/qwindowsysteminterface.h>
using namespace qstdweb;

static void inputCallback(emscripten::val event)
{
    // android sends all the characters typed since the user started typing in this element
    int length = event["target"]["value"]["length"].as<int>();
    if (length <= 0)
        return;

    // use only last character
    emscripten::val _incomingCharVal = event["target"]["value"][length - 1];
    if (_incomingCharVal != emscripten::val::undefined() && _incomingCharVal != emscripten::val::null()) {

        QString str = QString::fromStdString(_incomingCharVal.as<std::string>());
        QWasmInputContext *wasmInput =
                reinterpret_cast<QWasmInputContext*>(event["target"]["data-context"].as<quintptr>());
        wasmInput->inputStringChanged(str, wasmInput);
    }
    // this clears the input string, so backspaces do not send a character
    // but stops suggestions
    event["target"].set("value", "");
}

EMSCRIPTEN_BINDINGS(clipboard_module) {
    function("qt_InputContextCallback", &inputCallback);
}

QWasmInputContext::QWasmInputContext()
{
    emscripten::val document = emscripten::val::global("document");
    m_inputElement = document.call<emscripten::val>("createElement", std::string("input"));
    m_inputElement.set("type", "text");
    m_inputElement.set("style", "position:absolute;left:-1000px;top:-1000px"); // offscreen
    m_inputElement.set("contentaediable","true");

    if (QWasmIntegration::get()->platform == QWasmIntegration::AndroidPlatform) {
        emscripten::val body = document["body"];
        body.call<void>("appendChild", m_inputElement);

        m_inputElement.call<void>("addEventListener", std::string("input"),
                                  emscripten::val::module_property("qt_InputContextCallback"),
                                  emscripten::val(false));
        m_inputElement.set("data-context",
                           emscripten::val(quintptr(reinterpret_cast<void *>(this))));

        // android sends Enter through target window, let's just handle this here
        emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, (void *)this, 1,
                                        &androidKeyboardCallback);

    }
    if (QWasmIntegration::get()->platform == QWasmIntegration::MacOSPlatform ||
        QWasmIntegration::get()->platform == QWasmIntegration::iPhonePlatform)
     {
        auto callback = [=](emscripten::val) {
            m_inputElement["parentElement"].call<void>("removeChild", m_inputElement);
            inputPanelIsOpen = false;
        };
        m_blurEventHandler.reset(new EventCallback(m_inputElement, "blur", callback));
    }

    QObject::connect(qGuiApp, &QGuiApplication::focusWindowChanged, this,
                     &QWasmInputContext::focusWindowChanged);
}

QWasmInputContext::~QWasmInputContext()
{
    if (QWasmIntegration::get()->platform == QWasmIntegration::AndroidPlatform)
        emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 0, NULL);
}

void QWasmInputContext::focusWindowChanged(QWindow *focusWindow)
{
    m_focusWindow = focusWindow;
}

emscripten::val QWasmInputContext::focusCanvas()
{
    if (!m_focusWindow)
        return emscripten::val::undefined();
    QScreen *screen = m_focusWindow->screen();
    if (!screen)
        return emscripten::val::undefined();
    return QWasmScreen::get(screen)->canvas();
}

void QWasmInputContext::update(Qt::InputMethodQueries queries)
{
    QPlatformInputContext::update(queries);
}

void QWasmInputContext::showInputPanel()
{
    if (QWasmIntegration::get()->platform == QWasmIntegration::WindowsPlatform
        && inputPanelIsOpen) // call this only once for win32
        return;
    // this is called each time the keyboard is touched

    // Add the input element as a child of the canvas for the
    // currently focused window and give it focus. The browser
    // will not display the input element, but mobile browsers
    // should display the virtual keyboard. Key events will be
    // captured by the keyboard event handler installed on the
    // canvas.

    if (QWasmIntegration::get()->platform == QWasmIntegration::MacOSPlatform // keep for compatibility
     || QWasmIntegration::get()->platform == QWasmIntegration::iPhonePlatform
     || QWasmIntegration::get()->platform == QWasmIntegration::WindowsPlatform) {
        emscripten::val canvas = focusCanvas();
        if (canvas == emscripten::val::undefined())
            return;
        canvas.call<void>("appendChild", m_inputElement);
    }

    m_inputElement.call<void>("focus");
    inputPanelIsOpen = true;
}

void QWasmInputContext::hideInputPanel()
{
    if (QWasmIntegration::get()->touchPoints < 1)
        return;
    m_inputElement.call<void>("blur");
    inputPanelIsOpen = false;
}

void QWasmInputContext::inputStringChanged(QString &inputString, QWasmInputContext *context)
{
    Q_UNUSED(context)
    QKeySequence keys = QKeySequence::fromString(inputString);
    // synthesize this keyevent as android is not normal
    QWindowSystemInterface::handleKeyEvent<QWindowSystemInterface::SynchronousDelivery>(
                0, QEvent::KeyPress,keys[0].key(), keys[0].keyboardModifiers(), inputString);
}

int QWasmInputContext::androidKeyboardCallback(int eventType,
                                               const EmscriptenKeyboardEvent *keyEvent,
                                               void *userData)
{
    // we get Enter, Backspace and function keys via emscripten on target window
    Q_UNUSED(eventType)
    QString strKey(keyEvent->key);
    if (strKey == "Unidentified" || strKey == "Process")
        return false;

    QWasmInputContext *wasmInput = reinterpret_cast<QWasmInputContext*>(userData);
    wasmInput->inputStringChanged(strKey, wasmInput);

    return true;
}