1 /* 2 * Copyright (c) 2022-2023 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/declarative_frontend/jsview/models/swiper_model_impl.h" 17 18 #include "bridge/declarative_frontend/jsview/js_interactable_view.h" 19 #include "bridge/declarative_frontend/view_stack_processor.h" 20 #include "core/components/swiper/swiper_component.h" 21 #include "core/components_ng/base/view_abstract_model.h" 22 23 namespace OHOS::Ace::Framework { 24 Create()25RefPtr<SwiperController> SwiperModelImpl::Create() 26 { 27 std::list<RefPtr<OHOS::Ace::Component>> componentChildren; 28 RefPtr<OHOS::Ace::SwiperComponent> component = AceType::MakeRefPtr<OHOS::Ace::SwiperComponent>(componentChildren); 29 30 auto indicator = AceType::MakeRefPtr<OHOS::Ace::SwiperIndicator>(); 31 auto indicatorTheme = JSViewAbstract::GetTheme<SwiperIndicatorTheme>(); 32 if (indicatorTheme) { 33 indicator->InitStyle(indicatorTheme); 34 } 35 36 constexpr int32_t DEFAULT_SWIPER_CACHED_COUNT = 1; 37 component->SetIndicator(indicator); 38 component->SetMainSwiperSize(MainSwiperSize::MIN); 39 component->SetCachedSize(DEFAULT_SWIPER_CACHED_COUNT); 40 component->SetCurve(Curves::LINEAR); 41 ViewStackProcessor::GetInstance()->ClaimElementId(component); 42 ViewStackProcessor::GetInstance()->Push(component); 43 JSInteractableView::SetFocusNode(true); 44 45 return component->GetSwiperController(); 46 } 47 SetDirection(Axis axis)48void SwiperModelImpl::SetDirection(Axis axis) 49 { 50 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 51 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 52 if (swiper) { 53 swiper->SetAxis(axis); 54 } 55 } 56 SetIndex(uint32_t index)57void SwiperModelImpl::SetIndex(uint32_t index) 58 { 59 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 60 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 61 if (swiper) { 62 swiper->SetIndex(index); 63 } 64 } 65 SetDisplayMode(SwiperDisplayMode displayMode)66void SwiperModelImpl::SetDisplayMode(SwiperDisplayMode displayMode) 67 { 68 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 69 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 70 if (!swiper) { 71 return; 72 } 73 74 swiper->SetDisplayMode(displayMode); 75 } 76 SetDisplayCount(int32_t displayCount)77void SwiperModelImpl::SetDisplayCount(int32_t displayCount) 78 { 79 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 80 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 81 if (!swiper) { 82 return; 83 } 84 swiper->SetDisplayCount(displayCount); 85 } 86 SetShowIndicator(bool showIndicator)87void SwiperModelImpl::SetShowIndicator(bool showIndicator) 88 { 89 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 90 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 91 if (swiper) { 92 swiper->SetShowIndicator(showIndicator); 93 if (!showIndicator) { 94 swiper->SetIndicator(nullptr); 95 } 96 } 97 } 98 SetItemSpace(const Dimension & itemSpace)99void SwiperModelImpl::SetItemSpace(const Dimension& itemSpace) 100 { 101 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 102 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 103 if (swiper) { 104 swiper->SetItemSpace(itemSpace); 105 } 106 } 107 SetCachedCount(int32_t cachedCount)108void SwiperModelImpl::SetCachedCount(int32_t cachedCount) 109 { 110 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 111 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 112 if (swiper) { 113 swiper->SetCachedSize(cachedCount); 114 } 115 } 116 SetAutoPlay(bool autoPlay)117void SwiperModelImpl::SetAutoPlay(bool autoPlay) 118 { 119 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 120 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 121 if (swiper) { 122 swiper->SetAutoPlay(autoPlay); 123 } 124 } 125 SetAutoPlayInterval(uint32_t interval)126void SwiperModelImpl::SetAutoPlayInterval(uint32_t interval) 127 { 128 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 129 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 130 if (swiper) { 131 swiper->SetAutoPlayInterval(interval); 132 } 133 } 134 SetDuration(uint32_t duration)135void SwiperModelImpl::SetDuration(uint32_t duration) 136 { 137 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 138 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 139 if (swiper) { 140 swiper->SetDuration(duration); 141 } 142 } 143 SetLoop(bool loop)144void SwiperModelImpl::SetLoop(bool loop) 145 { 146 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 147 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 148 if (swiper) { 149 swiper->SetLoop(loop); 150 } 151 } 152 SetEnabled(bool enabled)153void SwiperModelImpl::SetEnabled(bool enabled) 154 { 155 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 156 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 157 if (swiper) { 158 swiper->SetDisabledStatus(!enabled); 159 } 160 } 161 SetDisableSwipe(bool disableSwipe)162void SwiperModelImpl::SetDisableSwipe(bool disableSwipe) 163 { 164 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 165 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 166 if (swiper) { 167 swiper->DisableSwipe(disableSwipe); 168 } 169 } 170 SetEdgeEffect(EdgeEffect edgeEffect)171void SwiperModelImpl::SetEdgeEffect(EdgeEffect edgeEffect) 172 { 173 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 174 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 175 if (!swiper) { 176 return; 177 } 178 179 if (edgeEffect == EdgeEffect::SPRING) { 180 swiper->SetEdgeEffect(EdgeEffect::SPRING); 181 } else if (edgeEffect == EdgeEffect::FADE) { 182 swiper->SetEdgeEffect(EdgeEffect::FADE); 183 } else { 184 swiper->SetEdgeEffect(EdgeEffect::NONE); 185 } 186 } 187 SetCurve(const RefPtr<Curve> & curve)188void SwiperModelImpl::SetCurve(const RefPtr<Curve>& curve) 189 { 190 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 191 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 192 swiper->SetCurve(curve); 193 } 194 SetOnChange(std::function<void (const BaseEventInfo * info)> && onChange)195void SwiperModelImpl::SetOnChange(std::function<void(const BaseEventInfo* info)>&& onChange) 196 { 197 auto onChangeEvent = EventMarker(std::move(onChange)); 198 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 199 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 200 if (swiper) { 201 swiper->SetChangeEventId(onChangeEvent); 202 } 203 } 204 SetOnAnimationStart(std::function<void (const BaseEventInfo * info)> && onAnimationStart)205void SwiperModelImpl::SetOnAnimationStart(std::function<void(const BaseEventInfo* info)>&& onAnimationStart) 206 { 207 auto onAnimationStartEvent = EventMarker(std::move(onAnimationStart)); 208 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 209 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 210 if (swiper) { 211 swiper->SetAnimationStartEventId(onAnimationStartEvent); 212 } 213 } 214 SetOnAnimationEnd(std::function<void (const BaseEventInfo * info)> && onAnimationEnd)215void SwiperModelImpl::SetOnAnimationEnd(std::function<void(const BaseEventInfo* info)>&& onAnimationEnd) 216 { 217 auto onAnimationEndEvent = EventMarker(std::move(onAnimationEnd)); 218 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 219 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 220 if (swiper) { 221 swiper->SetAnimationEndEventId(onAnimationEndEvent); 222 } 223 } 224 SetRemoteMessageEventId(RemoteCallback && remoteCallback)225void SwiperModelImpl::SetRemoteMessageEventId(RemoteCallback&& remoteCallback) 226 { 227 EventMarker remoteMessageEventId(std::move(remoteCallback)); 228 auto* stack = ViewStackProcessor::GetInstance(); 229 auto swiperComponent = AceType::DynamicCast<SwiperComponent>(stack->GetMainComponent()); 230 if (!swiperComponent) { 231 LOGE("swiperComponent is null"); 232 return; 233 } 234 swiperComponent->SetRemoteMessageEventId(remoteMessageEventId); 235 } 236 SetIndicatorStyle(const SwiperParameters & swiperParameters)237void SwiperModelImpl::SetIndicatorStyle(const SwiperParameters& swiperParameters) 238 { 239 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 240 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 241 if (swiper) { 242 auto indicator = swiper->GetIndicator(); 243 if (!indicator) { 244 return; 245 } 246 247 if (swiperParameters.dimLeft.has_value()) { 248 indicator->SetLeft(swiperParameters.dimLeft.value()); 249 } 250 if (swiperParameters.dimTop.has_value()) { 251 indicator->SetTop(swiperParameters.dimTop.value()); 252 } 253 if (swiperParameters.dimRight.has_value()) { 254 indicator->SetRight(swiperParameters.dimRight.value()); 255 } 256 if (swiperParameters.dimBottom.has_value()) { 257 indicator->SetBottom(swiperParameters.dimBottom.value()); 258 } 259 if (swiperParameters.itemWidth.has_value()) { 260 indicator->SetSize(swiperParameters.itemWidth.value()); 261 } 262 if (swiperParameters.itemHeight.has_value()) { 263 indicator->SetSize(swiperParameters.itemHeight.value()); 264 } 265 if (swiperParameters.maskValue.has_value()) { 266 indicator->SetIndicatorMask(swiperParameters.maskValue.value()); 267 } 268 if (swiperParameters.colorVal.has_value()) { 269 indicator->SetColor(swiperParameters.colorVal.value()); 270 } 271 if (swiperParameters.selectedColorVal.has_value()) { 272 indicator->SetSelectedColor(swiperParameters.selectedColorVal.value()); 273 } 274 } 275 } 276 SetOnClick(std::function<void (const BaseEventInfo * info,const RefPtr<V2::InspectorFunctionImpl> & impl)> && value)277void SwiperModelImpl::SetOnClick( 278 std::function<void(const BaseEventInfo* info, const RefPtr<V2::InspectorFunctionImpl>& impl)>&& value) 279 { 280 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 281 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 282 auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent(); 283 if (!inspector) { 284 LOGE("fail to get inspector for on get click event marker"); 285 if (swiper) { 286 swiper->SetClickEventId(EventMarker()); 287 } 288 return; 289 } 290 auto impl = inspector->GetInspectorFunctionImpl(); 291 auto onClick = [func = std::move(value), impl](const BaseEventInfo* info) { 292 { 293 func(info, impl); 294 } 295 }; 296 if (swiper) { 297 swiper->SetClickEventId(EventMarker(onClick)); 298 } 299 } 300 SetMainSwiperSizeWidth()301void SwiperModelImpl::SetMainSwiperSizeWidth() 302 { 303 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 304 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 305 if (swiper) { 306 if (swiper->GetMainSwiperSize() == MainSwiperSize::MAX || 307 swiper->GetMainSwiperSize() == MainSwiperSize::MAX_Y) { 308 swiper->SetMainSwiperSize(MainSwiperSize::MAX); 309 } else { 310 swiper->SetMainSwiperSize(MainSwiperSize::MAX_X); 311 } 312 } 313 } 314 SetMainSwiperSizeHeight()315void SwiperModelImpl::SetMainSwiperSizeHeight() 316 { 317 auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); 318 auto swiper = AceType::DynamicCast<OHOS::Ace::SwiperComponent>(component); 319 if (swiper) { 320 if (swiper->GetMainSwiperSize() == MainSwiperSize::MAX || 321 swiper->GetMainSwiperSize() == MainSwiperSize::MAX_X) { 322 swiper->SetMainSwiperSize(MainSwiperSize::MAX); 323 } else { 324 swiper->SetMainSwiperSize(MainSwiperSize::MAX_Y); 325 } 326 } 327 } 328 329 } // namespace OHOS::Ace::Framework 330