• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #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_model.h"
27 #include "core/components_ng/pattern/tabs/tabs_layout_algorithm.h"
28 #include "core/components_ng/pattern/tabs/tabs_layout_property.h"
29 
30 namespace OHOS::Ace::NG {
31 
32 class TabsPattern : public Pattern {
33     DECLARE_ACE_TYPE(TabsPattern, Pattern);
34 
35 public:
36     using ChangeEvent = std::function<void(int32_t)>;
37     using ChangeEventPtr = std::shared_ptr<ChangeEvent>;
38 
39     TabsPattern() = default;
40     ~TabsPattern() override = default;
41 
IsAtomicNode()42     bool IsAtomicNode() const override
43     {
44         return false;
45     }
46 
CreateLayoutProperty()47     RefPtr<LayoutProperty> CreateLayoutProperty() override
48     {
49         return MakeRefPtr<TabsLayoutProperty>();
50     }
51 
CreateLayoutAlgorithm()52     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
53     {
54         return MakeRefPtr<TabsLayoutAlgorithm>();
55     }
56 
GetFocusPattern()57     FocusPattern GetFocusPattern() const override
58     {
59         return { FocusType::SCOPE, true };
60     }
61 
62     ScopeFocusAlgorithm GetScopeFocusAlgorithm() override;
63 
64     void SetOnChangeEvent(std::function<void(const BaseEventInfo*)>&& event);
65 
GetChangeEvent()66     ChangeEventPtr GetChangeEvent()
67     {
68         return onChangeEvent_;
69     }
70 
71     void SetOnTabBarClickEvent(std::function<void(const BaseEventInfo*)>&& event);
72 
73     void SetAnimationStartEvent(AnimationStartEvent&& event);
74 
75     void SetAnimationEndEvent(AnimationEndEvent&& event);
76 
GetTabBarClickEvent()77     ChangeEventPtr GetTabBarClickEvent()
78     {
79         return onTabBarClickEvent_;
80     }
81 
82     void OnModifyDone() override;
83 
84     std::string ProvideRestoreInfo() override;
85 
86     void OnRestoreInfo(const std::string& restoreInfo) override;
87 
88     void SetOnIndexChangeEvent(std::function<void(const BaseEventInfo*)>&& event);
89 
GetIndexChangeEvent()90     ChangeEventPtr GetIndexChangeEvent()
91     {
92         return onIndexChangeEvent_;
93     }
94 
GetIsCustomAnimation()95     bool GetIsCustomAnimation() const
96     {
97         return isCustomAnimation_;
98     }
99 
SetIsCustomAnimation(bool isCustomAnimation)100     void SetIsCustomAnimation(bool isCustomAnimation)
101     {
102         isCustomAnimation_ = isCustomAnimation;
103     }
104 
SetIsDisableSwipe(bool isDisableSwipe)105     void SetIsDisableSwipe(bool isDisableSwipe)
106     {
107         isDisableSwipe_ = isDisableSwipe;
108     }
109 
SetOnContentWillChange(std::function<bool (int32_t,int32_t)> && callback)110     void SetOnContentWillChange(std::function<bool(int32_t, int32_t)>&& callback)
111     {
112         callback_ = callback;
113     }
114 
OnContentWillChange(int32_t preIndex,int32_t index)115     std::optional<bool> OnContentWillChange(int32_t preIndex, int32_t index) const
116     {
117         std::optional<bool> ret;
118         if (callback_) {
119             ret = callback_(preIndex, index);
120         }
121         return ret;
122     }
123 
SetInterceptStatus(bool status)124     void SetInterceptStatus(bool status)
125     {
126         interceptStatus_ = status;
127     }
128 
GetInterceptStatus()129     bool GetInterceptStatus() const
130     {
131         return interceptStatus_;
132     }
133 
134 private:
135     void OnAttachToFrameNode() override;
136     void OnAfterModifyDone() override;
137     void OnUpdateShowDivider();
138     WeakPtr<FocusHub> GetNextFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode);
139     void BeforeCreateLayoutWrapper() override;
140     std::string GetTabBarTextByIndex(int32_t index) const;
141     void UpdateSwiperDisableSwipe(bool disableSwipe);
142     void SetSwiperPaddingAndBorder();
143 
144     bool isCustomAnimation_ = false;
145     bool isDisableSwipe_ = false;
146 
147     ChangeEventPtr onChangeEvent_;
148     ChangeEventPtr onTabBarClickEvent_;
149     ChangeEventPtr onIndexChangeEvent_;
150     AnimationStartEventPtr animationStartEvent_;
151     AnimationEndEventPtr animationEndEvent_;
152     std::function<bool(int32_t, int32_t)> callback_;
153     bool interceptStatus_ = false;
154 };
155 
156 } // namespace OHOS::Ace::NG
157 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TABS_PATTERN_H
158