25void QWasmInputContext::inputCallback(
emscripten::val event)
27 emscripten::val inputType = event[
"inputType"];
28 if (inputType.isNull() || inputType.isUndefined())
30 const auto inputTypeString = inputType.as<
std::string>();
35 QString inputStr = (!inputData.isNull() && !inputData.isUndefined())
36 ? QString::fromEcmaString(inputData) : QString();
41 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType : " << inputTypeString;
42 if (!inputTypeString.compare(
"deleteContentBackward")) {
44 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
45 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
46 int cursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
47 int deleteLength = rangesPair.second - rangesPair.first;
50 if (cursorPosition >= rangesPair.first) {
51 deleteFrom = -(cursorPosition - rangesPair.first);
54 e.setCommitString(QString(), deleteFrom, deleteLength);
55 QCoreApplication::sendEvent(m_focusObject, &e);
58 rangesPair.second = 0;
60 event.call<
void>(
"stopImmediatePropagation");
62 }
else if (!inputTypeString.compare(
"deleteContentForward")) {
63 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier);
64 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Delete, Qt::NoModifier);
65 event.call<
void>(
"stopImmediatePropagation");
67 }
else if (!inputTypeString.compare(
"insertCompositionText")) {
68 qCDebug(qLcQpaWasmInputContext) <<
"insertCompositionText : " << inputStr;
69 event.call<
void>(
"stopImmediatePropagation");
71 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
72 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
74 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt() ;
75 int replaceIndex = (qCursorPosition - rangesPair.first);
76 int replaceLength = rangesPair.second - rangesPair.first;
78 setPreeditString(inputStr, replaceIndex);
79 insertPreedit(replaceLength);
82 rangesPair.second = 0;
83 event.call<
void>(
"stopImmediatePropagation");
85 }
else if (!inputTypeString.compare(
"insertReplacementText")) {
90 qCDebug(qLcQpaWasmInputContext) <<
"insertReplacementText >>>>" <<
"inputString : " << inputStr;
91 emscripten::val ranges = event.call<emscripten::val>(
"getTargetRanges");
93 m_preeditString.clear();
94 std::string elementString = m_inputElement[
"value"].as<std::string>();
95 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
96 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
97 QString textFieldString = queryEvent.value(Qt::ImTextBeforeCursor).toString();
98 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
100 if (rangesPair.first != 0 || rangesPair.second != 0) {
102 int replaceIndex = (qCursorPosition - rangesPair.first);
103 int replaceLength = rangesPair.second - rangesPair.first;
104 replaceText(inputStr, -replaceIndex, replaceLength);
105 rangesPair.first = 0;
106 rangesPair.second = 0;
109 int spaceIndex = textFieldString.lastIndexOf(
' ') + 1;
110 int replaceIndex = (qCursorPosition - spaceIndex);
112 replaceText(inputStr, -replaceIndex, replaceIndex);
115 event.call<
void>(
"stopImmediatePropagation");
118 }
else if (!inputTypeString.compare(
"deleteCompositionText")) {
119 setPreeditString(
"", 0);
121 event.call<
void>(
"stopImmediatePropagation");
123 }
else if (!inputTypeString.compare(
"insertFromComposition")) {
124 setPreeditString(inputStr, 0);
126 event.call<
void>(
"stopImmediatePropagation");
128 }
else if (!inputTypeString.compare(
"insertText")) {
129 if ((rangesPair.first != 0 || rangesPair.second != 0)
130 && rangesPair.first != rangesPair.second) {
132 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
133 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
135 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
136 int replaceIndex = (qCursorPosition - rangesPair.first);
137 int replaceLength = rangesPair.second - rangesPair.first;
139 replaceText(inputStr, -replaceIndex, replaceLength);
141 rangesPair.first = 0;
142 rangesPair.second = 0;
145 insertText(inputStr);
148 event.call<
void>(
"stopImmediatePropagation");
149#if QT_CONFIG(clipboard)
150 }
else if (!inputTypeString.compare(
"insertFromPaste")) {
151 insertText(QGuiApplication::clipboard()->text());
152 event.call<
void>(
"stopImmediatePropagation");
159 qCWarning(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType \"" <<
160 inputType.as<std::string>() <<
"\" is not supported in Qt yet";
239void QWasmInputContext::updateGeometry()
241 if (QWasmAccessibility::isEnabled())
244 if (m_inputElement.isNull())
247 const QWindow *focusWindow = QGuiApplication::focusWindow();
248 if (!m_focusObject || !focusWindow || !m_inputMethodAccepted) {
249 m_inputElement[
"style"].set(
"left",
"0px");
250 m_inputElement[
"style"].set(
"top",
"0px");
252 Q_ASSERT(focusWindow);
253 Q_ASSERT(m_focusObject);
254 Q_ASSERT(m_inputMethodAccepted);
256 const QRect inputItemRectangle = QPlatformInputContext::inputItemRectangle().toRect();
257 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"propagating inputItemRectangle:" << inputItemRectangle;
258 m_inputElement[
"style"].set(
"left", std::to_string(inputItemRectangle.x()) +
"px");
259 m_inputElement[
"style"].set(
"top", std::to_string(inputItemRectangle.y()) +
"px");
260 m_inputElement[
"style"].set(
"width",
"1px");
261 m_inputElement[
"style"].set(
"height",
"1px");
376void QWasmInputContext::insertPreedit(
int replaceLength)
378 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << m_preeditString;
379 if (replaceLength == 0)
380 replaceLength = m_preeditString.length();
382 QList<QInputMethodEvent::Attribute> attributes;
384 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
387 attributes.append(attr_cursor);
389 QTextCharFormat format;
390 format.setFontUnderline(
true);
391 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
392 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
394 replaceLength, format);
395 attributes.append(attr_format);
398 QInputMethodEvent e(m_preeditString, attributes);
399 if (m_replaceIndex > 0)
400 e.setCommitString(
"", -m_replaceIndex, replaceLength);
401 QCoreApplication::sendEvent(m_focusObject, &e);
429 void QWasmInputContext::replaceText(QString inputStr,
int replaceFrom,
int replaceSize)
431 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << inputStr << replaceFrom << replaceSize;
433 QList<QInputMethodEvent::Attribute> attributes;
435 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
438 attributes.append(attr_cursor);
440 QTextCharFormat format;
441 format.setFontUnderline(
true);
442 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
443 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
447 attributes.append(attr_format);
450 QInputMethodEvent e1(QString(), attributes);
451 e1.setCommitString(inputStr, replaceFrom, replaceSize);
452 QCoreApplication::sendEvent(m_focusObject, &e1);
454 m_preeditString.clear();