Middle mouse button can now also open links (#1644)

This commit is contained in:
Daniel Pasch 2020-05-02 13:19:58 +02:00 committed by GitHub
parent b4eb56f362
commit 7719816891
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 15 deletions

View file

@ -19,4 +19,9 @@ bool Link::isValid() const
return this->type != None;
}
bool Link::isUrl() const
{
return this->type == Url;
}
} // namespace chatterino

View file

@ -28,6 +28,7 @@ public:
QString value;
bool isValid() const;
bool isUrl() const;
};
} // namespace chatterino

View file

@ -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<MessageLayout> 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<MessageLayout> 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;
}