diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index b42223012..39a30545f 100644 --- a/src/messages/Image.cpp +++ b/src/messages/Image.cpp @@ -41,6 +41,14 @@ namespace detail { getApp()->emotes->gifTimer.signal.connect( [this] { this->advance(); }); } + + auto totalLength = std::accumulate( + this->items_.begin(), this->items_.end(), 0UL, + [](auto init, auto &&frame) { return init + frame.duration; }); + + this->durationOffset_ = std::min( + int(getApp()->emotes->gifTimer.position() % totalLength), 60000); + this->processOffset(); } Frames::~Frames() @@ -58,17 +66,16 @@ namespace detail { void Frames::advance() { - this->durationOffset_ += GIF_FRAME_LENGTH; + this->durationOffset_ += gifFrameLength; + this->processOffset(); + } + void Frames::processOffset() + { while (true) { this->index_ %= this->items_.size(); - // TODO: Figure out what this was supposed to achieve - // if (this->index_ >= this->items_.size()) { - // this->index_ = this->index_; - // } - if (this->durationOffset_ > this->items_[this->index_].duration) { this->durationOffset_ -= this->items_[this->index_].duration; diff --git a/src/messages/Image.hpp b/src/messages/Image.hpp index f114d1f0f..193ba5787 100644 --- a/src/messages/Image.hpp +++ b/src/messages/Image.hpp @@ -35,6 +35,7 @@ namespace detail { boost::optional first() const; private: + void processOffset(); QVector> items_; int index_{0}; int durationOffset_{0}; diff --git a/src/singletons/Emotes.hpp b/src/singletons/Emotes.hpp index fc475d752..be7fdc480 100644 --- a/src/singletons/Emotes.hpp +++ b/src/singletons/Emotes.hpp @@ -2,8 +2,6 @@ #include "common/Singleton.hpp" -#define GIF_FRAME_LENGTH 33 - #include "providers/bttv/BttvEmotes.hpp" #include "providers/emoji/Emojis.hpp" #include "providers/ffz/FfzEmotes.hpp" diff --git a/src/singletons/helper/GifTimer.cpp b/src/singletons/helper/GifTimer.cpp index 28e1b77ed..2edf7bacd 100644 --- a/src/singletons/helper/GifTimer.cpp +++ b/src/singletons/helper/GifTimer.cpp @@ -22,6 +22,7 @@ void GIFTimer::initialize() qApp->activeWindow() == nullptr) return; + this->position_ += gifFrameLength; this->signal.invoke(); getApp()->windows->repaintGifEmotes(); }); diff --git a/src/singletons/helper/GifTimer.hpp b/src/singletons/helper/GifTimer.hpp index b44fcfb7f..da47149b8 100644 --- a/src/singletons/helper/GifTimer.hpp +++ b/src/singletons/helper/GifTimer.hpp @@ -5,15 +5,22 @@ namespace chatterino { +constexpr long unsigned gifFrameLength = 33; + class GIFTimer { public: void initialize(); pajlada::Signals::NoArgSignal signal; + long unsigned position() + { + return this->position_; + } private: QTimer timer; + long unsigned position_{}; }; } // namespace chatterino