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>());
51 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType : " << inputTypeString;
52 if (!inputTypeString.compare(
"deleteContentBackward")) {
54 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
55 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
56 int cursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
57 int deleteLength = rangesPair.second - rangesPair.first;
60 if (cursorPosition >= rangesPair.first) {
61 deleteFrom = -(cursorPosition - rangesPair.first);
64 e.setCommitString(QString(), deleteFrom, deleteLength);
65 QCoreApplication::sendEvent(m_focusObject, &e);
68 rangesPair.second = 0;
70 event.call<
void>(
"stopImmediatePropagation");
72 }
else if (!inputTypeString.compare(
"deleteContentForward")) {
73 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier);
74 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Delete, Qt::NoModifier);
75 event.call<
void>(
"stopImmediatePropagation");
77 }
else if (!inputTypeString.compare(
"insertCompositionText")) {
78 qCDebug(qLcQpaWasmInputContext) <<
"insertCompositionText : " << inputStr;
79 event.call<
void>(
"stopImmediatePropagation");
81 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
82 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
84 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt() ;
85 int replaceIndex = (qCursorPosition - rangesPair.first);
86 int replaceLength = rangesPair.second - rangesPair.first;
88 setPreeditString(inputStr, replaceIndex);
89 insertPreedit(replaceLength);
92 rangesPair.second = 0;
93 event.call<
void>(
"stopImmediatePropagation");
95 }
else if (!inputTypeString.compare(
"insertReplacementText")) {
100 qCDebug(qLcQpaWasmInputContext) <<
"insertReplacementText >>>>" <<
"inputString : " << inputStr;
101 emscripten::val ranges = event.call<emscripten::val>(
"getTargetRanges");
103 m_preeditString.clear();
104 std::string elementString = m_inputElement[
"value"].as<std::string>();
105 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
106 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
107 QString textFieldString = queryEvent.value(Qt::ImTextBeforeCursor).toString();
108 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
110 if (rangesPair.first != 0 || rangesPair.second != 0) {
112 int replaceIndex = (qCursorPosition - rangesPair.first);
113 int replaceLength = rangesPair.second - rangesPair.first;
114 replaceText(inputStr, -replaceIndex, replaceLength);
115 rangesPair.first = 0;
116 rangesPair.second = 0;
119 int spaceIndex = textFieldString.lastIndexOf(
' ') + 1;
120 int replaceIndex = (qCursorPosition - spaceIndex);
122 replaceText(inputStr, -replaceIndex, replaceIndex);
125 event.call<
void>(
"stopImmediatePropagation");
128 }
else if (!inputTypeString.compare(
"deleteCompositionText")) {
129 setPreeditString(
"", 0);
131 event.call<
void>(
"stopImmediatePropagation");
133 }
else if (!inputTypeString.compare(
"insertFromComposition")) {
134 setPreeditString(inputStr, 0);
136 event.call<
void>(
"stopImmediatePropagation");
138 }
else if (!inputTypeString.compare(
"insertText")) {
139 if ((rangesPair.first != 0 || rangesPair.second != 0)
140 && rangesPair.first != rangesPair.second) {
142 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
143 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
145 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
146 int replaceIndex = (qCursorPosition - rangesPair.first);
147 int replaceLength = rangesPair.second - rangesPair.first;
149 replaceText(inputStr, -replaceIndex, replaceLength);
151 rangesPair.first = 0;
152 rangesPair.second = 0;
155 insertText(inputStr);
158 event.call<
void>(
"stopImmediatePropagation");
159 }
else if (!inputTypeString.compare(
"insertFromPaste")) {
160 insertText(inputStr);
161 event.call<
void>(
"stopImmediatePropagation");
167 qCWarning(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType \"" <<
168 inputType.as<std::string>() <<
"\" is not supported in Qt yet";
247void QWasmInputContext::updateGeometry()
249 if (QWasmAccessibility::isEnabled())
252 if (m_inputElement.isNull())
255 const QWindow *focusWindow = QGuiApplication::focusWindow();
256 if (!m_focusObject || !focusWindow || !m_inputMethodAccepted) {
257 m_inputElement[
"style"].set(
"left",
"0px");
258 m_inputElement[
"style"].set(
"top",
"0px");
260 Q_ASSERT(focusWindow);
261 Q_ASSERT(m_focusObject);
262 Q_ASSERT(m_inputMethodAccepted);
264 const QRect inputItemRectangle = QPlatformInputContext::inputItemRectangle().toRect();
265 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"propagating inputItemRectangle:" << inputItemRectangle;
266 m_inputElement[
"style"].set(
"left", std::to_string(inputItemRectangle.x()) +
"px");
267 m_inputElement[
"style"].set(
"top", std::to_string(inputItemRectangle.y()) +
"px");
268 m_inputElement[
"style"].set(
"width",
"1px");
269 m_inputElement[
"style"].set(
"height",
"1px");
384void QWasmInputContext::insertPreedit(
int replaceLength)
386 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << m_preeditString;
387 if (replaceLength == 0)
388 replaceLength = m_preeditString.length();
390 QList<QInputMethodEvent::Attribute> attributes;
392 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
395 attributes.append(attr_cursor);
397 QTextCharFormat format;
398 format.setFontUnderline(
true);
399 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
400 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
402 replaceLength, format);
403 attributes.append(attr_format);
406 QInputMethodEvent e(m_preeditString, attributes);
407 if (m_replaceIndex > 0)
408 e.setCommitString(
"", -m_replaceIndex, replaceLength);
409 QCoreApplication::sendEvent(m_focusObject, &e);
437 void QWasmInputContext::replaceText(QString inputStr,
int replaceFrom,
int replaceSize)
439 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << inputStr << replaceFrom << replaceSize;
441 QList<QInputMethodEvent::Attribute> attributes;
443 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
446 attributes.append(attr_cursor);
448 QTextCharFormat format;
449 format.setFontUnderline(
true);
450 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
451 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
455 attributes.append(attr_format);
458 QInputMethodEvent e1(QString(), attributes);
459 e1.setCommitString(inputStr, replaceFrom, replaceSize);
460 QCoreApplication::sendEvent(m_focusObject, &e1);
462 m_preeditString.clear();