Merge pull request #677 from 23rd/patch-4-link-tooltip-bttv

Added tooltip to links.
This commit is contained in:
pajlada 2018-08-29 01:23:17 +02:00 committed by GitHub
commit 6eb21f4b68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 90 additions and 4 deletions

View file

@ -237,6 +237,7 @@ SOURCES += \
src/messages/Emote.cpp \ src/messages/Emote.cpp \
src/messages/ImageSet.cpp \ src/messages/ImageSet.cpp \
src/providers/bttv/BttvEmotes.cpp \ src/providers/bttv/BttvEmotes.cpp \
src/providers/LinkResolver.cpp \
src/providers/ffz/FfzEmotes.cpp \ src/providers/ffz/FfzEmotes.cpp \
src/autogenerated/ResourcesAutogen.cpp \ src/autogenerated/ResourcesAutogen.cpp \
src/singletons/Badges.cpp \ src/singletons/Badges.cpp \
@ -431,6 +432,7 @@ HEADERS += \
src/messages/ImageSet.hpp \ src/messages/ImageSet.hpp \
src/common/Outcome.hpp \ src/common/Outcome.hpp \
src/providers/bttv/BttvEmotes.hpp \ src/providers/bttv/BttvEmotes.hpp \
src/providers/LinkResolver.hpp \
src/providers/ffz/FfzEmotes.hpp \ src/providers/ffz/FfzEmotes.hpp \
src/autogenerated/ResourcesAutogen.hpp \ src/autogenerated/ResourcesAutogen.hpp \
src/singletons/Badges.hpp \ src/singletons/Badges.hpp \

View file

@ -0,0 +1,42 @@
#include "providers/LinkResolver.hpp"
#include "common/Common.hpp"
#include "common/NetworkRequest.hpp"
#include <QString>
namespace chatterino {
void LinkResolver::getLinkInfo(const QString url,
std::function<void(QString)> successCallback)
{
QString requestUrl("https://braize.pajlada.com/chatterino/link_resolver/" +
QUrl::toPercentEncoding(url, "", "/:"));
NetworkRequest request(requestUrl);
request.setCaller(QThread::currentThread());
request.setTimeout(30000);
request.onSuccess([successCallback](auto result) mutable -> Outcome {
auto root = result.parseJson();
auto statusCode = root.value("status").toInt();
QString response = QString();
if (statusCode == 200) {
response = root.value("tooltip").toString();
} else {
response = root.value("message").toString();
}
successCallback(QUrl::fromPercentEncoding(response.toUtf8()));
return Success;
});
request.onError([successCallback](auto result) {
successCallback("No link info found");
return true;
});
request.execute();
}
} // namespace chatterino

View file

@ -0,0 +1,17 @@
#pragma once
#include <QString>
#include <functional>
namespace chatterino {
class LinkResolver
{
public:
static void getLinkInfo(const QString url,
std::function<void(QString)> callback);
private:
};
} // namespace chatterino

View file

@ -8,6 +8,7 @@
#include "messages/Message.hpp" #include "messages/Message.hpp"
#include "providers/twitch/TwitchBadges.hpp" #include "providers/twitch/TwitchBadges.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "providers/LinkResolver.hpp"
#include "singletons/Emotes.hpp" #include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
@ -268,12 +269,20 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_)
link = Link(Link::Url, linkString); link = Link(Link::Url, linkString);
textColor = MessageColor(MessageColor::Link); textColor = MessageColor(MessageColor::Link);
this->emplace<TextElement>(lowercaseLinkString, auto linkMELowercase = this->emplace<TextElement>(lowercaseLinkString,
MessageElementFlag::LowercaseLink, textColor) MessageElementFlag::LowercaseLink, textColor)
->setLink(link); ->setLink(link);
this->emplace<TextElement>(string, MessageElementFlag::OriginalLink, auto linkMEOriginal = this->emplace<TextElement>(string,
textColor) MessageElementFlag::OriginalLink, textColor)
->setLink(link); ->setLink(link);
LinkResolver::getLinkInfo(
linkString, [linkMELowercase, linkMEOriginal](QString tooltipText) {
if (!tooltipText.isEmpty()) {
linkMELowercase->setTooltip(tooltipText);
linkMEOriginal->setTooltip(tooltipText);
}
});
} }
// if (!linkString.isEmpty()) { // if (!linkString.isEmpty()) {

View file

@ -103,6 +103,7 @@ public:
/// Links /// Links
BoolSetting linksDoubleClickOnly = {"/links/doubleClickToOpen", false}; BoolSetting linksDoubleClickOnly = {"/links/doubleClickToOpen", false};
BoolSetting enableLinkInfoTooltip = {"/links/linkInfoTooltip", false};
BoolSetting enableLowercaseLink = {"/links/linkLowercase", true}; BoolSetting enableLowercaseLink = {"/links/linkLowercase", true};
/// Ingored Users /// Ingored Users

View file

@ -87,6 +87,11 @@ void TooltipWidget::setText(QString text)
this->displayText_->setText(text); this->displayText_->setText(text);
} }
void TooltipWidget::setWordWrap(bool wrap)
{
this->displayText_->setWordWrap(wrap);
}
void TooltipWidget::changeEvent(QEvent *) void TooltipWidget::changeEvent(QEvent *)
{ {
// clear parents event // clear parents event

View file

@ -19,6 +19,7 @@ public:
virtual ~TooltipWidget() override; virtual ~TooltipWidget() override;
void setText(QString text); void setText(QString text);
void setWordWrap(bool wrap);
#ifdef USEWINSDK #ifdef USEWINSDK
void raise(); void raise();

View file

@ -861,11 +861,15 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
return; return;
} }
const auto &tooltip = hoverLayoutElement->getCreator().getTooltip(); const auto &tooltip = hoverLayoutElement->getCreator().getTooltip();
bool isLinkValid = hoverLayoutElement->getLink().isValid();
if (tooltip.isEmpty()) { if (tooltip.isEmpty()) {
tooltipWidget->hide(); tooltipWidget->hide();
} else if (isLinkValid && !getSettings()->enableLinkInfoTooltip) {
tooltipWidget->hide();
} else { } else {
tooltipWidget->moveTo(this, event->globalPos()); tooltipWidget->moveTo(this, event->globalPos());
tooltipWidget->setWordWrap(isLinkValid);
tooltipWidget->setText(tooltip); tooltipWidget->setText(tooltip);
tooltipWidget->adjustSize(); tooltipWidget->adjustSize();
tooltipWidget->show(); tooltipWidget->show();
@ -873,7 +877,7 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
} }
// check if word has a link // check if word has a link
if (hoverLayoutElement->getLink().isValid()) { if (isLinkValid) {
this->setCursor(Qt::PointingHandCursor); this->setCursor(Qt::PointingHandCursor);
} else { } else {
this->setCursor(Qt::ArrowCursor); this->setCursor(Qt::ArrowCursor);

View file

@ -50,6 +50,9 @@ FeelPage::FeelPage()
form->addRow("Links:", form->addRow("Links:",
this->createCheckBox("Open links only on double click", this->createCheckBox("Open links only on double click",
getSettings()->linksDoubleClickOnly)); getSettings()->linksDoubleClickOnly));
form->addRow("",
this->createCheckBox("Show link info in tooltips",
getSettings()->enableLinkInfoTooltip));
} }
layout->addSpacing(16); layout->addSpacing(16);

View file

@ -458,6 +458,8 @@ void SplitHeader::enterEvent(QEvent *event)
tooltip->moveTo(this, this->mapToGlobal(this->rect().bottomLeft()), tooltip->moveTo(this, this->mapToGlobal(this->rect().bottomLeft()),
false); false);
tooltip->setText(this->tooltipText_); tooltip->setText(this->tooltipText_);
tooltip->setWordWrap(false);
tooltip->adjustSize();
tooltip->show(); tooltip->show();
tooltip->raise(); tooltip->raise();
} }