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_TABS_TABS_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TABS_PATTERN_H
18 
19 #include <optional>
20 
21 #include "base/geometry/axis.h"
22 #include "base/memory/referenced.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_ng/event/event_hub.h"
25 #include "core/components_ng/pattern/pattern.h"
26 #include "core/components_ng/pattern/swiper/swiper_event_hub.h"
27 #include "core/components_ng/pattern/swiper/swiper_model.h"
28 #include "core/components_ng/pattern/tabs/tab_bar_pattern.h"
29 #include "core/components_ng/pattern/tabs/tabs_layout_algorithm.h"
30 #include "core/components_ng/pattern/tabs/tabs_layout_property.h"
31 
32 namespace OHOS::Ace::NG {
33 
34 class TabsPattern : public Pattern {
35     DECLARE_ACE_TYPE(TabsPattern, Pattern);
36 
37 public:
38     TabsPattern() = default;
39     ~TabsPattern() override = default;
40 
IsAtomicNode()41     bool IsAtomicNode() const override
42     {
43         return false;
44     }
45 
IsNeedPercent()46     bool IsNeedPercent() const override
47     {
48         return true;
49     }
50 
CreateLayoutProperty()51     RefPtr<LayoutProperty> CreateLayoutProperty() override
52     {
53         return MakeRefPtr<TabsLayoutProperty>();
54     }
55 
CreateLayoutAlgorithm()56     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
57     {
58         return MakeRefPtr<TabsLayoutAlgorithm>();
59     }
60 
GetFocusPattern()61     FocusPattern GetFocusPattern() const override
62     {
63         return { FocusType::SCOPE, true };
64     }
65 
66     ScopeFocusAlgorithm GetScopeFocusAlgorithm() override;
67 
68     void SetOnChangeEvent(std::function<void(const BaseEventInfo*)>&& event);
69 
GetChangeEvent()70     ChangeEventWithPreIndexPtr GetChangeEvent()
71     {
72         return onChangeEvent_;
73     }
74 
75     void SetOnTabBarClickEvent(std::function<void(const BaseEventInfo*)>&& event);
76 
77     void SetAnimationStartEvent(AnimationStartEvent&& event);
78 
79     void SetAnimationEndEvent(AnimationEndEvent&& event);
80 
GetTabBarClickEvent()81     ChangeEventPtr GetTabBarClickEvent()
82     {
83         return onTabBarClickEvent_;
84     }
85 
86     void OnModifyDone() override;
87 
88     std::string ProvideRestoreInfo() override;
89 
90     void OnRestoreInfo(const std::string& restoreInfo) override;
91 
92     void AddInnerOnGestureRecognizerJudgeBegin(GestureRecognizerJudgeFunc&& gestureRecognizerJudgeFunc) override;
93 
94     void SetOnIndexChangeEvent(std::function<void(const BaseEventInfo*)>&& event);
95 
GetIndexChangeEvent()96     ChangeEventPtr GetIndexChangeEvent()
97     {
98         return onIndexChangeEvent_;
99     }
100 
GetIsCustomAnimation()101     bool GetIsCustomAnimation() const
102     {
103         return isCustomAnimation_;
104     }
105 
SetIsCustomAnimation(bool isCustomAnimation)106     void SetIsCustomAnimation(bool isCustomAnimation)
107     {
108         isCustomAnimation_ = isCustomAnimation;
109     }
110 
SetIsDisableSwipe(bool isDisableSwipe)111     void SetIsDisableSwipe(bool isDisableSwipe)
112     {
113         isDisableSwipe_ = isDisableSwipe;
114     }
115 
SetOnContentWillChange(std::function<bool (int32_t,int32_t)> && callback)116     void SetOnContentWillChange(std::function<bool(int32_t, int32_t)>&& callback)
117     {
118         callback_ = callback;
119     }
120 
OnContentWillChange(int32_t preIndex,int32_t index)121     std::optional<bool> OnContentWillChange(int32_t preIndex, int32_t index) const
122     {
123         std::optional<bool> ret;
124         if (callback_) {
125             ret = callback_(preIndex, index);
126         }
127         return ret;
128     }
129 
SetInterceptStatus(bool status)130     void SetInterceptStatus(bool status)
131     {
132         interceptStatus_ = status;
133     }
134 
GetInterceptStatus()135     bool GetInterceptStatus() const
136     {
137         return interceptStatus_;
138     }
139 
SetAnimateMode(TabAnimateMode mode)140     void SetAnimateMode(TabAnimateMode mode)
141     {
142         animateMode_ = mode;
143     }
144 
GetAnimateMode()145     TabAnimateMode GetAnimateMode() const
146     {
147         return animateMode_;
148     }
149 
150     void HandleChildrenUpdated(const RefPtr<FrameNode>& swiperNode, const RefPtr<FrameNode>& tabBarNode);
151 
152     void UpdateSelectedState(const RefPtr<FrameNode>& tabBarNode, const RefPtr<FrameNode>& swiperNode,
153         const RefPtr<TabBarPattern>& tabBarPattern, const RefPtr<TabsLayoutProperty>& tabsLayoutProperty, int index);
154 
155 private:
156     void OnAttachToFrameNode() override;
157     void OnAfterModifyDone() override;
158     void OnUpdateShowDivider();
159     WeakPtr<FocusHub> GetNextFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode);
160     void BeforeCreateLayoutWrapper() override;
161     std::string GetTabBarTextByIndex(int32_t index) const;
162     void UpdateSwiperDisableSwipe(bool disableSwipe);
163     void SetSwiperPaddingAndBorder();
164     void RecordChangeEvent(int32_t index);
165     void FireTabContentStateCallback(int32_t oldIndex, int32_t nextIndex) const;
166 
167     bool isCustomAnimation_ = false;
168     bool isDisableSwipe_ = false;
169     bool isInit_ = true;
170 
171     TabAnimateMode animateMode_ = TabAnimateMode::CONTENT_FIRST;
172     ChangeEventWithPreIndexPtr onChangeEvent_;
173     ChangeEventPtr onTabBarClickEvent_;
174     ChangeEventPtr onIndexChangeEvent_;
175     AnimationStartEventPtr animationStartEvent_;
176     AnimationEndEventPtr animationEndEvent_;
177     std::function<bool(int32_t, int32_t)> callback_;
178     bool interceptStatus_ = false;
179 };
180 
181 } // namespace OHOS::Ace::NG
182 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TABS_PATTERN_H
183