diff options
author | Lorn Potter <lorn.potter@gmail.com> | 2022-10-04 13:50:03 +1000 |
---|---|---|
committer | Lorn Potter <lorn.potter@gmail.com> | 2023-03-23 11:59:38 +1000 |
commit | 49ddb4b28af24ac2a0d9c07a910443dece2550ec (patch) | |
tree | cbcebd911517abe06861d51031109d35cacdd63a | |
parent | cb8f62660eb29abf332d2503dc31ace0b7552ea8 (diff) |
wasm: add support for input languages win32 mobile
on Windows virtual keyboard sends characters through the textInput handler,
we can use the same mechanism as Android to create an offscreen
input element and send the characters from there.
Fixes: QTBUG-107139
Pick-to: 6.5
Change-Id: I84a143aedf93d22521fcfa368532eed8c0e7d002
Reviewed-by: MikoĊaj Boc <Mikolaj.Boc@qt.io>
-rw-r--r-- | src/plugins/platforms/wasm/qwasminputcontext.cpp | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/plugins/platforms/wasm/qwasminputcontext.cpp b/src/plugins/platforms/wasm/qwasminputcontext.cpp index 28695cf6b6..157f96fe49 100644 --- a/src/plugins/platforms/wasm/qwasminputcontext.cpp +++ b/src/plugins/platforms/wasm/qwasminputcontext.cpp @@ -24,13 +24,12 @@ static void inputCallback(emscripten::val event) if (length <= 0) return; - // use only last character - emscripten::val _incomingCharVal = event["target"]["value"][length - 1]; + emscripten::val _incomingCharVal = event["data"]; if (_incomingCharVal != emscripten::val::undefined() && _incomingCharVal != emscripten::val::null()) { QString str = QString::fromStdString(_incomingCharVal.as<std::string>()); QWasmInputContext *wasmInput = - reinterpret_cast<QWasmInputContext*>(event["target"]["data-context"].as<quintptr>()); + reinterpret_cast<QWasmInputContext*>(event["target"]["data-qinputcontext"].as<quintptr>()); wasmInput->inputStringChanged(str, wasmInput); } // this clears the input string, so backspaces do not send a character @@ -39,7 +38,7 @@ static void inputCallback(emscripten::val event) } EMSCRIPTEN_BINDINGS(clipboard_module) { - function("qt_InputContextCallback", &inputCallback); + function("qtInputContextCallback", &inputCallback); } QWasmInputContext::QWasmInputContext() @@ -50,23 +49,24 @@ QWasmInputContext::QWasmInputContext() m_inputElement.set("style", "position:absolute;left:-1000px;top:-1000px"); // offscreen m_inputElement.set("contenteditable","true"); - if (platform() == Platform::Android) { - emscripten::val body = document["body"]; - body.call<void>("appendChild", m_inputElement); + if (platform() == Platform::Android || platform() == Platform::Windows) { + const std::string inputType = platform() == Platform::Windows ? "textInput" : "input"; - m_inputElement.call<void>("addEventListener", std::string("input"), - emscripten::val::module_property("qt_InputContextCallback"), + document.call<void>("addEventListener", inputType, + emscripten::val::module_property("qtInputContextCallback"), emscripten::val(false)); - m_inputElement.set("data-context", + m_inputElement.set("data-qinputcontext", emscripten::val(quintptr(reinterpret_cast<void *>(this)))); + emscripten::val body = document["body"]; + body.call<void>("appendChild", m_inputElement); - // android sends Enter through target window, let's just handle this here + // Enter is sent through target window, let's just handle this here emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, (void *)this, 1, &androidKeyboardCallback); } - if (platform() == Platform::MacOS || platform() == Platform::iPhone) - { + + if (platform() == Platform::MacOS || platform() == Platform::iPhone) { auto callback = [=](emscripten::val) { m_inputElement["parentElement"].call<void>("removeChild", m_inputElement); inputPanelIsOpen = false; @@ -80,7 +80,7 @@ QWasmInputContext::QWasmInputContext() QWasmInputContext::~QWasmInputContext() { - if (platform() == Platform::Android) + if (platform() == Platform::Android || platform() == Platform::Windows) emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 0, NULL); } @@ -104,8 +104,10 @@ void QWasmInputContext::update(Qt::InputMethodQueries queries) void QWasmInputContext::showInputPanel() { if (platform() == Platform::Windows - && inputPanelIsOpen) // call this only once for win32 - return; + && !inputPanelIsOpen) { // call this only once for win32 + m_inputElement.call<void>("focus"); + return; + } // this is called each time the keyboard is touched // Add the input element as a child of the screen for the |