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_TAB_CONTENT_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TAB_CONTENT_PATTERN_H 18 19 #include "base/log/dump_log.h" 20 #include "base/memory/referenced.h" 21 #include "base/utils/noncopyable.h" 22 #include "core/components_ng/event/event_hub.h" 23 #include "core/components_ng/pattern/pattern.h" 24 #include "core/components_ng/pattern/tabs/tabs_pattern.h" 25 #include "core/components_ng/pattern/tabs/tab_bar_pattern.h" 26 #include "core/components_ng/pattern/tabs/tab_content_event_hub.h" 27 #include "core/components_ng/pattern/tabs/tab_content_layout_property.h" 28 #include "core/components_ng/syntax/shallow_builder.h" 29 30 namespace OHOS::Ace::NG { 31 32 class ACE_EXPORT TabContentPattern : public Pattern { 33 DECLARE_ACE_TYPE(TabContentPattern, Pattern); 34 35 public: TabContentPattern(const RefPtr<ShallowBuilder> & shallowBuilder)36 explicit TabContentPattern(const RefPtr<ShallowBuilder>& shallowBuilder) 37 : shallowBuilder_(shallowBuilder), tabBarParam_(std::string(""), std::string(""), nullptr) 38 {} 39 ~TabContentPattern() override = default; 40 OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)41 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override 42 { 43 if (shallowBuilder_ && !shallowBuilder_->IsExecuteDeepRenderDone()) { 44 auto host = GetHost(); 45 CHECK_NULL_RETURN(host, false); 46 host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF); 47 return true; 48 } 49 return false; 50 } 51 IsAtomicNode()52 bool IsAtomicNode() const override 53 { 54 return false; 55 } 56 IsMeasureBoundary()57 bool IsMeasureBoundary() const override 58 { 59 return false; 60 } 61 UsResRegion()62 bool UsResRegion() override 63 { 64 return false; 65 } 66 OnAttachToFrameNode()67 void OnAttachToFrameNode() override 68 { 69 auto host = GetHost(); 70 CHECK_NULL_VOID(host); 71 host->GetRenderContext()->UpdateClipEdge(true); 72 FireWillShowEvent(); 73 } 74 CheckTabAnimateMode()75 void CheckTabAnimateMode() 76 { 77 if (!shallowBuilder_ || !firstTimeLayout_) { 78 return; 79 } 80 81 // Check whether current tabcontent belongs to tab component. 82 auto tabContentNode = GetHost(); 83 CHECK_NULL_VOID(tabContentNode); 84 auto parentNode = tabContentNode->GetAncestorNodeOfFrame(); 85 CHECK_NULL_VOID(parentNode); 86 if (parentNode->GetTag() != V2::SWIPER_ETS_TAG) { 87 return; 88 } 89 auto grandParentNode = parentNode->GetAncestorNodeOfFrame(); 90 CHECK_NULL_VOID(grandParentNode); 91 if (grandParentNode->GetTag() != V2::TABS_ETS_TAG) { 92 return; 93 } 94 95 auto tabsPattern = grandParentNode->GetPattern<TabsPattern>(); 96 CHECK_NULL_VOID(tabsPattern); 97 auto tabsLayoutProperty = grandParentNode->GetLayoutProperty<TabsLayoutProperty>(); 98 CHECK_NULL_VOID(tabsLayoutProperty); 99 TabAnimateMode mode = tabsPattern->GetAnimateMode(); 100 if (mode == TabAnimateMode::ACTION_FIRST 101 && !tabsLayoutProperty->GetHeightAutoValue(false) 102 && !tabsLayoutProperty->GetWidthAutoValue(false)) { 103 ACE_SCOPED_TRACE("TabContentMarkRenderDone"); 104 /* 105 * Set render done only when tab's height&weight is not set to 'auto' and its animateMode 106 * is set to AnimateMode::ACTION_FIRST. 107 * Set render done before first layout, so to measure tabcontent and its children 108 * in two seperate frame. 109 */ 110 shallowBuilder_->MarkIsExecuteDeepRenderDone(true); 111 } 112 } 113 BeforeCreateLayoutWrapper()114 void BeforeCreateLayoutWrapper() override 115 { 116 if (firstTimeLayout_) { 117 CheckTabAnimateMode(); 118 firstTimeLayout_ = false; 119 } 120 121 if (shallowBuilder_ && !shallowBuilder_->IsExecuteDeepRenderDone()) { 122 shallowBuilder_->ExecuteDeepRender(); 123 shallowBuilder_.Reset(); 124 } else if (shallowBuilder_ && shallowBuilder_->IsExecuteDeepRenderDone()) { 125 auto pipeline = PipelineContext::GetCurrentContextSafely(); 126 if (!pipeline) { 127 shallowBuilder_->MarkIsExecuteDeepRenderDone(false); 128 return; 129 } 130 131 pipeline->AddAfterRenderTask([weak = WeakClaim(this), shallowBuilder = shallowBuilder_]() { 132 CHECK_NULL_VOID(shallowBuilder); 133 shallowBuilder->MarkIsExecuteDeepRenderDone(false); 134 auto pattern = weak.Upgrade(); 135 CHECK_NULL_VOID(pattern); 136 auto host = pattern->GetHost(); 137 CHECK_NULL_VOID(host); 138 host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF); 139 }); 140 } 141 } 142 CreateLayoutProperty()143 RefPtr<LayoutProperty> CreateLayoutProperty() override 144 { 145 return MakeRefPtr<TabContentLayoutProperty>(); 146 } 147 SetTabBar(const std::string & text,const std::string & icon,const std::optional<TabBarSymbol> & tabBarSymbol,TabBarBuilderFunc && builder)148 void SetTabBar(const std::string& text, const std::string& icon, 149 const std::optional<TabBarSymbol>& tabBarSymbol, TabBarBuilderFunc&& builder) 150 { 151 tabBarParam_.SetText(text); 152 tabBarParam_.SetIcon(icon); 153 tabBarParam_.SetSymbol(tabBarSymbol); 154 if (tabBarSymbol.has_value()) { 155 symbol_ = tabBarSymbol.value(); 156 } 157 tabBarParam_.SetBuilder(move(builder)); 158 } 159 GetSymbol()160 const TabBarSymbol& GetSymbol() 161 { 162 return symbol_; 163 } 164 GetTabBarParam()165 const TabBarParam& GetTabBarParam() const 166 { 167 return tabBarParam_; 168 } 169 SetCustomTabBar(FrameNode * node)170 void SetCustomTabBar(FrameNode* node) 171 { 172 tabBarParam_.SetCustomNode(node); 173 } 174 SetTabBarStyle(TabBarStyle tabBarStyle)175 void SetTabBarStyle(TabBarStyle tabBarStyle) 176 { 177 tabBarParam_.SetTabBarStyle(tabBarStyle); 178 } 179 GetTabBarStyle()180 TabBarStyle GetTabBarStyle() const 181 { 182 return tabBarParam_.GetTabBarStyle(); 183 } 184 SetIndicatorStyle(const IndicatorStyle & indicatorStyle)185 void SetIndicatorStyle(const IndicatorStyle& indicatorStyle) 186 { 187 indicatorStyle_ = indicatorStyle; 188 } 189 GetIndicatorStyle()190 const IndicatorStyle& GetIndicatorStyle() const 191 { 192 return indicatorStyle_; 193 } 194 SetSelectedMode(SelectedMode selectedMode)195 void SetSelectedMode(SelectedMode selectedMode) 196 { 197 selectedMode_ = selectedMode; 198 } 199 GetSelectedMode()200 SelectedMode GetSelectedMode() const 201 { 202 return selectedMode_; 203 } 204 SetBoardStyle(const BoardStyle & boardStyle)205 void SetBoardStyle(const BoardStyle& boardStyle) 206 { 207 boardStyle_ = boardStyle; 208 } 209 GetBoardStyle()210 const BoardStyle& GetBoardStyle() const 211 { 212 return boardStyle_; 213 } 214 SetLabelStyle(const LabelStyle & labelStyle)215 void SetLabelStyle(const LabelStyle& labelStyle) 216 { 217 labelStyle_ = labelStyle; 218 } 219 GetLabelStyle()220 const LabelStyle& GetLabelStyle() const 221 { 222 return labelStyle_; 223 } 224 SetIconStyle(const IconStyle & iconStyle)225 void SetIconStyle(const IconStyle& iconStyle) 226 { 227 iconStyle_ = iconStyle; 228 } 229 GetIconStyle()230 const IconStyle& GetIconStyle() const 231 { 232 return iconStyle_; 233 } 234 SetPadding(const PaddingProperty & padding)235 void SetPadding(const PaddingProperty& padding) 236 { 237 padding_ = padding; 238 } 239 GetPadding()240 const PaddingProperty& GetPadding() const 241 { 242 return padding_; 243 } 244 SetUseLocalizedPadding(bool useLocalizedPadding)245 void SetUseLocalizedPadding(bool useLocalizedPadding) 246 { 247 useLocalizedPadding_ = useLocalizedPadding; 248 } 249 GetUseLocalizedPadding()250 bool GetUseLocalizedPadding() 251 { 252 return useLocalizedPadding_; 253 } 254 SetSymmetricExtensible(bool isExtensible)255 void SetSymmetricExtensible(bool isExtensible) 256 { 257 bottomTabBarStyle_.symmetricExtensible = isExtensible; 258 } 259 SetLayoutMode(LayoutMode layoutMode)260 void SetLayoutMode(LayoutMode layoutMode) 261 { 262 bottomTabBarStyle_.layoutMode = layoutMode; 263 } 264 SetVerticalAlign(FlexAlign verticalAlign)265 void SetVerticalAlign(FlexAlign verticalAlign) 266 { 267 bottomTabBarStyle_.verticalAlign = verticalAlign; 268 } 269 SetId(const std::string & id)270 void SetId(const std::string& id) 271 { 272 tabBarInspectorId_ = id; 273 } 274 GetId()275 const std::string& GetId() const 276 { 277 return tabBarInspectorId_; 278 } 279 GetBottomTabBarStyle()280 const BottomTabBarStyle& GetBottomTabBarStyle() const 281 { 282 return bottomTabBarStyle_; 283 } 284 DumpAdvanceInfo()285 void DumpAdvanceInfo() override 286 { 287 switch (selectedMode_) { 288 case SelectedMode::INDICATOR: { 289 DumpLog::GetInstance().AddDesc("SelectedMode:INDICATOR"); 290 break; 291 } 292 case SelectedMode::BOARD: { 293 DumpLog::GetInstance().AddDesc("SelectedMode:BOARD"); 294 break; 295 } 296 default: { 297 break; 298 } 299 } 300 } 301 CreateEventHub()302 RefPtr<EventHub> CreateEventHub() override 303 { 304 return MakeRefPtr<TabContentEventHub>(); 305 } 306 FireWillShowEvent()307 void FireWillShowEvent() 308 { 309 auto tabContentEventHub = GetEventHub<TabContentEventHub>(); 310 CHECK_NULL_VOID(tabContentEventHub); 311 tabContentEventHub->FireWillShowEvent(); 312 } 313 FireWillHideEvent()314 void FireWillHideEvent() 315 { 316 auto tabContentEventHub = GetEventHub<TabContentEventHub>(); 317 CHECK_NULL_VOID(tabContentEventHub); 318 tabContentEventHub->FireWillHideEvent(); 319 } 320 HasSubTabBarStyleNode()321 bool HasSubTabBarStyleNode() const 322 { 323 return customStyleNode_ != nullptr; 324 } 325 SetCustomStyleNode(const RefPtr<FrameNode> & customStyleNode)326 void SetCustomStyleNode(const RefPtr<FrameNode>& customStyleNode) 327 { 328 customStyleNode_ = customStyleNode; 329 } 330 FireCustomStyleNode()331 const RefPtr<FrameNode>& FireCustomStyleNode() const 332 { 333 return customStyleNode_; 334 } 335 336 private: 337 RefPtr<ShallowBuilder> shallowBuilder_; 338 TabBarParam tabBarParam_; 339 IndicatorStyle indicatorStyle_; 340 SelectedMode selectedMode_ = SelectedMode::INDICATOR; 341 BoardStyle boardStyle_; 342 LabelStyle labelStyle_; 343 IconStyle iconStyle_; 344 PaddingProperty padding_; 345 std::string tabBarInspectorId_; 346 BottomTabBarStyle bottomTabBarStyle_; 347 RefPtr<FrameNode> customStyleNode_ = nullptr; 348 TabBarSymbol symbol_; 349 350 bool firstTimeLayout_ = true; 351 bool useLocalizedPadding_ = false; 352 353 ACE_DISALLOW_COPY_AND_MOVE(TabContentPattern); 354 }; 355 356 } // namespace OHOS::Ace::NG 357 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TAB_CONTENT_PATTERN_H 358