From 2ed3cc8e7a7622ba4e464c3be7d4d1ed44d1ab92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Mon, 29 Nov 2021 09:26:50 +0100 Subject: [PATCH] wasm: move canvas configuration to Qt implementation The required canvas configuration is an implementation detail; move it out of the .html file (which we expect that users will replace), and into the QWasmScreen constructor. This also enables using a div element as the container element (instead of a canvas), since Qt then can create and configure the canvas during QWasmScreen construction. Change-Id: Ia849517d00fa3e8ec307065a524c0c91296dd490 Reviewed-by: Lorn Potter --- src/plugins/platforms/wasm/qwasmscreen.cpp | 18 ++++++++++++++++++ src/plugins/platforms/wasm/qwasmscreen.h | 2 ++ src/plugins/platforms/wasm/wasm_shell.html | 10 +++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/plugins/platforms/wasm/qwasmscreen.cpp b/src/plugins/platforms/wasm/qwasmscreen.cpp index eb77dc65b9..11bb57a1ab 100644 --- a/src/plugins/platforms/wasm/qwasmscreen.cpp +++ b/src/plugins/platforms/wasm/qwasmscreen.cpp @@ -57,6 +57,24 @@ QWasmScreen::QWasmScreen(const emscripten::val &canvas) , m_compositor(new QWasmCompositor(this)) , m_eventTranslator(new QWasmEventTranslator()) { + // Configure canvas + emscripten::val style = m_canvas["style"]; + style.set("border", std::string("0px none")); + style.set("background-color", std::string("white")); + + // Set contenteditable so that the canvas gets clipboard events, + // then hide the resulting focus frame, and reset the cursor. + m_canvas.set("contentEditable", std::string("true")); + style.set("outline", std::string("0px solid transparent")); + style.set("caret-color", std::string("transparent")); + style.set("cursor", std::string("default")); + + // Disable the default context menu; Qt applications typically + // provide custom right-click behavior. + m_onContextMenu = std::make_unique(m_canvas, "contextmenu", [](emscripten::val event){ + event.call("preventDefault"); + }); + updateQScreenAndCanvasRenderSize(); m_canvas.call("focus"); } diff --git a/src/plugins/platforms/wasm/qwasmscreen.h b/src/plugins/platforms/wasm/qwasmscreen.h index 4acf90d515..41b5aeeb2f 100644 --- a/src/plugins/platforms/wasm/qwasmscreen.h +++ b/src/plugins/platforms/wasm/qwasmscreen.h @@ -36,6 +36,7 @@ #include #include +#include #include @@ -93,6 +94,7 @@ private: QImage::Format m_format = QImage::Format_RGB32; QWasmCursor m_cursor; static const char * m_canvasResizeObserverCallbackContextPropertyName; + std::unique_ptr m_onContextMenu; }; QT_END_NAMESPACE diff --git a/src/plugins/platforms/wasm/wasm_shell.html b/src/plugins/platforms/wasm/wasm_shell.html index f5712d0418..5c30c8fadc 100644 --- a/src/plugins/platforms/wasm/wasm_shell.html +++ b/src/plugins/platforms/wasm/wasm_shell.html @@ -13,12 +13,8 @@ @@ -30,7 +26,7 @@ - +