Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qwasminputcontext.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <emscripten/bind.h>
5
6#include "qwasminputcontext.h"
7#include "qwasmintegration.h"
8#include "qwasmplatform.h"
9#include <QRectF>
10#include <qpa/qplatforminputcontext.h>
11#include "qwasmscreen.h"
12#include <qguiapplication.h>
13#include <qwindow.h>
14#include <QKeySequence>
15#include <qpa/qwindowsysteminterface.h>
16
18using namespace qstdweb;
19
20static void inputCallback(emscripten::val event)
21{
22 // android sends all the characters typed since the user started typing in this element
23 int length = event["target"]["value"]["length"].as<int>();
24 if (length <= 0)
25 return;
26
27 emscripten::val _incomingCharVal = event["data"];
28 if (_incomingCharVal != emscripten::val::undefined() && _incomingCharVal != emscripten::val::null()) {
29
30 QString str = QString::fromStdString(_incomingCharVal.as<std::string>());
31 QWasmInputContext *wasmInput =
32 reinterpret_cast<QWasmInputContext*>(event["target"]["data-qinputcontext"].as<quintptr>());
33 wasmInput->inputStringChanged(str, EMSCRIPTEN_EVENT_KEYDOWN, wasmInput);
34 }
35 // this clears the input string, so backspaces do not send a character
36 // but stops suggestions
37 event["target"].set("value", "");
38}
39
40EMSCRIPTEN_BINDINGS(clipboard_module) {
41 function("qtInputContextCallback", &inputCallback);
42}
43
45{
46 emscripten::val document = emscripten::val::global("document");
47 m_inputElement = document.call<emscripten::val>("createElement", std::string("input"));
48 m_inputElement.set("type", "text");
49 m_inputElement.set("style", "position:absolute;left:-1000px;top:-1000px"); // offscreen
50 m_inputElement.set("contenteditable","true");
51
53 auto callback = [=](emscripten::val) {
54 m_inputElement["parentElement"].call<void>("removeChild", m_inputElement);
55 inputPanelIsOpen = false;
56 };
57 m_blurEventHandler.reset(new EventCallback(m_inputElement, "blur", callback));
58
59 } else {
60
61 const std::string inputType = platform() == Platform::Windows ? "textInput" : "input";
62
63 document.call<void>("addEventListener", inputType,
64 emscripten::val::module_property("qtInputContextCallback"),
65 emscripten::val(false));
66 m_inputElement.set("data-qinputcontext",
67 emscripten::val(quintptr(reinterpret_cast<void *>(this))));
68 emscripten::val body = document["body"];
69 body.call<void>("appendChild", m_inputElement);
70 }
71
74}
75
77{
79 emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 0, NULL);
80}
81
83{
84 m_focusWindow = focusWindow;
85}
86
87emscripten::val QWasmInputContext::inputHandlerElementForFocusedWindow()
88{
89 if (!m_focusWindow)
90 return emscripten::val::undefined();
91 return static_cast<QWasmWindow *>(m_focusWindow->handle())->inputHandlerElement();
92}
93
94void QWasmInputContext::update(Qt::InputMethodQueries queries)
95{
97}
98
100{
102 && !inputPanelIsOpen) { // call this only once for win32
103 m_inputElement.call<void>("focus");
104 return;
105 }
106 // this is called each time the keyboard is touched
107
108 // Add the input element as a child of the screen for the
109 // currently focused window and give it focus. The browser
110 // will not display the input element, but mobile browsers
111 // should display the virtual keyboard. Key events will be
112 // captured by the keyboard event handler installed on the
113 // screen element.
114
115 if (platform() == Platform::MacOS // keep for compatibility
116 || platform() == Platform::iOS
117 || platform() == Platform::Windows) {
118 emscripten::val inputWrapper = inputHandlerElementForFocusedWindow();
119 if (inputWrapper.isUndefined())
120 return;
121 inputWrapper.call<void>("appendChild", m_inputElement);
122 }
123
124 m_inputElement.call<void>("focus");
125 inputPanelIsOpen = true;
126}
127
129{
130 if (QWasmIntegration::get()->touchPoints < 1)
131 return;
132 m_inputElement.call<void>("blur");
133 inputPanelIsOpen = false;
134}
135
137{
140 Qt::Key thisKey = keys[0].key();
141
142 // synthesize this keyevent as android is not normal
143 if (inputString.size() > 2 && (thisKey < Qt::Key_F35
144 || thisKey > Qt::Key_Back)) {
145 inputString.clear();
146 }
147 if (inputString == QStringLiteral("Escape")) {
148 thisKey = Qt::Key_Escape;
149 inputString.clear();
150 } else if (thisKey == Qt::Key(0)) {
151 thisKey = Qt::Key_Return;
152 }
153
155 0, eventType == EMSCRIPTEN_EVENT_KEYDOWN ? QEvent::KeyPress : QEvent::KeyRelease,
156 thisKey, keys[0].keyboardModifiers(),
157 eventType == EMSCRIPTEN_EVENT_KEYDOWN ? inputString : QStringLiteral(""));
158}
159
160
@ KeyRelease
Definition qcoreevent.h:65
@ KeyPress
Definition qcoreevent.h:64
void focusWindowChanged(QWindow *focusWindow)
This signal is emitted when the focused window changes.
The QKeySequence class encapsulates a key sequence as used by shortcuts.
static QKeySequence fromString(const QString &str, SequenceFormat format=PortableText)
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
virtual void update(Qt::InputMethodQueries)
Notification on editor updates.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromStdString(const std::string &s)
Definition qstring.h:1447
void showInputPanel() override
Request to show input panel.
emscripten::val m_inputElement
void hideInputPanel() override
Request to hide input panel.
void update(Qt::InputMethodQueries) override
Notification on editor updates.
void focusWindowChanged(QWindow *focusWindow)
void inputStringChanged(QString &, int eventType, QWasmInputContext *context)
static QWasmIntegration * get()
static bool handleKeyEvent(QWindow *window, QEvent::Type t, int k, Qt::KeyboardModifiers mods, const QString &text=QString(), bool autorep=false, ushort count=1)
\inmodule QtGui
Definition qwindow.h:63
QString str
[2]
Combined button and popup list for selecting options.
@ Key_Escape
Definition qnamespace.h:663
@ Key_Return
Definition qnamespace.h:667
@ Key_F35
Definition qnamespace.h:724
@ Key_Back
Definition qnamespace.h:846
EMSCRIPTEN_BINDINGS(qtStdwebCalback)
Definition qstdweb.cpp:753
static void * context
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
#define qGuiApp
GLenum GLuint GLenum GLsizei length
struct _cl_event * event
#define QStringLiteral(str)
#define Q_UNUSED(x)
size_t quintptr
Definition qtypes.h:167
static void inputCallback(emscripten::val event)
QT_BEGIN_NAMESPACE Platform platform()
QStringList keys