Fix preview on hover not working when Animated emotes options was disabled.

Fixes #1546

This change introduces a "window timer" that runs every 100ms that we
use to update the pixmap if necessary, since there is no signal for "let
me know when this image is done loading".
This commit is contained in:
Rasmus Karlsson 2020-02-15 17:16:10 +01:00
parent 64c58e724a
commit 7a08d73434
5 changed files with 57 additions and 10 deletions

View file

@ -2,3 +2,4 @@
## Unversioned ## Unversioned
- Minor: Emotes in the emote popup are now sorted in the same order as the tab completion (#1549) - Minor: Emotes in the emote popup are now sorted in the same order as the tab completion (#1549)
- Bugfix: Fix preview on hover not working when Animated emotes options was disabled (#1546)

View file

@ -5,6 +5,7 @@
#include "widgets/TooltipWidget.hpp" #include "widgets/TooltipWidget.hpp"
namespace chatterino { namespace chatterino {
TooltipPreviewImage &TooltipPreviewImage::instance() TooltipPreviewImage &TooltipPreviewImage::instance()
{ {
static TooltipPreviewImage *instance = new TooltipPreviewImage(); static TooltipPreviewImage *instance = new TooltipPreviewImage();
@ -13,19 +14,19 @@ TooltipPreviewImage &TooltipPreviewImage::instance()
TooltipPreviewImage::TooltipPreviewImage() TooltipPreviewImage::TooltipPreviewImage()
{ {
connections_.push_back(getApp()->windows->gifRepaintRequested.connect([&] { auto windows = getApp()->windows;
auto tooltipWidget = TooltipWidget::instance();
if (this->image_ && !tooltipWidget->isHidden()) this->connections_.push_back(windows->gifRepaintRequested.connect([&] {
if (this->image_ && this->image_->animated())
{ {
auto pixmap = this->image_->pixmapOrLoad(); this->refreshTooltipWidgetPixmap();
if (pixmap)
{
tooltipWidget->setImage(*pixmap);
}
} }
else }));
this->connections_.push_back(windows->miscUpdate.connect([&] {
if (this->attemptRefresh)
{ {
tooltipWidget->clearImage(); this->refreshTooltipWidgetPixmap();
} }
})); }));
} }
@ -33,5 +34,30 @@ TooltipPreviewImage::TooltipPreviewImage()
void TooltipPreviewImage::setImage(ImagePtr image) void TooltipPreviewImage::setImage(ImagePtr image)
{ {
this->image_ = image; this->image_ = image;
this->refreshTooltipWidgetPixmap();
} }
void TooltipPreviewImage::refreshTooltipWidgetPixmap()
{
auto tooltipWidget = TooltipWidget::instance();
if (this->image_ && !tooltipWidget->isHidden())
{
if (auto pixmap = this->image_->pixmapOrLoad())
{
tooltipWidget->setImage(*pixmap);
this->attemptRefresh = false;
}
else
{
this->attemptRefresh = true;
}
}
else
{
tooltipWidget->clearImage();
}
}
} // namespace chatterino } // namespace chatterino

View file

@ -3,6 +3,7 @@
#include "messages/Image.hpp" #include "messages/Image.hpp"
namespace chatterino { namespace chatterino {
class TooltipPreviewImage class TooltipPreviewImage
{ {
public: public:
@ -17,5 +18,13 @@ private:
private: private:
ImagePtr image_ = nullptr; ImagePtr image_ = nullptr;
std::vector<pajlada::Signals::ScopedConnection> connections_; std::vector<pajlada::Signals::ScopedConnection> connections_;
// attemptRefresh is set to true in case we want to preview an image that has not loaded yet (if pixmapOrLoad fails)
bool attemptRefresh{false};
// Refresh the pixmap used in the Tooltip Widget
// Called from setImage and from the "gif repaint" signal if the image is animated
void refreshTooltipWidgetPixmap();
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -111,6 +111,12 @@ WindowManager::WindowManager()
QObject::connect(this->saveTimer, &QTimer::timeout, [] { QObject::connect(this->saveTimer, &QTimer::timeout, [] {
getApp()->windows->save(); // getApp()->windows->save(); //
}); });
this->miscUpdateTimer_.start(100);
QObject::connect(&this->miscUpdateTimer_, &QTimer::timeout, [this] {
this->miscUpdate.invoke(); //
});
} }
MessageElementFlags WindowManager::getWordFlags() MessageElementFlags WindowManager::getWordFlags()

View file

@ -80,6 +80,10 @@ public:
pajlada::Signals::NoArgSignal wordFlagsChanged; pajlada::Signals::NoArgSignal wordFlagsChanged;
// This signal fires every 100ms and can be used to trigger random things that require a recheck.
// It is currently being used by the "Tooltip Preview Image" system to recheck if an image is ready to be rendered.
pajlada::Signals::NoArgSignal miscUpdate;
private: private:
void encodeNodeRecusively(SplitContainer::Node *node, QJsonObject &obj); void encodeNodeRecusively(SplitContainer::Node *node, QJsonObject &obj);
@ -96,6 +100,7 @@ private:
pajlada::SettingListener wordFlagsListener_; pajlada::SettingListener wordFlagsListener_;
QTimer *saveTimer; QTimer *saveTimer;
QTimer miscUpdateTimer_;
}; };
} // namespace chatterino } // namespace chatterino