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>();
33 emscripten::val inputData = event[
"data"];
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")
59 || !inputTypeString.compare(
"deleteContent")) {
63 const ResolvedRange resolved = resolveRange(getFirstRange());
65 const int deleteFrom = resolved.replaceFrom <= 0 ? resolved.replaceFrom : -1;
66 commitText(QString(), deleteFrom, resolved.replaceLength);
67 }
else if (!inputTypeString.compare(
"deleteContentForward")) {
68 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier);
69 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Delete, Qt::NoModifier);
70 }
else if (!inputTypeString.compare(
"deleteWordBackward")) {
71 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Backspace, Qt::ControlModifier);
72 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Backspace, Qt::ControlModifier);
73 }
else if (!inputTypeString.compare(
"deleteWordForward")) {
74 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Delete, Qt::ControlModifier);
75 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Delete, Qt::ControlModifier);
76 }
else if (!inputTypeString.compare(
"insertParagraph")) {
77 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
78 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Return, Qt::NoModifier);
79 }
else if (!inputTypeString.compare(
"insertLineBreak")) {
80 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_Return, Qt::ShiftModifier);
81 QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_Return, Qt::ShiftModifier);
82 }
else if (!inputTypeString.compare(
"insertCompositionText")) {
84 qCDebug(qLcQpaWasmInputContext) <<
"insertCompositionText : " << inputStr;
85 if (event[
"isComposing"].as<
bool>()) {
86 const CompositionRange range = getFirstRange();
87 setPreeditString(inputStr);
88 insertPreedit(range.end - range.start);
90 }
else if (!inputTypeString.compare(
"insertReplacementText")) {
92 qCDebug(qLcQpaWasmInputContext) <<
"insertReplacementText >>>>" <<
"inputString : " << inputStr;
93 const CompositionRange range = getFirstRange();
96 const ResolvedRange resolved = resolveRange(range);
97 commitText(inputStr, resolved.replaceFrom, resolved.replaceLength);
99 QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
100 QCoreApplication::sendEvent(m_focusObject, &queryEvent);
101 int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
102 QString textFieldString = queryEvent.value(Qt::ImTextBeforeCursor).toString();
104 int spaceIndex = textFieldString.lastIndexOf(
' ') + 1;
105 int replaceIndex = (qCursorPosition - spaceIndex);
106 commitText(inputStr, -replaceIndex, replaceIndex);
108 }
else if (!inputTypeString.compare(
"deleteCompositionText")) {
109 setPreeditString(
"");
111 }
else if (!inputTypeString.compare(
"insertFromComposition")) {
112 setPreeditString(inputStr);
114 }
else if (!inputTypeString.compare(
"insertText")) {
115 const CompositionRange range = getFirstRange();
117 if (range.isValid && range.start != range.end) {
118 const ResolvedRange resolved = resolveRange(range);
119 commitText(inputStr, resolved.replaceFrom, resolved.replaceLength);
121 commitText(inputStr);
123 }
else if (!inputTypeString.compare(
"insertFromPaste")) {
124 commitText(inputStr);
130 qCWarning(qLcQpaWasmInputContext) << Q_FUNC_INFO <<
"inputType \"" <<
131 inputType.as<std::string>() <<
"\" is not supported in Qt yet";
135 event.call<
void>(
"stopPropagation");
140 m_compositionRange = emscripten::val::null();
354void QWasmInputContext::insertPreedit(
int replaceLength)
356 qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << m_preeditString;
357 if (replaceLength == 0)
358 replaceLength = m_preeditString.length();
360 QList<QInputMethodEvent::Attribute> attributes;
362 QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
365 attributes.append(attr_cursor);
367 QTextCharFormat format;
368 format.setFontUnderline(
true);
369 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
370 QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
372 replaceLength, format);
373 attributes.append(attr_format);
376 QInputMethodEvent e(m_preeditString, attributes);
377 QCoreApplication::sendEvent(m_focusObject, &e);