1 /*
2 * Copyright (c) 2021 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 "frameworks/bridge/common/dom/dom_image_animator.h"
17
18 #include "frameworks/bridge/common/utils/utils.h"
19
20 namespace OHOS::Ace::Framework {
21 namespace {
22
23 constexpr int32_t MS_TO_S = 1000;
24 const char* STATE_PLAYING = "playing";
25 const char* STATE_PAUSED = "paused";
26 const char* STATE_STOPPED = "stopped";
27 const char* ITERATIONS_INFINITE = "infinite";
28
29 } // namespace
30
DOMImageAnimator(NodeId nodeId,const std::string & nodeName)31 DOMImageAnimator::DOMImageAnimator(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
32 {
33 imageAnimator_ = AceType::MakeRefPtr<ImageAnimatorComponent>(nodeName);
34 }
35
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)36 bool DOMImageAnimator::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
37 {
38 if (!imageAnimator_) {
39 return false;
40 }
41 if (attr.first == DOM_ITERATION) {
42 if (attr.second == ITERATIONS_INFINITE) {
43 imageAnimator_->SetIteration(ANIMATION_REPEAT_INFINITE);
44 } else {
45 imageAnimator_->SetIteration(StringToInt(attr.second));
46 }
47 return true;
48 }
49 if (attr.first == DOM_PREDECODE) {
50 imageAnimator_->SetPreDecode(StringToInt(attr.second));
51 return true;
52 }
53 if (attr.first == DOM_DURATION) {
54 auto val = attr.second;
55 if (val.find("ms") != std::string::npos) {
56 imageAnimator_->SetDuration(StringToInt(val));
57 } else if (val.find('s') != std::string::npos) {
58 imageAnimator_->SetDuration(StringToInt(val) * MS_TO_S);
59 } else {
60 imageAnimator_->SetDuration(StringToInt(val));
61 }
62 return true;
63 }
64 if (attr.first == DOM_FIXEDSIZE) {
65 imageAnimator_->SetIsFixedSize(StringToBool(attr.second));
66 return true;
67 }
68 if (attr.first == DOM_REVERSE) {
69 imageAnimator_->SetIsReverse(StringToBool(attr.second));
70 return true;
71 }
72 if (attr.first == DOM_FILLMODE) {
73 imageAnimator_->SetFillMode(StringToFillMode(attr.second));
74 return true;
75 }
76 return false;
77 }
78
AddSpecializedEvent(int32_t pageId,const std::string & event)79 bool DOMImageAnimator::AddSpecializedEvent(int32_t pageId, const std::string& event)
80 {
81 if (!imageAnimator_) {
82 return false;
83 }
84 const auto& controller = imageAnimator_->GetImageAnimatorController();
85 if (!controller) {
86 return false;
87 }
88 if (event == DOM_IMAGE_ANIMATOR_START) {
89 auto startEvent = EventMarker(GetNodeIdForEvent(), event, pageId);
90 controller->SetStartEvent(startEvent);
91 return true;
92 } else if (event == DOM_IMAGE_ANIMATOR_STOP) {
93 auto stopEvent = EventMarker(GetNodeIdForEvent(), event, pageId);
94 controller->SetStopEvent(stopEvent);
95 return true;
96 } else if (event == DOM_IMAGE_ANIMATOR_PAUSE) {
97 auto pauseEvent = EventMarker(GetNodeIdForEvent(), event, pageId);
98 controller->SetPauseEvent(pauseEvent);
99 return true;
100 } else if (event == DOM_IMAGE_ANIMATOR_RESUME) {
101 auto resumeEvent = EventMarker(GetNodeIdForEvent(), event, pageId);
102 controller->SetResumeEvent(resumeEvent);
103 return true;
104 } else {
105 return false;
106 }
107 }
108
CallSpecializedMethod(const std::string & method,const std::string & args)109 void DOMImageAnimator::CallSpecializedMethod(const std::string& method, const std::string& args)
110 {
111 if (!imageAnimator_) {
112 return;
113 }
114 const auto& controller = imageAnimator_->GetImageAnimatorController();
115 if (!controller) {
116 return;
117 }
118 controller->CallAnimationFunc(method);
119 }
120
GetState() const121 const char* DOMImageAnimator::GetState() const
122 {
123 if (!imageAnimator_) {
124 return STATE_STOPPED;
125 }
126 const auto& controller = imageAnimator_->GetImageAnimatorController();
127 if (!controller) {
128 return STATE_STOPPED;
129 }
130 auto currentStatus = controller->CallAnimatorGetStatusFunc();
131 if (currentStatus == Animator::Status::PAUSED) {
132 return STATE_PAUSED;
133 } else if (currentStatus == Animator::Status::RUNNING) {
134 return STATE_PLAYING;
135 } else {
136 // IDLE and STOP
137 return STATE_STOPPED;
138 }
139 }
140
PrepareSpecializedComponent()141 void DOMImageAnimator::PrepareSpecializedComponent()
142 {
143 if (!imageAnimator_) {
144 return;
145 }
146 if (declaration_) {
147 auto& borderStyle = static_cast<CommonBorderStyle&>(declaration_->GetStyle(StyleTag::COMMON_BORDER_STYLE));
148 if (borderStyle.IsValid() && borderStyle.border.HasRadius()) {
149 imageAnimator_->SetBorder(borderStyle.border);
150 }
151 }
152 imageAnimator_->SetImageProperties(imagesAttr_);
153 }
154
155 } // namespace OHOS::Ace::Framework
156