From 738a11eeac0ae9056fda9b28563139924a8bc88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Mon, 14 Feb 2022 17:19:40 +0100 Subject: [PATCH] wasm: reliably determine QScreen position MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix bug where QWindow content would be drawn with an offset from the QScreen’s topLeft if the page was scrolled. On Qt for WebAssembly, screen geometry is equivalent to canvas geometry, where the canvas position is the position of the canvas on the document body. getBoundingClientRect() is the correct function to use here, however it returns a rect relative to the viewport. Offsetting this rect by offsetLeft/Top can work in some bases, but this offset is relative to HTMLElement.offsetParent and not necessarily to the document body. Determine the correct QScreen position by subtracting the body position from the canvas position (both relative to the viewport). Change-Id: Ifb7fd28adedbf997d63f79f9b626cfcf3664ff54 Reviewed-by: Lorn Potter --- src/plugins/platforms/wasm/qwasmscreen.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/plugins/platforms/wasm/qwasmscreen.cpp b/src/plugins/platforms/wasm/qwasmscreen.cpp index 11bb57a1ab..51020c45bf 100644 --- a/src/plugins/platforms/wasm/qwasmscreen.cpp +++ b/src/plugins/platforms/wasm/qwasmscreen.cpp @@ -226,14 +226,15 @@ void QWasmScreen::updateQScreenAndCanvasRenderSize() m_canvas.set("width", canvasSize.width()); m_canvas.set("height", canvasSize.height()); - QPoint offset; - offset.setX(m_canvas["offsetLeft"].as()); - offset.setY(m_canvas["offsetTop"].as()); + // Returns the html elments document/body position + auto getElementBodyPosition = [](const emscripten::val &element) -> QPoint { + emscripten::val bodyRect = element["ownerDocument"]["body"].call("getBoundingClientRect"); + emscripten::val canvasRect = element.call("getBoundingClientRect"); + return QPoint (canvasRect["left"].as() - bodyRect["left"].as(), + canvasRect["top"].as() - bodyRect["top"].as()); + }; - emscripten::val rect = m_canvas.call("getBoundingClientRect"); - QPoint position(rect["left"].as() - offset.x(), rect["top"].as() - offset.y()); - - setGeometry(QRect(position, cssSize.toSize())); + setGeometry(QRect(getElementBodyPosition(m_canvas), cssSize.toSize())); m_compositor->requestUpdateAllWindows(); }