From dbc20baa656a572af37bdc686b44f6319ae5d91f Mon Sep 17 00:00:00 2001 From: fourtf Date: Wed, 21 Aug 2019 01:44:19 +0200 Subject: [PATCH] fixed emote preview not using 3x images --- src/messages/Image.cpp | 15 ++++++--- src/messages/Image.hpp | 3 +- src/messages/ImageSet.cpp | 56 +++++++++++++++++++-------------- src/messages/ImageSet.hpp | 3 ++ src/messages/MessageElement.cpp | 6 ++-- 5 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index 3a348a82e..a0c82418d 100644 --- a/src/messages/Image.cpp +++ b/src/messages/Image.cpp @@ -286,13 +286,20 @@ boost::optional Image::pixmapOrLoad() const { assertInGuiThread(); + this->load(); + + return this->frames_->current(); +} + +void Image::load() const +{ + assertInGuiThread(); + if (this->shouldLoad_) { const_cast(this)->shouldLoad_ = false; - const_cast(this)->load(); + const_cast(this)->actuallyLoad(); } - - return this->frames_->current(); } qreal Image::scale() const @@ -332,7 +339,7 @@ int Image::height() const return 16; } -void Image::load() +void Image::actuallyLoad() { NetworkRequest(this->url().string) .concurrent() diff --git a/src/messages/Image.hpp b/src/messages/Image.hpp index e7f3f4ebd..1cd30d81b 100644 --- a/src/messages/Image.hpp +++ b/src/messages/Image.hpp @@ -56,6 +56,7 @@ public: bool loaded() const; // either returns the current pixmap, or triggers loading it (lazy loading) boost::optional pixmapOrLoad() const; + void load() const; qreal scale() const; bool isEmpty() const; int width() const; @@ -72,7 +73,7 @@ private: void setPixmap(const QPixmap &pixmap); - void load(); + void actuallyLoad(); Url url_{}; qreal scale_{1}; diff --git a/src/messages/ImageSet.cpp b/src/messages/ImageSet.cpp index cabaa6a00..5b526dec8 100644 --- a/src/messages/ImageSet.cpp +++ b/src/messages/ImageSet.cpp @@ -58,31 +58,36 @@ const ImagePtr &ImageSet::getImage3() const return this->imageX3_; } -const ImagePtr &ImageSet::getImage(float scale) const +const std::shared_ptr &getImagePriv(const ImageSet &set, float scale) { + scale *= getSettings()->emoteScale; + + int quality = 1; + + if (scale > 2.001f) + quality = 3; + else if (scale > 1.001f) + quality = 2; + + if (!set.getImage3()->isEmpty() && quality == 3) + { + return set.getImage3(); + } + + if (!set.getImage2()->isEmpty() && quality >= 2) + { + return set.getImage2(); + } + + return set.getImage1(); +} + +const ImagePtr &ImageSet::getImageOrLoaded(float scale) const +{ + auto &&result = getImagePriv(*this, scale); + // get best image based on scale - auto &&result = [&]() -> const std::shared_ptr & { - scale *= getSettings()->emoteScale; - - int quality = 1; - - if (scale > 2.001f) - quality = 3; - else if (scale > 1.001f) - quality = 2; - - if (!this->imageX3_->isEmpty() && quality == 3) - { - return this->imageX3_; - } - - if (!this->imageX2_->isEmpty() && quality == 2) - { - return this->imageX2_; - } - - return this->imageX1_; - }(); + result->load(); // prefer other image if selected image is not loaded yet if (result->loaded()) @@ -99,6 +104,11 @@ const ImagePtr &ImageSet::getImage(float scale) const return result; } +const ImagePtr &ImageSet::getImage(float scale) const +{ + return getImagePriv(*this, scale); +} + bool ImageSet::operator==(const ImageSet &other) const { return std::tie(this->imageX1_, this->imageX2_, this->imageX3_) == diff --git a/src/messages/ImageSet.hpp b/src/messages/ImageSet.hpp index e8872b0b7..4e484eedb 100644 --- a/src/messages/ImageSet.hpp +++ b/src/messages/ImageSet.hpp @@ -19,6 +19,9 @@ public: const ImagePtr &getImage2() const; const ImagePtr &getImage3() const; + /// Preferes getting an already loaded image, even if it is smaller/bigger. + /// However, it starts loading the proper image. + const ImagePtr &getImageOrLoaded(float scale) const; const ImagePtr &getImage(float scale) const; bool operator==(const ImageSet &other) const; diff --git a/src/messages/MessageElement.cpp b/src/messages/MessageElement.cpp index a9db2ab95..03240db45 100644 --- a/src/messages/MessageElement.cpp +++ b/src/messages/MessageElement.cpp @@ -134,7 +134,8 @@ void EmoteElement::addToContainer(MessageLayoutContainer &container, { if (flags.has(MessageElementFlag::EmoteImages)) { - auto image = this->emote_->images.getImage(container.getScale()); + auto image = + this->emote_->images.getImageOrLoaded(container.getScale()); if (image->isEmpty()) return; @@ -171,7 +172,8 @@ void BadgeElement::addToContainer(MessageLayoutContainer &container, { if (flags.hasAny(this->getFlags())) { - auto image = this->emote_->images.getImage(container.getScale()); + auto image = + this->emote_->images.getImageOrLoaded(container.getScale()); if (image->isEmpty()) return;