• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,std::function<void ()> && builder,bool useContentOnly)86 void TabContentModelImpl::SetTabBar(const std::optional<std::string>& text, const std::optional<std::string>& icon,
87     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         LOGD("Setting ID for tab bar item to %{public}d tabContentItemComponent id %{public}d", id,
144             tabContentItemComponent->GetBarElementId());
145     }
146 }
147 
ProcessTabBarBuilderFunction(RefPtr<V2::TabContentItemComponent> & tabContent,std::function<void ()> && builderFunc)148 RefPtr<Component> TabContentModelImpl::ProcessTabBarBuilderFunction(
149     RefPtr<V2::TabContentItemComponent>& tabContent, std::function<void()>&& builderFunc)
150 {
151     CHECK_NULL_RETURN(builderFunc, nullptr);
152     tabContent->SetBarText("custom");
153     if (Container::IsCurrentUsePartialUpdate()) {
154         auto jsWrapperFunc = [builder = std::move(builderFunc)]() -> RefPtr<Component> {
155             builder();
156             return ViewStackProcessor::GetInstance()->Finish();
157         };
158         tabContent->SetBarBuilder(jsWrapperFunc);
159         return nullptr;
160     }
161 
162     ScopedViewStackProcessor builderViewStackProcessor;
163     builderFunc();
164     RefPtr<Component> builderGeneratedRootComponent = ViewStackProcessor::GetInstance()->Finish();
165     return builderGeneratedRootComponent;
166 }
167 
CreateTabBarLabelComponent(RefPtr<V2::TabContentItemComponent> & tabContent,const std::string & labelStr)168 RefPtr<Component> TabContentModelImpl::CreateTabBarLabelComponent(
169     RefPtr<V2::TabContentItemComponent>& tabContent, const std::string& labelStr)
170 {
171     tabContent->SetBarText(labelStr);
172     return TabBarItemComponent::BuildWithTextIcon(labelStr, std::string());
173 }
174 
ProcessTabBarLabel(RefPtr<V2::TabContentItemComponent> & tabContent,const std::optional<std::string> & textVal)175 RefPtr<Component> TabContentModelImpl::ProcessTabBarLabel(
176     RefPtr<V2::TabContentItemComponent>& tabContent, const std::optional<std::string>& textVal)
177 {
178     std::string textStr = textVal.value_or(DEFAULT_TAB_BAR_NAME);
179     tabContent->SetBarText(textStr);
180 
181     if (!Container::IsCurrentUsePartialUpdate()) {
182         return CreateTabBarLabelComponent(tabContent, textStr);
183     }
184     return nullptr;
185 }
186 
ProcessTabBarTextIconPair(RefPtr<V2::TabContentItemComponent> & tabContent,const std::optional<std::string> & textVal,const std::optional<std::string> & iconVal)187 RefPtr<Component> TabContentModelImpl::ProcessTabBarTextIconPair(RefPtr<V2::TabContentItemComponent>& tabContent,
188     const std::optional<std::string>& textVal, const std::optional<std::string>& iconVal)
189 {
190     if (!iconVal.has_value()) {
191         return ProcessTabBarLabel(tabContent, textVal);
192     }
193 
194     auto textStr = textVal.value_or(DEFAULT_TAB_BAR_NAME);
195     tabContent->SetBarText(textStr);
196     tabContent->SetBarIcon(iconVal.value());
197 
198     if (!Container::IsCurrentUsePartialUpdate()) {
199         return TabBarItemComponent::BuildWithTextIcon(textStr, iconVal.value());
200     }
201     return nullptr;
202 }
203 
204 } // namespace OHOS::Ace::Framework
205