Skip to content

Commit

Permalink
Allow .tgs Lottie::Icon-s from resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Apr 1, 2022
1 parent e052821 commit 2ae9d29
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lottie/lottie_icon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ namespace {
#endif
}

[[nodiscard]] QByteArray ReadIconContent(
const QString &name,
const QByteArray &json,
const QString &path) {
return !json.isEmpty()
? json
: !path.isEmpty()
? ReadContent(json, path)
: Images::UnpackGzip(
ReadContent({}, u":/animations/"_q + name + u".tgs"_q));
}

} // namespace

struct Icon::Frame {
Expand All @@ -71,6 +83,7 @@ class Icon::Inner final : public std::enable_shared_from_this<Inner> {
Inner(int frameIndex, base::weak_ptr<Icon> weak);

void prepareFromAsync(
const QString &name,
const QString &path,
const QByteArray &json,
QSize sizeOverride,
Expand Down Expand Up @@ -119,6 +132,7 @@ Icon::Inner::Inner(int frameIndex, base::weak_ptr<Icon> weak)
}

void Icon::Inner::prepareFromAsync(
const QString &name,
const QString &path,
const QByteArray &json,
QSize sizeOverride,
Expand All @@ -127,7 +141,9 @@ void Icon::Inner::prepareFromAsync(
if (!_weak) {
return;
}
auto rlottie = CreateFromContent(ReadContent(json, path), color);
auto rlottie = CreateFromContent(
ReadIconContent(name, json, path),
color);
if (!rlottie || !_weak) {
return;
}
Expand Down Expand Up @@ -275,12 +291,13 @@ Icon::Icon(IconDescriptor &&descriptor)
, _animationFrameTo(descriptor.frame) {
crl::async([
inner = _inner,
name = descriptor.name,
path = descriptor.path,
bytes = descriptor.json,
sizeOverride = descriptor.sizeOverride,
color = (_color ? (*_color)->c : Qt::white)
] {
inner->prepareFromAsync(path, bytes, sizeOverride, color);
inner->prepareFromAsync(name, path, bytes, sizeOverride, color);
});
}

Expand Down Expand Up @@ -465,4 +482,8 @@ bool Icon::animating() const {
return _animation.animating();
}

std::unique_ptr<Icon> MakeIcon(IconDescriptor &&descriptor) {
return std::make_unique<Icon>(std::move(descriptor));
}

} // namespace Lottie
3 changes: 3 additions & 0 deletions lottie/lottie_icon.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Lottie {

struct IconDescriptor {
QString name;
QString path;
QByteArray json;
const style::color *color = nullptr;
Expand Down Expand Up @@ -83,4 +84,6 @@ class Icon final : public base::has_weak_ptr {

};

[[nodiscard]] std::unique_ptr<Icon> MakeIcon(IconDescriptor &&descriptor);

} // namespace Lottie

0 comments on commit 2ae9d29

Please sign in to comment.