1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cj_animate_param_ffi.h"
17 #include "base/utils/utils.h"
18 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
19 #include "bridge/common/utils/utils.h"
20 #include "frameworks/base/memory/referenced.h"
21 #include "core/components_ng/base/view_stack_processor.h"
22 #include "core/pipeline_ng/pipeline_context.h"
23 #include "frameworks/bridge/common/utils/engine_helper.h"
24 #include "cj_lambda.h"
25 
26 using namespace OHOS::Ace;
27 using namespace OHOS::FFI;
28 using namespace OHOS::Ace::Framework;
29 
30 namespace {
31 const std::vector<AnimationDirection> ANIMATION_DIRECTION = {
32     AnimationDirection::NORMAL, AnimationDirection::REVERSE,
33     AnimationDirection::ALTERNATE, AnimationDirection::ALTERNATE_REVERSE
34 };
35 const std::vector<FinishCallbackType> FINISH_CALLBACK_TYPE = {
36     FinishCallbackType::REMOVED, FinishCallbackType::LOGICALLY
37 };
38 }
39 
40 namespace OHOS::Ace::Framework {
ParseCjAnimation(NativeAnimateParam animationValue,AnimationOption & result)41 void ParseCjAnimation(NativeAnimateParam animationValue, AnimationOption& result)
42 {
43     if (animationValue.duration.hasValue) {
44         result.SetDuration(animationValue.duration.value);
45     }
46 
47     if (animationValue.delay.hasValue) {
48         result.SetDelay(animationValue.delay.value);
49     }
50 
51     if (animationValue.iterations.hasValue) {
52         result.SetIteration(animationValue.iterations.value);
53     }
54 
55     if (animationValue.tempo.hasValue) {
56         float tempo = animationValue.tempo.value;
57         if (NonPositive(tempo)) {
58             tempo = 1.0f;
59         }
60         result.SetTempo(tempo);
61     }
62 
63     if (animationValue.playMode.hasValue) {
64         result.SetAnimationDirection(ANIMATION_DIRECTION[animationValue.playMode.value]);
65     }
66 
67     if (animationValue.curve.hasValue) {
68         RefPtr<Curve> curve = CreateCurve(animationValue.curve.value);
69         result.SetCurve(curve);
70     }
71 
72     if (animationValue.finishCallbackType.hasValue) {
73         result.SetFinishCallbackType(FINISH_CALLBACK_TYPE[animationValue.finishCallbackType.value]);
74     }
75 
76     if (animationValue.min.hasValue && animationValue.max.hasValue && animationValue.expected.hasValue) {
77         RefPtr<FrameRateRange> range = AceType::MakeRefPtr<FrameRateRange>(
78             animationValue.min.hasValue,
79             animationValue.max.hasValue,
80             animationValue.expected.hasValue
81         );
82         result.SetFrameRateRange(range);
83     }
84 
85     if (animationValue.onFinish.hasValue) {
86         auto id = Container::CurrentIdSafely();
87         WeakPtr<NG::FrameNode> frameNode =
88             AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
89         auto onFinishEvent = [node = frameNode, id, cjCallback = CJLambda::Create(animationValue.onFinish.value)]() {
90             ContainerScope scope(id);
91             auto pipelineContext = PipelineContext::GetCurrentContextSafely();
92             CHECK_NULL_VOID(pipelineContext);
93             pipelineContext->UpdateCurrentActiveNode(node);
94             cjCallback();
95         };
96         result.SetOnFinishEvent(onFinishEvent);
97     }
98 }
99 }
100