From 070f75d3b0f5313a0f486fe5c811a8e40dd97967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Sat, 23 Feb 2019 09:40:30 +0100 Subject: [PATCH] wasm: improve clipboard fallback path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This improves handling of cut/copy/paste clipboard events, ands allows clipboard access via the common keyboard shortcuts. Make the canvas be eligible for clipboard events by setting the contenteditable attribute. Install clipboard event handlers directly on the canvas. Suppress Ctrl+X/C/V key event handling in the keyboard event handler in order to make the browser generate clipboard events. Send synthetic key events from the clipboard event handlers to make the app copy/paste to Qt’s clipboard at the correct time. Access the system clipboard data using event.clipboardData. Task-number: QTBUG-64638 Change-Id: I584b78ffa2b755b1b76e477b970255c6e5522f6a Reviewed-by: Lorn Potter --- src/plugins/platforms/wasm/qtloader.js | 9 +++- src/plugins/platforms/wasm/qwasmclipboard.cpp | 50 +++++++++++++------ .../platforms/wasm/qwasmeventtranslator.cpp | 8 +++ src/plugins/platforms/wasm/wasm_shell.html | 8 ++- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/plugins/platforms/wasm/qtloader.js b/src/plugins/platforms/wasm/qtloader.js index 84694c7b9fe..203213db56b 100644 --- a/src/plugins/platforms/wasm/qtloader.js +++ b/src/plugins/platforms/wasm/qtloader.js @@ -168,7 +168,14 @@ function QtLoader(config) removeChildren(container); var canvas = document.createElement("canvas"); canvas.className = "QtCanvas" - canvas.style = "height: 100%; width: 100%;" + canvas.style.height = "100%" + canvas.style.width = "100%" + + // Set contentEditable in order to enable clipboard events; hide the resulting focus frame. + canvas.contentEditable = true; + canvas.style.outline = "0px solid transparent"; + canvas.style.cursor = "default"; + return canvas; } diff --git a/src/plugins/platforms/wasm/qwasmclipboard.cpp b/src/plugins/platforms/wasm/qwasmclipboard.cpp index ec058f05dd4..63fea7738de 100644 --- a/src/plugins/platforms/wasm/qwasmclipboard.cpp +++ b/src/plugins/platforms/wasm/qwasmclipboard.cpp @@ -67,23 +67,42 @@ static void qClipboardPromiseResolve(emscripten::val something) pasteClipboardData(emscripten::val("text/plain"), something); } -static void qClipboardCopyTo(val event) +static void qClipboardCutTo(val event) { - val target = event["target"]; - val clipboard = event["clipboardData"]; + if (!QWasmIntegration::get()->getWasmClipboard()->hasClipboardApi) { + // Send synthetic Ctrl+X to make the app cut data to Qt's clipboard + QWindowSystemInterface::handleKeyEvent( + 0, QEvent::KeyPress, Qt::Key_X, Qt::ControlModifier, "X"); + } val module = val::global("Module"); val clipdata = module.call("getClipboardData"); val clipFormat = module.call("getClipboardFormat"); - clipboard.call("setData", clipFormat, clipdata); - target.call("preventDefault"); + event["clipboardData"].call("setData", clipFormat, clipdata); + event.call("preventDefault"); +} + +static void qClipboardCopyTo(val event) +{ + if (!QWasmIntegration::get()->getWasmClipboard()->hasClipboardApi) { + // Send synthetic Ctrl+C to make the app copy data to Qt's clipboard + QWindowSystemInterface::handleKeyEvent( + 0, QEvent::KeyPress, Qt::Key_C, Qt::ControlModifier, "C"); + } + + val module = val::global("Module"); + val clipdata = module.call("getClipboardData"); + val clipFormat = module.call("getClipboardFormat"); + event["clipboardData"].call("setData", clipFormat, clipdata); + event.call("preventDefault"); } static void qClipboardPasteTo(val event) { - val target = event["clipboardData"]; - val module = val::global("Module"); - val clipdata = module.call("getClipboardData"); + bool hasClipboardApi = QWasmIntegration::get()->getWasmClipboard()->hasClipboardApi; + val clipdata = hasClipboardApi ? + val::global("Module").call("getClipboardData") : + event["clipboardData"].call("getData", std::string("text")); const std::string data = clipdata.as(); if (data.length() > 0) { @@ -99,6 +118,7 @@ EMSCRIPTEN_BINDINGS(clipboard_module) { function("getClipboardFormat", &getClipboardFormat); function("pasteClipboardData", &pasteClipboardData); function("qClipboardPromiseResolve", &qClipboardPromiseResolve); + function("qClipboardCutTo", &qClipboardCutTo); function("qClipboardCopyTo", &qClipboardCopyTo); function("qClipboardPasteTo", &qClipboardPasteTo); } @@ -161,7 +181,7 @@ void QWasmClipboard::initClipboardEvents() val permissions = navigator["permissions"]; val clipboard = navigator["clipboard"]; - hasClipboardApi = (!clipboard.isUndefined()); + hasClipboardApi = (!clipboard.isUndefined() && !clipboard["readText"].isUndefined()); if (hasClipboardApi) { val readPermissionsMap = val::object(); readPermissionsMap.set("name", val("clipboard-read")); @@ -172,13 +192,13 @@ void QWasmClipboard::initClipboardEvents() permissions.call("query", writePermissionsMap); } else { - - val window = val::global("window"); - window.call("addEventListener", std::string("paste"), - val::module_property("qClipboardPasteTo")); - - window.call("addEventListener", std::string("copy"), + val canvas = val::module_property("canvas"); + canvas.call("addEventListener", std::string("cut"), + val::module_property("qClipboardCutTo")); + canvas.call("addEventListener", std::string("copy"), val::module_property("qClipboardCopyTo")); + canvas.call("addEventListener", std::string("paste"), + val::module_property("qClipboardPasteTo")); } } diff --git a/src/plugins/platforms/wasm/qwasmeventtranslator.cpp b/src/plugins/platforms/wasm/qwasmeventtranslator.cpp index 3fc8f600a19..05c09ec9a01 100644 --- a/src/plugins/platforms/wasm/qwasmeventtranslator.cpp +++ b/src/plugins/platforms/wasm/qwasmeventtranslator.cpp @@ -832,6 +832,14 @@ bool QWasmEventTranslator::processKeyboard(int eventType, const EmscriptenKeyboa return 0; QFlags mods = translateKeyboardEventModifier(keyEvent); + + // Clipboard fallback path: cut/copy/paste are handled by clipboard event + // handlers if direct clipboard access is not available. + if (!QWasmIntegration::get()->getWasmClipboard()->hasClipboardApi && modifiers & Qt::ControlModifier && + (qtKey == Qt::Key_X || qtKey == Qt::Key_C || qtKey == Qt::Key_V)) { + return 0; + } + bool accepted = false; if (keyType == QEvent::KeyPress && diff --git a/src/plugins/platforms/wasm/wasm_shell.html b/src/plugins/platforms/wasm/wasm_shell.html index 67bfcdfbdc2..110d45e0361 100644 --- a/src/plugins/platforms/wasm/wasm_shell.html +++ b/src/plugins/platforms/wasm/wasm_shell.html @@ -7,7 +7,11 @@ @@ -19,7 +23,7 @@ - +