12#include <emscripten/bind.h>
13#include <emscripten/val.h>
15#include <qpa/qwindowsysteminterface.h>
16#include <QtCore/qcoreapplication.h>
17#include <QtGui/qguiapplication.h>
18#include <private/qhighdpiscaling_p.h>
26const char *QWasmScreen::m_canvasResizeObserverCallbackContextPropertyName =
27 "data-qtCanvasResizeObserverCallbackContext";
30 : m_container(containerOrCanvas),
35 auto document = m_container[
"ownerDocument"];
39 if (m_container[
"tagName"].call<std::string>(
"toLowerCase") ==
"canvas") {
40 qWarning() <<
"Support for canvas elements as an element backing screen is deprecated. The "
41 "canvas provided for the screen will be transformed into a div.";
42 auto container = document.call<emscripten::val>(
"createElement", emscripten::val(
"div"));
43 m_container[
"parentNode"].call<
void>(
"replaceChild", container, m_container);
44 m_container = container;
46 auto shadowOptions = emscripten::val::object();
47 shadowOptions.set(
"mode",
"open");
48 auto shadow = m_container.call<emscripten::val>(
"attachShadow", shadowOptions);
50 m_shadowContainer = document.call<emscripten::val>(
"createElement", emscripten::val(
"div"));
54 shadow.call<
void>(
"appendChild", m_shadowContainer);
56 m_shadowContainer.set(
"id", std::string(
"qt-screen-") + std::to_string(uintptr_t(
this)));
58 m_shadowContainer[
"classList"].call<
void>(
"add", std::string(
"qt-screen"));
62 m_onContextMenu = std::make_unique<qstdweb::EventCallback>(
63 m_shadowContainer,
"contextmenu",
64 [](emscripten::val
event) {
event.call<
void>(
"preventDefault"); });
68 emscripten::val::module_property(
"specialHTMLTargets")
71 emscripten::val::module_property(
"specialHTMLTargets")
75 m_shadowContainer.call<
void>(
"focus");
77 m_touchDevice = std::make_unique<QPointingDevice>(
89 emscripten::val::module_property(
"specialHTMLTargets")
90 .set(
eventTargetId().toStdString(), emscripten::val::undefined());
92 m_shadowContainer.set(m_canvasResizeObserverCallbackContextPropertyName,
116 return m_compositor.get();
121 return m_shadowContainer;
128 return QString(
"!qtcanvas_%1").
arg(uintptr_t(
this));
133 return QString(
"!outerscreen_%1").
arg(uintptr_t(
this));
153 emscripten::val dpi = emscripten::val::module_property(
"qtFontDpi");
154 if (!dpi.isUndefined()) {
156 return QDpi(dpiValue, dpiValue);
158 const qreal defaultDpi = 96;
159 return QDpi(defaultDpi, defaultDpi);
176 double dpr = emscripten::val::global(
"window")[
"devicePixelRatio"].as<
double>();
177 emscripten::val visualViewport = emscripten::val::global(
"window")[
"visualViewport"];
178 double scale = visualViewport.isUndefined() ? 1.0 : visualViewport[
"scale"].as<
double>();
179 double effectiveDevicePixelRatio = std::min(
dpr *
scale,
dpr);
180 return qreal(effectiveDevicePixelRatio);
185 return QString::fromJsString(m_shadowContainer[
"id"]);
213 return found !=
childStack().
end() ? (*found)->window() :
nullptr;
224 return QPointF(
qBound(geometryF.left(),
p.x(), geometryF.right()),
225 qBound(geometryF.top(),
p.y(), geometryF.bottom()));
230 m_geometry =
QRect();
250 m_compositor->onWindowTreeChanged(changeType,
child);
263 emscripten_get_element_css_size(
outerScreenId().toUtf8().constData(), &css_width, &css_height);
264 QSizeF cssSize(css_width, css_height);
268 m_shadowContainer.set(
"width", canvasSize.
width());
269 m_shadowContainer.set(
"height", canvasSize.
height());
272 auto getElementBodyPosition = [](
const emscripten::val &
element) ->
QPoint {
273 emscripten::val bodyRect =
274 element[
"ownerDocument"][
"body"].call<emscripten::val>(
"getBoundingClientRect");
275 emscripten::val canvasRect =
element.call<emscripten::val>(
"getBoundingClientRect");
276 return QPoint(canvasRect[
"left"].as<int>() - bodyRect[
"left"].as<int>(),
277 canvasRect[
"top"].as<int>() - bodyRect[
"top"].as<int>());
285 int count = entries[
"length"].as<
int>();
288 emscripten::val
entry = entries[0];
290 entry[
"target"][m_canvasResizeObserverCallbackContextPropertyName].as<
intptr_t>());
292 qWarning() <<
"QWasmScreen::canvasResizeObserverCallback: missing screen pointer";
299 screen->updateQScreenAndCanvasRenderSize();
304 emscripten::function(
"qtCanvasResizeObserverCallback",
310 emscripten::val ResizeObserver = emscripten::val::global(
"ResizeObserver");
311 if (ResizeObserver == emscripten::val::undefined())
313 emscripten::val resizeObserver =
314 ResizeObserver.new_(emscripten::val::module_property(
"qtCanvasResizeObserverCallback"));
315 if (resizeObserver == emscripten::val::undefined())
320 m_shadowContainer.set(m_canvasResizeObserverCallbackContextPropertyName,
323 resizeObserver.call<
void>(
"observe", m_shadowContainer);
328 return m_shadowContainer;
343 [](
const QWindow *
window) { return static_cast<QWasmWindow *>(window->handle()); });
Format
The following image formats are available in Qt.
void push_back(parameter_type t)
QObject * parent() const
Returns a pointer to the parent object.
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
constexpr QPoint topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
bool contains(const QRect &r, bool proper=false) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr QRectF toRectF() const noexcept
The QScreen class is used to query screen properties. \inmodule QtGui.
QRect geometry
the screen's geometry in pixels
QPlatformScreen * handle() const
Get the platform screen handle.
constexpr QSize toSize() const noexcept
Returns an integer based copy of this size.
constexpr qreal width() const noexcept
Returns the width.
constexpr qreal height() const noexcept
Returns the height.
\macro QT_RESTRICTED_CAST_FROM_ASCII
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
static QWasmScreen * get(QPlatformScreen *screen)
QString name() const override
void installCanvasResizeObserver()
emscripten::val element() const
QPointF mapFromLocal(const QPointF &p) const
QWindow * topLevelAt(const QPoint &p) const override
Return the given top level window for a given position.
void setGeometry(const QRect &rect)
QPointF clipPoint(const QPointF &p) const
QWasmCompositor * compositor()
void updateQScreenAndCanvasRenderSize()
QPlatformCursor * cursor() const override
Reimplement this function in subclass to return the cursor of the screen.
QImage::Format format() const override
Reimplement in subclass to return the image format which corresponds to the screen format.
QWasmWindowTreeNode * parentNode() final
qreal devicePixelRatio() const override
Reimplement this function in subclass to return the device pixel ratio for the screen.
QWasmScreen(const emscripten::val &containerOrCanvas)
void resizeMaximizedWindows()
void onSubtreeChanged(QWasmWindowTreeNodeChangeType changeType, QWasmWindowTreeNode *parent, QWasmWindow *child) final
QString eventTargetId() const
QString outerScreenId() const
int depth() const override
Reimplement in subclass to return current depth of the screen.
QWindow * topWindow() const
QDpi logicalDpi() const override
Reimplement this function in subclass to return the logical horizontal and vertical dots per inch met...
static void canvasResizeObserverCallback(emscripten::val entries, emscripten::val)
emscripten::val containerElement() final
QList< QWasmWindow * > allWindows()
QRect geometry() const override
Reimplement in subclass to return the pixel geometry of the screen.
QWasmWindow * activeChild() const
virtual void onSubtreeChanged(QWasmWindowTreeNodeChangeType changeType, QWasmWindowTreeNode *parent, QWasmWindow *child)
const QWasmWindowStack & childStack() const
static void handleScreenGeometryChange(QScreen *screen, const QRect &newGeometry, const QRect &newAvailableGeometry)
static void registerInputDevice(const QInputDevice *device)
static void handleScreenRemoved(QPlatformScreen *screen)
Should be called by the implementation whenever a screen is removed.
Combined button and popup list for selecting options.
emscripten::val createStyleElement(emscripten::val parent)
@ FindChildrenRecursively
@ WindowStaysOnBottomHint
QPair< qreal, qreal > QDpi
constexpr const T & qBound(const T &min, const T &val, const T &max)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei count
GLenum GLenum GLenum GLenum GLenum scale
static QT_BEGIN_NAMESPACE qreal dpr(const QWindow *w)
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
EMSCRIPTEN_BINDINGS(qtCanvasResizeObserverCallback)
QWasmWindowTreeNodeChangeType
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent