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