diff --git a/src/plugins/platforms/wasm/qwasmcursor.cpp b/src/plugins/platforms/wasm/qwasmcursor.cpp index 2fc82bfcdc..1ffa00780d 100644 --- a/src/plugins/platforms/wasm/qwasmcursor.cpp +++ b/src/plugins/platforms/wasm/qwasmcursor.cpp @@ -3,6 +3,7 @@ #include "qwasmcursor.h" #include "qwasmscreen.h" +#include "qwasmwindow.h" #include #include @@ -13,122 +14,72 @@ QT_BEGIN_NAMESPACE using namespace emscripten; -void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window) -{ - if (!window) - return; - QScreen *screen = window->screen(); - if (!screen) - return; - - if (windowCursor) { - - // Bitmap and custom cursors are not implemented (will fall back to "auto") - if (windowCursor->shape() == Qt::BitmapCursor || windowCursor->shape() >= Qt::CustomCursor) - qWarning() << "QWasmCursor: bitmap and custom cursors are not supported"; - - - htmlCursorName = cursorShapeToHtml(windowCursor->shape()); - } - if (htmlCursorName.isEmpty()) - htmlCursorName = "default"; - - setWasmCursor(screen, htmlCursorName); -} - -QByteArray QWasmCursor::cursorShapeToHtml(Qt::CursorShape shape) +namespace { +QByteArray cursorShapeToCss(Qt::CursorShape shape) { QByteArray cursorName; switch (shape) { case Qt::ArrowCursor: - cursorName = "default"; - break; + return "default"; case Qt::UpArrowCursor: - cursorName = "n-resize"; - break; + return "n-resize"; case Qt::CrossCursor: - cursorName = "crosshair"; - break; + return "crosshair"; case Qt::WaitCursor: - cursorName = "wait"; - break; + return "wait"; case Qt::IBeamCursor: - cursorName = "text"; - break; + return "text"; case Qt::SizeVerCursor: - cursorName = "ns-resize"; - break; + return "ns-resize"; case Qt::SizeHorCursor: - cursorName = "ew-resize"; - break; + return "ew-resize"; case Qt::SizeBDiagCursor: - cursorName = "nesw-resize"; - break; + return "nesw-resize"; case Qt::SizeFDiagCursor: - cursorName = "nwse-resize"; - break; + return "nwse-resize"; case Qt::SizeAllCursor: - cursorName = "move"; - break; + return "move"; case Qt::BlankCursor: - cursorName = "none"; - break; + return "none"; case Qt::SplitVCursor: - cursorName = "row-resize"; - break; + return "row-resize"; case Qt::SplitHCursor: - cursorName = "col-resize"; - break; + return "col-resize"; case Qt::PointingHandCursor: - cursorName = "pointer"; - break; + return "pointer"; case Qt::ForbiddenCursor: - cursorName = "not-allowed"; - break; + return "not-allowed"; case Qt::WhatsThisCursor: - cursorName = "help"; - break; + return "help"; case Qt::BusyCursor: - cursorName = "progress"; - break; + return "progress"; case Qt::OpenHandCursor: - cursorName = "grab"; - break; + return "grab"; case Qt::ClosedHandCursor: - cursorName = "grabbing"; - break; + return "grabbing"; case Qt::DragCopyCursor: - cursorName = "copy"; - break; + return "copy"; case Qt::DragMoveCursor: - cursorName = "default"; - break; + return "default"; case Qt::DragLinkCursor: - cursorName = "alias"; - break; + return "alias"; default: - break; + static_assert(Qt::BitmapCursor == 24 && Qt::CustomCursor == 25, + "New cursor type added, handle it"); + qWarning() << "QWasmCursor: " << shape << " unsupported"; + return "default"; } - - return cursorName; } +} // namespace -void QWasmCursor::setWasmCursor(QScreen *screen, const QByteArray &name) +void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window) { - QWasmScreen::get(screen)->element()["style"].set("cursor", val(name.constData())); -} + if (!window || !window->handle()) + return; -void QWasmCursor::setOverrideWasmCursor(const QCursor &windowCursor, QScreen *screen) -{ - QWasmCursor *wCursor = static_cast(QWasmScreen::get(screen)->cursor()); - wCursor->setWasmCursor(screen, wCursor->cursorShapeToHtml(windowCursor.shape())); -} - -void QWasmCursor::clearOverrideWasmCursor(QScreen *screen) -{ - QWasmCursor *wCursor = static_cast(QWasmScreen::get(screen)->cursor()); - wCursor->setWasmCursor(screen, wCursor->htmlCursorName); + static_cast(window->handle()) + ->setWindowCursor(cursorShapeToCss(windowCursor->shape())); } QT_END_NAMESPACE diff --git a/src/plugins/platforms/wasm/qwasmcursor.h b/src/plugins/platforms/wasm/qwasmcursor.h index bb3e2a1346..6873602caf 100644 --- a/src/plugins/platforms/wasm/qwasmcursor.h +++ b/src/plugins/platforms/wasm/qwasmcursor.h @@ -12,13 +12,6 @@ class QWasmCursor : public QPlatformCursor { public: void changeCursor(QCursor *windowCursor, QWindow *window) override; - - QByteArray cursorShapeToHtml(Qt::CursorShape shape); - static void setOverrideWasmCursor(const QCursor &windowCursor, QScreen *screen); - static void clearOverrideWasmCursor(QScreen *screen); -private: - QByteArray htmlCursorName = "default"; - void setWasmCursor(QScreen *screen, const QByteArray &name); }; QT_END_NAMESPACE diff --git a/src/plugins/platforms/wasm/qwasmwindow.cpp b/src/plugins/platforms/wasm/qwasmwindow.cpp index 7ad844a67c..be0dd74797 100644 --- a/src/plugins/platforms/wasm/qwasmwindow.cpp +++ b/src/plugins/platforms/wasm/qwasmwindow.cpp @@ -217,6 +217,11 @@ void QWasmWindow::setZOrder(int z) m_qtWindow["style"].set("zIndex", std::to_string(z)); } +void QWasmWindow::setWindowCursor(QByteArray cssCursorName) +{ + m_canvas["style"].set("cursor", emscripten::val(cssCursorName.constData())); +} + void QWasmWindow::setGeometry(const QRect &rect) { const auto margins = frameMargins(); diff --git a/src/plugins/platforms/wasm/qwasmwindow.h b/src/plugins/platforms/wasm/qwasmwindow.h index 20e30f3d97..46bb1f81a7 100644 --- a/src/plugins/platforms/wasm/qwasmwindow.h +++ b/src/plugins/platforms/wasm/qwasmwindow.h @@ -44,6 +44,7 @@ public: void destroy(); void paint(); void setZOrder(int order); + void setWindowCursor(QByteArray cssCursorName); void onActivationChanged(bool active); bool isVisible() const;