mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Added tooltip to links via BTTV API.
This commit is contained in:
parent
fec89a4807
commit
5253ab3f85
8 changed files with 76 additions and 1 deletions
|
@ -237,6 +237,7 @@ SOURCES += \
|
|||
src/messages/Emote.cpp \
|
||||
src/messages/ImageSet.cpp \
|
||||
src/providers/bttv/BttvEmotes.cpp \
|
||||
src/providers/bttv/BttvTooltip.cpp \
|
||||
src/providers/ffz/FfzEmotes.cpp \
|
||||
src/autogenerated/ResourcesAutogen.cpp \
|
||||
src/singletons/Badges.cpp \
|
||||
|
@ -433,6 +434,7 @@ HEADERS += \
|
|||
src/messages/ImageSet.hpp \
|
||||
src/common/Outcome.hpp \
|
||||
src/providers/bttv/BttvEmotes.hpp \
|
||||
src/providers/bttv/BttvTooltip.hpp \
|
||||
src/providers/ffz/FfzEmotes.hpp \
|
||||
src/autogenerated/ResourcesAutogen.hpp \
|
||||
src/singletons/Badges.hpp \
|
||||
|
|
39
src/providers/bttv/BttvTooltip.cpp
Normal file
39
src/providers/bttv/BttvTooltip.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include "providers/bttv/BttvTooltip.hpp"
|
||||
|
||||
#include "common/Common.hpp"
|
||||
#include "common/NetworkRequest.hpp"
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
void BttvTooltip::getUrlTooltip(const QString url,
|
||||
std::function<void(QString)> successCallback)
|
||||
{
|
||||
QString requestUrl("https://api.betterttv.net/2/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();
|
||||
/* When tooltip is not a string, in this case,
|
||||
onError runs before onSuccess,
|
||||
so there is no point in doing "if" condition. */
|
||||
auto tooltip = root.value("tooltip").toString();
|
||||
successCallback(QUrl::fromPercentEncoding(tooltip.toUtf8()));
|
||||
|
||||
return Success;
|
||||
});
|
||||
|
||||
request.onError([successCallback](auto result) {
|
||||
successCallback("No link info found");
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
request.execute();
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
17
src/providers/bttv/BttvTooltip.hpp
Normal file
17
src/providers/bttv/BttvTooltip.hpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <functional>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
class BttvTooltip
|
||||
{
|
||||
public:
|
||||
static void getUrlTooltip(const QString url,
|
||||
std::function<void(QString)> callback);
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
|
@ -8,6 +8,7 @@
|
|||
#include "messages/Message.hpp"
|
||||
#include "providers/twitch/TwitchBadges.hpp"
|
||||
#include "providers/twitch/TwitchChannel.hpp"
|
||||
#include "providers/bttv/BttvTooltip.hpp"
|
||||
#include "singletons/Emotes.hpp"
|
||||
#include "singletons/Resources.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
|
@ -268,12 +269,19 @@ void TwitchMessageBuilder::addTextOrEmoji(const QString &string_)
|
|||
link = Link(Link::Url, linkString);
|
||||
|
||||
textColor = MessageColor(MessageColor::Link);
|
||||
this->emplace<TextElement>(lowercaseLinkString,
|
||||
auto linkMessageElement = this->emplace<TextElement>(lowercaseLinkString,
|
||||
MessageElementFlag::LowercaseLink, textColor)
|
||||
->setLink(link);
|
||||
this->emplace<TextElement>(string, MessageElementFlag::OriginalLink,
|
||||
textColor)
|
||||
->setLink(link);
|
||||
|
||||
BttvTooltip::getUrlTooltip(
|
||||
linkString, [linkMessageElement](QString tooltipText) {
|
||||
if (!tooltipText.isEmpty()) {
|
||||
linkMessageElement->setTooltip(tooltipText);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// if (!linkString.isEmpty()) {
|
||||
|
|
|
@ -87,6 +87,11 @@ void TooltipWidget::setText(QString text)
|
|||
this->displayText_->setText(text);
|
||||
}
|
||||
|
||||
void TooltipWidget::setWordWrap(bool wrap)
|
||||
{
|
||||
this->displayText_->setWordWrap(wrap);
|
||||
}
|
||||
|
||||
void TooltipWidget::changeEvent(QEvent *)
|
||||
{
|
||||
// clear parents event
|
||||
|
|
|
@ -19,6 +19,7 @@ public:
|
|||
virtual ~TooltipWidget() override;
|
||||
|
||||
void setText(QString text);
|
||||
void setWordWrap(bool wrap);
|
||||
|
||||
#ifdef USEWINSDK
|
||||
void raise();
|
||||
|
|
|
@ -866,6 +866,7 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
|
|||
tooltipWidget->hide();
|
||||
} else {
|
||||
tooltipWidget->moveTo(this, event->globalPos());
|
||||
tooltipWidget->setWordWrap(hoverLayoutElement->getLink().isValid());
|
||||
tooltipWidget->setText(tooltip);
|
||||
tooltipWidget->adjustSize();
|
||||
tooltipWidget->show();
|
||||
|
|
|
@ -458,6 +458,8 @@ void SplitHeader::enterEvent(QEvent *event)
|
|||
tooltip->moveTo(this, this->mapToGlobal(this->rect().bottomLeft()),
|
||||
false);
|
||||
tooltip->setText(this->tooltipText_);
|
||||
tooltip->setWordWrap(false);
|
||||
tooltip->adjustSize();
|
||||
tooltip->show();
|
||||
tooltip->raise();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue