Skip to content

Commit

Permalink
Set the cursor in QWasmWindow
Browse files Browse the repository at this point in the history
Also, trim QWasmCursor as some of it was dead code.

Change-Id: If6fee3390e4c2a2c66ceaef5917d7387f8dbd46c
Reviewed-by: Morten Johan Sørvig <[email protected]>
  • Loading branch information
mboc-qt committed Jan 24, 2023
1 parent 5006fdd commit 783b63c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 91 deletions.
119 changes: 35 additions & 84 deletions src/plugins/platforms/wasm/qwasmcursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "qwasmcursor.h"
#include "qwasmscreen.h"
#include "qwasmwindow.h"

#include <QtCore/qdebug.h>
#include <QtGui/qwindow.h>
Expand All @@ -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;
}

void QWasmCursor::setWasmCursor(QScreen *screen, const QByteArray &name)
{
QWasmScreen::get(screen)->element()["style"].set("cursor", val(name.constData()));
}
} // namespace

void QWasmCursor::setOverrideWasmCursor(const QCursor &windowCursor, QScreen *screen)
void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window)
{
QWasmCursor *wCursor = static_cast<QWasmCursor *>(QWasmScreen::get(screen)->cursor());
wCursor->setWasmCursor(screen, wCursor->cursorShapeToHtml(windowCursor.shape()));
}
if (!window || !window->handle())
return;

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
7 changes: 0 additions & 7 deletions src/plugins/platforms/wasm/qwasmcursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/platforms/wasm/qwasmwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/plugins/platforms/wasm/qwasmwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class QWasmWindow final : public QPlatformWindow
void destroy();
void paint();
void setZOrder(int order);
void setWindowCursor(QByteArray cssCursorName);
void onActivationChanged(bool active);
bool isVisible() const;

Expand Down

0 comments on commit 783b63c

Please sign in to comment.