Refactor Image & Image's Frames (#4773)

This commit is contained in:
pajlada 2023-09-13 21:26:45 +02:00 committed by GitHub
parent be7140ecce
commit 9ca2578c1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 30 deletions

View file

@ -14,6 +14,7 @@
- Dev: Remove `boost::noncopyable` use & `boost_random` dependency. (#4776) - Dev: Remove `boost::noncopyable` use & `boost_random` dependency. (#4776)
- Dev: Fix clang-tidy `cppcoreguidelines-pro-type-member-init` warnings. (#4426) - Dev: Fix clang-tidy `cppcoreguidelines-pro-type-member-init` warnings. (#4426)
- Dev: Immediate layout for invisible `ChannelView`s is skipped. (#4811) - Dev: Immediate layout for invisible `ChannelView`s is skipped. (#4811)
- Dev: Refactor `Image` & Image's `Frames`. (#4773)
## 2.4.5 ## 2.4.5

View file

@ -170,17 +170,23 @@ namespace detail {
return this->items_.size() > 1; return this->items_.size() > 1;
} }
boost::optional<QPixmap> Frames::current() const std::optional<QPixmap> Frames::current() const
{ {
if (this->items_.size() == 0) if (this->items_.empty())
return boost::none; {
return std::nullopt;
}
return this->items_[this->index_].image; return this->items_[this->index_].image;
} }
boost::optional<QPixmap> Frames::first() const std::optional<QPixmap> Frames::first() const
{ {
if (this->items_.size() == 0) if (this->items_.empty())
return boost::none; {
return std::nullopt;
}
return this->items_.front().image; return this->items_.front().image;
} }
@ -208,7 +214,7 @@ namespace detail {
} }
} }
if (frames.size() == 0) if (frames.empty())
{ {
qCDebug(chatterinoImage) qCDebug(chatterinoImage)
<< "Error while reading image" << url.string << ": '" << "Error while reading image" << url.string << ": '"
@ -344,11 +350,9 @@ ImagePtr Image::fromResourcePixmap(const QPixmap &pixmap, qreal scale)
{ {
return shared; return shared;
} }
else
{
cache.erase(it); cache.erase(it);
} }
}
auto newImage = ImagePtr(new Image(scale)); auto newImage = ImagePtr(new Image(scale));
@ -416,10 +420,10 @@ bool Image::loaded() const
{ {
assertInGuiThread(); assertInGuiThread();
return bool(this->frames_->current()); return this->frames_->current().has_value();
} }
boost::optional<QPixmap> Image::pixmapOrLoad() const std::optional<QPixmap> Image::pixmapOrLoad() const
{ {
assertInGuiThread(); assertInGuiThread();
@ -470,8 +474,11 @@ int Image::width() const
assertInGuiThread(); assertInGuiThread();
if (auto pixmap = this->frames_->first()) if (auto pixmap = this->frames_->first())
{
return int(pixmap->width() * this->scale_); return int(pixmap->width() * this->scale_);
else }
// No frames loaded, use our default magic width 16
return 16; return 16;
} }
@ -480,20 +487,26 @@ int Image::height() const
assertInGuiThread(); assertInGuiThread();
if (auto pixmap = this->frames_->first()) if (auto pixmap = this->frames_->first())
{
return int(pixmap->height() * this->scale_); return int(pixmap->height() * this->scale_);
else }
// No frames loaded, use our default magic height 16
return 16; return 16;
} }
void Image::actuallyLoad() void Image::actuallyLoad()
{ {
auto weak = weakOf(this);
NetworkRequest(this->url().string) NetworkRequest(this->url().string)
.concurrent() .concurrent()
.cache() .cache()
.onSuccess([weak = weakOf(this)](auto result) -> Outcome { .onSuccess([weak](auto result) -> Outcome {
auto shared = weak.lock(); auto shared = weak.lock();
if (!shared) if (!shared)
{
return Failure; return Failure;
}
auto data = result.getData(); auto data = result.getData();
@ -540,20 +553,23 @@ void Image::actuallyLoad()
auto parsed = detail::readFrames(reader, shared->url()); auto parsed = detail::readFrames(reader, shared->url());
postToThread(makeConvertCallback(parsed, [weak](auto &&frames) { postToThread(makeConvertCallback(
parsed, [weak = std::weak_ptr<Image>(shared)](auto &&frames) {
if (auto shared = weak.lock()) if (auto shared = weak.lock())
{ {
shared->frames_ = shared->frames_ = std::make_unique<detail::Frames>(
std::make_unique<detail::Frames>(std::move(frames)); std::forward<decltype(frames)>(frames));
} }
})); }));
return Success; return Success;
}) })
.onError([weak = weakOf(this)](auto /*result*/) { .onError([weak](auto /*result*/) {
auto shared = weak.lock(); auto shared = weak.lock();
if (!shared) if (!shared)
{
return false; return false;
}
// fourtf: is this the right thing to do? // fourtf: is this the right thing to do?
shared->empty_ = true; shared->empty_ = true;

View file

@ -3,7 +3,6 @@
#include "common/Aliases.hpp" #include "common/Aliases.hpp"
#include "common/Common.hpp" #include "common/Common.hpp"
#include <boost/optional.hpp>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <pajlada/signals/signal.hpp> #include <pajlada/signals/signal.hpp>
#include <QPixmap> #include <QPixmap>
@ -17,6 +16,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <optional>
namespace chatterino { namespace chatterino {
namespace detail { namespace detail {
@ -42,8 +42,8 @@ namespace detail {
bool empty() const; bool empty() const;
bool animated() const; bool animated() const;
void advance(); void advance();
boost::optional<QPixmap> current() const; std::optional<QPixmap> current() const;
boost::optional<QPixmap> first() const; std::optional<QPixmap> first() const;
private: private:
int64_t memoryUsage() const; int64_t memoryUsage() const;
@ -80,7 +80,7 @@ public:
const Url &url() const; const Url &url() const;
bool loaded() const; bool loaded() const;
// either returns the current pixmap, or triggers loading it (lazy loading) // either returns the current pixmap, or triggers loading it (lazy loading)
boost::optional<QPixmap> pixmapOrLoad() const; std::optional<QPixmap> pixmapOrLoad() const;
void load() const; void load() const;
qreal scale() const; qreal scale() const;
bool isEmpty() const; bool isEmpty() const;