summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmwindow.cpp
diff options
context:
space:
mode:
authorMorten Sørvig <[email protected]>2024-08-29 15:06:51 +0200
committerMorten Johan Sørvig <[email protected]>2024-09-03 14:42:24 +0000
commit3117d88cdf2324a7833cdf9ceecc4cba30e1e090 (patch)
tree93560ad5e4d3080e1495ee1ad70f0184b2e3616d /src/plugins/platforms/wasm/qwasmwindow.cpp
parent051488c5faf5410e938978d4c1609c7a85074c57 (diff)
wasm: simplify QWasmWindow constructor
The code implements parts of the event handling as lambdas in the constructor. This makes it hard to see what the constructor code itself does. Instead, move the initial handling of the emscripten::val event to event handler functions, which then call the existing process event functions. Pick-to: 6.8 Change-Id: I3abc5be47b080c772c699c21f2fe1cc555ff8192 Reviewed-by: Tor Arne Vestbø <[email protected]> Reviewed-by: Piotr Wierciński <[email protected]>
Diffstat (limited to 'src/plugins/platforms/wasm/qwasmwindow.cpp')
-rw-r--r--src/plugins/platforms/wasm/qwasmwindow.cpp173
1 files changed, 87 insertions, 86 deletions
diff --git a/src/plugins/platforms/wasm/qwasmwindow.cpp b/src/plugins/platforms/wasm/qwasmwindow.cpp
index a9be3909599..ff3a39b2716 100644
--- a/src/plugins/platforms/wasm/qwasmwindow.cpp
+++ b/src/plugins/platforms/wasm/qwasmwindow.cpp
@@ -106,99 +106,27 @@ QWasmWindow::QWasmWindow(QWindow *w, QWasmDeadKeySupport *deadKeySupport,
m_flags = window()->flags();
- const auto pointerCallback = std::function([this](emscripten::val event) {
- const auto eventTypeString = event["type"].as<std::string>();
-
- // Ideally it should not be happened but
- // it takes place sometime with some reason
- // without compositionend.
- QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext();
- if (wasmInput && !wasmInput->preeditString().isEmpty())
- wasmInput->commitPreeditAndClear();
-
- if (processPointer(*PointerEvent::fromWeb(event)))
- event.call<void>("preventDefault");
- });
-
- m_pointerEnterCallback =
- std::make_unique<qstdweb::EventCallback>(m_qtWindow, "pointerenter", pointerCallback);
- m_pointerLeaveCallback =
- std::make_unique<qstdweb::EventCallback>(m_qtWindow, "pointerleave", pointerCallback);
-
- m_wheelEventCallback = std::make_unique<qstdweb::EventCallback>(
- m_qtWindow, "wheel", [this](emscripten::val event) {
- if (processWheel(*WheelEvent::fromWeb(event)))
- event.call<void>("preventDefault");
- });
-
- const auto keyCallbackForInputContext = std::function([this](emscripten::val event) {
- QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext();
- if (wasmInput) {
- const auto keyString = QString::fromStdString(event["key"].as<std::string>());
- qCDebug(qLcQpaWasmInputContext) << "Key callback" << keyString << keyString.size();
-
- if (keyString == "Unidentified") {
- // Android makes a bunch of KeyEvents as "Unidentified"
- // They will be processed just in InputContext.
- return;
- } else if (event["ctrlKey"].as<bool>()
- || event["altKey"].as<bool>()
- || event["metaKey"].as<bool>()) {
- if (processKeyForInputContext(*KeyEvent::fromWebWithDeadKeyTranslation(event, m_deadKeySupport)))
- event.call<void>("preventDefault");
- event.call<void>("stopImmediatePropagation");
- return;
- } else if (keyString.size() != 1) {
- if (!wasmInput->preeditString().isEmpty()) {
- if (keyString == "Process" || keyString == "Backspace") {
- // processed by InputContext
- // "Process" should be handled by InputContext but
- // QWasmInputContext's function is incomplete now
- // so, there will be some exceptions here.
- return;
- } else if (keyString != "Shift"
- && keyString != "Meta"
- && keyString != "Alt"
- && keyString != "Control"
- && !keyString.startsWith("Arrow")) {
- wasmInput->commitPreeditAndClear();
- }
- }
- } else if (wasmInput->inputMethodAccepted()) {
- // processed in inputContext with skipping processKey
- return;
- }
- }
-
- qCDebug(qLcQpaWasmInputContext) << "processKey as KeyEvent";
- if (processKeyForInputContext(*KeyEvent::fromWebWithDeadKeyTranslation(event, m_deadKeySupport)))
- event.call<void>("preventDefault");
- event.call<void>("stopImmediatePropagation");
- });
-
- const auto keyCallback = std::function([this](emscripten::val event) {
- qCDebug(qLcQpaWasmInputContext) << "processKey as KeyEvent";
- if (processKey(*KeyEvent::fromWebWithDeadKeyTranslation(event, m_deadKeySupport)))
- event.call<void>("preventDefault");
- event.call<void>("stopPropagation");
- });
+ m_pointerEnterCallback = std::make_unique<qstdweb::EventCallback>(m_qtWindow, "pointerenter",
+ [this](emscripten::val event) { this->handlePointerEvent(event); });
+ m_pointerLeaveCallback = std::make_unique<qstdweb::EventCallback>(m_qtWindow, "pointerleave",
+ [this](emscripten::val event) { this->handlePointerEvent(event); });
+ m_wheelEventCallback = std::make_unique<qstdweb::EventCallback>( m_qtWindow, "wheel",
+ [this](emscripten::val event) { this->handleWheelEvent(event); });
QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext();
if (wasmInput) {
m_keyDownCallbackForInputContext =
- std::make_unique<qstdweb::EventCallback>(wasmInput->m_inputElement,
- "keydown",
- keyCallbackForInputContext);
+ std::make_unique<qstdweb::EventCallback>(wasmInput->m_inputElement, "keydown",
+ [this](emscripten::val event) { this->handleKeyForInputContextEvent(event); });
m_keyUpCallbackForInputContext =
- std::make_unique<qstdweb::EventCallback>(wasmInput->m_inputElement,
- "keyup",
- keyCallbackForInputContext);
+ std::make_unique<qstdweb::EventCallback>(wasmInput->m_inputElement, "keyup",
+ [this](emscripten::val event) { this->handleKeyForInputContextEvent(event); });
}
- m_keyDownCallback =
- std::make_unique<qstdweb::EventCallback>(m_qtWindow, "keydown", keyCallback);
- m_keyUpCallback =std::make_unique<qstdweb::EventCallback>(m_qtWindow, "keyup", keyCallback);
-
+ m_keyDownCallback = std::make_unique<qstdweb::EventCallback>(m_qtWindow, "keydown",
+ [this](emscripten::val event) { this->handleKeyEvent(event); });
+ m_keyUpCallback =std::make_unique<qstdweb::EventCallback>(m_qtWindow, "keyup",
+ [this](emscripten::val event) { this->handleKeyEvent(event); });
setParent(parent());
}
@@ -563,6 +491,14 @@ void QWasmWindow::commitParent(QWasmWindowTreeNode *parent)
m_commitedParent = parent;
}
+void QWasmWindow::handleKeyEvent(const emscripten::val &event)
+{
+ qCDebug(qLcQpaWasmInputContext) << "processKey as KeyEvent";
+ if (processKey(*KeyEvent::fromWebWithDeadKeyTranslation(event, m_deadKeySupport)))
+ event.call<void>("preventDefault");
+ event.call<void>("stopPropagation");
+}
+
bool QWasmWindow::processKey(const KeyEvent &event)
{
constexpr bool ProceedToNativeEvent = false;
@@ -583,6 +519,52 @@ bool QWasmWindow::processKey(const KeyEvent &event)
: result;
}
+void QWasmWindow::handleKeyForInputContextEvent(const emscripten::val &event)
+{
+ QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext();
+ if (wasmInput) {
+ const auto keyString = QString::fromStdString(event["key"].as<std::string>());
+ qCDebug(qLcQpaWasmInputContext) << "Key callback" << keyString << keyString.size();
+
+ if (keyString == "Unidentified") {
+ // Android makes a bunch of KeyEvents as "Unidentified"
+ // They will be processed just in InputContext.
+ return;
+ } else if (event["ctrlKey"].as<bool>()
+ || event["altKey"].as<bool>()
+ || event["metaKey"].as<bool>()) {
+ if (processKeyForInputContext(*KeyEvent::fromWebWithDeadKeyTranslation(event, m_deadKeySupport)))
+ event.call<void>("preventDefault");
+ event.call<void>("stopImmediatePropagation");
+ return;
+ } else if (keyString.size() != 1) {
+ if (!wasmInput->preeditString().isEmpty()) {
+ if (keyString == "Process" || keyString == "Backspace") {
+ // processed by InputContext
+ // "Process" should be handled by InputContext but
+ // QWasmInputContext's function is incomplete now
+ // so, there will be some exceptions here.
+ return;
+ } else if (keyString != "Shift"
+ && keyString != "Meta"
+ && keyString != "Alt"
+ && keyString != "Control"
+ && !keyString.startsWith("Arrow")) {
+ wasmInput->commitPreeditAndClear();
+ }
+ }
+ } else if (wasmInput->inputMethodAccepted()) {
+ // processed in inputContext with skipping processKey
+ return;
+ }
+ }
+
+ qCDebug(qLcQpaWasmInputContext) << "processKey as KeyEvent";
+ if (processKeyForInputContext(*KeyEvent::fromWebWithDeadKeyTranslation(event, m_deadKeySupport)))
+ event.call<void>("preventDefault");
+ event.call<void>("stopImmediatePropagation");
+}
+
bool QWasmWindow::processKeyForInputContext(const KeyEvent &event)
{
qCDebug(qLcQpaWasmInputContext) << Q_FUNC_INFO;
@@ -606,6 +588,19 @@ bool QWasmWindow::processKeyForInputContext(const KeyEvent &event)
return result;
}
+void QWasmWindow::handlePointerEvent(const emscripten::val &event)
+{
+ // Ideally it should not be happened but
+ // it takes place sometime with some reason
+ // without compositionend.
+ QWasmInputContext *wasmInput = QWasmIntegration::get()->wasmInputContext();
+ if (wasmInput && !wasmInput->preeditString().isEmpty())
+ wasmInput->commitPreeditAndClear();
+
+ if (processPointer(*PointerEvent::fromWeb(event)))
+ event.call<void>("preventDefault");
+}
+
bool QWasmWindow::processPointer(const PointerEvent &event)
{
if (event.pointerType != PointerType::Mouse && event.pointerType != PointerType::Pen)
@@ -629,6 +624,12 @@ bool QWasmWindow::processPointer(const PointerEvent &event)
return false;
}
+void QWasmWindow::handleWheelEvent(const emscripten::val &event)
+{
+ if (processWheel(*WheelEvent::fromWeb(event)))
+ event.call<void>("preventDefault");
+}
+
bool QWasmWindow::processWheel(const WheelEvent &event)
{
// Web scroll deltas are inverted from Qt deltas - negate.