1 /*
2 * Copyright (c) 2021-2022 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 "core/components/declaration/swiper/swiper_declaration.h"
17
18 #include "core/components/declaration/common/declaration_constants.h"
19 #include "frameworks/bridge/common/dom/dom_type.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21
22 namespace OHOS::Ace {
23 namespace {
24
25 constexpr uint32_t METHOD_SWIPE_TO_ARGS_SIZE = 1;
26 constexpr double INDICATOR_POINT_SCALE = 1.33;
27
28 } // namespace
29
30 using namespace Framework;
31
SwiperDeclaration()32 SwiperDeclaration::SwiperDeclaration()
33 {
34 indicator_ = AceType::MakeRefPtr<SwiperIndicator>();
35 swiperController_ = AceType::MakeRefPtr<SwiperController>();
36 rotationController_ = AceType::MakeRefPtr<RotationController>();
37 }
38
InitializeStyle()39 void SwiperDeclaration::InitializeStyle()
40 {
41 RefPtr<SwiperIndicatorTheme> theme = GetTheme<SwiperIndicatorTheme>();
42 if (theme) {
43 indicator_->InitStyle(theme);
44 }
45 }
InitSpecialized()46 void SwiperDeclaration::InitSpecialized()
47 {
48 AddSpecializedAttribute(DeclarationConstants::DEFAULT_SWIPER_ATTR);
49 AddSpecializedStyle(DeclarationConstants::DEFAULT_SWIPER_STYLE);
50 AddSpecializedEvent(DeclarationConstants::DEFAULT_SWIPER_EVENT);
51 AddSpecializedMethod(DeclarationConstants::DEFAULT_SWIPER_METHOD);
52 AddSpecializedRemoteMessageEvent(DeclarationConstants::DEFAULT_SWIPER_EVENT);
53 }
54
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)55 bool SwiperDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
56 {
57 static const LinearMapNode<void (*)(const std::string&, SwiperDeclaration&)> swiperAttrsOperators[] = {
58 { DOM_SWIPER_ANIMATION_OPACITY,
59 [](const std::string& val, SwiperDeclaration& swiper) { swiper.SetAnimationOpacity(StringToBool(val)); } },
60 { DOM_AUTOPLAY,
61 [](const std::string& val, SwiperDeclaration& swiper) { swiper.SetAutoPlay(StringToBool(val)); } },
62 { DOM_CACHED_SIZE,
63 [](const std::string& val, SwiperDeclaration& swiper) {
64 swiper.SetCachedSize(StringUtils::StringToInt(val));
65 } },
66 { DOM_DIGITAL_INDICATOR,
67 [](const std::string& val, SwiperDeclaration& swiper) { swiper.SetDigitalIndicator(StringToBool(val)); } },
68 { DOM_DISPLAY_MODE,
69 [](const std::string& val, SwiperDeclaration& swiper) {
70 if (val == DOM_DISPLAY_MODE_AUTO_LINEAR) {
71 swiper.SetDisplayMode(SwiperDisplayMode::AUTO_LINEAR);
72 } else {
73 swiper.SetDisplayMode(SwiperDisplayMode::STRETCH);
74 }
75 } },
76 { DOM_DURATION,
77 [](const std::string& val, SwiperDeclaration& swiper) { swiper.SetDuration(StringToDouble(val)); } },
78 { DOM_INDEX,
79 [](const std::string& val, SwiperDeclaration& swiper) { swiper.SetIndex(StringUtils::StringToInt(val)); } },
80 { DOM_INDICATOR,
81 [](const std::string& val, SwiperDeclaration& swiper) { swiper.showIndicator_ = StringToBool(val); } },
82 { DOM_INDICATOR_DISABLED,
83 [](const std::string& val, SwiperDeclaration& swiper) {
84 swiper.indicator_->SetIndicatorDisabled(StringToBool(val));
85 } },
86 { DOM_INDICATOR_MASK,
87 [](const std::string& val, SwiperDeclaration& swiper) {
88 swiper.indicator_->SetIndicatorMask(StringToBool(val));
89 } },
90 { DOM_INTERVAL, [](const std::string& val,
91 SwiperDeclaration& swiper) { swiper.SetAutoPlayInterval(StringToDouble(val)); } },
92 { DOM_LOOP, [](const std::string& val, SwiperDeclaration& swiper) { swiper.SetLoop(StringToBool(val)); } },
93 { DOM_SCROLL_EFFECT, [](const std::string& val, SwiperDeclaration& swiper) {
94 if (val == DOM_SCROLL_EFFECT_SPRING) {
95 swiper.SetEdgeEffect(EdgeEffect::SPRING);
96 } else if (val == DOM_SCROLL_EFFECT_FADE) {
97 swiper.SetEdgeEffect(EdgeEffect::FADE);
98 } else {
99 swiper.SetEdgeEffect(EdgeEffect::NONE);
100 }
101 } },
102 { DOM_VERTICAL,
103 [](const std::string& val, SwiperDeclaration& swiper) {
104 swiper.SetAxis(StringToBool(val) ? Axis::VERTICAL : Axis::HORIZONTAL);
105 } },
106 };
107 auto operatorIter =
108 BinarySearchFindIndex(swiperAttrsOperators, ArraySize(swiperAttrsOperators), attr.first.c_str());
109 if (operatorIter != -1) {
110 swiperAttrsOperators[operatorIter].value(attr.second, *this);
111 return true;
112 } else {
113 return false;
114 }
115 }
116
SetSpecializedStyle(const std::pair<std::string,std::string> & style)117 bool SwiperDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
118 {
119 // static linear map must be sorted buy key.
120 static const LinearMapNode<void (*)(const std::string&, SwiperDeclaration&)> swiperStylesOperators[] = {
121 { DOM_ANIMATION_CURVE,
122 [](const std::string& val, SwiperDeclaration& swiper) {
123 swiper.SetAnimationCurve(ConvertStrToAnimationCurve(val));
124 } },
125 { DOM_FADE_COLOR,
126 [](const std::string& val, SwiperDeclaration& swiper) {
127 swiper.SetFadeColor(swiper.ParseColor(val));
128 } },
129 { DOM_INDICATOR_BOTTOM,
130 [](const std::string& val, SwiperDeclaration& swiper) {
131 swiper.indicator_->SetBottom(swiper.ParseDimension(val));
132 } },
133 { DOM_INDICATOR_COLOR,
134 [](const std::string& val, SwiperDeclaration& swiper) {
135 swiper.indicator_->SetColor(swiper.ParseColor(val));
136 } },
137 { DOM_INDICATOR_LEFT,
138 [](const std::string& val, SwiperDeclaration& swiper) {
139 swiper.indicator_->SetLeft(swiper.ParseDimension(val));
140 } },
141 { DOM_INDICATOR_RIGHT,
142 [](const std::string& val, SwiperDeclaration& swiper) {
143 swiper.indicator_->SetRight(swiper.ParseDimension(val));
144 } },
145 { DOM_INDICATOR_SELECTEDCOLOR,
146 [](const std::string& val, SwiperDeclaration& swiper) {
147 swiper.indicator_->SetSelectedColor(swiper.ParseColor(val));
148 } },
149 { DOM_INDICATOR_SIZE,
150 [](const std::string& val, SwiperDeclaration& swiper) {
151 Dimension indicatorSize = swiper.ParseDimension(val);
152 swiper.indicator_->SetSize(indicatorSize);
153 swiper.indicator_->SetSelectedSize(indicatorSize);
154 swiper.indicator_->SetPressSize(indicatorSize * INDICATOR_POINT_SCALE);
155 swiper.indicator_->SetHoverSize(indicatorSize * INDICATOR_POINT_SCALE * INDICATOR_POINT_SCALE);
156 } },
157 { DOM_INDICATOR_TOP,
158 [](const std::string& val, SwiperDeclaration& swiper) {
159 swiper.indicator_->SetTop(swiper.ParseDimension(val));
160 } },
161 { DOM_NEXT_MARGIN,
162 [](const std::string& val, SwiperDeclaration& swiper) {
163 swiper.SetNextMargin(swiper.ParseDimension(val));
164 } },
165 { DOM_PREVIOUS_MARGIN,
166 [](const std::string& val, SwiperDeclaration& swiper) {
167 swiper.SetPreviousMargin(swiper.ParseDimension(val));
168 } },
169 };
170 auto operatorIter =
171 BinarySearchFindIndex(swiperStylesOperators, ArraySize(swiperStylesOperators), style.first.c_str());
172 if (operatorIter != -1) {
173 swiperStylesOperators[operatorIter].value(style.second, *this);
174 return true;
175 } else {
176 return false;
177 }
178 }
179
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)180 bool SwiperDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
181 {
182 if (event == DOM_CHANGE) {
183 SetChangeEventId(EventMarker(eventId, event, pageId));
184 return true;
185 } else if (event == DOM_ROTATION) {
186 SetRotationEventId(EventMarker(eventId, event, pageId));
187 return true;
188 } else if (event == DOM_CLICK) {
189 SetClickEventId(EventMarker(eventId, event, pageId));
190 return true;
191 } else if (event == DOM_CATCH_BUBBLE_CLICK) {
192 EventMarker eventMarker(eventId, event, pageId);
193 eventMarker.SetCatchMode(true);
194 SetClickEventId(eventMarker);
195 return true;
196 } else if (event == DOM_ANIMATION_FINISH) {
197 SetAnimationFinishEventId(EventMarker(eventId, event, pageId));
198 return true;
199 } else {
200 return false;
201 }
202 }
203
CallSpecializedMethod(const std::string & method,const std::string & args)204 void SwiperDeclaration::CallSpecializedMethod(const std::string& method, const std::string& args)
205 {
206 if (method == DOM_METHOD_SWIPE_TO) {
207 auto controller = GetSwiperController();
208 if (!controller) {
209 LOGE("get controller failed");
210 return;
211 }
212
213 std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
214 if (!argsValue || !argsValue->IsArray() || argsValue->GetArraySize() != METHOD_SWIPE_TO_ARGS_SIZE) {
215 LOGE("parse args error");
216 return;
217 }
218
219 std::unique_ptr<JsonValue> indexValue = argsValue->GetArrayItem(0)->GetValue("index");
220 if (!indexValue || !indexValue->IsNumber()) {
221 LOGE("get index failed");
222 return;
223 }
224 int32_t index = indexValue->GetInt();
225 controller->SwipeTo(index);
226 } else if (method == DOM_METHOD_SHOW_PREVIOUS) {
227 auto controller = GetSwiperController();
228 if (!controller) {
229 return;
230 }
231 controller->ShowPrevious();
232 } else if (method == DOM_METHOD_SHOW_NEXT) {
233 auto controller = GetSwiperController();
234 if (!controller) {
235 return;
236 }
237 controller->ShowNext();
238 } else if (method == DOM_ROTATION) {
239 auto controller = GetRotationController();
240 if (controller) {
241 LOGD("SwiperDeclaration rotation focus request");
242 controller->RequestRotation(true);
243 }
244 } else {
245 LOGW("unsupported method: %{public}s", method.c_str());
246 }
247 }
248
249 } // namespace OHOS::Ace
250