summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmwindow.cpp
diff options
context:
space:
mode:
authorMorten Sørvig <[email protected]>2025-01-27 16:01:24 +0100
committerMorten Sørvig <[email protected]>2025-01-29 13:59:09 +0100
commita9bbcdd7bf0580cf7c0a8faa9779357758d45681 (patch)
tree70859a97c45a254c6424e8d65f302200ede92ebc /src/plugins/platforms/wasm/qwasmwindow.cpp
parentadce1fe9b5b707dfd0ef492af7a61e6f6f735a1d (diff)
wasm: support foreign windows
Add support for embedding native html elements using QWindow::fromWinId(). WId is an emscripten::val *, e.g. a pointer to val which holds a html element. The element can be created either from C++ using emscripten::val, or from JavaScript. User code owns the val * as usual for WId; ownership is not passed to the QWindow instance. Set QWasmWindow::m_window to be the native element when fromWinId() is used, and skip the rest of the QWasmWindow implementation in that case: We don't need to install event handlers or provide accessibility elements. Make key and pointer event handlers stop propagation only if the event was not accepted. This makes sure that input events reach the embedded native element. Limit setPointerCapture calls to when the event is targeted for Qt elements only. Determining the true target can be a bit tricky when shadow DOM is in use since the browsers may retarget the event. Use composedPath() to get the true event target. Task-number: QTBUG-128804 Task-number: QTBUG-128732 Change-Id: I5ce66e93bacb06abfd042916687cd45fc9588c51 Reviewed-by: Morten Johan Sørvig <[email protected]>
Diffstat (limited to 'src/plugins/platforms/wasm/qwasmwindow.cpp')
-rw-r--r--src/plugins/platforms/wasm/qwasmwindow.cpp41
1 files changed, 32 insertions, 9 deletions
diff --git a/src/plugins/platforms/wasm/qwasmwindow.cpp b/src/plugins/platforms/wasm/qwasmwindow.cpp
index 036de1514bd..dc092416bd8 100644
--- a/src/plugins/platforms/wasm/qwasmwindow.cpp
+++ b/src/plugins/platforms/wasm/qwasmwindow.cpp
@@ -48,7 +48,7 @@ QWasmWindowStack::PositionPreference positionPreferenceFromWindowFlags(Qt::Windo
Q_GUI_EXPORT int qt_defaultDpiX();
QWasmWindow::QWasmWindow(QWindow *w, QWasmDeadKeySupport *deadKeySupport,
- QWasmCompositor *compositor, QWasmBackingStore *backingStore)
+ QWasmCompositor *compositor, QWasmBackingStore *backingStore, WId nativeHandle)
: QPlatformWindow(w),
m_compositor(compositor),
m_backingStore(backingStore),
@@ -65,6 +65,22 @@ QWasmWindow::QWasmWindow(QWindow *w, QWasmDeadKeySupport *deadKeySupport,
m_nonClientArea = std::make_unique<NonClientArea>(this, m_decoratedWindow);
m_nonClientArea->titleBar()->setTitle(window()->title());
+ // If we are wrapping a foregin window, a.k.a. a native html element then that element becomes
+ // the m_window element. In this case setting up event handlers and accessibility etc is not
+ // needed since that is (presumably) handled by the native html element.
+ //
+ // The WId is an emscripten::val *, owned by QWindow user code. We dereference and make
+ // a copy of the val here and don't strictly need it to be kept alive, but that's an
+ // implementation detail. The pointer will be dereferenced again if the window is destroyed
+ // and recreated.
+ if (nativeHandle) {
+ m_window = *(emscripten::val *)(nativeHandle);
+ m_winId = nativeHandle;
+ m_decoratedWindow.set("id", "qt-window-" + std::to_string(m_winId));
+ m_decoratedWindow.call<void>("appendChild", m_window);
+ return;
+ }
+
m_window.set("className", "qt-window");
m_decoratedWindow.call<void>("appendChild", m_window);
@@ -91,8 +107,8 @@ QWasmWindow::QWasmWindow(QWindow *w, QWasmDeadKeySupport *deadKeySupport,
const bool rendersTo2dContext = w->surfaceType() != QSurface::OpenGLSurface;
if (rendersTo2dContext)
m_context2d = m_canvas.call<emscripten::val>("getContext", emscripten::val("2d"));
- static int serialNo = 0;
- m_winId = ++serialNo;
+
+ m_winId = WId(&m_window);
m_decoratedWindow.set("id", "qt-window-" + std::to_string(m_winId));
emscripten::val::module_property("specialHTMLTargets").set(canvasSelector(), m_canvas);
@@ -540,9 +556,10 @@ void QWasmWindow::commitParent(QWasmWindowTreeNode *parent)
void QWasmWindow::handleKeyEvent(const KeyEvent &event)
{
qCDebug(qLcQpaWasmInputContext) << "processKey as KeyEvent";
- if (processKey(event))
+ if (processKey(event)) {
event.webEvent.call<void>("preventDefault");
- event.webEvent.call<void>("stopPropagation");
+ event.webEvent.call<void>("stopPropagation");
+ }
}
bool QWasmWindow::processKey(const KeyEvent &event)
@@ -667,14 +684,17 @@ void QWasmWindow::processPointer(const PointerEvent &event)
{
switch (event.type) {
case EventType::PointerDown:
- m_window.call<void>("setPointerCapture", event.pointerId);
+ if (event.isTargetedForQtElement())
+ m_window.call<void>("setPointerCapture", event.pointerId);
+
if ((window()->flags() & Qt::WindowDoesNotAcceptFocus)
!= Qt::WindowDoesNotAcceptFocus
&& window()->isTopLevel())
window()->requestActivate();
break;
case EventType::PointerUp:
- m_window.call<void>("releasePointerCapture", event.pointerId);
+ if (event.isTargetedForQtElement())
+ m_window.call<void>("releasePointerCapture", event.pointerId);
break;
default:
break;
@@ -683,8 +703,11 @@ void QWasmWindow::processPointer(const PointerEvent &event)
const bool eventAccepted = deliverPointerEvent(event);
if (!eventAccepted && event.type == EventType::PointerDown)
QGuiApplicationPrivate::instance()->closeAllPopups();
- event.webEvent.call<void>("preventDefault");
- event.webEvent.call<void>("stopPropagation");
+
+ if (eventAccepted) {
+ event.webEvent.call<void>("preventDefault");
+ event.webEvent.call<void>("stopPropagation");
+ }
}
bool QWasmWindow::deliverPointerEvent(const PointerEvent &event)