summaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/platforms/wasm/qwasminputcontext.cpp156
-rw-r--r--src/plugins/platforms/wasm/qwasminputcontext.h5
-rw-r--r--src/plugins/platforms/wasm/qwasmwindow.cpp6
3 files changed, 117 insertions, 50 deletions
diff --git a/src/plugins/platforms/wasm/qwasminputcontext.cpp b/src/plugins/platforms/wasm/qwasminputcontext.cpp
index a0546fdc215..6dfb4284149 100644
--- a/src/plugins/platforms/wasm/qwasminputcontext.cpp
+++ b/src/plugins/platforms/wasm/qwasminputcontext.cpp
@@ -24,13 +24,13 @@ using namespace qstdweb;
void QWasmInputContext::inputCallback(emscripten::val event)
{
- qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << "isComposing : " << event["isComposing"].as<bool>();
-
emscripten::val inputType = event["inputType"];
if (inputType.isNull() || inputType.isUndefined())
return;
const auto inputTypeString = inputType.as<std::string>();
+ // also may be dataTransfer
+ // containing rich text
emscripten::val inputData = event["data"];
QString inputStr = (!inputData.isNull() && !inputData.isUndefined())
? QString::fromEcmaString(inputData) : QString();
@@ -44,10 +44,10 @@ void QWasmInputContext::inputCallback(emscripten::val event)
QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
QCoreApplication::sendEvent(m_focusObject, &queryEvent);
int cursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
-
int deleteLength = rangesPair.second - rangesPair.first;
int deleteFrom = -1;
- if (cursorPosition > rangesPair.first) {
+
+ if (cursorPosition >= rangesPair.first) {
deleteFrom = -(cursorPosition - rangesPair.first);
}
QInputMethodEvent e;
@@ -65,21 +65,55 @@ void QWasmInputContext::inputCallback(emscripten::val event)
event.call<void>("stopImmediatePropagation");
return;
} else if (!inputTypeString.compare("insertCompositionText")) {
- qCDebug(qLcQpaWasmInputContext) << "inputString : " << inputStr;
- insertPreedit();
+ qCDebug(qLcQpaWasmInputContext) << "insertCompositionText : " << inputStr;
+ event.call<void>("stopImmediatePropagation");
+
+ QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
+ QCoreApplication::sendEvent(m_focusObject, &queryEvent);
+
+ int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt() ;
+ int replaceIndex = (qCursorPosition - rangesPair.first);
+ int replaceLength = rangesPair.second - rangesPair.first;
+
+ setPreeditString(inputStr, replaceIndex);
+ insertPreedit(replaceLength);
+
+ rangesPair.first = 0;
+ rangesPair.second = 0;
event.call<void>("stopImmediatePropagation");
return;
} else if (!inputTypeString.compare("insertReplacementText")) {
- qCDebug(qLcQpaWasmInputContext) << "inputString : " << inputStr;
- //auto ranges = event.call<emscripten::val>("getTargetRanges");
- //qCDebug(qLcQpaWasmInputContext) << ranges["length"].as<int>();
- // WA For Korean IME
- // insertReplacementText should have targetRanges but
- // Safari cannot have it and just it seems to be supposed
- // to replace previous input.
- insertText(inputStr, true);
+ // the previous input string up to the space, needs replaced with this
+ // used on iOS when continuing composition after focus change
+ // there's no range given
+
+ qCDebug(qLcQpaWasmInputContext) << "insertReplacementText >>>>" << "inputString : " << inputStr;
+ emscripten::val ranges = event.call<emscripten::val>("getTargetRanges");
+
+ m_preeditString.clear();
+ std::string elementString = m_inputElement["value"].as<std::string>();
+ QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
+ QCoreApplication::sendEvent(m_focusObject, &queryEvent);
+ QString textFieldString = queryEvent.value(Qt::ImTextBeforeCursor).toString();
+ int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
+
+ if (rangesPair.first != 0 || rangesPair.second != 0) {
+
+ int replaceIndex = (qCursorPosition - rangesPair.first);
+ int replaceLength = rangesPair.second - rangesPair.first;
+ replaceText(inputStr, -replaceIndex, replaceLength);
+ rangesPair.first = 0;
+ rangesPair.second = 0;
+
+ } else {
+ int spaceIndex = textFieldString.lastIndexOf(' ') + 1;
+ int replaceIndex = (qCursorPosition - spaceIndex);
+
+ replaceText(inputStr, -replaceIndex, replaceIndex);
+ }
event.call<void>("stopImmediatePropagation");
+
return;
} else if (!inputTypeString.compare("deleteCompositionText")) {
setPreeditString("", 0);
@@ -92,7 +126,25 @@ void QWasmInputContext::inputCallback(emscripten::val event)
event.call<void>("stopImmediatePropagation");
return;
} else if (!inputTypeString.compare("insertText")) {
- insertText(inputStr);
+ if ((rangesPair.first != 0 || rangesPair.second != 0)
+ && rangesPair.first != rangesPair.second) {
+
+ QInputMethodQueryEvent queryEvent(Qt::ImQueryAll);
+ QCoreApplication::sendEvent(m_focusObject, &queryEvent);
+
+ int qCursorPosition = queryEvent.value(Qt::ImCursorPosition).toInt();
+ int replaceIndex = (qCursorPosition - rangesPair.first);
+ int replaceLength = rangesPair.second - rangesPair.first;
+
+ replaceText(inputStr, -replaceIndex, replaceLength);
+
+ rangesPair.first = 0;
+ rangesPair.second = 0;
+
+ } else {
+ insertText(inputStr);
+ }
+
event.call<void>("stopImmediatePropagation");
#if QT_CONFIG(clipboard)
} else if (!inputTypeString.compare("insertFromPaste")) {
@@ -112,9 +164,8 @@ void QWasmInputContext::inputCallback(emscripten::val event)
void QWasmInputContext::compositionEndCallback(emscripten::val event)
{
const auto inputStr = QString::fromEcmaString(event["data"]);
- qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << inputStr;
- if (preeditString().isEmpty())
+ if (preeditString().isEmpty()) // we get final results from inputCallback
return;
if (inputStr != preeditString()) {
@@ -127,27 +178,11 @@ void QWasmInputContext::compositionEndCallback(emscripten::val event)
void QWasmInputContext::compositionStartCallback(emscripten::val event)
{
- Q_UNUSED(event);
- qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO;
+ Q_UNUSED(event);
// Do nothing when starting composition
}
-/*
-// Test implementation
-static void beforeInputCallback(emscripten::val event)
-{
- qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO;
-
- auto ranges = event.call<emscripten::val>("getTargetRanges");
- auto length = ranges["length"].as<int>();
- for (auto i = 0; i < length; i++) {
- qCDebug(qLcQpaWasmInputContext) << ranges.call<emscripten::val>("get", i)["startOffset"].as<int>();
- qCDebug(qLcQpaWasmInputContext) << ranges.call<emscripten::val>("get", i)["endOffset"].as<int>();
- }
-}
-*/
-
void QWasmInputContext::compositionUpdateCallback(emscripten::val event)
{
const auto compositionStr = QString::fromEcmaString(event["data"]);
@@ -317,18 +352,21 @@ void QWasmInputContext::hideInputPanel()
void QWasmInputContext::setPreeditString(QString preeditStr, int replaceSize)
{
+ qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << preeditStr << replaceSize;
m_preeditString = preeditStr;
- m_replaceSize = replaceSize;
+ m_replaceIndex = replaceSize;
}
-void QWasmInputContext::insertPreedit()
+void QWasmInputContext::insertPreedit(int replaceLength)
{
qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << m_preeditString;
+ if (replaceLength == 0)
+ replaceLength = m_preeditString.length();
QList<QInputMethodEvent::Attribute> attributes;
{
QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
- m_preeditString.length(),
+ 0,
1);
attributes.append(attr_cursor);
@@ -336,21 +374,19 @@ void QWasmInputContext::insertPreedit()
format.setFontUnderline(true);
format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
- 0,
- m_preeditString.length(), format);
+ 0,
+ replaceLength, format);
attributes.append(attr_format);
}
QInputMethodEvent e(m_preeditString, attributes);
- if (m_replaceSize > 0)
- e.setCommitString("", -m_replaceSize, m_replaceSize);
+ if (m_replaceIndex > 0)
+ e.setCommitString("", -m_replaceIndex, replaceLength);
QCoreApplication::sendEvent(m_focusObject, &e);
}
void QWasmInputContext::commitPreeditAndClear()
{
- qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << m_preeditString;
-
if (m_preeditString.isEmpty())
return;
QInputMethodEvent e;
@@ -360,7 +396,8 @@ void QWasmInputContext::commitPreeditAndClear()
}
void QWasmInputContext::insertText(QString inputStr, bool replace)
-{
+{ // commitString
+ qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << inputStr << replace;
Q_UNUSED(replace);
if (!inputStr.isEmpty()) {
const int replaceLen = 0;
@@ -370,4 +407,35 @@ void QWasmInputContext::insertText(QString inputStr, bool replace)
}
}
+/* This will replace the text in the focusobject at replaceFrom position, and replaceSize length
+ with the text in inputStr. */
+
+ void QWasmInputContext::replaceText(QString inputStr, int replaceFrom, int replaceSize)
+ {
+ qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO << inputStr << replaceFrom << replaceSize;
+
+ QList<QInputMethodEvent::Attribute> attributes;
+ {
+ QInputMethodEvent::Attribute attr_cursor(QInputMethodEvent::Cursor,
+ 0, // start
+ 1); // length
+ attributes.append(attr_cursor);
+
+ QTextCharFormat format;
+ format.setFontUnderline(true);
+ format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
+ QInputMethodEvent::Attribute attr_format(QInputMethodEvent::TextFormat,
+ 0,
+ replaceSize,
+ format);
+ attributes.append(attr_format);
+ }
+
+ QInputMethodEvent e1(QString(), attributes);
+ e1.setCommitString(inputStr, replaceFrom, replaceSize);
+ QCoreApplication::sendEvent(m_focusObject, &e1);
+
+ m_preeditString.clear();
+ }
+
QT_END_NAMESPACE
diff --git a/src/plugins/platforms/wasm/qwasminputcontext.h b/src/plugins/platforms/wasm/qwasminputcontext.h
index 97415451b2a..006a03455d7 100644
--- a/src/plugins/platforms/wasm/qwasminputcontext.h
+++ b/src/plugins/platforms/wasm/qwasminputcontext.h
@@ -33,10 +33,11 @@ public:
const QString preeditString() { return m_preeditString; }
void setPreeditString(QString preeditStr, int replaceSize);
- void insertPreedit();
+ void insertPreedit(int repalcementLength = 0);
void commitPreeditAndClear();
void insertText(QString inputStr, bool replace = false);
+ void replaceText(QString inputString, int replaceFrom, int replaceSize);
bool usingTextInput() const { return m_inputMethodAccepted; }
void setFocusObject(QObject *object) override;
@@ -58,7 +59,7 @@ private:
private:
QString m_preeditString;
- int m_replaceSize = 0;
+ int m_replaceIndex = 0;
bool m_inputMethodAccepted = false;
QObject *m_focusObject = nullptr;
diff --git a/src/plugins/platforms/wasm/qwasmwindow.cpp b/src/plugins/platforms/wasm/qwasmwindow.cpp
index a88299975d0..d318c977a90 100644
--- a/src/plugins/platforms/wasm/qwasmwindow.cpp
+++ b/src/plugins/platforms/wasm/qwasmwindow.cpp
@@ -761,12 +761,10 @@ void QWasmWindow::handleCompositionEndEvent(emscripten::val event)
void QWasmWindow::handleBeforeInputEvent(emscripten::val event)
{
- qWarning() << Q_FUNC_INFO;
-
if (QWasmInputContext *inputContext = QWasmIntegration::get()->wasmInputContext(); inputContext->isActive())
inputContext->beforeInputCallback(event);
- // else
- // m_focusHelper.set("innerHTML", std::string());
+ else
+ m_focusHelper.set("innerHTML", std::string());
}
void QWasmWindow::handlePointerEnterLeaveEvent(const PointerEvent &event)