diff --git a/ui/text/custom_emoji_instance.cpp b/ui/text/custom_emoji_instance.cpp index 928691a..1a5d004 100644 --- a/ui/text/custom_emoji_instance.cpp +++ b/ui/text/custom_emoji_instance.cpp @@ -220,6 +220,10 @@ int Cache::frames() const { return _frames; } +bool Cache::readyInDefaultState() const { + return (_frames > 0) && !_frame; +} + Cache::Frame Cache::frame(int index) const { Expects(index < _frames); @@ -401,6 +405,10 @@ PaintFrameResult Cached::paint(QPainter &p, const Context &context) { return _cache.paintCurrentFrame(p, context); } +bool Cached::inDefaultState() const { + return _cache.readyInDefaultState(); +} + Preview Cached::makePreview() const { return _cache.makePreview(); } @@ -534,6 +542,10 @@ Preview Renderer::makePreview() const { return _cache.makePreview(); } +bool Renderer::readyInDefaultState() const { + return _cache.readyInDefaultState(); +} + void Renderer::setRepaintCallback(Fn repaint) { _repaint = std::move(repaint); } @@ -659,6 +671,20 @@ bool Instance::ready() { }); } +bool Instance::readyInDefaultState() { + return v::match(_state, [&](Loading &state) { + if (state.hasImagePreview()) { + return true; + } + load(state); + return false; + }, [](Caching &state) { + return state.renderer->readyInDefaultState(); + }, [](Cached &state) { + return state.inDefaultState(); + }); +} + void Instance::load(Loading &state) { state.load([=](Loader::LoadResult result) { if (auto caching = std::get_if(&result)) { @@ -768,6 +794,14 @@ bool Object::ready() { return _instance->ready(); } +bool Object::readyInDefaultState() { + if (!_using) { + _using = true; + _instance->incrementUsage(this); + } + return _instance->readyInDefaultState(); +} + void Object::repaint() { _repaint(); } diff --git a/ui/text/custom_emoji_instance.h b/ui/text/custom_emoji_instance.h index 6efb6d1..4ba9b31 100644 --- a/ui/text/custom_emoji_instance.h +++ b/ui/text/custom_emoji_instance.h @@ -81,6 +81,7 @@ public: [[nodiscard]] int size() const; [[nodiscard]] int frames() const; + [[nodiscard]] bool readyInDefaultState() const; [[nodiscard]] Frame frame(int index) const; void reserve(int frames); void add(crl::time duration, const QImage &frame); @@ -122,6 +123,7 @@ public: [[nodiscard]] QString entityData() const; [[nodiscard]] Preview makePreview() const; PaintFrameResult paint(QPainter &p, const Context &context); + [[nodiscard]] bool inDefaultState() const; [[nodiscard]] Loading unload(); private: @@ -149,6 +151,7 @@ public: [[nodiscard]] bool canMakePreview() const; [[nodiscard]] Preview makePreview() const; + [[nodiscard]] bool readyInDefaultState() const; void setRepaintCallback(Fn repaint); [[nodiscard]] Cache takeCache(); @@ -227,6 +230,7 @@ public: [[nodiscard]] QString entityData() const; void paint(QPainter &p, const Context &context); [[nodiscard]] bool ready(); + [[nodiscard]] bool readyInDefaultState(); [[nodiscard]] bool hasImagePreview() const; [[nodiscard]] Preview imagePreview() const; void updatePreview(Preview preview); @@ -260,6 +264,7 @@ public: void paint(QPainter &p, const Context &context) override; void unload() override; bool ready() override; + bool readyInDefaultState() override; void repaint(); diff --git a/ui/text/text_custom_emoji.cpp b/ui/text/text_custom_emoji.cpp index 0157951..ee2bae9 100644 --- a/ui/text/text_custom_emoji.cpp +++ b/ui/text/text_custom_emoji.cpp @@ -37,6 +37,10 @@ bool ShiftedEmoji::ready() { return _wrapped->ready(); } +bool ShiftedEmoji::readyInDefaultState() { + return _wrapped->readyInDefaultState(); +} + FirstFrameEmoji::FirstFrameEmoji(std::unique_ptr wrapped) : _wrapped(std::move(wrapped)) { } @@ -59,4 +63,8 @@ bool FirstFrameEmoji::ready() { return _wrapped->ready(); } +bool FirstFrameEmoji::readyInDefaultState() { + return _wrapped->readyInDefaultState(); +} + } // namespace Ui::Text diff --git a/ui/text/text_custom_emoji.h b/ui/text/text_custom_emoji.h index 53d134b..f960d40 100644 --- a/ui/text/text_custom_emoji.h +++ b/ui/text/text_custom_emoji.h @@ -39,6 +39,7 @@ public: virtual void paint(QPainter &p, const Context &context) = 0; virtual void unload() = 0; [[nodiscard]] virtual bool ready() = 0; + [[nodiscard]] virtual bool readyInDefaultState() = 0; }; @@ -54,6 +55,7 @@ public: void paint(QPainter &p, const Context &context) override; void unload() override; bool ready() override; + bool readyInDefaultState() override; private: const std::unique_ptr _wrapped; @@ -69,6 +71,7 @@ public: void paint(QPainter &p, const Context &context) override; void unload() override; bool ready() override; + bool readyInDefaultState() override; private: const std::unique_ptr _wrapped;