Set the cursor in QWasmWindow

Also, trim QWasmCursor as some of it was dead code.

Change-Id: If6fee3390e4c2a2c66ceaef5917d7387f8dbd46c
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Mikolaj Boc 2023-01-20 16:55:04 +01:00
parent 5006fddd33
commit 783b63ce51
4 changed files with 41 additions and 91 deletions

View File

@ -3,6 +3,7 @@
#include "qwasmcursor.h"
#include "qwasmscreen.h"
#include "qwasmwindow.h"
#include <QtCore/qdebug.h>
#include <QtGui/qwindow.h>
@ -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<QWasmCursor *>(QWasmScreen::get(screen)->cursor());
wCursor->setWasmCursor(screen, wCursor->cursorShapeToHtml(windowCursor.shape()));
}
void QWasmCursor::clearOverrideWasmCursor(QScreen *screen)
{
QWasmCursor *wCursor = static_cast<QWasmCursor *>(QWasmScreen::get(screen)->cursor());
wCursor->setWasmCursor(screen, wCursor->htmlCursorName);
static_cast<QWasmWindow *>(window->handle())
->setWindowCursor(cursorShapeToCss(windowCursor->shape()));
}
QT_END_NAMESPACE

View File

@ -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

View File

@ -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();

View File

@ -44,6 +44,7 @@ public:
void destroy();
void paint();
void setZOrder(int order);
void setWindowCursor(QByteArray cssCursorName);
void onActivationChanged(bool active);
bool isVisible() const;