refactor: Turn link-info into its own element and class (#5178)

This commit is contained in:
nerix
2024-02-18 13:34:00 +01:00
committed by GitHub
parent 42e4559910
commit e130c48f76
22 changed files with 752 additions and 314 deletions
+66 -46
View File
@@ -19,7 +19,8 @@
#include "messages/MessageElement.hpp"
#include "messages/MessageThread.hpp"
#include "providers/colors/ColorProvider.hpp"
#include "providers/LinkResolver.hpp"
#include "providers/links/LinkInfo.hpp"
#include "providers/links/LinkResolver.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
@@ -1937,55 +1938,16 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
}
else
{
if (element->getTooltip() == "No link info loaded")
{
std::weak_ptr<MessageLayout> weakLayout = layout;
LinkResolver::getLinkInfo(
element->getLink().value, nullptr,
[weakLayout, element](QString tooltipText,
Link originalLink,
ImagePtr thumbnail) {
auto shared = weakLayout.lock();
if (!shared)
{
return;
}
element->setTooltip(tooltipText);
element->setThumbnail(thumbnail);
});
}
auto thumbnailSize = getSettings()->thumbnailSize;
if (thumbnailSize == 0)
auto *linkElement = dynamic_cast<LinkElement *>(element);
if (linkElement)
{
// "Show thumbnails" is set to "Off", show text only
this->tooltipWidget_->setOne({nullptr, element->getTooltip()});
}
else
{
const auto shouldHideThumbnail =
isInStreamerMode() &&
getSettings()->streamerModeHideLinkThumbnails &&
element->getThumbnail() != nullptr &&
!element->getThumbnail()->url().string.isEmpty();
auto thumb =
shouldHideThumbnail
? Image::fromResourcePixmap(getResources().streamerMode)
: element->getThumbnail();
if (element->getThumbnailType() ==
MessageElement::ThumbnailType::Link_Thumbnail)
if (linkElement->linkInfo()->isPending())
{
this->tooltipWidget_->setOne({
std::move(thumb),
element->getTooltip(),
thumbnailSize,
thumbnailSize,
});
}
else
{
this->tooltipWidget_->setOne({std::move(thumb), ""});
getIApp()->getLinkResolver()->resolve(
linkElement->linkInfo());
}
this->setLinkInfoTooltip(linkElement->linkInfo());
}
}
@@ -3103,4 +3065,62 @@ bool ChannelView::canReplyToMessages() const
return true;
}
void ChannelView::setLinkInfoTooltip(LinkInfo *info)
{
assert(info);
auto thumbnailSize = getSettings()->thumbnailSize;
ImagePtr thumbnail;
if (info->hasThumbnail() && thumbnailSize > 0)
{
if (isInStreamerMode() && getSettings()->streamerModeHideLinkThumbnails)
{
thumbnail = Image::fromResourcePixmap(getResources().streamerMode);
}
else
{
thumbnail = info->thumbnail();
}
}
this->tooltipWidget_->setOne({
.image = thumbnail,
.text = info->tooltip(),
.customWidth = thumbnailSize,
.customHeight = thumbnailSize,
});
if (info->isLoaded())
{
this->pendingLinkInfo_.clear();
return; // Either resolved or errored (can't change anymore)
}
// listen to changes
if (this->pendingLinkInfo_.data() == info)
{
return; // same info - already registered
}
if (this->pendingLinkInfo_)
{
QObject::disconnect(this->pendingLinkInfo_.data(),
&LinkInfo::stateChanged, this, nullptr);
}
QObject::connect(info, &LinkInfo::stateChanged, this,
&ChannelView::pendingLinkInfoStateChanged);
this->pendingLinkInfo_ = info;
}
void ChannelView::pendingLinkInfoStateChanged()
{
if (!this->pendingLinkInfo_)
{
return;
}
this->setLinkInfoTooltip(this->pendingLinkInfo_.data());
}
} // namespace chatterino