Skip to content

Commit

Permalink
WASM builds now handle bitmap and pixmap cursors
Browse files Browse the repository at this point in the history
[ChangeLog][QtGui][Platform Specific Changes][wasm] Previously, bitmap
and pixmap cursors were nonfunctional in wasm builds and would trigger
warnings.  These cursors now work as expected.

Fixes: QTBUG-116796
Pick-to: 6.5 6.6 6.7
Change-Id: Idd9aa4d458a36452fd5b49f72cc595756fc50923
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
  • Loading branch information
KevKeating authored and lpotter committed Feb 8, 2024
1 parent 1d6bb23 commit 38c62c5
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/plugins/platforms/wasm/qwasmcursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "qwasmscreen.h"
#include "qwasmwindow.h"

#include <QtCore/qbuffer.h>
#include <QtCore/qdebug.h>
#include <QtCore/qstring.h>
#include <QtGui/qwindow.h>

#include <emscripten/emscripten.h>
Expand All @@ -15,10 +17,9 @@ QT_BEGIN_NAMESPACE
using namespace emscripten;

namespace {
QByteArray cursorShapeToCss(Qt::CursorShape shape)
QByteArray cursorToCss(const QCursor *cursor)
{
QByteArray cursorName;

auto shape = cursor->shape();
switch (shape) {
case Qt::ArrowCursor:
return "default";
Expand Down Expand Up @@ -64,8 +65,24 @@ QByteArray cursorShapeToCss(Qt::CursorShape shape)
return "default";
case Qt::DragLinkCursor:
return "alias";
case Qt::BitmapCursor: {
auto pixmap = cursor->pixmap();
QByteArray cursorAsPng;
QBuffer buffer(&cursorAsPng);
buffer.open(QBuffer::WriteOnly);
pixmap.save(&buffer, "PNG");
buffer.close();
auto cursorAsBase64 = cursorAsPng.toBase64();
auto hotSpot = cursor->hotSpot();
auto encodedCursor =
QString("url(data:image/png;base64,%1) %2 %3, auto")
.arg(QString::fromUtf8(cursorAsBase64),
QString::number(hotSpot.x()),
QString::number(hotSpot.y()));
return encodedCursor.toUtf8();
}
default:
static_assert(Qt::BitmapCursor == 24 && Qt::CustomCursor == 25,
static_assert(Qt::CustomCursor == 25,
"New cursor type added, handle it");
qWarning() << "QWasmCursor: " << shape << " unsupported";
return "default";
Expand All @@ -78,7 +95,7 @@ void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window)
if (!window)
return;
if (QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle()))
wasmWindow->setWindowCursor(windowCursor ? cursorShapeToCss(windowCursor->shape()) : "default");
wasmWindow->setWindowCursor(windowCursor ? cursorToCss(windowCursor) : "default");
}

QT_END_NAMESPACE

0 comments on commit 38c62c5

Please sign in to comment.