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 140 void SetAnimateMode(TabAnimateMode mode); 141 GetAnimateMode()142 TabAnimateMode GetAnimateMode() const 143 { 144 return animateMode_; 145 } 146 147 void HandleChildrenUpdated(const RefPtr<FrameNode>& swiperNode, const RefPtr<FrameNode>& tabBarNode); 148 149 void UpdateSelectedState(const RefPtr<FrameNode>& swiperNode, const RefPtr<TabBarPattern>& tabBarPattern, 150 const RefPtr<TabsLayoutProperty>& tabsLayoutProperty, int index); 151 152 private: 153 void OnAttachToFrameNode() override; 154 void OnAfterModifyDone() override; 155 void OnUpdateShowDivider(); 156 WeakPtr<FocusHub> GetNextFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode); 157 void BeforeCreateLayoutWrapper() override; 158 std::string GetTabBarTextByIndex(int32_t index) const; 159 void UpdateSwiperDisableSwipe(bool disableSwipe); 160 void SetSwiperPaddingAndBorder(); 161 void RecordChangeEvent(int32_t index); 162 void FireTabContentStateCallback(int32_t oldIndex, int32_t nextIndex) const; 163 void UpdateIndex(const RefPtr<FrameNode>& tabsNode, const RefPtr<FrameNode>& tabBarNode, 164 const RefPtr<FrameNode>& swiperNode, const RefPtr<TabsLayoutProperty>& tabsLayoutProperty); 165 void InitFocusEvent(); 166 RefPtr<FocusHub> GetCurrentFocusNode(FocusIntension intension); 167 void SetLastWeakFocusNode(const RefPtr<FrameNode>& tabsNode, const RefPtr<FrameNode>& tabBarNode, 168 const RefPtr<TabsLayoutProperty>& tabsLayoutProperty, int32_t index); 169 170 bool isCustomAnimation_ = false; 171 bool isDisableSwipe_ = false; 172 bool isInit_ = true; 173 174 TabAnimateMode animateMode_ = TabAnimateMode::CONTENT_FIRST; 175 ChangeEventWithPreIndexPtr onChangeEvent_; 176 ChangeEventPtr onTabBarClickEvent_; 177 ChangeEventPtr onIndexChangeEvent_; 178 AnimationStartEventPtr animationStartEvent_; 179 AnimationEndEventPtr animationEndEvent_; 180 std::function<bool(int32_t, int32_t)> callback_; 181 bool interceptStatus_ = false; 182 }; 183 184 } // namespace OHOS::Ace::NG 185 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TABS_PATTERN_H 186