From 7719816891a641042752cb52bf48837cfe9fd870 Mon Sep 17 00:00:00 2001 From: Daniel Pasch Date: Sat, 2 May 2020 13:19:58 +0200 Subject: [PATCH] Middle mouse button can now also open links (#1644) --- src/messages/Link.cpp | 5 +++ src/messages/Link.hpp | 1 + src/widgets/helper/ChannelView.cpp | 71 +++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/messages/Link.cpp b/src/messages/Link.cpp index 7b5bcdb7d..e87c214a3 100644 --- a/src/messages/Link.cpp +++ b/src/messages/Link.cpp @@ -19,4 +19,9 @@ bool Link::isValid() const return this->type != None; } +bool Link::isUrl() const +{ + return this->type == Url; +} + } // namespace chatterino diff --git a/src/messages/Link.hpp b/src/messages/Link.hpp index 52a52444b..4545ec8cf 100644 --- a/src/messages/Link.hpp +++ b/src/messages/Link.hpp @@ -28,6 +28,7 @@ public: QString value; bool isValid() const; + bool isUrl() const; }; } // namespace chatterino diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index a069db0b9..3a5ad0651 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -1374,10 +1374,22 @@ void ChannelView::mousePressEvent(QMouseEvent *event) break; case Qt::MiddleButton: { - if (this->isScrolling_) - this->disableScrolling(); + const MessageLayoutElement *hoverLayoutElement = + layout->getElementAt(relativePos); + + if (hoverLayoutElement != nullptr && + hoverLayoutElement->getLink().isUrl() && + this->isScrolling_ == false) + { + break; + } else - this->enableScrolling(event->screenPos()); + { + if (this->isScrolling_) + this->disableScrolling(); + else + this->enableScrolling(event->screenPos()); + } } break; @@ -1389,6 +1401,16 @@ void ChannelView::mousePressEvent(QMouseEvent *event) void ChannelView::mouseReleaseEvent(QMouseEvent *event) { + // find message + this->queueLayout(); + + std::shared_ptr layout; + QPoint relativePos; + int messageIndex; + + bool foundMessage = + tryGetMessageAt(event->pos(), layout, relativePos, messageIndex); + // check if mouse was pressed if (event->button() == Qt::LeftButton) { @@ -1439,25 +1461,35 @@ void ChannelView::mouseReleaseEvent(QMouseEvent *event) } else if (event->button() == Qt::MiddleButton) { - if (event->screenPos() == this->lastMiddlePressPosition_) - this->enableScrolling(event->screenPos()); - else - this->disableScrolling(); + if (this->isScrolling_) + { + if (event->screenPos() == this->lastMiddlePressPosition_) + this->enableScrolling(event->screenPos()); + else + this->disableScrolling(); + + return; + } + else if (foundMessage) + { + const MessageLayoutElement *hoverLayoutElement = + layout->getElementAt(relativePos); + + if (hoverLayoutElement == nullptr || + hoverLayoutElement->getLink().isUrl() == false) + { + return; + } + } } else { // not left or right button return; } - // find message - this->queueLayout(); - - std::shared_ptr layout; - QPoint relativePos; - int messageIndex; // no message found - if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex)) + if (!foundMessage) { // No message at clicked position return; @@ -1548,6 +1580,14 @@ void ChannelView::handleMouseClick(QMouseEvent *event, } } break; + case Qt::MiddleButton: { + auto &link = hoveredElement->getLink(); + if (!getSettings()->linksDoubleClickOnly) + { + this->handleLinkClick(event, link, layout); + } + } + break; default:; } } @@ -1730,7 +1770,8 @@ void ChannelView::showUserInfoPopup(const QString &userName) void ChannelView::handleLinkClick(QMouseEvent *event, const Link &link, MessageLayout *layout) { - if (event->button() != Qt::LeftButton) + if (event->button() != Qt::LeftButton && + event->button() != Qt::MiddleButton) { return; }