26void QWasmInputContext::inputCallback(
emscripten::val event)
28 emscripten::val inputType = event[
"inputType"];
29 if (inputType.isNull() || inputType.isUndefined())
31 const auto inputTypeString = inputType.as<
std::string>();
34 QString inputStr = (!inputData.isNull() && !inputData.isUndefined())
35 ? QString::fromEcmaString(inputData) : QString();
40 emscripten::val dataTr = event[
"dataTransfer"];
41 if (!dataTr.isUndefined() && !dataTr.isNull()) {
42 qCDebug(qLcQpaWasmInputContext) <<
"inputEvent dataTransfer" << inputStr;
43 inputStr = QString::fromStdString(dataTr.
44 call<emscripten::val>(
"getData",
45 std::string(
"text")).as<std::string>());
48 if (m_ignoreNextInput) {
49 m_ignoreNextInput =
false;
56 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType : " << inputTypeString;
58 if (!inputTypeString.compare(
"deleteContentBackward")) {
60 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
61 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
62 int cursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
63 int deleteLength = rangesPair.second - rangesPair.first;
66 if (cursorPosition >= rangesPair.first) {
67 deleteFrom = -(cursorPosition - rangesPair.first);
70 e.setCommitString(QString(), deleteFrom, deleteLength);
71 QCoreApplication::sendEvent(m_focusObject, &e);
74 rangesPair.second = 0;
76 event.call<
void>(
"stopImmediatePropagation");
78 }
else if (!inputTypeString.compare(
"deleteContentForward")) {
79 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier);
80 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Delete, Qt::NoModifier);
81 event.call<
void>(
"stopImmediatePropagation");
83 }
else if (!inputTypeString.compare(
"insertCompositionText")) {
85 qCDebug(qLcQpaWasmInputContext) <<
"insertCompositionText : " << inputStr;
86 event.call<
void>(
"stopImmediatePropagation");
87 if (event[
"isComposing"].as<
bool>()) {
88 int replaceLength = rangesPair.second - rangesPair.first;
90 setPreeditString(inputStr, 0);
91 insertPreedit(replaceLength);
94 rangesPair.second = 0;
95 event.call<
void>(
"stopImmediatePropagation");
98 }
else if (!inputTypeString.compare(
"insertReplacementText")) {
100 qCDebug(qLcQpaWasmInputContext) <<
"insertReplacementText >>>>" <<
"inputString : " << inputStr;
102 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
103 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
104 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
106 if (rangesPair.first != 0 || rangesPair.second != 0) {
107 int replaceLength = rangesPair.second - rangesPair.first;
109 replaceText(inputStr, -replaceLength, replaceLength);
111 rangesPair.first = 0;
112 rangesPair.second = 0;
115 QString textFieldString = queryEvent.value(Qt::ImTextBeforeCursor).toString();
116 int spaceIndex = textFieldString.lastIndexOf(
' ') + 1;
117 int replaceIndex = (qCursorPosition - spaceIndex);
119 replaceText(inputStr, -replaceIndex, replaceIndex);
122 event.call<
void>(
"stopImmediatePropagation");
125 }
else if (!inputTypeString.compare(
"deleteCompositionText")) {
126 setPreeditString(
"", 0);
128 event.call<
void>(
"stopImmediatePropagation");
130 }
else if (!inputTypeString.compare(
"insertFromComposition")) {
131 setPreeditString(inputStr, 0);
133 event.call<
void>(
"stopImmediatePropagation");
135 }
else if (!inputTypeString.compare(
"insertText")) {
136 if ((rangesPair.first != 0 || rangesPair.second != 0)
137 && rangesPair.first != rangesPair.second) {
139 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
140 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
142 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
143 int replaceIndex = (qCursorPosition - rangesPair.first);
144 int replaceLength = rangesPair.second - rangesPair.first;
146 replaceText(inputStr, replaceIndex, replaceLength);
148 rangesPair.first = 0;
149 rangesPair.second = 0;
152 insertText(inputStr);
155 event.call<
void>(
"stopImmediatePropagation");
156 }
else if (!inputTypeString.compare(
"insertFromPaste")) {
157 insertText(inputStr);
158 event.call<
void>(
"stopImmediatePropagation");
164 qCWarning(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType \"" <<
165 inputType.as<std::string>() <<
"\" is not supported in Qt yet";
244void QWasmInputContext::updateGeometry()
246 if (QWasmAccessibility::isEnabled())
249 if (m_inputElement.isNull())
252 const QWindow *focusWindow = QGuiApplication::focusWindow();
253 if (!m_focusObject || !focusWindow || !m_inputMethodAccepted) {
254 m_inputElement[
"style"].set(
"left",
"0px");
255 m_inputElement[
"style"].set(
"top",
"0px");
257 Q_ASSERT(focusWindow);
258 Q_ASSERT(m_focusObject);
259 Q_ASSERT(m_inputMethodAccepted);
261 const QRect inputItemRectangle = QPlatformInputContext::inputItemRectangle().toRect();
262 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"propagating inputItemRectangle:" << inputItemRectangle;
263 m_inputElement[
"style"].set(
"left", std::to_string(inputItemRectangle.x()) +
"px");
264 m_inputElement[
"style"].set(
"top", std::to_string(inputItemRectangle.y()) +
"px");
265 m_inputElement[
"style"].set(
"width",
"1px");
266 m_inputElement[
"style"].set(
"height",
"1px");
381void QWasmInputContext::insertPreedit(
int replaceLength)
383 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << m_preeditString;
384 if (replaceLength == 0)
385 replaceLength = m_preeditString.length();
387 QList<QInputMethodEvent::Attribute> attributes;
389 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
392 attributes.append(attr_cursor);
394 QTextCharFormat format;
395 format.setFontUnderline(
true);
396 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
397 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
399 replaceLength, format);
400 attributes.append(attr_format);
403 QInputMethodEvent e(m_preeditString, attributes);
404 if (m_replaceIndex > 0)
405 e.setCommitString(
"", -m_replaceIndex, replaceLength);
406 QCoreApplication::sendEvent(m_focusObject, &e);
434 void QWasmInputContext::replaceText(QString inputStr,
int replaceFrom,
int replaceSize)
436 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << inputStr << replaceFrom << replaceSize;
438 QList<QInputMethodEvent::Attribute> attributes;
440 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
443 attributes.append(attr_cursor);
445 QTextCharFormat format;
446 format.setFontUnderline(
true);
447 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
448 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
452 attributes.append(attr_format);
455 QInputMethodEvent e1(QString(), attributes);
456 e1.setCommitString(inputStr, replaceFrom, replaceSize);
457 QCoreApplication::sendEvent(m_focusObject, &e1);
459 m_preeditString.clear();