wasm: add resizeCanvasElement() API to qtloader.js

HTMl does not have per-element resize events, which
means Qt has not way of knowing that a canvas has been
resized and that the canvas buffer size should be updated.

Depending on the use case, the hosting JavaScript code
that caused the canvas resize could also inform Qt
that the canvas has been resized. Add API to do this,
which calls the existing canvas/screen resize implementation.

Other solutions taken/not taken:

- browser window resize events: these are available,
and we install an event handler in qwasmeventtranslator.cpp.

- DOM mutation events: would detect changes to the
the size attributes themselves, but not if the size
indirectly changed, e.g. “width: 100%” depends on the
parent width. Not implemented.

Change-Id: Ib324bb30f523e9fceea68000b95bf857a1d36b6c
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Morten Johan Sørvig 2019-03-16 23:58:36 +01:00
parent 73db765aaf
commit 69beb5f5a0
3 changed files with 21 additions and 0 deletions

View File

@ -122,6 +122,8 @@
// Add canvas at run-time. Adds a corresponding QScreen,
// removeCanvasElement
// Remove canvas at run-time. Removes the corresponding QScreen.
// resizeCanvasElement
// Signals to the application that a canvas has been resized.
var Module = {}
@ -233,6 +235,7 @@ function QtLoader(config)
publicAPI.loadEmscriptenModule = loadEmscriptenModule;
publicAPI.addCanvasElement = addCanvasElement;
publicAPI.removeCanvasElement = removeCanvasElement;
publicAPI.resizeCanvasElement = resizeCanvasElement;
restartCount = 0;
@ -548,6 +551,11 @@ function QtLoader(config)
console.log("Error: removeCanvasElement can only be called in the Running state");
}
function resizeCanvasElement(element) {
if (publicAPI.status == "Running")
Module.qtResizeCanvasElement(element);
}
setStatus("Created");
return publicAPI;

View File

@ -73,11 +73,18 @@ static void removeCanvasElement(emscripten::val canvas)
QWasmIntegration::get()->removeScreen(canvasId);
}
static void resizeCanvasElement(emscripten::val canvas)
{
QString canvasId = QString::fromStdString(canvas["id"].as<std::string>());
QWasmIntegration::get()->resizeScreen(canvasId);
}
EMSCRIPTEN_BINDINGS(qtQWasmIntegraton)
{
function("qtBrowserBeforeUnload", &browserBeforeUnload);
function("qtAddCanvasElement", &addCanvasElement);
function("qtRemoveCanvasElement", &removeCanvasElement);
function("qtResizeCanvasElement", &resizeCanvasElement);
}
QWasmIntegration *QWasmIntegration::s_instance;
@ -212,4 +219,9 @@ void QWasmIntegration::removeScreen(const QString &canvasId)
QWindowSystemInterface::handleScreenRemoved(m_screens.take(canvasId));
}
void QWasmIntegration::resizeScreen(const QString &canvasId)
{
m_screens.value(canvasId)->updateQScreenAndCanvasRenderSize();
}
QT_END_NAMESPACE

View File

@ -77,6 +77,7 @@ public:
void addScreen(const QString &canvasId);
void removeScreen(const QString &canvasId);
void resizeScreen(const QString &canvasId);
private:
mutable QWasmFontDatabase *m_fontDb;