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; return this->type != None;
} }
bool Link::isUrl() const
{
return this->type == Url;
}
} // namespace chatterino } // namespace chatterino

View file

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

View file

@ -1374,11 +1374,23 @@ void ChannelView::mousePressEvent(QMouseEvent *event)
break; break;
case Qt::MiddleButton: { case Qt::MiddleButton: {
const MessageLayoutElement *hoverLayoutElement =
layout->getElementAt(relativePos);
if (hoverLayoutElement != nullptr &&
hoverLayoutElement->getLink().isUrl() &&
this->isScrolling_ == false)
{
break;
}
else
{
if (this->isScrolling_) if (this->isScrolling_)
this->disableScrolling(); this->disableScrolling();
else else
this->enableScrolling(event->screenPos()); this->enableScrolling(event->screenPos());
} }
}
break; break;
default:; default:;
@ -1389,6 +1401,16 @@ void ChannelView::mousePressEvent(QMouseEvent *event)
void ChannelView::mouseReleaseEvent(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 // check if mouse was pressed
if (event->button() == Qt::LeftButton) if (event->button() == Qt::LeftButton)
{ {
@ -1438,26 +1460,36 @@ void ChannelView::mouseReleaseEvent(QMouseEvent *event)
} }
} }
else if (event->button() == Qt::MiddleButton) else if (event->button() == Qt::MiddleButton)
{
if (this->isScrolling_)
{ {
if (event->screenPos() == this->lastMiddlePressPosition_) if (event->screenPos() == this->lastMiddlePressPosition_)
this->enableScrolling(event->screenPos()); this->enableScrolling(event->screenPos());
else else
this->disableScrolling(); this->disableScrolling();
return;
}
else if (foundMessage)
{
const MessageLayoutElement *hoverLayoutElement =
layout->getElementAt(relativePos);
if (hoverLayoutElement == nullptr ||
hoverLayoutElement->getLink().isUrl() == false)
{
return;
}
}
} }
else else
{ {
// not left or right button // not left or right button
return; return;
} }
// find message
this->queueLayout();
std::shared_ptr<MessageLayout> layout;
QPoint relativePos;
int messageIndex;
// no message found // no message found
if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex)) if (!foundMessage)
{ {
// No message at clicked position // No message at clicked position
return; return;
@ -1548,6 +1580,14 @@ void ChannelView::handleMouseClick(QMouseEvent *event,
} }
} }
break; break;
case Qt::MiddleButton: {
auto &link = hoveredElement->getLink();
if (!getSettings()->linksDoubleClickOnly)
{
this->handleLinkClick(event, link, layout);
}
}
break;
default:; default:;
} }
} }
@ -1730,7 +1770,8 @@ void ChannelView::showUserInfoPopup(const QString &userName)
void ChannelView::handleLinkClick(QMouseEvent *event, const Link &link, void ChannelView::handleLinkClick(QMouseEvent *event, const Link &link,
MessageLayout *layout) MessageLayout *layout)
{ {
if (event->button() != Qt::LeftButton) if (event->button() != Qt::LeftButton &&
event->button() != Qt::MiddleButton)
{ {
return; return;
} }