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>();
36 QString inputStr = (!inputData.isNull() && !inputData.isUndefined())
37 ? QString::fromEcmaString(inputData) : QString();
42 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType : " << inputTypeString;
43 if (!inputTypeString.compare(
"deleteContentBackward")) {
45 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
46 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
47 int cursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
48 int deleteLength = rangesPair.second - rangesPair.first;
51 if (cursorPosition >= rangesPair.first) {
52 deleteFrom = -(cursorPosition - rangesPair.first);
55 e.setCommitString(QString(), deleteFrom, deleteLength);
56 QCoreApplication::sendEvent(m_focusObject, &e);
59 rangesPair.second = 0;
61 event.call<
void>(
"stopImmediatePropagation");
63 }
else if (!inputTypeString.compare(
"deleteContentForward")) {
64 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier);
65 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Delete, Qt::NoModifier);
66 event.call<
void>(
"stopImmediatePropagation");
68 }
else if (!inputTypeString.compare(
"insertCompositionText")) {
69 qCDebug(qLcQpaWasmInputContext) <<
"insertCompositionText : " << inputStr;
70 event.call<
void>(
"stopImmediatePropagation");
72 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
73 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
75 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt() ;
76 int replaceIndex = (qCursorPosition - rangesPair.first);
77 int replaceLength = rangesPair.second - rangesPair.first;
79 setPreeditString(inputStr, replaceIndex);
80 insertPreedit(replaceLength);
83 rangesPair.second = 0;
84 event.call<
void>(
"stopImmediatePropagation");
86 }
else if (!inputTypeString.compare(
"insertReplacementText")) {
91 qCDebug(qLcQpaWasmInputContext) <<
"insertReplacementText >>>>" <<
"inputString : " << inputStr;
92 emscripten::val ranges = event.call<emscripten::val>(
"getTargetRanges");
94 m_preeditString.clear();
95 std::string elementString = m_inputElement[
"value"].as<std::string>();
96 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
97 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
98 QString textFieldString = queryEvent.value(Qt::ImTextBeforeCursor).toString();
99 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
101 if (rangesPair.first != 0 || rangesPair.second != 0) {
103 int replaceIndex = (qCursorPosition - rangesPair.first);
104 int replaceLength = rangesPair.second - rangesPair.first;
105 replaceText(inputStr, -replaceIndex, replaceLength);
106 rangesPair.first = 0;
107 rangesPair.second = 0;
110 int spaceIndex = textFieldString.lastIndexOf(
' ') + 1;
111 int replaceIndex = (qCursorPosition - spaceIndex);
113 replaceText(inputStr, -replaceIndex, replaceIndex);
116 event.call<
void>(
"stopImmediatePropagation");
119 }
else if (!inputTypeString.compare(
"deleteCompositionText")) {
120 setPreeditString(
"", 0);
122 event.call<
void>(
"stopImmediatePropagation");
124 }
else if (!inputTypeString.compare(
"insertFromComposition")) {
125 setPreeditString(inputStr, 0);
127 event.call<
void>(
"stopImmediatePropagation");
129 }
else if (!inputTypeString.compare(
"insertText")) {
130 if ((rangesPair.first != 0 || rangesPair.second != 0)
131 && rangesPair.first != rangesPair.second) {
133 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
134 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
136 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
137 int replaceIndex = (qCursorPosition - rangesPair.first);
138 int replaceLength = rangesPair.second - rangesPair.first;
140 replaceText(inputStr, -replaceIndex, replaceLength);
142 rangesPair.first = 0;
143 rangesPair.second = 0;
146 insertText(inputStr);
149 event.call<
void>(
"stopImmediatePropagation");
150#if QT_CONFIG(clipboard)
151 }
else if (!inputTypeString.compare(
"insertFromPaste")) {
152 insertText(QGuiApplication::clipboard()->text());
153 event.call<
void>(
"stopImmediatePropagation");
160 qCWarning(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType \"" <<
161 inputType.as<std::string>() <<
"\" is not supported in Qt yet";
240void QWasmInputContext::updateGeometry()
242 if (QWasmAccessibility::isEnabled())
245 if (m_inputElement.isNull())
248 const QWindow *focusWindow = QGuiApplication::focusWindow();
249 if (!m_focusObject || !focusWindow || !m_inputMethodAccepted) {
250 m_inputElement[
"style"].set(
"left",
"0px");
251 m_inputElement[
"style"].set(
"top",
"0px");
253 Q_ASSERT(focusWindow);
254 Q_ASSERT(m_focusObject);
255 Q_ASSERT(m_inputMethodAccepted);
257 const QRect inputItemRectangle = QPlatformInputContext::inputItemRectangle().toRect();
258 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"propagating inputItemRectangle:" << inputItemRectangle;
259 m_inputElement[
"style"].set(
"left", std::to_string(inputItemRectangle.x()) +
"px");
260 m_inputElement[
"style"].set(
"top", std::to_string(inputItemRectangle.y()) +
"px");
261 m_inputElement[
"style"].set(
"width",
"1px");
262 m_inputElement[
"style"].set(
"height",
"1px");
377void QWasmInputContext::insertPreedit(
int replaceLength)
379 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << m_preeditString;
380 if (replaceLength == 0)
381 replaceLength = m_preeditString.length();
383 QList<QInputMethodEvent::Attribute> attributes;
385 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
388 attributes.append(attr_cursor);
390 QTextCharFormat format;
391 format.setFontUnderline(
true);
392 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
393 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
395 replaceLength, format);
396 attributes.append(attr_format);
399 QInputMethodEvent e(m_preeditString, attributes);
400 if (m_replaceIndex > 0)
401 e.setCommitString(
"", -m_replaceIndex, replaceLength);
402 QCoreApplication::sendEvent(m_focusObject, &e);
430 void QWasmInputContext::replaceText(QString inputStr,
int replaceFrom,
int replaceSize)
432 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << inputStr << replaceFrom << replaceSize;
434 QList<QInputMethodEvent::Attribute> attributes;
436 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
439 attributes.append(attr_cursor);
441 QTextCharFormat format;
442 format.setFontUnderline(
true);
443 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
444 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
448 attributes.append(attr_format);
451 QInputMethodEvent e1(QString(), attributes);
452 e1.setCommitString(inputStr, replaceFrom, replaceSize);
453 QCoreApplication::sendEvent(m_focusObject, &e1);
455 m_preeditString.clear();