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 "bridge/cj_frontend/interfaces/cj_ffi/cj_swiper_ffi.h"
17
18 #include <cinttypes>
19
20 #include "bridge/cj_frontend/cppview/swiper_controller.h"
21 #include "bridge/cj_frontend/cppview/view_abstract.h"
22 #include "cj_lambda.h"
23 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
24 #include "bridge/common/utils/utils.h"
25 #include "core/components_ng/base/view_abstract_model_ng.h"
26 #include "core/components_ng/pattern/swiper/swiper_model.h"
27 #include "core/gestures/click_recognizer.h"
28
29 using namespace OHOS::Ace;
30 using namespace OHOS::FFI;
31 using namespace OHOS::Ace::Framework;
32
33 namespace {
34 const std::vector<SwiperDisplayMode> SWIPER_DISPLAY_MODES = { SwiperDisplayMode::STRETCH,
35 SwiperDisplayMode::AUTO_LINEAR };
36 const std::vector<EdgeEffect> EDGE_EFFECTS = { EdgeEffect::SPRING, EdgeEffect::FADE, EdgeEffect::NONE };
37 } // namespace
38
39 extern "C" {
FfiOHOSAceFrameworkSwiperCreate(int64_t controllerId)40 void FfiOHOSAceFrameworkSwiperCreate(int64_t controllerId)
41 {
42 auto nativeSwiperController = FFIData::GetData<NativeSwiperController>(controllerId);
43 if (nativeSwiperController == nullptr) {
44 LOGE("Swiper create error, Cannot get NativeSwiperController by id: %{public}" PRId64, controllerId);
45 return;
46 }
47 auto controller = SwiperModel::GetInstance()->Create();
48 nativeSwiperController->SetController(controller);
49 }
50
FfiOHOSAceFrameworkSwiperSetAutoplay(bool autoPlay)51 void FfiOHOSAceFrameworkSwiperSetAutoplay(bool autoPlay)
52 {
53 SwiperModel::GetInstance()->SetAutoPlay(autoPlay);
54 }
55
FfiOHOSAceFrameworkSwiperSetDigital(bool digitalIndicator)56 void FfiOHOSAceFrameworkSwiperSetDigital(bool digitalIndicator)
57 {
58 // deleted in the future
59 }
60
FfiOHOSAceFrameworkSwiperSetDuration(uint32_t duration)61 void FfiOHOSAceFrameworkSwiperSetDuration(uint32_t duration)
62 {
63 SwiperModel::GetInstance()->SetDuration(duration);
64 }
65
FfiOHOSAceFrameworkSwiperSetIndex(uint32_t index)66 void FfiOHOSAceFrameworkSwiperSetIndex(uint32_t index)
67 {
68 SwiperModel::GetInstance()->SetIndex(index);
69 }
70
FfiOHOSAceFrameworkSwiperSetInterval(uint32_t interval)71 void FfiOHOSAceFrameworkSwiperSetInterval(uint32_t interval)
72 {
73 SwiperModel::GetInstance()->SetAutoPlayInterval(interval);
74 }
75
FfiOHOSAceFrameworkSwiperSetLoop(bool loop)76 void FfiOHOSAceFrameworkSwiperSetLoop(bool loop)
77 {
78 SwiperModel::GetInstance()->SetLoop(loop);
79 }
80
FfiOHOSAceFrameworkSwiperSetVertical(bool isVertical)81 void FfiOHOSAceFrameworkSwiperSetVertical(bool isVertical)
82 {
83 SwiperModel::GetInstance()->SetDirection(isVertical ? Axis::VERTICAL : Axis::HORIZONTAL);
84 }
85
FfiOHOSAceFrameworkSwiperSetIndicator(bool showIndicator)86 void FfiOHOSAceFrameworkSwiperSetIndicator(bool showIndicator)
87 {
88 SwiperModel::GetInstance()->SetShowIndicator(showIndicator);
89 }
90
FfiOHOSAceFrameworkSwiperSetWidth(double width,int32_t unit)91 void FfiOHOSAceFrameworkSwiperSetWidth(double width, int32_t unit)
92 {
93 Dimension value(width, static_cast<DimensionUnit>(unit));
94 ViewAbstractModel::GetInstance()->SetWidth(value);
95 SwiperModel::GetInstance()->SetMainSwiperSizeWidth();
96 }
97
FfiOHOSAceFrameworkSwiperSetHeight(double height,int32_t unit)98 void FfiOHOSAceFrameworkSwiperSetHeight(double height, int32_t unit)
99 {
100 Dimension value(height, static_cast<DimensionUnit>(unit));
101 ViewAbstractModel::GetInstance()->SetHeight(value);
102 SwiperModel::GetInstance()->SetMainSwiperSizeHeight();
103 }
104
FfiOHOSAceFrameworkSwiperSetSize(double width,int32_t widthUnit,double height,int32_t heightUnit)105 void FfiOHOSAceFrameworkSwiperSetSize(double width, int32_t widthUnit, double height, int32_t heightUnit)
106 {
107 Dimension widthValue(width, static_cast<DimensionUnit>(widthUnit));
108 Dimension heightValue(height, static_cast<DimensionUnit>(heightUnit));
109 ViewAbstractModel::GetInstance()->SetWidth(widthValue);
110 ViewAbstractModel::GetInstance()->SetHeight(heightValue);
111 }
112
FfiOHOSAceFrameworkSwiperSetIndicatorStyle(CJIndicatorStyle value)113 void FfiOHOSAceFrameworkSwiperSetIndicatorStyle(CJIndicatorStyle value)
114 {
115 Dimension leftValue(value.left, static_cast<DimensionUnit>(value.leftUnit));
116 Dimension topValue(value.top, static_cast<DimensionUnit>(value.topUnit));
117 Dimension rightValue(value.right, static_cast<DimensionUnit>(value.rightUnit));
118 Dimension bottomValue(value.bottom, static_cast<DimensionUnit>(value.bottomUnit));
119 Dimension sizeValue(value.size, static_cast<DimensionUnit>(value.sizeUnit));
120 SwiperParameters swiperParameters {
121 .dimLeft = leftValue,
122 .dimTop = topValue,
123 .dimRight = rightValue,
124 .dimBottom = bottomValue,
125 .maskValue = value.mask,
126 .colorVal = Color(value.color),
127 .selectedColorVal = Color(value.selectedColor) };
128 SwiperModel::GetInstance()->SetIndicatorStyle(swiperParameters);
129 }
130
FfiOHOSAceFrameworkSwiperSetItemSpace(double itemSpace,int32_t unit)131 void FfiOHOSAceFrameworkSwiperSetItemSpace(double itemSpace, int32_t unit)
132 {
133 Dimension value(itemSpace, static_cast<DimensionUnit>(unit));
134 if (LessNotEqual(value.Value(), 0.0)) {
135 value.SetValue(0.0);
136 }
137 SwiperModel::GetInstance()->SetItemSpace(value);
138 }
139
FfiOHOSAceFrameworkSwiperSetDisplayMode(int32_t modeValue)140 void FfiOHOSAceFrameworkSwiperSetDisplayMode(int32_t modeValue)
141 {
142 if (!Utils::CheckParamsValid(modeValue, SWIPER_DISPLAY_MODES.size())) {
143 LOGE("invalid value for swiper display mode");
144 return;
145 }
146 SwiperModel::GetInstance()->SetDisplayMode(SWIPER_DISPLAY_MODES[modeValue]);
147 }
148
FfiOHOSAceFrameworkSwiperSetEffectMode(int32_t modeValue)149 void FfiOHOSAceFrameworkSwiperSetEffectMode(int32_t modeValue)
150 {
151 if (!Utils::CheckParamsValid(modeValue, EDGE_EFFECTS.size())) {
152 LOGE("invalid value for effect mode");
153 return;
154 }
155 SwiperModel::GetInstance()->SetEdgeEffect(EDGE_EFFECTS[modeValue]);
156 }
157
FfiOHOSAceFrameworkSwiperSetDisplayCount(int32_t count)158 void FfiOHOSAceFrameworkSwiperSetDisplayCount(int32_t count)
159 {
160 SwiperModel::GetInstance()->SetDisplayCount(count);
161 }
162
FfiOHOSAceFrameworkSwiperSetCachedCount(int32_t cachedCount)163 void FfiOHOSAceFrameworkSwiperSetCachedCount(int32_t cachedCount)
164 {
165 SwiperModel::GetInstance()->SetCachedCount(cachedCount);
166 }
167
FfiOHOSAceFrameworkSwiperSetEnabled(bool value)168 void FfiOHOSAceFrameworkSwiperSetEnabled(bool value)
169 {
170 ViewAbstract::CjEnabled(value);
171 SwiperModel::GetInstance()->SetEnabled(value);
172 }
173
FfiOHOSAceFrameworkSwiperSetDisableSwipe(bool disable)174 void FfiOHOSAceFrameworkSwiperSetDisableSwipe(bool disable)
175 {
176 SwiperModel::GetInstance()->SetDisableSwipe(disable);
177 }
178
FfiOHOSAceFrameworkSwiperSetCurve(const char * curveStr)179 void FfiOHOSAceFrameworkSwiperSetCurve(const char* curveStr)
180 {
181 RefPtr<Curve> curve = CreateCurve(curveStr);
182 SwiperModel::GetInstance()->SetCurve(curve);
183 }
184
FfiOHOSAceFrameworkSwiperSetOnChange(void (* callback)(int32_t index))185 void FfiOHOSAceFrameworkSwiperSetOnChange(void (*callback)(int32_t index))
186 {
187 auto lambda = CJLambda::Create(callback);
188 auto onChange = [lambda](const BaseEventInfo* info) {
189 const auto* swiperInfo = TypeInfoHelper::DynamicCast<SwiperChangeEvent>(info);
190 if (!swiperInfo) {
191 return;
192 }
193 lambda(swiperInfo->GetIndex());
194 };
195 SwiperModel::GetInstance()->SetOnChange(std::move(onChange));
196 }
197
FfiOHOSAceFrameworkSwiperControllerCtor()198 int64_t FfiOHOSAceFrameworkSwiperControllerCtor()
199 {
200 auto ret_ = FFIData::Create<NativeSwiperController>();
201 if (ret_ == nullptr) {
202 return FFI_ERROR_CODE;
203 }
204 return ret_->GetID();
205 }
206
FfiOHOSAceFrameworkSwiperControllerShowNext(int64_t selfId)207 void FfiOHOSAceFrameworkSwiperControllerShowNext(int64_t selfId)
208 {
209 auto self_ = FFIData::GetData<NativeSwiperController>(selfId);
210 if (self_ != nullptr) {
211 self_->ShowNext();
212 }
213 }
214
FfiOHOSAceFrameworkSwiperControllerShowPrevious(int64_t selfId)215 void FfiOHOSAceFrameworkSwiperControllerShowPrevious(int64_t selfId)
216 {
217 auto self_ = FFIData::GetData<NativeSwiperController>(selfId);
218 if (self_ != nullptr) {
219 self_->ShowPrevious();
220 }
221 }
222
FfiOHOSAceFrameworkSwiperControllerFinishAnimation(int64_t selfId)223 void FfiOHOSAceFrameworkSwiperControllerFinishAnimation(int64_t selfId)
224 {
225 auto self_ = FFIData::GetData<NativeSwiperController>(selfId);
226 if (self_ != nullptr) {
227 self_->FinishAnimation();
228 }
229 }
230
FfiOHOSAceFrameworkSwiperControllerFinishAnimationWithCallback(int64_t selfId,void (* callback)())231 void FfiOHOSAceFrameworkSwiperControllerFinishAnimationWithCallback(int64_t selfId, void (*callback)())
232 {
233 auto self_ = FFIData::GetData<NativeSwiperController>(selfId);
234 if (self_ != nullptr) {
235 self_->FinishAnimationWithCallback(CJLambda::Create(callback));
236 }
237 }
238 }
239