Skip to content

Commit

Permalink
Improve navigate via click reliability on touch screen
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpurcell committed Feb 12, 2025
1 parent bf8238f commit 591791e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/qvapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ qreal QVApplication::keyboardAutoRepeatInterval()
#endif
}

bool QVApplication::isMouseEventSynthesized(const QMouseEvent *event)
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
return event->source() != Qt::MouseEventNotSynthesized;
#else
return event->deviceType() != QInputDevice::DeviceType::Mouse;
#endif
}

bool QVApplication::supportsSessionPersistence()
{
#if defined(Q_OS_MACOS)
Expand Down
2 changes: 2 additions & 0 deletions src/qvapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class QVApplication : public QApplication

static qreal keyboardAutoRepeatInterval();

static bool isMouseEventSynthesized(const QMouseEvent *event);

static bool supportsSessionPersistence();

static bool tryRestoreLastSession();
Expand Down
29 changes: 16 additions & 13 deletions src/qvgraphicsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void QVGraphicsView::mousePressEvent(QMouseEvent *event)
isDelayingDrag = delayStart;
if (!isDelayingDrag)
viewport()->setCursor(Qt::ClosedHandCursor);
isLastMousePosDubious = event->type() == QEvent::MouseButtonDblClick && QVApplication::isMouseEventSynthesized(event);
lastMousePos = event->pos();
setCursorVisible(true);
};
Expand Down Expand Up @@ -199,6 +200,14 @@ void QVGraphicsView::mouseMoveEvent(QMouseEvent *event)
const QPoint delta = event->pos() - lastMousePos;
if (isDelayingDrag)
{
if (isLastMousePosDubious)
{
// On the second press of a double tap on a touch screen, the position may
// have been copied from the first press, so we can't rely on it
isLastMousePosDubious = false;
lastMousePos = event->pos();
return;
}
if (qMax(qAbs(delta.x()), qAbs(delta.y())) < startDragDistance)
return;
isDelayingDrag = false;
Expand All @@ -224,23 +233,17 @@ void QVGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
if (event->button() == Qt::MouseButton::LeftButton)
{
const bool isAltAction = event->modifiers().testFlag(Qt::ControlModifier);
if (!isAltAction && enableNavigationRegions && getNavigationRegion(lastMousePos).has_value())
const bool isInNavRegion = !isAltAction && enableNavigationRegions && getNavigationRegion(lastMousePos).has_value();
if (!isInNavRegion)
{
// Special case for rapid clicking in the navigation region
mousePressEvent(event);
executeClickAction(isAltAction ? altDoubleClickAction : doubleClickAction);
return;
}
executeClickAction(isAltAction ? altDoubleClickAction : doubleClickAction);
return;
}
else if (event->button() == Qt::MouseButton::MiddleButton && middleButtonMode == Qv::ClickOrDrag::Click)
{
// Special case for rapid clicking the middle button
mousePressEvent(event);
return;
}

QGraphicsView::mouseDoubleClickEvent(event);
// Pass unhandled events to QWidget instead of QGraphicsView otherwise we won't
// receive a press event for the second click of a double click
QWidget::mouseDoubleClickEvent(event);
}

bool QVGraphicsView::event(QEvent *event)
Expand Down Expand Up @@ -974,7 +977,7 @@ void QVGraphicsView::matchContentCenter(const QRect target)

std::optional<Qv::GoToFileMode> QVGraphicsView::getNavigationRegion(const QPoint mousePos) const
{
const int regionWidth = qMin(width() / 3, 200);
const int regionWidth = qMin(width() / 3, 175);
if (mousePos.x() < regionWidth)
return isRightToLeft() ? Qv::GoToFileMode::Next : Qv::GoToFileMode::Previous;
if (mousePos.x() >= width() - regionWidth)
Expand Down
1 change: 1 addition & 0 deletions src/qvgraphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private slots:
Qt::MouseButton pressedMouseButton {Qt::MouseButton::NoButton};
Qt::KeyboardModifiers mousePressModifiers {Qt::KeyboardModifier::NoModifier};
bool isDelayingDrag {false};
bool isLastMousePosDubious {false};
QPoint lastMousePos;
QElapsedTimer lastFocusIn;

Expand Down

0 comments on commit 591791e

Please sign in to comment.