Allow querying in-default-state from custom emoji.

This commit is contained in:
John Preston 2022-08-29 17:27:32 +04:00
parent 3287bf45c6
commit 6dc6309269
4 changed files with 50 additions and 0 deletions

View file

@ -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<void()> 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<Caching>(&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();
}

View file

@ -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<void()> 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();

View file

@ -37,6 +37,10 @@ bool ShiftedEmoji::ready() {
return _wrapped->ready();
}
bool ShiftedEmoji::readyInDefaultState() {
return _wrapped->readyInDefaultState();
}
FirstFrameEmoji::FirstFrameEmoji(std::unique_ptr<CustomEmoji> wrapped)
: _wrapped(std::move(wrapped)) {
}
@ -59,4 +63,8 @@ bool FirstFrameEmoji::ready() {
return _wrapped->ready();
}
bool FirstFrameEmoji::readyInDefaultState() {
return _wrapped->readyInDefaultState();
}
} // namespace Ui::Text

View file

@ -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<Ui::Text::CustomEmoji> _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<Ui::Text::CustomEmoji> _wrapped;