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