1 /*
2 * Copyright (c) 2022 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 #include "bridge/declarative_frontend/jsview/models/tab_content_model_impl.h"
17
18 #include "base/utils/utils.h"
19 #include "bridge/declarative_frontend/jsview/js_container_base.h"
20 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
21 #include "bridge/declarative_frontend/jsview/js_utils.h"
22 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
23 #include "bridge/declarative_frontend/view_stack_processor.h"
24 #include "core/components/tab_bar/tab_bar_component.h"
25 #include "core/components/tab_bar/tab_content_component.h"
26 #include "core/components/tab_bar/tab_theme.h"
27 #include "core/components_v2/tabs/tabs_component.h"
28
29 namespace OHOS::Ace::Framework {
30 namespace {
31
32 constexpr char DEFAULT_TAB_BAR_NAME[] = "TabBar";
33
34 } // namespace
35
Create()36 void TabContentModelImpl::Create()
37 {
38 std::list<RefPtr<Component>> components;
39 auto tabContentItemComponent = AceType::MakeRefPtr<V2::TabContentItemComponent>(components);
40 tabContentItemComponent->SetCrossAxisSize(CrossAxisSize::MAX);
41 ViewStackProcessor::GetInstance()->ClaimElementId(tabContentItemComponent);
42
43 RefPtr<V2::TabsComponent> tabsComponent = nullptr;
44 tabsComponent = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetTopTabs());
45 CHECK_NULL_VOID(tabsComponent);
46 // GetTabsComponent used only by JSTabContent::SetTabBar
47 // To Find TabBarComponent eventually
48 tabContentItemComponent->SetTabsComponent(AceType::WeakClaim(AceType::RawPtr(tabsComponent)));
49
50 auto tabBar = tabsComponent->GetTabBarChild();
51 tabBar->AppendChild(CreateTabBarLabelComponent(tabContentItemComponent, std::string(DEFAULT_TAB_BAR_NAME)));
52
53 ViewStackProcessor::GetInstance()->Push(tabContentItemComponent);
54 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
55 CHECK_NULL_VOID(box);
56 box->SetBoxClipFlag(true);
57 }
58
Create(std::function<void ()> && deepRenderFunc)59 void TabContentModelImpl::Create(std::function<void()>&& deepRenderFunc)
60 {
61 std::list<RefPtr<Component>> components;
62 auto tabContentItemComponent = AceType::MakeRefPtr<V2::TabContentItemComponent>(components);
63 tabContentItemComponent->SetCrossAxisSize(CrossAxisSize::MAX);
64 ViewStackProcessor::GetInstance()->ClaimElementId(tabContentItemComponent);
65
66 RefPtr<V2::TabsComponent> tabsComponent = nullptr;
67 ViewStackProcessor::GetInstance()->Push(tabContentItemComponent);
68 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
69 if (box) {
70 box->SetBoxClipFlag(true);
71 }
72
73 auto jsWrapperFunc = [builder = std::move(deepRenderFunc)]() -> RefPtr<Component> {
74 CHECK_NULL_RETURN(builder, nullptr);
75 builder();
76 return ViewStackProcessor::GetInstance()->Finish();
77 };
78 tabContentItemComponent->SetBuilder(std::move(jsWrapperFunc));
79 }
80
Pop()81 void TabContentModelImpl::Pop()
82 {
83 JSContainerBase::Pop();
84 }
85
SetTabBar(const std::optional<std::string> & text,const std::optional<std::string> & icon,const std::optional<TabBarSymbol> & tabBarSymbol,std::function<void ()> && builder,bool useContentOnly)86 void TabContentModelImpl::SetTabBar(const std::optional<std::string>& text, const std::optional<std::string>& icon,
87 const std::optional<TabBarSymbol>& tabBarSymbol, std::function<void()>&& builder, bool useContentOnly)
88 {
89 auto tabContentItemComponent =
90 AceType::DynamicCast<V2::TabContentItemComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
91 CHECK_NULL_VOID(tabContentItemComponent);
92
93 auto weakTabs = tabContentItemComponent->GetTabsComponent();
94 // Full update: tabs and tabBar always exist
95 // Partial update: tabs and tabBar exist for initial render and nullptr for rerender
96 auto tabs = weakTabs.Upgrade();
97 auto tabBar = tabs ? tabs->GetTabBarChild() : nullptr;
98
99 if (!Container::IsCurrentUsePartialUpdate()) {
100 CHECK_NULL_VOID(tabs);
101 CHECK_NULL_VOID(tabBar);
102 }
103
104 RefPtr<Component> tabBarChild = nullptr;
105 if (useContentOnly) {
106 auto textVal = text.value_or(DEFAULT_TAB_BAR_NAME);
107 if (!Container::IsCurrentUsePartialUpdate()) {
108 tabBarChild = CreateTabBarLabelComponent(tabContentItemComponent, textVal);
109 } else {
110 tabContentItemComponent->SetBarText(textVal);
111 }
112 } else {
113 // For Partial Update ProcessTabBarXXX methods
114 // do not create any components for the tab bar items
115 // and return nullptr.
116 // Tab bar items created and added later by
117 // TabContentItemElementProxy class.
118 if (builder) {
119 tabBarChild = ProcessTabBarBuilderFunction(tabContentItemComponent, std::move(builder));
120 // Update tabBar always for full update and for initial render only for partial update
121 if (tabBar) {
122 tabBar->ResetIndicator();
123 tabBar->SetAlignment(Alignment::TOP_LEFT);
124 }
125 } else if (text.has_value() && icon.has_value()) {
126 tabBarChild = ProcessTabBarTextIconPair(tabContentItemComponent, text, icon);
127 } else if (text.has_value() && !icon.has_value()) {
128 tabBarChild = ProcessTabBarLabel(tabContentItemComponent, text);
129 }
130 }
131
132 if (!Container::IsCurrentUsePartialUpdate()) {
133 auto defaultTabChild = tabBar->GetChildren().back();
134 tabBar->RemoveChildDirectly(defaultTabChild);
135 tabBar->AppendChild(tabBarChild);
136 return;
137 }
138
139 // Partial Update only
140 if (tabContentItemComponent->GetBarElementId() == ElementRegister::UndefinedElementId) {
141 const auto id = ElementRegister::GetInstance()->MakeUniqueId();
142 tabContentItemComponent->SetBarElementId(id);
143 }
144 }
145
ProcessTabBarBuilderFunction(RefPtr<V2::TabContentItemComponent> & tabContent,std::function<void ()> && builderFunc)146 RefPtr<Component> TabContentModelImpl::ProcessTabBarBuilderFunction(
147 RefPtr<V2::TabContentItemComponent>& tabContent, std::function<void()>&& builderFunc)
148 {
149 CHECK_NULL_RETURN(builderFunc, nullptr);
150 tabContent->SetBarText("custom");
151 if (Container::IsCurrentUsePartialUpdate()) {
152 auto jsWrapperFunc = [builder = std::move(builderFunc)]() -> RefPtr<Component> {
153 builder();
154 return ViewStackProcessor::GetInstance()->Finish();
155 };
156 tabContent->SetBarBuilder(jsWrapperFunc);
157 return nullptr;
158 }
159
160 ScopedViewStackProcessor builderViewStackProcessor;
161 builderFunc();
162 RefPtr<Component> builderGeneratedRootComponent = ViewStackProcessor::GetInstance()->Finish();
163 return builderGeneratedRootComponent;
164 }
165
CreateTabBarLabelComponent(RefPtr<V2::TabContentItemComponent> & tabContent,const std::string & labelStr)166 RefPtr<Component> TabContentModelImpl::CreateTabBarLabelComponent(
167 RefPtr<V2::TabContentItemComponent>& tabContent, const std::string& labelStr)
168 {
169 tabContent->SetBarText(labelStr);
170 return TabBarItemComponent::BuildWithTextIcon(labelStr, std::string());
171 }
172
ProcessTabBarLabel(RefPtr<V2::TabContentItemComponent> & tabContent,const std::optional<std::string> & textVal)173 RefPtr<Component> TabContentModelImpl::ProcessTabBarLabel(
174 RefPtr<V2::TabContentItemComponent>& tabContent, const std::optional<std::string>& textVal)
175 {
176 std::string textStr = textVal.value_or(DEFAULT_TAB_BAR_NAME);
177 tabContent->SetBarText(textStr);
178
179 if (!Container::IsCurrentUsePartialUpdate()) {
180 return CreateTabBarLabelComponent(tabContent, textStr);
181 }
182 return nullptr;
183 }
184
ProcessTabBarTextIconPair(RefPtr<V2::TabContentItemComponent> & tabContent,const std::optional<std::string> & textVal,const std::optional<std::string> & iconVal)185 RefPtr<Component> TabContentModelImpl::ProcessTabBarTextIconPair(RefPtr<V2::TabContentItemComponent>& tabContent,
186 const std::optional<std::string>& textVal, const std::optional<std::string>& iconVal)
187 {
188 if (!iconVal.has_value()) {
189 return ProcessTabBarLabel(tabContent, textVal);
190 }
191
192 auto textStr = textVal.value_or(DEFAULT_TAB_BAR_NAME);
193 tabContent->SetBarText(textStr);
194 tabContent->SetBarIcon(iconVal.value());
195
196 if (!Container::IsCurrentUsePartialUpdate()) {
197 return TabBarItemComponent::BuildWithTextIcon(textStr, iconVal.value());
198 }
199 return nullptr;
200 }
201
202 } // namespace OHOS::Ace::Framework
203