[Feature Request] Add "lottie-react-native" compatibility mode

未关闭
#409 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
cpp, react-native
领域
desktop, tooling

调研方向

从 issue 中描述的 LottieGen 兼容性标志请求和 ILottieMediaSource 接口开始。首先确定 FrameToProgress 是否足够,或者是否还需要帧数和时长。完成的标准是:版本化标志使生成的运行时类和实现符合约定的 lottie-react-native 接口,同时不导出 COM 接口。

由索引模型根据 Issue 内容生成。

描述

feature-lottiegen

Overview

The C++/WinRT implementation of lottie-react-native is a wrapper around AnimatedVisualPlayer to support playback of Lottie animations in React Native.

On the native side, it expects consuming applications to implement ILottieSourceProvider and supply the player with a mapping between animation names and implementations of IAnimatedVisualSource (such as those created by LottieGen).

// LottieReactNative
interface ILottieSourceProvider
{
   Microsoft.UI.Xaml.Controls.IAnimatedVisualSource GetSourceFromName(String name);
}

// Consuming app implements mapping
IAnimatedVisualSource MySourceProvider::GetSourceFromName(hstring name) {
    if (name == L"MyAnimation") return winrt::AnimatedVisuals::MyAnimation();
    if (name == L"Foo") return winrt::AnimatedVisuals::Foo();
    ...
}

// Consuming app registers Lottie player with React and supplies it with the name mapper
App() {
    ...
    PackageProviders().Append(winrt::LottieReactNative::ReactPackageProvider(winrt::MySourceProvider());
}

On the JS side, the app can then use Lottie like:

<LottieView source="MyAnimation" />

This works fairly well except that the LottieView API, which is shared with iOS and Android, accepts calls like play(from, to) where from and to are frame numbers, not the normalized time values that AnimatedVisualPlayer expects. As such, our Windows implementation needs a way to convert.

We added an interface to LottieReactNative that we can query in the player. We then tell LottieGen to add the interface to its generated objects. This interface matches the code that LottieGen is already designed to generate.

// LottieReactNative
interface ILottieMediaSource
{
    Double FrameToProgress(Double frame);
};

void LottieView::Play(int64_t from, int64_t to) {
    auto lottieMediaSource = m_player.Source().try_as<ILottieMediaSource>();
    if (lottieMediaSource) {
        lottieMediaSource.Play( lottieMediaSource.FrameToProgress(from), lottieMediaSource.FrameToProgress(to) );
    } else {
        //  TODO: Don't know how to convert frames to time. Maybe fall back to default framerate?
    }
}

// Generated code in app project
// Created with: lottiegen -AdditionalInterface ILottieMediaSource ...
runtimeclass MyAnimation : [default] IAnimatedVisualSource, ILottieMediaSource {
    MyAnimation();
};

class MyAnimation ... {
    ...
    double FrameToProgress(double frame);
    ...
}

Problem

As you can see, there's an expectation here that the code LottieGen creates will adhere to the schema of the interface exposed by lottie-react-native. That's not guaranteed. If a future version of LottieGen unknowingly introduces a breaking change, apps would not be able to consume that new version unless lottie-react-native is also updated with a new interface. Since LottieGen is currently unaware of these needs, such a change would come easily.

Solution

We can codify the requirements of lottie-react-native into LottieGen to prevent breaking changes. Add a flag to the tool, say -LottieReactNative 1.0, that has the following effects:

  1. Each generated runtimeclass is configured to extend the interface LottieReactNative.ILottieMediaSource (the same as if we had specified using -AdditionalInterface
  2. The code in the generated implementations adheres to that interface

Note that the COM interface itself is NOT exported. The expectation is that lottie-react-native will be exporting the interface. LottieGen just needs to create animations that match it.

TODO: Settle on what the interface should be. Is Double FrameToProgress(Double frame); enough? Should we go ahead and add Int64 FrameCount { get; } and Double Duration {get;}?

主要语言
C#
星标
724
派生
79
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CommunityToolkit/Lottie-Windows 的其他 Issue

查看 CommunityToolkit/Lottie-Windows 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。