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