Use floating point coords for mouse/wheel events on wasm

Fractional mouse movements may be reported on hi-dpi. Floating point
event fields help us perform correct calculations in line with the
web platform.

Change-Id: Ic0c457db408c2bf28179ffcfdb032cde64ca8bbd
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
Reviewed-by: Aleksandr Reviakin <aleksandr.reviakin@qt.io>
This commit is contained in:
Mikolaj Boc 2023-02-03 18:16:17 +01:00
parent 249d613a60
commit d6eea89bc6
11 changed files with 51 additions and 52 deletions

View File

@ -292,11 +292,10 @@ bool QWasmCompositor::processTouch(int eventType, const EmscriptenTouchEvent *to
const EmscriptenTouchPoint *emTouchPoint = &touchEvent->touches[i];
QPoint targetPointInScreenCoords =
QPointF targetPointInScreenCoords =
screen()->mapFromLocal(QPoint(emTouchPoint->targetX, emTouchPoint->targetY));
targetWindow = screen()->compositor()->windowAt(targetPointInScreenCoords, 5);
targetWindow = screen()->compositor()->windowAt(targetPointInScreenCoords.toPoint(), 5);
if (targetWindow == nullptr)
continue;
@ -312,7 +311,7 @@ bool QWasmCompositor::processTouch(int eventType, const EmscriptenTouchEvent *to
if (tp != m_pressedTouchIds.constEnd())
touchPoint.normalPosition = tp.value();
QPointF pointInTargetWindowCoords = QPointF(targetWindow->mapFromGlobal(targetPointInScreenCoords));
QPointF pointInTargetWindowCoords = targetWindow->mapFromGlobal(targetPointInScreenCoords);
QPointF normalPosition(pointInTargetWindowCoords.x() / targetWindow->width(),
pointInTargetWindowCoords.y() / targetWindow->height());

View File

@ -25,15 +25,15 @@ void syncCSSClassWith(emscripten::val element, std::string cssClassName, bool fl
element["classList"].call<void>("remove", emscripten::val(std::move(cssClassName)));
}
QPoint mapPoint(emscripten::val source, emscripten::val target, const QPoint &point)
QPointF mapPoint(emscripten::val source, emscripten::val target, const QPointF &point)
{
auto sourceBoundingRect =
const auto sourceBoundingRect =
QRectF::fromDOMRect(source.call<emscripten::val>("getBoundingClientRect"));
auto targetBoundingRect =
const auto targetBoundingRect =
QRectF::fromDOMRect(target.call<emscripten::val>("getBoundingClientRect"));
auto offset = sourceBoundingRect.topLeft() - targetBoundingRect.topLeft();
return (point + offset).toPoint();
const auto offset = sourceBoundingRect.topLeft() - targetBoundingRect.topLeft();
return point + offset;
}
} // namespace dom

View File

@ -24,7 +24,7 @@ inline emscripten::val document()
void syncCSSClassWith(emscripten::val element, std::string cssClassName, bool flag);
QPoint mapPoint(emscripten::val source, emscripten::val target, const QPoint &point);
QPointF mapPoint(emscripten::val source, emscripten::val target, const QPointF &point);
} // namespace dom
QT_END_NAMESPACE

View File

@ -114,9 +114,9 @@ MouseEvent::MouseEvent(EventType type, emscripten::val event) : Event(type, even
// it up here.
if (type == EventType::PointerDown)
mouseButtons |= mouseButton;
localPoint = QPoint(event["offsetX"].as<int>(), event["offsetY"].as<int>());
pointInPage = QPoint(event["pageX"].as<int>(), event["pageY"].as<int>());
pointInViewport = QPoint(event["clientX"].as<int>(), event["clientY"].as<int>());
localPoint = QPointF(event["offsetX"].as<qreal>(), event["offsetY"].as<qreal>());
pointInPage = QPointF(event["pageX"].as<qreal>(), event["pageY"].as<qreal>());
pointInViewport = QPointF(event["clientX"].as<qreal>(), event["clientY"].as<qreal>());
modifiers = KeyboardModifier::getForEvent(event);
}
@ -222,7 +222,7 @@ WheelEvent::WheelEvent(EventType type, emscripten::val event) : MouseEvent(type,
return DeltaMode::Page;
})();
delta = QPoint(event["deltaX"].as<int>(), event["deltaY"].as<int>());
delta = QPointF(event["deltaX"].as<qreal>(), event["deltaY"].as<qreal>());
webkitDirectionInvertedFromDevice = event["webkitDirectionInvertedFromDevice"].as<bool>();
}

View File

@ -190,9 +190,9 @@ struct MouseEvent : public Event
}
}
QPoint localPoint;
QPoint pointInPage;
QPoint pointInViewport;
QPointF localPoint;
QPointF pointInPage;
QPointF pointInViewport;
Qt::MouseButton mouseButton;
Qt::MouseButtons mouseButtons;
QFlags<Qt::KeyboardModifier> modifiers;
@ -241,7 +241,7 @@ struct WheelEvent : public MouseEvent
DeltaMode deltaMode;
bool webkitDirectionInvertedFromDevice;
QPoint delta;
QPointF delta;
};
QT_END_NAMESPACE

View File

@ -203,15 +203,16 @@ QWindow *QWasmScreen::topLevelAt(const QPoint &p) const
return m_compositor->windowAt(p);
}
QPoint QWasmScreen::mapFromLocal(const QPoint &p) const
QPointF QWasmScreen::mapFromLocal(const QPointF &p) const
{
return geometry().topLeft() + p;
}
QPoint QWasmScreen::clipPoint(const QPoint &p) const
QPointF QWasmScreen::clipPoint(const QPointF &p) const
{
return QPoint(qBound(screen()->geometry().left(), p.x(), screen()->geometry().right()),
qBound(screen()->geometry().top(), p.y(), screen()->geometry().bottom()));
const auto geometryF = screen()->geometry().toRectF();
return QPointF(qBound(geometryF.left(), p.x(), geometryF.right()),
qBound(geometryF.top(), p.y(), geometryF.bottom()));
}
void QWasmScreen::invalidateSize()

View File

@ -52,8 +52,8 @@ public:
QWindow *topWindow() const;
QWindow *topLevelAt(const QPoint &p) const override;
QPoint mapFromLocal(const QPoint &p) const;
QPoint clipPoint(const QPoint &p) const;
QPointF mapFromLocal(const QPointF &p) const;
QPointF clipPoint(const QPointF &p) const;
void invalidateSize();
void updateQScreenAndCanvasRenderSize();

View File

@ -165,7 +165,7 @@ void QWasmWindow::onNonClientAreaInteraction()
bool QWasmWindow::onNonClientEvent(const PointerEvent &event)
{
QPoint pointInScreen = platformScreen()->mapFromLocal(
QPointF pointInScreen = platformScreen()->mapFromLocal(
dom::mapPoint(event.target, platformScreen()->element(), event.localPoint));
return QWindowSystemInterface::handleMouseEvent(
window(), QWasmIntegration::getTimestamp(), window()->mapFromGlobal(pointInScreen),
@ -504,13 +504,13 @@ bool QWasmWindow::processDrop(const DragEvent &event)
return image;
},
[this, event](std::unique_ptr<QMimeData> data) {
QWindowSystemInterface::handleDrag(window(), data.get(), event.pointInPage,
event.dropAction, event.mouseButton,
event.modifiers);
QWindowSystemInterface::handleDrag(window(), data.get(),
event.pointInPage.toPoint(), event.dropAction,
event.mouseButton, event.modifiers);
QWindowSystemInterface::handleDrop(window(), data.get(), event.pointInPage,
event.dropAction, event.mouseButton,
event.modifiers);
QWindowSystemInterface::handleDrop(window(), data.get(),
event.pointInPage.toPoint(), event.dropAction,
event.mouseButton, event.modifiers);
QWindowSystemInterface::handleDrag(window(), nullptr, QPoint(), Qt::IgnoreAction,
{}, {});
@ -536,10 +536,10 @@ bool QWasmWindow::processWheel(const WheelEvent &event)
dom::mapPoint(event.target, platformScreen()->element(), event.localPoint));
return QWindowSystemInterface::handleWheelEvent(
window(), QWasmIntegration::getTimestamp(), mapFromGlobal(pointInScreen), pointInScreen,
event.delta * scrollFactor, event.delta * scrollFactor, event.modifiers,
Qt::NoScrollPhase, Qt::MouseEventNotSynthesized,
event.webkitDirectionInvertedFromDevice);
window(), QWasmIntegration::getTimestamp(), window()->mapFromGlobal(pointInScreen),
pointInScreen, (event.delta * scrollFactor).toPoint(),
(event.delta * scrollFactor).toPoint(), event.modifiers, Qt::NoScrollPhase,
Qt::MouseEventNotSynthesized, event.webkitDirectionInvertedFromDevice);
}
QRect QWasmWindow::normalGeometry() const

View File

@ -39,7 +39,7 @@ bool ClientArea::processPointer(const PointerEvent &event)
dom::mapPoint(event.target, m_screen->element(), event.localPoint);
const auto pointInScreen = m_screen->mapFromLocal(localScreenPoint);
const QPoint pointInTargetWindowCoords = m_window->mapFromGlobal(pointInScreen);
const QPointF pointInTargetWindowCoords = m_window->window()->mapFromGlobal(pointInScreen);
switch (event.type) {
case EventType::PointerDown: {
@ -73,11 +73,10 @@ bool ClientArea::deliverEvent(const PointerEvent &event)
const auto pointInScreen = m_screen->mapFromLocal(
dom::mapPoint(event.target, m_screen->element(), event.localPoint));
const QPoint targetPointClippedToScreen(
std::max(m_screen->geometry().left(),
std::min(m_screen->geometry().right(), pointInScreen.x())),
std::max(m_screen->geometry().top(),
std::min(m_screen->geometry().bottom(), pointInScreen.y())));
const auto geometryF = m_screen->geometry().toRectF();
const QPointF targetPointClippedToScreen(
qBound(geometryF.left(), pointInScreen.x(), geometryF.right()),
qBound(geometryF.top(), pointInScreen.y(), geometryF.bottom()));
const QEvent::Type eventType =
MouseEvent::mouseEventTypeFromEventType(event.type, WindowArea::Client);

View File

@ -219,7 +219,7 @@ void Resizer::continueResize(const PointerEvent &event)
{
const auto pointInScreen =
dom::mapPoint(event.target, m_window->platformScreen()->element(), event.localPoint);
const auto amount = pointInScreen - m_currentResizeData->originInScreenCoords;
const auto amount = (pointInScreen - m_currentResizeData->originInScreenCoords).toPoint();
const QPoint cappedGrowVector(
std::min(m_currentResizeData->maxGrow.x(),
std::max(m_currentResizeData->minShrink.x(),
@ -377,8 +377,8 @@ bool TitleBar::onPointerDown(const PointerEvent &event)
m_element.call<void>("setPointerCapture", event.pointerId);
m_capturedPointerId = event.pointerId;
const QPoint targetPointClippedToScreen = clipPointWithScreen(event.localPoint);
m_lastMovePoint = targetPointClippedToScreen;
m_moveStartWindowPosition = m_window->window()->position();
m_moveStartPoint = clipPointWithScreen(event.localPoint);
m_window->onNonClientEvent(event);
return true;
}
@ -388,11 +388,9 @@ bool TitleBar::onPointerMove(const PointerEvent &event)
if (m_capturedPointerId != event.pointerId)
return false;
const QPoint targetPointClippedToScreen = clipPointWithScreen(event.localPoint);
const QPoint delta = targetPointClippedToScreen - m_lastMovePoint;
m_lastMovePoint = targetPointClippedToScreen;
const QPoint delta = (clipPointWithScreen(event.localPoint) - m_moveStartPoint).toPoint();
m_window->window()->setPosition(m_window->window()->position() + delta);
m_window->window()->setPosition(m_moveStartWindowPosition + delta);
m_window->onNonClientEvent(event);
return true;
}
@ -414,7 +412,7 @@ bool TitleBar::onDoubleClick()
return true;
}
QPoint TitleBar::clipPointWithScreen(const QPoint &pointInTitleBarCoords) const
QPointF TitleBar::clipPointWithScreen(const QPointF &pointInTitleBarCoords) const
{
auto *screen = m_window->platformScreen();
return screen->clipPoint(screen->mapFromLocal(

View File

@ -4,6 +4,7 @@
#ifndef QWASMWINDOWNONCLIENTAREA_H
#define QWASMWINDOWNONCLIENTAREA_H
#include <QtCore/qrect.h>
#include <QtCore/qtconfigmacros.h>
#include <QtCore/qnamespace.h>
@ -155,7 +156,7 @@ private:
struct ResizeData
{
Qt::Edges edges = Qt::Edges::fromInt(0);
QPoint originInScreenCoords;
QPointF originInScreenCoords;
QPoint minShrink;
QPoint maxGrow;
QRect initialBounds;
@ -188,7 +189,7 @@ private:
bool onPointerUp(const PointerEvent &event);
bool onDoubleClick();
QPoint clipPointWithScreen(const QPoint &pointInTitleBarCoords) const;
QPointF clipPointWithScreen(const QPointF &pointInTitleBarCoords) const;
QWasmWindow *m_window;
@ -201,7 +202,8 @@ private:
std::unique_ptr<WebImageButton> m_icon;
int m_capturedPointerId = -1;
QPoint m_lastMovePoint;
QPointF m_moveStartPoint;
QPoint m_moveStartWindowPosition;
std::unique_ptr<qstdweb::EventCallback> m_mouseDownEvent;
std::unique_ptr<qstdweb::EventCallback> m_mouseMoveEvent;