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