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");
88 int replaceLength = rangesPair.second - rangesPair.first;
90 setPreeditString(inputStr, 0);
91 insertPreedit(replaceLength);
94 rangesPair.second = 0;
95 event.call<
void>(
"stopImmediatePropagation");
97 }
else if (!inputTypeString.compare(
"insertReplacementText")) {
99 qCDebug(qLcQpaWasmInputContext) <<
"insertReplacementText >>>>" <<
"inputString : " << inputStr;
101 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
102 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
103 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
105 if (rangesPair.first != 0 || rangesPair.second != 0) {
106 int replaceLength = rangesPair.second - rangesPair.first;
108 replaceText(inputStr, -replaceLength, replaceLength);
110 rangesPair.first = 0;
111 rangesPair.second = 0;
114 QString textFieldString = queryEvent.value(Qt::ImTextBeforeCursor).toString();
115 int spaceIndex = textFieldString.lastIndexOf(
' ') + 1;
116 int replaceIndex = (qCursorPosition - spaceIndex);
118 replaceText(inputStr, -replaceIndex, replaceIndex);
121 event.call<
void>(
"stopImmediatePropagation");
124 }
else if (!inputTypeString.compare(
"deleteCompositionText")) {
125 setPreeditString(
"", 0);
127 event.call<
void>(
"stopImmediatePropagation");
129 }
else if (!inputTypeString.compare(
"insertFromComposition")) {
130 setPreeditString(inputStr, 0);
132 event.call<
void>(
"stopImmediatePropagation");
134 }
else if (!inputTypeString.compare(
"insertText")) {
135 if ((rangesPair.first != 0 || rangesPair.second != 0)
136 && rangesPair.first != rangesPair.second) {
138 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
139 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
141 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
142 int replaceIndex = (qCursorPosition - rangesPair.first);
143 int replaceLength = rangesPair.second - rangesPair.first;
145 replaceText(inputStr, replaceIndex, replaceLength);
147 rangesPair.first = 0;
148 rangesPair.second = 0;
151 insertText(inputStr);
154 event.call<
void>(
"stopImmediatePropagation");
155 }
else if (!inputTypeString.compare(
"insertFromPaste")) {
156 insertText(inputStr);
157 event.call<
void>(
"stopImmediatePropagation");
163 qCWarning(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType \"" <<
164 inputType.as<std::string>() <<
"\" is not supported in Qt yet";
243void QWasmInputContext::updateGeometry()
245 if (QWasmAccessibility::isEnabled())
248 if (m_inputElement.isNull())
251 const QWindow *focusWindow = QGuiApplication::focusWindow();
252 if (!m_focusObject || !focusWindow || !m_inputMethodAccepted) {
253 m_inputElement[
"style"].set(
"left",
"0px");
254 m_inputElement[
"style"].set(
"top",
"0px");
256 Q_ASSERT(focusWindow);
257 Q_ASSERT(m_focusObject);
258 Q_ASSERT(m_inputMethodAccepted);
260 const QRect inputItemRectangle = QPlatformInputContext::inputItemRectangle().toRect();
261 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"propagating inputItemRectangle:" << inputItemRectangle;
262 m_inputElement[
"style"].set(
"left", std::to_string(inputItemRectangle.x()) +
"px");
263 m_inputElement[
"style"].set(
"top", std::to_string(inputItemRectangle.y()) +
"px");
264 m_inputElement[
"style"].set(
"width",
"1px");
265 m_inputElement[
"style"].set(
"height",
"1px");
380void QWasmInputContext::insertPreedit(
int replaceLength)
382 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << m_preeditString;
383 if (replaceLength == 0)
384 replaceLength = m_preeditString.length();
386 QList<QInputMethodEvent::Attribute> attributes;
388 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
391 attributes.append(attr_cursor);
393 QTextCharFormat format;
394 format.setFontUnderline(
true);
395 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
396 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
398 replaceLength, format);
399 attributes.append(attr_format);
402 QInputMethodEvent e(m_preeditString, attributes);
403 if (m_replaceIndex > 0)
404 e.setCommitString(
"", -m_replaceIndex, replaceLength);
405 QCoreApplication::sendEvent(m_focusObject, &e);
433 void QWasmInputContext::replaceText(QString inputStr,
int replaceFrom,
int replaceSize)
435 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << inputStr << replaceFrom << replaceSize;
437 QList<QInputMethodEvent::Attribute> attributes;
439 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
442 attributes.append(attr_cursor);
444 QTextCharFormat format;
445 format.setFontUnderline(
true);
446 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
447 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
451 attributes.append(attr_format);
454 QInputMethodEvent e1(QString(), attributes);
455 e1.setCommitString(inputStr, replaceFrom, replaceSize);
456 QCoreApplication::sendEvent(m_focusObject, &e1);
458 m_preeditString.clear();