1 /* 2 * Copyright (c) 2022-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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_SWIPER_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_SWIPER_PATTERN_H 18 19 #include <functional> 20 #include <optional> 21 #include <vector> 22 23 #include "base/geometry/axis.h" 24 #include "base/geometry/ng/offset_t.h" 25 #include "base/memory/referenced.h" 26 #include "core/components/common/layout/constants.h" 27 #include "core/components/swiper/swiper_controller.h" 28 #include "core/components/swiper/swiper_indicator_theme.h" 29 #include "core/components_ng/base/frame_node.h" 30 #include "core/components_ng/base/frame_scene_status.h" 31 #include "core/components_ng/base/inspector_filter.h" 32 #include "core/components_ng/event/event_hub.h" 33 #include "core/components_ng/event/input_event.h" 34 #include "core/components_ng/pattern/pattern.h" 35 #include "core/components_ng/pattern/scrollable/nestable_scroll_container.h" 36 #include "core/components_ng/pattern/swiper/swiper_accessibility_property.h" 37 #include "core/components_ng/pattern/swiper/swiper_event_hub.h" 38 #include "core/components_ng/pattern/swiper/swiper_layout_algorithm.h" 39 #include "core/components_ng/pattern/swiper/swiper_layout_property.h" 40 #include "core/components_ng/pattern/swiper/swiper_model.h" 41 #include "core/components_ng/pattern/swiper/swiper_paint_property.h" 42 #include "core/components_ng/pattern/swiper/swiper_utils.h" 43 #include "core/components_ng/pattern/tabs/tab_content_transition_proxy.h" 44 #include "core/components_v2/inspector/utils.h" 45 46 #ifdef SUPPORT_DIGITAL_CROWN 47 #include "core/event/crown_event.h" 48 #endif 49 namespace OHOS::Ace::NG { 50 enum class GestureStatus { 51 INIT = 0, 52 START, 53 END, 54 }; 55 56 enum class PageFlipMode { 57 CONTINUOUS = 0, 58 SINGLE, 59 }; 60 61 using SwiperHoverFlag = uint32_t; 62 constexpr SwiperHoverFlag HOVER_NONE = 0; 63 constexpr SwiperHoverFlag HOVER_SWIPER = 1; 64 constexpr SwiperHoverFlag HOVER_INDICATOR = 1 << 1; 65 constexpr SwiperHoverFlag HOVER_ARROW = 1 << 2; 66 constexpr int32_t NEW_STYLE_MIN_TURN_PAGE_VELOCITY = 780; 67 constexpr float SWIPER_CURVE_MASS = 1.0f; 68 constexpr float SWIPER_CURVE_STIFFNESS = 328.0f; 69 constexpr float SWIPER_CURVE_DAMPING = 34.0f; 70 71 class SwiperPattern : public NestableScrollContainer { 72 DECLARE_ACE_TYPE(SwiperPattern, NestableScrollContainer); 73 74 public: 75 using CustomContentTransitionPtr = std::shared_ptr<std::function<TabContentAnimatedTransition(int32_t, int32_t)>>; 76 using PanEventFunction = std::function<void(const GestureEvent& info)>; 77 78 SwiperPattern(); 79 ~SwiperPattern() override = default; 80 IsAtomicNode()81 bool IsAtomicNode() const override 82 { 83 return false; 84 } 85 IsNeedPercent()86 bool IsNeedPercent() const override 87 { 88 return true; 89 } 90 ShouldDelayChildPressedState()91 bool ShouldDelayChildPressedState() const override 92 { 93 return true; 94 } 95 96 void RegisterScrollingListener(const RefPtr<ScrollingListener> listener) override; 97 void FireAndCleanScrollingListener() override; 98 void CleanScrollingListener() override; 99 UsResRegion()100 bool UsResRegion() override 101 { 102 return false; 103 } 104 CreateLayoutProperty()105 RefPtr<LayoutProperty> CreateLayoutProperty() override 106 { 107 return MakeRefPtr<SwiperLayoutProperty>(); 108 } 109 CreatePaintProperty()110 RefPtr<PaintProperty> CreatePaintProperty() override 111 { 112 return MakeRefPtr<SwiperPaintProperty>(); 113 } 114 CreateAccessibilityProperty()115 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 116 { 117 return MakeRefPtr<SwiperAccessibilityProperty>(); 118 } 119 120 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override; 121 122 RefPtr<NodePaintMethod> CreateNodePaintMethod() override; 123 CreateEventHub()124 RefPtr<EventHub> CreateEventHub() override 125 { 126 return MakeRefPtr<SwiperEventHub>(); 127 } 128 129 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 130 void FromJson(const std::unique_ptr<JsonValue>& json) override; 131 GetArcDotIndicatorStyle()132 virtual std::string GetArcDotIndicatorStyle() const { return ""; } 133 // ArcSwiper will implement this interface in order to set transitionAnimation disable SetDisableTransitionAnimation(bool isDisable)134 virtual void SetDisableTransitionAnimation(bool isDisable) {}; GetCurrentShownIndex()135 int32_t GetCurrentShownIndex() const 136 { 137 return IsLoop() ? currentIndex_ : GetLoopIndex(currentIndex_); 138 } 139 GetSwiperController()140 RefPtr<SwiperController> GetSwiperController() const 141 { 142 return swiperController_; 143 } 144 145 void SetSwiperController(const RefPtr<SwiperController>& controller); 146 147 void SetPropertyAnimationIsRunning(bool propertyAnimationIsRunning); 148 149 bool GetPropertyAnimationIsRunning(); 150 GetCurrentFirstIndex()151 int32_t GetCurrentFirstIndex() const 152 { 153 return currentFirstIndex_; 154 } 155 156 int32_t GetCurrentIndex(bool original = false); 157 GetTurnPageRate()158 float GetTurnPageRate() const 159 { 160 return turnPageRate_; 161 } 162 GetGroupTurnPageRate()163 float GetGroupTurnPageRate() const 164 { 165 return groupTurnPageRate_; 166 } 167 168 GestureState GetGestureState(); 169 GetTouchBottomTypeLoop()170 TouchBottomTypeLoop GetTouchBottomTypeLoop() const 171 { 172 return touchBottomType_; 173 } 174 IsIndicatorAnimatorRunning()175 bool IsIndicatorAnimatorRunning() const 176 { 177 return indicatorAnimationIsRunning_; 178 } 179 SetTurnPageRate(float turnPageRate)180 void SetTurnPageRate(float turnPageRate) 181 { 182 turnPageRate_ = turnPageRate; 183 } 184 SetGroupTurnPageRate(float groupTurnPageRate)185 void SetGroupTurnPageRate(float groupTurnPageRate) 186 { 187 groupTurnPageRate_ = groupTurnPageRate; 188 } 189 GetTouchBottomRate()190 float GetTouchBottomRate() const 191 { 192 return touchBottomRate_; 193 } 194 SetTouchBottomRate(float touchBottomRate)195 void SetTouchBottomRate(float touchBottomRate) 196 { 197 touchBottomRate_ = touchBottomRate; 198 } 199 GetController()200 RefPtr<Animator> GetController() 201 { 202 return controller_; 203 } 204 SetIndicatorDoingAnimation(bool indicatorDoingAnimation)205 void SetIndicatorDoingAnimation(bool indicatorDoingAnimation) 206 { 207 indicatorDoingAnimation_ = indicatorDoingAnimation; 208 } 209 210 void UpdateCurrentOffset(float offset); 211 /** 212 * @brief Checks if the given offset exceeds the bounds of the swiper container and triggers overScroll. 213 * 214 * @param offset The offset to check. 215 * @return True if overScroll is triggered, false otherwise. 216 */ 217 bool CheckOverScroll(float offset); 218 219 /** 220 * @brief Applies spring effect to the over-scrolling of the swiper. 221 * 222 * @param offset The offset of the swiper. 223 * @return true if the spring effect is applied successfully, false otherwise. 224 */ 225 bool SpringOverScroll(float offset); 226 227 void CheckMarkDirtyNodeForRenderIndicator( 228 float additionalOffset = 0.0f, std::optional<int32_t> nextIndex = std::nullopt); 229 230 int32_t TotalCount() const; 231 232 Axis GetDirection() const; 233 GetFocusPattern()234 FocusPattern GetFocusPattern() const override 235 { 236 return { FocusType::SCOPE, true }; 237 } 238 GetScopeFocusAlgorithm()239 ScopeFocusAlgorithm GetScopeFocusAlgorithm() override 240 { 241 return ScopeFocusAlgorithm(direction_ != Axis::HORIZONTAL, true, ScopeType::OTHERS, 242 [wp = WeakClaim(this)]( 243 FocusStep step, const WeakPtr<FocusHub>& currFocusNode, WeakPtr<FocusHub>& nextFocusNode) -> bool { 244 auto swiper = wp.Upgrade(); 245 if (swiper) { 246 nextFocusNode = swiper->GetNextFocusNode(step, currFocusNode); 247 } 248 return nextFocusNode.Upgrade() != currFocusNode.Upgrade(); 249 }); 250 } 251 UpdateChangeEvent(ChangeEvent && event)252 void UpdateChangeEvent(ChangeEvent&& event) 253 { 254 if (!changeEvent_) { 255 changeEvent_ = std::make_shared<ChangeEvent>(event); 256 auto eventHub = GetEventHub<SwiperEventHub>(); 257 CHECK_NULL_VOID(eventHub); 258 eventHub->AddOnChangeEvent(changeEvent_); 259 } else { 260 (*changeEvent_).swap(event); 261 } 262 } 263 UpdateOnChangeEvent(ChangeEvent && event)264 void UpdateOnChangeEvent(ChangeEvent&& event) 265 { 266 if (!onIndexChangeEvent_) { 267 onIndexChangeEvent_ = std::make_shared<ChangeEvent>(event); 268 auto eventHub = GetEventHub<SwiperEventHub>(); 269 CHECK_NULL_VOID(eventHub); 270 eventHub->AddOnChangeEvent(onIndexChangeEvent_); 271 } else { 272 (*onIndexChangeEvent_).swap(event); 273 } 274 } 275 UpdateAnimationStartEvent(AnimationStartEvent && event)276 void UpdateAnimationStartEvent(AnimationStartEvent&& event) 277 { 278 if (!animationStartEvent_) { 279 animationStartEvent_ = std::make_shared<AnimationStartEvent>(event); 280 auto eventHub = GetEventHub<SwiperEventHub>(); 281 CHECK_NULL_VOID(eventHub); 282 eventHub->AddAnimationStartEvent(animationStartEvent_); 283 } else { 284 (*animationStartEvent_).swap(event); 285 } 286 } 287 UpdateAnimationEndEvent(AnimationEndEvent && event)288 void UpdateAnimationEndEvent(AnimationEndEvent&& event) 289 { 290 if (!animationEndEvent_) { 291 animationEndEvent_ = std::make_shared<AnimationEndEvent>(event); 292 auto eventHub = GetEventHub<SwiperEventHub>(); 293 CHECK_NULL_VOID(eventHub); 294 eventHub->AddAnimationEndEvent(animationEndEvent_); 295 } else { 296 (*animationEndEvent_).swap(event); 297 } 298 } 299 UpdateOnSelectedEvent(ChangeEvent && event)300 void UpdateOnSelectedEvent(ChangeEvent&& event) 301 { 302 if (!selectedEvent_) { 303 selectedEvent_ = std::make_shared<ChangeEvent>(event); 304 auto eventHub = GetEventHub<SwiperEventHub>(); 305 CHECK_NULL_VOID(eventHub); 306 eventHub->AddOnSlectedEvent(selectedEvent_); 307 } else { 308 (*selectedEvent_).swap(event); 309 } 310 } 311 UpdateOnUnselectedEvent(ChangeEvent && event)312 void UpdateOnUnselectedEvent(ChangeEvent&& event) 313 { 314 if (!unselectedEvent_) { 315 unselectedEvent_ = std::make_shared<ChangeEvent>(event); 316 auto eventHub = GetEventHub<SwiperEventHub>(); 317 CHECK_NULL_VOID(eventHub); 318 eventHub->AddOnUnselectedEvent(unselectedEvent_); 319 } else { 320 (*unselectedEvent_).swap(event); 321 } 322 } 323 SetSwiperParameters(const SwiperParameters & swiperParameters)324 void SetSwiperParameters(const SwiperParameters& swiperParameters) 325 { 326 swiperParameters_ = std::make_shared<SwiperParameters>(swiperParameters); 327 } 328 SetSwiperDigitalParameters(const SwiperDigitalParameters & swiperDigitalParameters)329 void SetSwiperDigitalParameters(const SwiperDigitalParameters& swiperDigitalParameters) 330 { 331 swiperDigitalParameters_ = std::make_shared<SwiperDigitalParameters>(swiperDigitalParameters); 332 } 333 ResetIndicatorParameters()334 void ResetIndicatorParameters() 335 { 336 if (GetIndicatorType() == SwiperIndicatorType::DOT) { 337 swiperParameters_ = nullptr; 338 } else { 339 swiperDigitalParameters_ = nullptr; 340 } 341 } 342 SetSwiperArcDotParameters(const SwiperArcDotParameters & swiperArcDotParameters)343 virtual void SetSwiperArcDotParameters(const SwiperArcDotParameters& swiperArcDotParameters) {} 344 345 void ShowNext(bool needCheckWillScroll = false); 346 void ShowPrevious(bool needCheckWillScroll = false); 347 void SwipeTo(int32_t index); 348 void ChangeIndex(int32_t index, bool useAnimation); 349 void ChangeIndex(int32_t index, SwiperAnimationMode mode); 350 351 void OnVisibleChange(bool isVisible) override; 352 GetStartIndex()353 int32_t GetStartIndex() const 354 { 355 return startIndex_; 356 } 357 GetEndIndex()358 int32_t GetEndIndex() const 359 { 360 return endIndex_; 361 } 362 HasIndicatorNode()363 bool HasIndicatorNode() const 364 { 365 return indicatorId_.has_value() || GetIndicatorNode() != nullptr; 366 } 367 HasLeftButtonNode()368 bool HasLeftButtonNode() const 369 { 370 return leftButtonId_.has_value(); 371 } 372 HasRightButtonNode()373 bool HasRightButtonNode() const 374 { 375 return rightButtonId_.has_value(); 376 } 377 CreateIndicatorId()378 int32_t CreateIndicatorId() 379 { 380 indicatorId_ = ElementRegister::GetInstance()->MakeUniqueId(); 381 return indicatorId_.value(); 382 } 383 GetIndicatorId()384 int32_t GetIndicatorId() const 385 { 386 return indicatorId_.has_value() ? indicatorId_.value() : -1; 387 } 388 GetLeftButtonId()389 int32_t GetLeftButtonId() 390 { 391 if (!leftButtonId_.has_value()) { 392 leftButtonId_ = ElementRegister::GetInstance()->MakeUniqueId(); 393 } 394 return leftButtonId_.value(); 395 } 396 GetRightButtonId()397 int32_t GetRightButtonId() 398 { 399 if (!rightButtonId_.has_value()) { 400 rightButtonId_ = ElementRegister::GetInstance()->MakeUniqueId(); 401 } 402 return rightButtonId_.value(); 403 } 404 GetId()405 int32_t GetId() const 406 { 407 return swiperId_; 408 } 409 RemoveIndicatorNode()410 void RemoveIndicatorNode() 411 { 412 CHECK_NULL_VOID(HasIndicatorNode()); 413 auto swiperNode = GetHost(); 414 CHECK_NULL_VOID(swiperNode); 415 swiperNode->RemoveChildAtIndex(swiperNode->GetChildIndexById(GetIndicatorId())); 416 indicatorId_ = std::nullopt; 417 } 418 RemoveLeftButtonNode()419 void RemoveLeftButtonNode() 420 { 421 CHECK_NULL_VOID(HasLeftButtonNode()); 422 auto swiperNode = GetHost(); 423 CHECK_NULL_VOID(swiperNode); 424 swiperNode->RemoveChildAtIndex(swiperNode->GetChildIndexById(GetLeftButtonId())); 425 leftButtonId_ = std::nullopt; 426 } 427 RemoveRightButtonNode()428 void RemoveRightButtonNode() 429 { 430 CHECK_NULL_VOID(HasRightButtonNode()); 431 auto swiperNode = GetHost(); 432 CHECK_NULL_VOID(swiperNode); 433 swiperNode->RemoveChildAtIndex(swiperNode->GetChildIndexById(GetRightButtonId())); 434 rightButtonId_ = std::nullopt; 435 } 436 437 SwiperIndicatorType GetIndicatorType() const; 438 IsIndicatorCustomSize()439 bool IsIndicatorCustomSize() const 440 { 441 return isCustomSize_; 442 } 443 SetIsIndicatorCustomSize(bool isCustomSize)444 void SetIsIndicatorCustomSize(bool isCustomSize) 445 { 446 isCustomSize_ = isCustomSize; 447 } 448 SetIndicatorIsBoolean(bool isBoolean)449 void SetIndicatorIsBoolean(bool isBoolean) 450 { 451 indicatorIsBoolean_ = isBoolean; 452 } 453 GetIsAtHotRegion()454 bool GetIsAtHotRegion() const 455 { 456 return isAtHotRegion_; 457 } 458 HasSurfaceChangedCallback()459 bool HasSurfaceChangedCallback() 460 { 461 return surfaceChangedCallbackId_.has_value(); 462 } 463 UpdateSurfaceChangedCallbackId(int32_t id)464 void UpdateSurfaceChangedCallbackId(int32_t id) 465 { 466 surfaceChangedCallbackId_ = id; 467 } 468 SetIndicatorLongPress(bool isIndicatorLongPress)469 void SetIndicatorLongPress(bool isIndicatorLongPress) 470 { 471 isIndicatorLongPress_ = isIndicatorLongPress; 472 } SetCachedCount(int32_t cachedCount)473 void SetCachedCount(int32_t cachedCount) 474 { 475 if (cachedCount_.has_value() && cachedCount_.value() != cachedCount) { 476 SetLazyLoadFeature(true); 477 } 478 cachedCount_ = cachedCount; 479 } 480 SetFinishCallbackType(FinishCallbackType finishCallbackType)481 void SetFinishCallbackType(FinishCallbackType finishCallbackType) 482 { 483 finishCallbackType_ = finishCallbackType; 484 } 485 GetFinishCallbackType()486 FinishCallbackType GetFinishCallbackType() const 487 { 488 return finishCallbackType_; 489 } 490 SetStopIndicatorAnimationCb(const std::function<void (bool)> & stopCallback)491 void SetStopIndicatorAnimationCb(const std::function<void(bool)>& stopCallback) 492 { 493 stopIndicatorAnimationFunc_ = std::move(stopCallback); 494 } 495 SetUpdateOverlongForceStopPageRateCb(const std::function<void (float)> & updateCallback)496 void SetUpdateOverlongForceStopPageRateCb(const std::function<void(float)>& updateCallback) 497 { 498 updateOverlongForceStopPageRateFunc_ = std::move(updateCallback); 499 } 500 501 std::shared_ptr<SwiperParameters> GetSwiperParameters() const; GetSwiperArcDotParameters()502 virtual std::shared_ptr<SwiperArcDotParameters> GetSwiperArcDotParameters() const { return nullptr; } 503 std::shared_ptr<SwiperDigitalParameters> GetSwiperDigitalParameters() const; 504 505 void ArrowHover(bool isHover, SwiperHoverFlag flag); IsHoverNone()506 bool IsHoverNone() 507 { 508 return hoverFlag_ == HOVER_NONE; 509 } 510 virtual bool IsLoop() const; 511 bool IsEnabled() const; 512 void OnWindowShow() override; 513 void OnWindowHide() override; 514 std::string ProvideRestoreInfo() override; 515 void OnRestoreInfo(const std::string& restoreInfo) override; 516 bool IsAutoFill() const; 517 void SwipeToWithoutAnimation(int32_t index); 518 void StopAutoPlay(); 519 void StartAutoPlay(); 520 void StopTranslateAnimation(); 521 void StopSpringAnimationImmediately(); 522 void StopSpringAnimation(); 523 void DumpAdvanceInfo() override; 524 void DumpAdvanceInfo(std::unique_ptr<JsonValue>& json) override; 525 void BuildOffsetInfo(std::unique_ptr<JsonValue>& json); 526 void BuildAxisInfo(std::unique_ptr<JsonValue>& json); 527 void BuildItemPositionInfo(std::unique_ptr<JsonValue>& json); 528 void BuildIndicatorTypeInfo(std::unique_ptr<JsonValue>& json); 529 void BuildPanDirectionInfo(std::unique_ptr<JsonValue>& json); 530 int32_t GetLoopIndex(int32_t originalIndex) const; 531 virtual int32_t GetDuration() const; 532 void UpdateDragFRCSceneInfo(float speed, SceneStatus sceneStatus); 533 void AdjustCurrentIndexOnSwipePage(int32_t index); 534 void OnCustomContentTransition(int32_t toIndex); 535 void OnCustomAnimationFinish(int32_t fromIndex, int32_t toIndex, bool hasOnChanged); 536 void OnSwiperCustomAnimationFinish(CancelableCallback<void()>& task, int32_t index, bool isFinishAnimation); 537 SetCustomAnimationToIndex(int32_t toIndex)538 void SetCustomAnimationToIndex(int32_t toIndex) 539 { 540 customAnimationToIndex_ = toIndex; 541 } 542 GetCustomAnimationToIndex()543 std::optional<int32_t> GetCustomAnimationToIndex() const 544 { 545 return customAnimationToIndex_; 546 } 547 SetTabsCustomContentTransition(std::function<TabContentAnimatedTransition (int32_t,int32_t)> && event)548 void SetTabsCustomContentTransition(std::function<TabContentAnimatedTransition(int32_t, int32_t)>&& event) 549 { 550 onTabsCustomContentTransition_ = 551 std::make_shared<std::function<TabContentAnimatedTransition(int32_t, int32_t)>>(event); 552 } 553 GetTabsCustomContentTransition()554 CustomContentTransitionPtr GetTabsCustomContentTransition() const 555 { 556 return onTabsCustomContentTransition_; 557 } 558 SetSwiperCustomContentTransition(SwiperContentAnimatedTransition & transition)559 void SetSwiperCustomContentTransition(SwiperContentAnimatedTransition& transition) 560 { 561 onSwiperCustomContentTransition_ = std::make_shared<SwiperContentAnimatedTransition>(transition); 562 } 563 GetSwiperCustomContentTransition()564 std::shared_ptr<SwiperContentAnimatedTransition> GetSwiperCustomContentTransition() const 565 { 566 return onSwiperCustomContentTransition_; 567 } 568 SetOnContentDidScroll(ContentDidScrollEvent && onContentDidScroll)569 void SetOnContentDidScroll(ContentDidScrollEvent&& onContentDidScroll) 570 { 571 onContentDidScroll_ = std::make_shared<ContentDidScrollEvent>(onContentDidScroll); 572 } 573 SetOnContentWillScroll(ContentWillScrollEvent && onContentWillScroll)574 void SetOnContentWillScroll(ContentWillScrollEvent&& onContentWillScroll) 575 { 576 onContentWillScroll_ = std::make_shared<ContentWillScrollEvent>(onContentWillScroll); 577 } 578 HasOnContentWillScroll()579 bool HasOnContentWillScroll() const 580 { 581 return onContentWillScroll_ && *onContentWillScroll_; 582 } 583 GetOnContentDidScroll()584 std::shared_ptr<ContentDidScrollEvent> GetOnContentDidScroll() const 585 { 586 return onContentDidScroll_; 587 } 588 589 void SetSwiperEventCallback(bool disableSwipe); 590 void UpdateSwiperPanEvent(bool disableSwipe); 591 bool IsUseCustomAnimation() const; 592 SetTabsPaddingAndBorder(const PaddingPropertyF & tabsPaddingAndBorder)593 void SetTabsPaddingAndBorder(const PaddingPropertyF& tabsPaddingAndBorder) 594 { 595 tabsPaddingAndBorder_ = tabsPaddingAndBorder; 596 } 597 598 void InitAnimationCurve(); 599 RefPtr<Curve> GetCurveIncludeMotion(); 600 RefPtr<Curve> GetIndicatorHeadCurve(); GetMotionVelocity()601 float GetMotionVelocity() 602 { 603 return motionVelocity_; 604 } 605 606 int32_t RealTotalCount() const; 607 bool IsSwipeByGroup() const; 608 int32_t DisplayIndicatorTotalCount() const; 609 std::pair<int32_t, int32_t> CalculateStepAndItemCount() const; 610 int32_t GetDisplayCount() const; 611 int32_t GetCachedCount() const; 612 bool ContentWillChange(int32_t comingIndex); 613 bool ContentWillChange(int32_t currentIndex, int32_t comingIndex); 614 bool CheckSwiperPanEvent(float mainDeltaOrVelocity); InitIndexCanChangeMap()615 void InitIndexCanChangeMap() 616 { 617 indexCanChangeMap_.clear(); 618 } 619 GetNextValidIndex()620 int32_t GetNextValidIndex() const 621 { 622 return nextValidIndex_; 623 } 624 void UpdateNextValidIndex(); 625 void CheckMarkForIndicatorBoundary(); 626 bool IsHorizontalAndRightToLeft() const; 627 TextDirection GetNonAutoLayoutDirection() const; 628 void FireSelectedEvent(int32_t currentIndex, int32_t targetIndex); 629 void FireWillHideEvent(int32_t willHideIndex) const; 630 void FireWillShowEvent(int32_t willShowIndex) const; 631 void SetOnHiddenChangeForParent(); 632 void RemoveOnHiddenChange(); 633 SetHasTabsAncestor(bool hasTabsAncestor)634 void SetHasTabsAncestor(bool hasTabsAncestor) 635 { 636 hasTabsAncestor_ = hasTabsAncestor; 637 } 638 SetIndicatorInteractive(bool isInteractive)639 void SetIndicatorInteractive(bool isInteractive) 640 { 641 isIndicatorInteractive_ = isInteractive; 642 } 643 644 bool IsAtStart() const; 645 bool IsAtEnd() const; 646 IsIndicatorInteractive()647 bool IsIndicatorInteractive() const 648 { 649 return isIndicatorInteractive_; 650 } 651 SetNextMarginIgnoreBlank(bool nextMarginIgnoreBlank)652 void SetNextMarginIgnoreBlank(bool nextMarginIgnoreBlank) 653 { 654 nextMarginIgnoreBlank_ = nextMarginIgnoreBlank; 655 } 656 SetPrevMarginIgnoreBlank(bool prevMarginIgnoreBlank)657 void SetPrevMarginIgnoreBlank(bool prevMarginIgnoreBlank) 658 { 659 prevMarginIgnoreBlank_ = prevMarginIgnoreBlank; 660 } 661 SetFrameRateRange(const RefPtr<FrameRateRange> & rateRange,SwiperDynamicSyncSceneType type)662 void SetFrameRateRange(const RefPtr<FrameRateRange>& rateRange, SwiperDynamicSyncSceneType type) override 663 { 664 if (rateRange) { 665 frameRateRange_[type] = rateRange; 666 } 667 } 668 void UpdateNodeRate(); 669 #ifdef SUPPORT_DIGITAL_CROWN SetDigitalCrownSensitivity(CrownSensitivity sensitivity)670 virtual void SetDigitalCrownSensitivity(CrownSensitivity sensitivity) {} InitOnCrownEventInternal(const RefPtr<FocusHub> & focusHub)671 virtual void InitOnCrownEventInternal(const RefPtr<FocusHub>& focusHub) {} IsCrownSpring()672 virtual bool IsCrownSpring() const { return false; } SetIsCrownSpring(bool isCrownSpring)673 virtual void SetIsCrownSpring(bool isCrownSpring) {} 674 #endif 675 int32_t GetMaxDisplayCount() const; 676 SaveCircleDotIndicatorProperty(const RefPtr<FrameNode> & indicatorNode)677 virtual void SaveCircleDotIndicatorProperty(const RefPtr<FrameNode>& indicatorNode) {} GetPrevMarginIgnoreBlank()678 bool GetPrevMarginIgnoreBlank() 679 { 680 return prevMarginIgnoreBlank_; 681 } 682 GetNextMarginIgnoreBlank()683 bool GetNextMarginIgnoreBlank() 684 { 685 return nextMarginIgnoreBlank_; 686 } 687 GetCachedItems()688 const std::set<int32_t>& GetCachedItems() const 689 { 690 return cachedItems_; 691 } 692 SetCachedItems(const std::set<int32_t> & cachedItems)693 void SetCachedItems(const std::set<int32_t>& cachedItems) 694 { 695 cachedItems_ = cachedItems; 696 } 697 GetLayoutConstraint()698 LayoutConstraintF GetLayoutConstraint() const 699 { 700 return layoutConstraint_; 701 } 702 SetContentMainSize(float contentMainSize)703 void SetContentMainSize(float contentMainSize) 704 { 705 contentMainSize_ = contentMainSize; 706 } 707 GetRequestLongPredict()708 bool GetRequestLongPredict() const 709 { 710 return requestLongPredict_; 711 } 712 IsPropertyAnimationRunning()713 bool IsPropertyAnimationRunning() const 714 { 715 return propertyAnimationIsRunning_; 716 } 717 IsTranslateAnimationRunning()718 bool IsTranslateAnimationRunning() const 719 { 720 return translateAnimationIsRunning_; 721 } 722 IsTouchDown()723 bool IsTouchDown() const 724 { 725 return isTouchDown_; 726 } 727 IsTouchDownOnOverlong()728 bool IsTouchDownOnOverlong() const 729 { 730 return isTouchDownOnOverlong_ || isDragging_; 731 } 732 733 bool IsAutoPlay() const; 734 735 void SetPageFlipMode(int32_t pageFlipMode); 736 GetPageFlipMode()737 int32_t GetPageFlipMode() const 738 { 739 return static_cast<int32_t>(pageFlipMode_); 740 } 741 IsStopWhenTouched()742 bool IsStopWhenTouched() const 743 { 744 return stopWhenTouched_; 745 } 746 SetStopWhenTouched(bool stopWhenTouched)747 void SetStopWhenTouched(bool stopWhenTouched) 748 { 749 stopWhenTouched_ = stopWhenTouched; 750 } 751 SetJumpAnimationMode(TabAnimateMode tabAnimationMode)752 void SetJumpAnimationMode(TabAnimateMode tabAnimationMode) 753 { 754 tabAnimationMode_ = tabAnimationMode; 755 } 756 757 bool NeedFastAnimation() const; 758 bool IsInFastAnimation() const; 759 760 float CalcCurrentTurnPageRate(bool isTouchBottom = false) const; 761 int32_t GetFirstIndexInVisibleArea() const; 762 float CalculateGroupTurnPageRate(float additionalOffset); 763 IsBindIndicator()764 bool IsBindIndicator() const 765 { 766 return isBindIndicator_; 767 } 768 SetBindIndicator(bool bind)769 void SetBindIndicator(bool bind) 770 { 771 isBindIndicator_ = bind; 772 } 773 774 void SetIndicatorNode(const WeakPtr<NG::UINode>& indicatorNode); 775 GetIndicatorNode()776 RefPtr<FrameNode> GetIndicatorNode() const 777 { 778 auto refUINode = indicatorNode_.Upgrade(); 779 CHECK_NULL_RETURN(refUINode, nullptr); 780 auto frameNode = DynamicCast<FrameNode>(refUINode); 781 CHECK_NULL_RETURN(frameNode, nullptr); 782 return frameNode; 783 } 784 785 bool IsFocusNodeInItemPosition(const RefPtr<FrameNode>& focusNode); 786 virtual RefPtr<Curve> GetCurve() const; 787 SetGestureStatus(GestureStatus gestureStatus)788 void SetGestureStatus(GestureStatus gestureStatus) 789 { 790 gestureStatus_ = gestureStatus; 791 } 792 793 protected: 794 void MarkDirtyNodeSelf(); 795 void OnPropertyTranslateAnimationFinish(const OffsetF& offset); 796 void OnAnimationTranslateZero(int32_t nextIndex, bool stopAutoPlay); 797 bool NeedStartNewAnimation(const OffsetF& offset) const; 798 void StopPropertyTranslateAnimation(bool isFinishAnimation, 799 bool isBeforeCreateLayoutWrapper = false, bool isInterrupt = false); 800 void StopIndicatorAnimation(bool ifImmediately = false); 801 void FireAnimationStartEvent(int32_t currentIndex, int32_t nextIndex, const AnimationCallbackInfo& info) const; 802 void SetLazyLoadFeature(bool useLazyLoad); 803 void UpdateItemRenderGroup(bool itemRenderGroup); 804 805 float GetCustomPropertyOffset() const; 806 float GetCustomPropertyTargetOffset() const; CalculateVisibleSize()807 float CalculateVisibleSize() const 808 { 809 return contentMainSize_ - GetPrevMarginWithItemSpace() - GetNextMarginWithItemSpace(); 810 } SupportSwiperCustomAnimation()811 bool SupportSwiperCustomAnimation() const 812 { 813 auto swiperLayoutProperty = GetLayoutProperty<SwiperLayoutProperty>(); 814 return (onSwiperCustomContentTransition_ || onContentDidScroll_) && 815 !hasCachedCapture_ && SwiperUtils::IsStretch(swiperLayoutProperty); 816 } 817 818 GestureState gestureState_ = GestureState::GESTURE_STATE_INIT; 819 int32_t currentIndex_ = 0; 820 std::optional<int32_t> fastCurrentIndex_; 821 SwiperLayoutAlgorithm::PositionMap itemPosition_; 822 SwiperLayoutAlgorithm::PositionMap itemPositionInAnimation_; 823 SwiperLayoutAlgorithm::PositionMap itemPositionWillInvisible_; 824 std::optional<int32_t> targetIndex_; 825 float swiperProportion_ = 2.0f; 826 int32_t newMinTurnPageVelocity_ = NEW_STYLE_MIN_TURN_PAGE_VELOCITY; 827 int32_t propertyAnimationIndex_ = -1; 828 829 bool hasTabsAncestor_ = false; 830 bool usePropertyAnimation_ = false; 831 bool stopIndicatorAnimation_ = true; 832 bool isFinishAnimation_ = false; 833 bool isDragging_ = false; 834 float motionVelocity_ = 0.0f; 835 float currentIndexOffset_ = 0.0f; 836 837 std::unordered_map<SwiperDynamicSyncSceneType, RefPtr<FrameRateRange>> frameRateRange_; 838 void HandleDragStart(const GestureEvent& info); 839 void HandleDragUpdate(const GestureEvent& info); 840 void HandleDragEnd(double dragVelocity, float mainDelta = 0.0f); 841 842 void HandleTouchDown(const TouchLocationInfo& locationInfo); 843 void HandleTouchUp(); 844 845 /** 846 * @brief Notifies the parent component that the scroll has started at the specified position. 847 * 848 * @param position The position where the scroll has started. 849 */ 850 void NotifyParentScrollStart(WeakPtr<NestableScrollContainer> child, float position); 851 /** 852 * @brief Notifies the parent NestableScrollContainer that the scroll has ended. 853 */ 854 void NotifyParentScrollEnd(); 855 856 Axis direction_ = Axis::HORIZONTAL; 857 858 private: 859 void OnModifyDone() override; 860 void OnAfterModifyDone() override; 861 void OnAttachToFrameNode() override; 862 void OnDetachFromFrameNode(FrameNode* node) override; 863 void OnAttachToMainTree() override; 864 void OnDetachFromMainTree() override; 865 void InitSurfaceChangedCallback(); 866 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 867 void HandleTargetIndex(const RefPtr<LayoutWrapper>& dirty, const RefPtr<SwiperLayoutAlgorithm>& algo); 868 void HandleRunningTranslateAnimation(); 869 void HandleTargetItemNotFound( 870 const RefPtr<SwiperLayoutProperty>& props, int32_t targetIndexValue, const RefPtr<SwiperLayoutAlgorithm>& algo); 871 bool IsNeedForwardTranslate(const RefPtr<SwiperLayoutProperty>& props, int32_t targetIndexValue); 872 bool IsNeedBackwardTranslate(const RefPtr<SwiperLayoutProperty>& props, int32_t targetIndexValue); 873 void HandleTabsAncestor(); 874 void UpdateLayoutProperties(const RefPtr<SwiperLayoutAlgorithm>& algo); 875 // Init pan recognizer to move items when drag update, play translate animation when drag end. 876 void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub); 877 void AddPanEvent(const RefPtr<GestureEventHub>& gestureHub, GestureEventFunc&& actionStart, 878 GestureEventFunc&& actionUpdate, GestureEventFunc&& actionEnd, GestureEventNoParameter&& actionCancel); 879 PanEventFunction ActionStartTask(); 880 PanEventFunction ActionUpdateTask(); 881 PanEventFunction ActionEndTask(); 882 883 // Init touch event, stop animation when touch down. 884 void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub); 885 void InitHoverMouseEvent(); 886 // Init on key event 887 void InitOnFocusInternal(const RefPtr<FocusHub>& focusHub); 888 void HandleFocusInternal(); 889 void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub); 890 bool OnKeyEvent(const KeyEvent& event); 891 bool IsFocusNodeInItemPosition(const RefPtr<FocusHub>& targetFocusHub); 892 void FlushFocus(const RefPtr<FrameNode>& curShowFrame); 893 WeakPtr<FocusHub> GetNextFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode); 894 895 // Init indicator 896 void InitIndicator(); 897 void InitArrow(); 898 void SaveIndicatorProperty(const RefPtr<FrameNode>& indicatorNode, SwiperIndicatorType swiperIndicatorType); 899 900 bool InsideIndicatorRegion(const TouchLocationInfo& locationInfo); 901 void HandleTouchEvent(const TouchEventInfo& info); 902 903 void HandleMouseEvent(const MouseInfo& info); 904 void PlayTranslateAnimation( 905 float startPos, float endPos, int32_t nextIndex, bool restartAutoPlay = false, float velocity = 0.0f); 906 void OnTranslateAnimationFinish(); 907 void PlaySpringAnimation(double dragVelocity); 908 void PlayFadeAnimation(); 909 910 // use property animation feature 911 // ArcSwiper will implement this interface in order to reset the animation effect parameters. ResetAnimationParam()912 virtual void ResetAnimationParam() {}; 913 // ArcSwiper will implement this interface in order to reset the animation effect of the current node. ResetCurrentFrameNodeAnimation()914 virtual void ResetCurrentFrameNodeAnimation() {}; 915 // ArcSwiper will implement this interface in order to reset the background color of parent node. ResetParentNodeColor()916 virtual void ResetParentNodeColor() {}; 917 // ArcSwiper will implement this interface in order to achieve the function of the manual effect. PlayScrollAnimation(float currentDelta,float currentIndexOffset)918 virtual void PlayScrollAnimation(float currentDelta, float currentIndexOffset) {}; 919 virtual void PlayPropertyTranslateAnimation(float translate, int32_t nextIndex, float velocity = 0.0f, 920 bool stopAutoPlay = false, std::optional<float> pixelRoundTargetPos = std::nullopt); 921 void UpdateOffsetAfterPropertyAnimation(float offset); 922 void PlayIndicatorTranslateAnimation(float translate, std::optional<int32_t> nextIndex = std::nullopt); 923 void PropertyCancelAnimationFinish(bool isFinishAnimation, bool isBeforeCreateLayoutWrapper, bool isInterrupt); 924 925 // Implement of swiper controller 926 927 void FinishAnimation(); 928 void StopFadeAnimation(); 929 930 bool IsOutOfBoundary(float mainOffset = 0.0f) const; 931 bool IsOutOfStart(float mainOffset = 0.0f) const; 932 bool IsOutOfEnd(float mainOffset = 0.0f) const; 933 bool AutoLinearIsOutOfBoundary(float mainOffset) const; 934 float GetDistanceToEdge() const; 935 float MainSize() const; 936 float GetMainContentSize() const; 937 void FireChangeEvent(int32_t preIndex, int32_t currentIndex, bool isInLayout = false) const; 938 void FireAnimationEndEvent(int32_t currentIndex, const AnimationCallbackInfo& info, bool isInterrupt = false) const; 939 void FireGestureSwipeEvent(int32_t currentIndex, const AnimationCallbackInfo& info) const; 940 void FireUnselectedEvent(int32_t currentIndex, int32_t targetIndex); 941 void FireSwiperCustomAnimationEvent(); 942 void FireContentDidScrollEvent(); 943 void HandleSwiperCustomAnimation(float offset); 944 void CalculateAndUpdateItemInfo(float offset = 0.0f); 945 void UpdateItemInfoInCustomAnimation(int32_t index, float startPos, float endPos); 946 void UpdateTabBarAnimationDuration(int32_t index); 947 948 float GetItemSpace() const; 949 float GetPrevMargin() const; 950 float GetNextMargin() const; GetPrevMarginWithItemSpace()951 float GetPrevMarginWithItemSpace() const 952 { 953 return Positive(GetPrevMargin()) ? GetPrevMargin() + GetItemSpace() : 0.0f; 954 } GetNextMarginWithItemSpace()955 float GetNextMarginWithItemSpace() const 956 { 957 return Positive(GetNextMargin()) ? GetNextMargin() + GetItemSpace() : 0.0f; 958 } 959 int32_t CurrentIndex() const; 960 int32_t CalculateDisplayCount() const; 961 int32_t CalculateCount( 962 float contentWidth, float minSize, float margin, float gutter, float swiperPadding = 0.0f) const; 963 int32_t GetInterval() const; 964 EdgeEffect GetEdgeEffect() const; 965 bool IsDisableSwipe() const; 966 bool IsShowIndicator() const; 967 std::pair<int32_t, SwiperItemInfo> GetFirstItemInfoInVisibleArea() const; 968 std::pair<int32_t, SwiperItemInfo> GetLastItemInfoInVisibleArea() const; 969 std::pair<int32_t, SwiperItemInfo> GetSecondItemInfoInVisibleArea() const; 970 void OnIndexChange(bool isInLayout = false); 971 bool IsOutOfHotRegion(const PointF& dragPoint) const; 972 void SetDigitStartAndEndProperty(const RefPtr<FrameNode>& indicatorNode); 973 void UpdatePaintProperty(const RefPtr<FrameNode>& indicatorNode); 974 void PostTranslateTask(uint32_t delayTime); 975 void HandleVisibleChange(bool visible); 976 void RegisterVisibleAreaChange(); 977 bool NeedAutoPlay() const; 978 void OnTranslateFinish(int32_t nextIndex, bool restartAutoPlay, bool isFinishAnimation, bool forceStop = false, 979 bool isInterrupt = false); 980 bool IsShowArrow() const; 981 void SaveArrowProperty(const RefPtr<FrameNode>& arrowNode); 982 RefPtr<FocusHub> GetFocusHubChild(std::string childFrameName); 983 WeakPtr<FocusHub> PreviousFocus(const RefPtr<FocusHub>& curFocusNode); 984 WeakPtr<FocusHub> NextFocus(const RefPtr<FocusHub>& curFocusNode); 985 void SetAccessibilityAction(); 986 bool NeedStartAutoPlay() const; 987 void CheckAndSetArrowHoverState(const PointF& mousePoint); 988 RectF GetArrowFrameRect(const int32_t index) const; 989 void UpdateAnimationProperty(float velocity); 990 void TriggerAnimationEndOnForceStop(bool isInterrupt = false); 991 void TriggerAnimationEndOnSwipeToLeft(); 992 void TriggerAnimationEndOnSwipeToRight(); 993 void TriggerEventOnFinish(int32_t nextIndex); 994 bool IsVisibleChildrenSizeLessThanSwiper() const; 995 void BeforeCreateLayoutWrapper() override; 996 int32_t CheckUserSetIndex(int32_t index); 997 998 void SetLazyForEachLongPredict(bool useLazyLoad) const; 999 void SetLazyLoadIsLoop() const; 1000 void SetLazyForEachFlag() const; 1001 float GetVelocityCoefficient(); 1002 int32_t ComputeNextIndexByVelocity(float velocity, bool onlyDistance = false) const; 1003 void UpdateCurrentIndex(int32_t index); 1004 void OnSpringAnimationStart(float velocity); 1005 void OnSpringAnimationFinish(); 1006 float EstimateSpringOffset(float realOffset); 1007 void OnSpringAndFadeAnimationFinish(); 1008 void OnFadeAnimationStart(); 1009 int32_t TotalDisPlayCount() const; 1010 void StopAndResetSpringAnimation(); 1011 void CheckLoopChange(); 1012 void StopSpringAnimationAndFlushImmediately(); 1013 void ResetAndUpdateIndexOnAnimationEnd(int32_t nextIndex); 1014 int32_t GetLoopIndex(int32_t index, int32_t childrenSize) const; 1015 bool IsAutoLinear() const; 1016 bool AutoLinearAnimationNeedReset(float translate) const; 1017 void TriggerCustomContentTransitionEvent(int32_t fromIndex, int32_t toIndex); 1018 /** 1019 * @brief Preprocess drag delta when received from DragUpdate event. 1020 * 1021 * Drag offset in Swiper can't go beyond a full page. Apply the restriction through this function. 1022 * 1023 * @param delta 1024 * @param mainSize content length along the main axis. 1025 * @param deltaSum accumulated delta in the current drag event. 1026 */ 1027 static void ProcessDelta(float& delta, float mainSize, float deltaSum); 1028 1029 /** 1030 * @brief Stops animations when the scroll starts. 1031 * 1032 * @param flushImmediately Whether to flush layout immediately. 1033 * @param stopLongPointAnimation Whether to stop indicator long point animation immediately. 1034 */ 1035 void StopAnimationOnScrollStart(bool flushImmediately, bool stopLongPointAnimation = false); 1036 /** 1037 * @return true if any translate animation (switching page / spring) is running, false otherwise. 1038 */ 1039 inline bool DuringTranslateAnimation() const; 1040 /** 1041 * @return true if any translate animation (switching page / spring) is running, ignore animation pause etc. 1042 */ 1043 inline bool RunningTranslateAnimation() const; 1044 /** 1045 * @return true if fade animation is running, false otherwise. 1046 */ 1047 inline bool DuringFadeAnimation() const; 1048 1049 /** 1050 * NestableScrollContainer implementations 1051 * ============================================================ 1052 */ GetAxis()1053 Axis GetAxis() const override 1054 { 1055 return GetDirection(); 1056 } 1057 1058 /** 1059 * @brief Closes gap to the edge, called before Swiper transfers extra offset to parent/child to ensure that Swiper 1060 * actually reaches the edge. 1061 * 1062 * @param offset The scroll offset from DragUpdate. 1063 */ 1064 void CloseTheGap(float& offset); 1065 1066 void HandleOutBoundarySelf(float offset, float& selfOffset, float& remainOffset); 1067 1068 ScrollResult HandleOutBoundary(float offset, int32_t source, float velocity); 1069 1070 ScrollResult HandleScroll( 1071 float offset, int32_t source, NestedState state = NestedState::GESTURE, float velocity = 0.f) override; 1072 1073 ScrollResult HandleScrollSelfFirst(float offset, int32_t source, NestedState state, float velocity = 0.f); 1074 1075 ScrollResult HandleScrollParentFirst(float offset, int32_t source, NestedState state, float velocity = 0.f); 1076 NestedScrollOutOfBoundary()1077 bool NestedScrollOutOfBoundary() override 1078 { 1079 return IsOutOfBoundary(); 1080 } 1081 1082 bool HandleScrollVelocity(float velocity, const RefPtr<NestableScrollContainer>& child = nullptr) override; 1083 1084 void OnScrollStartRecursive(WeakPtr<NestableScrollContainer> child, float position, float velocity) override; 1085 void OnScrollEndRecursive(const std::optional<float>& velocity) override; 1086 void OnScrollDragEndRecursive() override; 1087 1088 inline bool ChildFirst(NestedState state); 1089 RefPtr<FrameNode> GetCurrentFrameNode(int32_t currentIndex) const; 1090 bool FadeOverScroll(float offset); 1091 int32_t ComputeSwipePageNextIndex(float velocity, bool onlyDistance = false) const; 1092 int32_t ComputeNextIndexInSinglePage(float velocity, bool onlyDistance) const; 1093 int32_t ComputePageIndex(int32_t index) const; 1094 void UpdateIndexOnAnimationStop(); 1095 void UpdateIndexOnSwipePageStop(int32_t pauseTargetIndex); 1096 void AdjustCurrentFocusIndex(); 1097 bool IsContentFocused(); 1098 1099 int32_t CheckTargetIndex(int32_t targetIndex, bool isForceBackward = false); 1100 1101 void HandleTouchBottomLoop(); 1102 void HandleTouchBottomLoopOnRTL(); 1103 void CalculateGestureStateOnRTL(float additionalOffset, float currentTurnPageRate, int32_t preFirstIndex); 1104 void CalculateGestureState(float additionalOffset, float currentTurnPageRate, int32_t preFirstIndex); 1105 std::pair<float, float> CalcCurrentPageStatus(float additionalOffset) const; 1106 std::pair<float, float> CalcCurrentPageStatusOnRTL(float additionalOffset, bool isTouchBottom = false) const; 1107 1108 void PreloadItems(const std::set<int32_t>& indexSet); 1109 void DoTabsPreloadItems(const std::set<int32_t>& indexSet); 1110 void DoSwiperPreloadItems(const std::set<int32_t>& indexSet); 1111 void BuildForEachChild(const std::set<int32_t>& indexSet, const RefPtr<UINode>& child); 1112 void FirePreloadFinishEvent(int32_t errorCode, std::string message = ""); 1113 // capture node start 1114 void InitCapture(); GetLeftCaptureId()1115 int32_t GetLeftCaptureId() 1116 { 1117 if (!leftCaptureId_.has_value()) { 1118 leftCaptureId_ = ElementRegister::GetInstance()->MakeUniqueId(); 1119 } 1120 return leftCaptureId_.value(); 1121 } GetRightCaptureId()1122 int32_t GetRightCaptureId() 1123 { 1124 if (!rightCaptureId_.has_value()) { 1125 rightCaptureId_ = ElementRegister::GetInstance()->MakeUniqueId(); 1126 } 1127 return rightCaptureId_.value(); 1128 } RemoveAllCaptureNode()1129 void RemoveAllCaptureNode() 1130 { 1131 auto swiperNode = GetHost(); 1132 CHECK_NULL_VOID(swiperNode); 1133 swiperNode->RemoveChildAtIndex(swiperNode->GetChildIndexById(GetLeftCaptureId())); 1134 leftCaptureId_ = std::nullopt; 1135 swiperNode->RemoveChildAtIndex(swiperNode->GetChildIndexById(GetRightCaptureId())); 1136 rightCaptureId_ = std::nullopt; 1137 } GetLeftCaptureNode()1138 RefPtr<FrameNode> GetLeftCaptureNode() 1139 { 1140 auto swiperNode = GetHost(); 1141 CHECK_NULL_RETURN(swiperNode, nullptr); 1142 return DynamicCast<FrameNode>(swiperNode->GetChildAtIndex(swiperNode->GetChildIndexById(GetLeftCaptureId()))); 1143 } GetRightCaptureNode()1144 RefPtr<FrameNode> GetRightCaptureNode() 1145 { 1146 auto swiperNode = GetHost(); 1147 CHECK_NULL_RETURN(swiperNode, nullptr); 1148 return DynamicCast<FrameNode>(swiperNode->GetChildAtIndex(swiperNode->GetChildIndexById(GetRightCaptureId()))); 1149 } IsCaptureNodeValid()1150 bool IsCaptureNodeValid() 1151 { 1152 return hasCachedCapture_ && GetLeftCaptureNode() && GetRightCaptureNode(); 1153 } 1154 void UpdateTranslateForCaptureNode(const OffsetF& offset, bool cancel = false); 1155 void UpdateFinalTranslateForSwiperItem(const SwiperLayoutAlgorithm::PositionMap& itemPosition); 1156 void UpdateTranslateForSwiperItem(SwiperLayoutAlgorithm::PositionMap& itemPosition, 1157 const OffsetF& offset, bool cancel = false); 1158 void UpdateTargetCapture(bool forceUpdate); 1159 void CreateCaptureCallback(int32_t targetIndex, int32_t captureId, bool forceUpdate); 1160 void UpdateCaptureSource(std::shared_ptr<Media::PixelMap> pixelMap, int32_t captureId, int32_t targetIndex); 1161 SupportSwiperCustomAnimation()1162 bool SupportSwiperCustomAnimation() 1163 { 1164 auto swiperLayoutProperty = GetLayoutProperty<SwiperLayoutProperty>(); 1165 return (onSwiperCustomContentTransition_ || onContentDidScroll_) && !hasCachedCapture_ && 1166 SwiperUtils::IsStretch(swiperLayoutProperty); 1167 } 1168 1169 void ResetOnForceMeasure(); 1170 void ResetTabBar(); 1171 void UpdateTabBarIndicatorCurve(); 1172 bool CheckDragOutOfBoundary(double dragVelocity); 1173 void UpdateCurrentFocus(); 1174 1175 void CreateSpringProperty(); 1176 1177 std::optional<RefPtr<UINode>> FindLazyForEachNode(RefPtr<UINode> baseNode, bool isSelfNode = true) const; 1178 std::optional<RefPtr<UINode>> FindForEachNode(const RefPtr<UINode>& baseNode, bool isSelfNode = true) const; 1179 bool NeedForceMeasure() const; 1180 void SetIndicatorChangeIndexStatus(bool withAnimation, std::optional<int32_t> startIndex = std::nullopt); 1181 void SetIndicatorJumpIndex(std::optional<int32_t> jumpIndex); 1182 1183 void PostIdleTask(const RefPtr<FrameNode>& frameNode); 1184 1185 float AdjustIgnoreBlankOverScrollOffSet(bool isStartOverScroll) const; 1186 void UpdateIgnoreBlankOffsetWithIndex(); 1187 // overSrollDirection is true means over start boundary, false means over end boundary. 1188 void UpdateIgnoreBlankOffsetWithDrag(bool overSrollDirection); 1189 void UpdateIgnoreBlankOffsetInMap(float lastIgnoreBlankOffset); NeedEnableIgnoreBlankOffset()1190 bool NeedEnableIgnoreBlankOffset() const 1191 { 1192 return !IsLoop() && (prevMarginIgnoreBlank_ || nextMarginIgnoreBlank_) && TotalCount() > GetDisplayCount(); 1193 } 1194 1195 bool IsItemOverlay() const; 1196 void UpdateIndicatorOnChildChange(); 1197 void UpdateDigitalIndicator(); 1198 1199 void CheckSpecialItemCount() const; 1200 int32_t CheckIndexRange(int32_t index) const; 1201 void CheckAndFireCustomAnimation(); 1202 void UpdateOverlongForceStopPageRate(float forceStopPageRate); 1203 bool IsCachedShow() const; 1204 bool ContentWillScroll(int32_t currentIndex, int32_t comingIndex, float offset); 1205 bool CheckContentWillScroll(float checkValue, float mainDelta); 1206 float CalcWillScrollOffset(int32_t comingIndex); 1207 std::optional<bool> OnContentWillScroll(int32_t currentIndex, int32_t comingIndex, float offset) const; 1208 std::pair<int32_t, SwiperItemInfo> CalcFirstItemWithoutItemSpace() const; 1209 int32_t CalcComingIndex(float mainDelta) const; 1210 void TriggerAddTabBarEvent() const; 1211 1212 bool ComputeTargetIndex(int32_t index, int32_t& targetIndex) const; 1213 void FastAnimation(int32_t targetIndex); 1214 void MarkDirtyBindIndicatorNode() const; 1215 1216 RefPtr<FrameNode> GetCommonIndicatorNode(); IsIndicator(const std::string & tag)1217 bool IsIndicator(const std::string& tag) const 1218 { 1219 return tag == V2::SWIPER_INDICATOR_ETS_TAG || tag == V2::INDICATOR_ETS_TAG; 1220 } 1221 1222 void CheckAndReportEvent(); 1223 1224 void UpdateItemsLatestSwitched(); 1225 void HandleTabsCachedMaxCount(int32_t startIndex, int32_t endIndex); 1226 void PostIdleTaskToCleanTabContent(); 1227 std::shared_ptr<SwiperParameters> GetBindIndicatorParameters() const; 1228 1229 friend class SwiperHelper; 1230 1231 RefPtr<PanEvent> panEvent_; 1232 RefPtr<TouchEventImpl> touchEvent_; 1233 RefPtr<InputEvent> hoverEvent_; 1234 1235 // Control translate animation when drag end. 1236 RefPtr<Animator> controller_; 1237 1238 // Control spring animation when drag beyond boundary and drag end. 1239 std::shared_ptr<AnimationUtils::Animation> springAnimation_; 1240 1241 // Control fade animation when drag beyond boundary and drag end. 1242 std::shared_ptr<AnimationUtils::Animation> fadeAnimation_; 1243 1244 // Control translate animation for indicator. 1245 std::shared_ptr<AnimationUtils::Animation> indicatorAnimation_; 1246 1247 std::shared_ptr<AnimationUtils::Animation> translateAnimation_; 1248 1249 bool indicatorAnimationIsRunning_ = false; 1250 bool translateAnimationIsRunning_ = false; 1251 1252 // stop indicator animation callback 1253 std::function<void(bool)> stopIndicatorAnimationFunc_; 1254 std::function<void(float)> updateOverlongForceStopPageRateFunc_; 1255 1256 RefPtr<SwiperController> swiperController_; 1257 RefPtr<InputEvent> mouseEvent_; 1258 1259 bool isLastIndicatorFocused_ = false; 1260 int32_t startIndex_ = 0; 1261 int32_t endIndex_ = 0; 1262 int32_t oldIndex_ = 0; 1263 int32_t nextIndex_ = 0; 1264 int32_t prevStartIndex_ = 0; 1265 int32_t prevEndIndex_ = 0; 1266 1267 PanDirection panDirection_; 1268 1269 float currentOffset_ = 0.0f; 1270 float fadeOffset_ = 0.0f; 1271 float springOffset_ = 0.0f; 1272 float turnPageRate_ = 0.0f; 1273 float groupTurnPageRate_ = 0.0f; 1274 float translateAnimationEndPos_ = 0.0f; 1275 TouchBottomTypeLoop touchBottomType_ = TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_NONE; 1276 float touchBottomRate_ = 1.0f; 1277 int32_t gestureSwipeIndex_ = 0; 1278 int32_t currentFirstIndex_ = 0; 1279 int32_t nextValidIndex_ = 0; 1280 int32_t currentFocusIndex_ = 0; 1281 int32_t selectedIndex_ = -1; 1282 int32_t unselectedIndex_ = -1; 1283 1284 bool moveDirection_ = false; 1285 bool indicatorDoingAnimation_ = false; 1286 bool isInit_ = true; 1287 bool hasVisibleChangeRegistered_ = false; 1288 bool isVisible_ = true; 1289 bool isVisibleArea_ = false; 1290 bool isWindowShow_ = true; 1291 bool isCustomSize_ = false; 1292 bool indicatorIsBoolean_ = true; 1293 bool isAtHotRegion_ = false; 1294 bool needTurn_ = false; 1295 bool isParentHiddenChange_ = false; 1296 /** 1297 * @brief Indicates whether the child NestableScrollContainer is currently scrolling and affecting Swiper. 1298 */ 1299 bool childScrolling_ = false; 1300 bool isTouchDown_ = false; 1301 bool isTouchDownOnOverlong_ = false; 1302 std::optional<bool> preLoop_; 1303 1304 ChangeEventPtr changeEvent_; 1305 ChangeEventPtr onIndexChangeEvent_; 1306 ChangeEventPtr selectedEvent_; 1307 ChangeEventPtr unselectedEvent_; 1308 AnimationStartEventPtr animationStartEvent_; 1309 AnimationEndEventPtr animationEndEvent_; 1310 1311 mutable std::shared_ptr<SwiperParameters> swiperParameters_; 1312 mutable std::shared_ptr<SwiperDigitalParameters> swiperDigitalParameters_; 1313 1314 WeakPtr<FrameNode> lastWeakShowNode_; 1315 1316 CancelableCallback<void()> translateTask_; 1317 CancelableCallback<void()> resetLayoutTask_; 1318 1319 std::optional<int32_t> indicatorId_; 1320 std::optional<int32_t> leftButtonId_; 1321 std::optional<int32_t> rightButtonId_; 1322 std::optional<int32_t> leftCaptureId_; 1323 std::optional<int32_t> rightCaptureId_; 1324 std::optional<SwiperIndicatorType> lastSwiperIndicatorType_; 1325 1326 float startMainPos_ = 0.0f; 1327 float endMainPos_ = 0.0f; 1328 float contentMainSize_ = 0.0f; 1329 float oldContentMainSize_ = 0.0f; 1330 float contentCrossSize_ = 0.0f; 1331 bool crossMatchChild_ = false; 1332 1333 std::optional<int32_t> uiCastJumpIndex_; 1334 std::optional<int32_t> jumpIndex_; 1335 std::optional<int32_t> runningTargetIndex_; 1336 std::optional<int32_t> pauseTargetIndex_; 1337 std::optional<int32_t> oldChildrenSize_; 1338 std::optional<int32_t> oldRealTotalCount_; 1339 std::optional<float> placeItemWidth_; 1340 float currentDelta_ = 0.0f; 1341 // cumulated delta in a single drag event 1342 float mainDeltaSum_ = 0.0f; 1343 std::optional<float> velocity_; 1344 bool mainSizeIsMeasured_ = false; 1345 bool propertyAnimationIsRunning_ = false; 1346 bool syncCancelAniIsFailed_ = false; 1347 bool springAnimationIsRunning_ = false; 1348 bool isTouchDownSpringAnimation_ = false; 1349 bool isTouchDownFadeAnimation_ = false; 1350 bool isUserFinish_ = true; 1351 bool isVoluntarilyClear_ = false; 1352 bool isIndicatorLongPress_ = false; 1353 bool isTouchPad_ = false; 1354 bool fadeAnimationIsRunning_ = false; 1355 bool autoLinearReachBoundary_ = false; 1356 bool needAdjustIndex_ = false; 1357 bool isIndicatorInteractive_ = true; 1358 bool nextMarginIgnoreBlank_ = false; 1359 bool prevMarginIgnoreBlank_ = false; 1360 float ignoreBlankOffset_ = 0.0f; 1361 int32_t swiperId_ = -1; 1362 float animationCurveStiffness_ = SWIPER_CURVE_STIFFNESS; 1363 float animationCurveDamping_ = SWIPER_CURVE_DAMPING; 1364 1365 std::optional<int32_t> cachedCount_; 1366 1367 std::optional<int32_t> surfaceChangedCallbackId_; 1368 1369 WindowSizeChangeReason windowSizeChangeReason_ = WindowSizeChangeReason::UNDEFINED; 1370 std::vector<RefPtr<ScrollingListener>> scrollingListener_; 1371 FinishCallbackType finishCallbackType_ = FinishCallbackType::REMOVED; 1372 1373 CustomContentTransitionPtr onTabsCustomContentTransition_; 1374 std::shared_ptr<SwiperContentAnimatedTransition> onSwiperCustomContentTransition_; 1375 std::shared_ptr<ContentDidScrollEvent> onContentDidScroll_; 1376 std::shared_ptr<ContentWillScrollEvent> onContentWillScroll_; 1377 std::set<int32_t> indexsInAnimation_; 1378 std::set<int32_t> needUnmountIndexs_; 1379 std::optional<int32_t> customAnimationToIndex_; 1380 RefPtr<TabContentTransitionProxy> currentProxyInAnimation_; 1381 PaddingPropertyF tabsPaddingAndBorder_; 1382 std::map<int32_t, bool> indexCanChangeMap_; 1383 // capture 1384 std::optional<int32_t> leftCaptureIndex_; 1385 std::optional<int32_t> rightCaptureIndex_; 1386 bool hasCachedCapture_ = false; 1387 bool isCaptureReverse_ = false; 1388 OffsetF captureFinalOffset_; 1389 bool isInAutoPlay_ = false; 1390 1391 bool needFireCustomAnimationEvent_ = true; 1392 // Indicates whether previous frame animation is running, only used on swiper custom animation. 1393 bool prevFrameAnimationRunning_ = false; 1394 std::optional<bool> isSwipeByGroup_; 1395 std::set<WeakPtr<FrameNode>> groupedItems_; 1396 1397 std::set<int32_t> cachedItems_; 1398 LayoutConstraintF layoutConstraint_; 1399 bool requestLongPredict_ = false; 1400 1401 PageFlipMode pageFlipMode_ = PageFlipMode::CONTINUOUS; 1402 bool jumpOnChange_ = false; 1403 TabAnimateMode tabAnimationMode_ = TabAnimateMode::NO_ANIMATION; 1404 bool isFirstAxisAction_ = true; 1405 bool stopWhenTouched_ = true; 1406 WeakPtr<NG::UINode> indicatorNode_; 1407 bool isBindIndicator_ = false; 1408 1409 SwiperHoverFlag hoverFlag_ = HOVER_NONE; 1410 GestureStatus gestureStatus_ = GestureStatus::INIT; 1411 1412 std::list<int32_t> itemsLatestSwitched_; 1413 std::set<int32_t> itemsNeedClean_; 1414 }; 1415 } // namespace OHOS::Ace::NG 1416 1417 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_SWIPER_PATTERN_H 1418