From 0f306550bf19e09c9d74f5139e58555055032f09 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Thu, 7 May 2020 03:04:40 +1000 Subject: [PATCH] slider: use custom dragging code on mac The slider dragging implementation used for other platforms caused issues on macOS. We now have a custom Mac implementation that doesn't cause problems, and is arguably better than the other implementation anyway. --- app/CMakeLists.txt | 6 ++++ app/widget/slider/sliderlabel.cpp | 50 ++++++++++++++++++++----------- app/widget/slider/sliderlabel.h | 2 ++ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index b22b69ced..2bcbd2de3 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -155,6 +155,12 @@ if (WIN32) PRIVATE DbgHelp ) +elseif (APPLE) + target_link_libraries( + ${OLIVE_TARGET} + PRIVATE + "-framework ApplicationServices" + ) endif() set(OLIVE_TS_FILES diff --git a/app/widget/slider/sliderlabel.cpp b/app/widget/slider/sliderlabel.cpp index a54a54cf0..aee4a8409 100644 --- a/app/widget/slider/sliderlabel.cpp +++ b/app/widget/slider/sliderlabel.cpp @@ -22,6 +22,11 @@ #include #include +#include + +#ifdef Q_OS_MAC +#include +#endif OLIVE_NAMESPACE_ENTER @@ -48,40 +53,51 @@ SliderLabel::SliderLabel(QWidget *parent) : setFocusPolicy(Qt::TabFocus); } -void SliderLabel::mousePressEvent(QMouseEvent *ev) +void SliderLabel::mousePressEvent(QMouseEvent *) { - QLabel::mousePressEvent(ev); + emit drag_start(); +#if defined(Q_OS_MAC) + CGAssociateMouseAndMouseCursorPosition(false); + CGDisplayHideCursor(kCGDirectMainDisplay); + CGGetLastMouseDelta(nullptr, nullptr); +#else drag_start_ = QCursor::pos(); static_cast(QApplication::instance())->setOverrideCursor(Qt::BlankCursor); - - emit drag_start(); +#endif } -void SliderLabel::mouseMoveEvent(QMouseEvent *ev) +void SliderLabel::mouseMoveEvent(QMouseEvent *) { - QLabel::mouseMoveEvent(ev); - - QPoint current_pos = QCursor::pos(); - - int x_mvmt = current_pos.x() - drag_start_.x(); - int y_mvmt = drag_start_.y() - current_pos.y(); - - emit dragged(x_mvmt + y_mvmt); + int32_t x_mvmt, y_mvmt; // Keep cursor in the same position +#if defined(Q_OS_MAC) + CGGetLastMouseDelta(&x_mvmt, &y_mvmt); +#else + QPoint current_pos = QCursor::pos(); + + x_mvmt = current_pos.x() - drag_start_.x(); + y_mvmt = drag_start_.y() - current_pos.y(); + QCursor::setPos(drag_start_); +#endif + + emit dragged(x_mvmt + y_mvmt); } -void SliderLabel::mouseReleaseEvent(QMouseEvent *ev) +void SliderLabel::mouseReleaseEvent(QMouseEvent *) { - QWidget::mouseReleaseEvent(ev); +#if defined(Q_OS_MAC) + CGAssociateMouseAndMouseCursorPosition(true); + CGDisplayShowCursor(kCGDirectMainDisplay); +#else + static_cast(QApplication::instance())->restoreOverrideCursor(); +#endif // Emit a clicked signal emit drag_stop(); - - static_cast(QApplication::instance())->restoreOverrideCursor(); } void SliderLabel::focusInEvent(QFocusEvent *event) diff --git a/app/widget/slider/sliderlabel.h b/app/widget/slider/sliderlabel.h index 92771e669..c83e86bfc 100644 --- a/app/widget/slider/sliderlabel.h +++ b/app/widget/slider/sliderlabel.h @@ -54,6 +54,8 @@ signals: private: QPoint drag_start_; + bool cancel_mm_event_; + }; OLIVE_NAMESPACE_EXIT