From fc0e89edf3c9bf70ae1de5549c49000a031e1d00 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 24 Jun 2018 16:09:11 +0000 Subject: [PATCH] Implement "frameless draggable" windows on Linux Not yet tested on Mac but should work just the same Fix #472 --- src/widgets/basewindow.cpp | 59 ++++++++++++++++++++++++++++++++++++++ src/widgets/basewindow.hpp | 6 ++++ 2 files changed, 65 insertions(+) diff --git a/src/widgets/basewindow.cpp b/src/widgets/basewindow.cpp index d6e1500cc..150f86c9f 100644 --- a/src/widgets/basewindow.cpp +++ b/src/widgets/basewindow.cpp @@ -254,6 +254,64 @@ void BaseWindow::wheelEvent(QWheelEvent *event) } } +void BaseWindow::mousePressEvent(QMouseEvent *event) +{ +#ifndef Q_OS_WIN + if (this->flags_ & FramelessDraggable) { + this->movingRelativePos = event->localPos(); + if (auto widget = this->childAt(event->localPos().x(), event->localPos().y())) { + std::function recursiveCheckMouseTracking; + recursiveCheckMouseTracking = [&](QWidget *widget) { + if (widget == nullptr) { + return false; + } + + if (widget->hasMouseTracking()) { + return true; + } + + return recursiveCheckMouseTracking(widget->parentWidget()); + }; + + if (!recursiveCheckMouseTracking(widget)) { + debug::Log("Start moving"); + this->moving = true; + } + } + } +#endif + + BaseWidget::mousePressEvent(event); +} + +void BaseWindow::mouseReleaseEvent(QMouseEvent *event) +{ +#ifndef Q_OS_WIN + if (this->flags_ & FramelessDraggable) { + if (this->moving) { + debug::Log("Stop moving"); + this->moving = false; + } + } +#endif + + BaseWidget::mouseReleaseEvent(event); +} + +void BaseWindow::mouseMoveEvent(QMouseEvent *event) +{ +#ifndef Q_OS_WIN + if (this->flags_ & FramelessDraggable) { + if (this->moving) { + const auto &newPos = event->screenPos() - this->movingRelativePos; + this->move(newPos.x(), newPos.y()); + } + } +#endif + + BaseWidget::mouseMoveEvent(event); +} + void BaseWindow::addTitleBarButton(const TitleBarButton::Style &style, std::function onClicked) { @@ -588,5 +646,6 @@ void BaseWindow::calcButtonsSizes() this->ui_.exitButton->setScaleIndependantSize(46, 30); } } + } // namespace widgets } // namespace chatterino diff --git a/src/widgets/basewindow.hpp b/src/widgets/basewindow.hpp index 37b98570f..b88bbccc4 100644 --- a/src/widgets/basewindow.hpp +++ b/src/widgets/basewindow.hpp @@ -63,6 +63,12 @@ protected: virtual bool event(QEvent *event) override; virtual void wheelEvent(QWheelEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; + QPointF movingRelativePos; + bool moving{}; + void updateScale(); private: