diff --git a/src/plugins/platforms/wasm/qwasmclipboard.cpp b/src/plugins/platforms/wasm/qwasmclipboard.cpp index 2d00d9f723a..5112c3ae462 100644 --- a/src/plugins/platforms/wasm/qwasmclipboard.cpp +++ b/src/plugins/platforms/wasm/qwasmclipboard.cpp @@ -46,7 +46,7 @@ static void commonCopyEvent(val event) event.call("preventDefault"); } -static void qClipboardCutTo(val event) +void QWasmClipboard::cut(val event) { QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext(); if (wasmInput && wasmInput->usingTextInput()) @@ -61,7 +61,7 @@ static void qClipboardCutTo(val event) commonCopyEvent(event); } -static void qClipboardCopyTo(val event) +void QWasmClipboard::copy(val event) { QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext(); if (wasmInput && wasmInput->usingTextInput()) @@ -75,7 +75,7 @@ static void qClipboardCopyTo(val event) commonCopyEvent(event); } -static void qClipboardPasteTo(val event) +void QWasmClipboard::paste(val event) { QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext(); if (wasmInput && wasmInput->usingTextInput()) @@ -86,10 +86,26 @@ static void qClipboardPasteTo(val event) QWasmIntegration::get()->getWasmClipboard()->sendClipboardData(event); } +void QWasmClipboard::beforeInput(emscripten::val event) +{ + event.call("preventDefault"); + event.call("stopPropagation"); +} + +void QWasmClipboard::input(emscripten::val event) +{ + event.call("preventDefault"); + event.call("stopPropagation"); + event["target"].set("innerHTML", std::string()); + event["target"].set("value", std::string()); +} + EMSCRIPTEN_BINDINGS(qtClipboardModule) { - function("qtClipboardCutTo", &qClipboardCutTo); - function("qtClipboardCopyTo", &qClipboardCopyTo); - function("qtClipboardPasteTo", &qClipboardPasteTo); + function("qtClipboardCutTo", &QWasmClipboard::cut); + function("qtClipboardCopyTo", &QWasmClipboard::copy); + function("qtClipboardPasteTo", &QWasmClipboard::paste); + function("qtClipboardBeforeInput", &QWasmClipboard::beforeInput); + function("qtClipboardInput", &QWasmClipboard::input); } QWasmClipboard::QWasmClipboard() @@ -167,7 +183,7 @@ void QWasmClipboard::initClipboardPermissions() })()); } -void QWasmClipboard::installEventHandlers(const emscripten::val &target) +void QWasmClipboard::installEventHandlers(emscripten::val &target) { emscripten::val cContext = val::undefined(); emscripten::val isChromium = val::global("window")["chrome"]; @@ -175,7 +191,17 @@ void QWasmClipboard::installEventHandlers(const emscripten::val &target) cContext = val::global("document"); } else { cContext = target; + // Set contentEditable so that the window gets clipboard events, + // then hide the resulting focus frame. + cContext.set("contentEditable", std::string("true")); + cContext["style"].set("outline", std::string("none")); + + cContext.call("addEventListener", val("beforeinput"), + val::module_property("qtClipboardBeforeInput"), true); + cContext.call("addEventListener", val("input"), + val::module_property("qtClipboardInput"), true); } + // Fallback path for browsers which do not support direct clipboard access cContext.call("addEventListener", val("cut"), val::module_property("qtClipboardCutTo"), true); diff --git a/src/plugins/platforms/wasm/qwasmclipboard.h b/src/plugins/platforms/wasm/qwasmclipboard.h index 86618dd5604..67c65613c90 100644 --- a/src/plugins/platforms/wasm/qwasmclipboard.h +++ b/src/plugins/platforms/wasm/qwasmclipboard.h @@ -36,10 +36,16 @@ public: bool ownsMode(QClipboard::Mode mode) const override; ProcessKeyboardResult processKeyboard(const KeyEvent &event); - static void installEventHandlers(const emscripten::val &target); + static void installEventHandlers(emscripten::val &target); bool hasClipboardApi(); void sendClipboardData(emscripten::val event); + static void cut(emscripten::val event); + static void copy(emscripten::val event); + static void paste(emscripten::val event); + static void beforeInput(emscripten::val event); + static void input(emscripten::val event); + private: void initClipboardPermissions(); void writeToClipboardApi(); diff --git a/src/plugins/platforms/wasm/qwasmwindow.cpp b/src/plugins/platforms/wasm/qwasmwindow.cpp index 22935ccac80..c9769302edd 100644 --- a/src/plugins/platforms/wasm/qwasmwindow.cpp +++ b/src/plugins/platforms/wasm/qwasmwindow.cpp @@ -56,6 +56,7 @@ QWasmWindow::QWasmWindow(QWindow *w, QWasmDeadKeySupport *deadKeySupport, m_document(dom::document()), m_decoratedWindow(m_document.call("createElement", emscripten::val("div"))), m_window(m_document.call("createElement", emscripten::val("div"))), + m_windowInput(m_document.call("createElement", emscripten::val("div"))), m_a11yContainer(m_document.call("createElement", emscripten::val("div"))), m_canvas(m_document.call("createElement", emscripten::val("canvas"))) { @@ -70,18 +71,17 @@ QWasmWindow::QWasmWindow(QWindow *w, QWasmDeadKeySupport *deadKeySupport, m_window.set("className", "qt-window"); m_decoratedWindow.call("appendChild", m_window); + m_window.call("appendChild", m_windowInput); m_canvas["classList"].call("add", emscripten::val("qt-window-canvas")); - // Set contentEditable so that the window gets clipboard events, - // then hide the resulting focus frame. - m_window.set("contentEditable", std::string("true")); - m_window["style"].set("outline", std::string("none")); - - QWasmClipboard::installEventHandlers(m_window); +#if QT_CONFIG(clipboard) + QWasmClipboard::installEventHandlers(m_windowInput); +#endif // Set inputMode to none to stop the mobile keyboard from opening // when the user clicks on the window. m_window.set("inputMode", std::string("none")); + m_windowInput.set("inputMode", std::string("none")); // Hide the canvas from screen readers. m_canvas.call("setAttribute", std::string("aria-hidden"), std::string("true")); @@ -713,7 +713,7 @@ void QWasmWindow::requestActivateWindow() void QWasmWindow::focus() { - m_canvas.call("focus"); + m_windowInput.call("focus"); } bool QWasmWindow::setMouseGrabEnabled(bool grab) diff --git a/src/plugins/platforms/wasm/qwasmwindow.h b/src/plugins/platforms/wasm/qwasmwindow.h index 7a24b41cb08..9f3b484aeeb 100644 --- a/src/plugins/platforms/wasm/qwasmwindow.h +++ b/src/plugins/platforms/wasm/qwasmwindow.h @@ -140,6 +140,7 @@ private: emscripten::val m_document; emscripten::val m_decoratedWindow; emscripten::val m_window; + emscripten::val m_windowInput; emscripten::val m_a11yContainer; emscripten::val m_canvas; emscripten::val m_context2d = emscripten::val::undefined();