• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
81     void SetOnSelectedEvent(std::function<void(const BaseEventInfo*)>&& event);
82 
83     void SetOnUnselectedEvent(std::function<void(const BaseEventInfo*)>&& event);
84 
GetTabBarClickEvent()85     ChangeEventPtr GetTabBarClickEvent()
86     {
87         return onTabBarClickEvent_;
88     }
89 
90     void OnModifyDone() override;
91 
92     std::string ProvideRestoreInfo() override;
93 
94     void OnRestoreInfo(const std::string& restoreInfo) override;
95 
96     void AddInnerOnGestureRecognizerJudgeBegin(GestureRecognizerJudgeFunc&& gestureRecognizerJudgeFunc) override;
97 
98     void RecoverInnerOnGestureRecognizerJudgeBegin() override;
99 
100     void SetOnIndexChangeEvent(std::function<void(const BaseEventInfo*)>&& event);
101 
GetIndexChangeEvent()102     ChangeEventPtr GetIndexChangeEvent()
103     {
104         return onIndexChangeEvent_;
105     }
106 
GetIsCustomAnimation()107     bool GetIsCustomAnimation() const
108     {
109         return isCustomAnimation_;
110     }
111 
SetIsCustomAnimation(bool isCustomAnimation)112     void SetIsCustomAnimation(bool isCustomAnimation)
113     {
114         isCustomAnimation_ = isCustomAnimation;
115     }
116 
SetIsDisableSwipe(bool isDisableSwipe)117     void SetIsDisableSwipe(bool isDisableSwipe)
118     {
119         isDisableSwipe_ = isDisableSwipe;
120     }
121 
SetOnContentWillChange(std::function<bool (int32_t,int32_t)> && callback)122     void SetOnContentWillChange(std::function<bool(int32_t, int32_t)>&& callback)
123     {
124         callback_ = callback;
125     }
126 
OnContentWillChange(int32_t preIndex,int32_t index)127     std::optional<bool> OnContentWillChange(int32_t preIndex, int32_t index) const
128     {
129         std::optional<bool> ret;
130         if (callback_) {
131             ret = callback_(preIndex, index);
132         }
133         return ret;
134     }
135 
SetInterceptStatus(bool status)136     void SetInterceptStatus(bool status)
137     {
138         interceptStatus_ = status;
139     }
140 
GetInterceptStatus()141     bool GetInterceptStatus() const
142     {
143         return interceptStatus_;
144     }
145 
146     void SetAnimateMode(TabAnimateMode mode);
147 
GetAnimateMode()148     TabAnimateMode GetAnimateMode() const
149     {
150         return animateMode_;
151     }
152 
153     void HandleChildrenUpdated(const RefPtr<FrameNode>& swiperNode, const RefPtr<FrameNode>& tabBarNode);
154 
155     void UpdateSelectedState(const RefPtr<FrameNode>& swiperNode, const RefPtr<TabBarPattern>& tabBarPattern,
156         const RefPtr<TabsLayoutProperty>& tabsLayoutProperty, int index);
157 
UpdateDividerStrokeWidth()158     void UpdateDividerStrokeWidth()
159     {
160         OnUpdateShowDivider();
161     }
UpdateDividerColor()162     void UpdateDividerColor()
163     {
164         OnUpdateShowDivider();
165     }
166 
167     void OnColorModeChange(uint32_t colorMode) override;
168 
169 private:
170     void OnAttachToFrameNode() override;
171     void OnAfterModifyDone() override;
172     void OnUpdateShowDivider();
173     WeakPtr<FocusHub> GetNextFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode);
174     void BeforeCreateLayoutWrapper() override;
175     std::string GetTabBarTextByIndex(int32_t index) const;
176     void UpdateSwiperDisableSwipe(bool disableSwipe);
177     void SetSwiperPaddingAndBorder();
178     void RecordChangeEvent(int32_t index);
179     void FireTabContentStateCallback(int32_t oldIndex, int32_t nextIndex) const;
180     void UpdateIndex(const RefPtr<FrameNode>& tabsNode, const RefPtr<FrameNode>& tabBarNode,
181         const RefPtr<FrameNode>& swiperNode, const RefPtr<TabsLayoutProperty>& tabsLayoutProperty);
182     void InitFocusEvent();
183     RefPtr<FocusHub> GetCurrentFocusNode(FocusIntension intension);
184     void InitAccessibilityZIndex();
185 
186     bool isCustomAnimation_ = false;
187     bool isDisableSwipe_ = false;
188     bool isInit_ = true;
189 
190     TabAnimateMode animateMode_ = TabAnimateMode::CONTENT_FIRST;
191     ChangeEventWithPreIndexPtr onChangeEvent_;
192     ChangeEventPtr selectedEvent_;
193     ChangeEventPtr unselectedEvent_;
194     ChangeEventPtr onTabBarClickEvent_;
195     ChangeEventPtr onIndexChangeEvent_;
196     AnimationStartEventPtr animationStartEvent_;
197     AnimationEndEventPtr animationEndEvent_;
198     std::function<bool(int32_t, int32_t)> callback_;
199     bool interceptStatus_ = false;
200     BarPosition barPosition_ = BarPosition::END; // default accessibilityZIndex is consistent with BarPosition::END
201 };
202 
203 } // namespace OHOS::Ace::NG
204 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TABS_PATTERN_H
205