Use large image previews more.

This commit is contained in:
John Preston 2022-07-25 13:56:24 +03:00
parent 2d6008f6ca
commit 2a4d269eca
3 changed files with 47 additions and 0 deletions

View file

@ -382,6 +382,11 @@ std::unique_ptr<Ui::Text::CustomEmoji> CustomEmojiManager::create(
factory(), factory(),
prepareNonExactPreview(documentId, tag) prepareNonExactPreview(documentId, tag)
}, std::move(repaint))).first; }, std::move(repaint))).first;
} else if (!i->second->hasImagePreview()) {
auto preview = prepareNonExactPreview(documentId, tag);
if (preview.isImage()) {
i->second->updatePreview(std::move(preview));
}
} }
return std::make_unique<Ui::CustomEmoji::Object>( return std::make_unique<Ui::CustomEmoji::Object>(
i->second.get(), i->second.get(),

View file

@ -536,10 +536,26 @@ void Loading::paint(QPainter &p, int x, int y, const QColor &preview) {
_preview.paint(p, x, y, preview); _preview.paint(p, x, y, preview);
} }
bool Loading::hasImagePreview() const {
return _preview.isImage();
}
Preview Loading::imagePreview() const { Preview Loading::imagePreview() const {
return _preview.isImage() ? _preview : Preview(); return _preview.isImage() ? _preview : Preview();
} }
void Loading::updatePreview(Preview preview) {
if (!_preview.isImage() && preview.isImage()) {
_preview = std::move(preview);
} else if (!_preview) {
if (auto loaderPreview = _loader->preview()) {
_preview = std::move(loaderPreview);
} else if (preview) {
_preview = std::move(preview);
}
}
}
void Loading::cancel() { void Loading::cancel() {
_loader->cancel(); _loader->cancel();
invalidate_weak_ptrs(this); invalidate_weak_ptrs(this);
@ -606,6 +622,17 @@ void Instance::paint(
} }
} }
bool Instance::hasImagePreview() const {
if (const auto loading = std::get_if<Loading>(&_state)) {
return loading->hasImagePreview();
} else if (const auto caching = std::get_if<Caching>(&_state)) {
return caching->preview.isImage();
} else if (const auto cached = std::get_if<Cached>(&_state)) {
return true;
}
return false;
}
Preview Instance::imagePreview() const { Preview Instance::imagePreview() const {
if (const auto loading = std::get_if<Loading>(&_state)) { if (const auto loading = std::get_if<Loading>(&_state)) {
return loading->imagePreview(); return loading->imagePreview();
@ -617,6 +644,17 @@ Preview Instance::imagePreview() const {
return {}; return {};
} }
void Instance::updatePreview(Preview preview) {
if (const auto loading = std::get_if<Loading>(&_state)) {
loading->updatePreview(std::move(preview));
} else if (const auto caching = std::get_if<Caching>(&_state)) {
if ((!caching->preview.isImage() && preview.isImage())
|| (!caching->preview && preview)) {
caching->preview = std::move(preview);
}
}
}
void Instance::repaint() { void Instance::repaint() {
for (const auto &object : _usage) { for (const auto &object : _usage) {
object->repaint(); object->repaint();

View file

@ -200,7 +200,9 @@ public:
void load(Fn<void(Loader::LoadResult)> done); void load(Fn<void(Loader::LoadResult)> done);
[[nodiscard]] bool loading() const; [[nodiscard]] bool loading() const;
void paint(QPainter &p, int x, int y, const QColor &preview); void paint(QPainter &p, int x, int y, const QColor &preview);
[[nodiscard]] bool hasImagePreview() const;
[[nodiscard]] Preview imagePreview() const; [[nodiscard]] Preview imagePreview() const;
void updatePreview(Preview preview);
void cancel(); void cancel();
private: private:
@ -231,7 +233,9 @@ public:
crl::time now, crl::time now,
const QColor &preview, const QColor &preview,
bool paused); bool paused);
[[nodiscard]] bool hasImagePreview() const;
[[nodiscard]] Preview imagePreview() const; [[nodiscard]] Preview imagePreview() const;
void updatePreview(Preview preview);
void incrementUsage(not_null<Object*> object); void incrementUsage(not_null<Object*> object);
void decrementUsage(not_null<Object*> object); void decrementUsage(not_null<Object*> object);