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/tabs_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 Dimension DEFAULT_TAB_BAR_HEIGHT = 56.0_vp;
33
34 } // namespace
35
Create(BarPosition barPosition,int32_t,const RefPtr<TabController> & tabController,const RefPtr<SwiperController> &)36 void TabsModelImpl::Create(BarPosition barPosition, int32_t /*index*/, const RefPtr<TabController>& tabController,
37 const RefPtr<SwiperController>& /*swiperController*/)
38 {
39 std::list<RefPtr<Component>> children;
40 auto tabsComponent = AceType::MakeRefPtr<V2::TabsComponent>(children, barPosition, tabController);
41 auto tabBar = tabsComponent->GetTabBarChild();
42 if (tabBar) {
43 tabBar->InitStyle(GetTheme());
44 auto box = AceType::DynamicCast<BoxComponent>(tabBar->GetParent().Upgrade());
45 if (box) {
46 box->SetHeight(DEFAULT_TAB_BAR_HEIGHT);
47 }
48 }
49 ViewStackProcessor::GetInstance()->PushTabs(tabsComponent);
50 ViewStackProcessor::GetInstance()->Push(tabsComponent);
51 }
52
Pop()53 void TabsModelImpl::Pop()
54 {
55 ViewStackProcessor::GetInstance()->PopTabs();
56 JSContainerBase::Pop();
57 }
58
SetIndex(int32_t index)59 void TabsModelImpl::SetIndex(int32_t index)
60 {
61 auto component = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
62 CHECK_NULL_VOID(component);
63 auto controller = component->GetTabsController();
64 CHECK_NULL_VOID(controller);
65 controller->SetPendingIndex(index);
66 }
67
SetTabBarPosition(BarPosition tabBarPosition)68 void TabsModelImpl::SetTabBarPosition(BarPosition tabBarPosition)
69 {
70 auto component = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
71 CHECK_NULL_VOID(component);
72 auto tabBar = component->GetTabBarChild();
73 CHECK_NULL_VOID(tabBar);
74 tabBar->SetBarPosition(tabBarPosition);
75 }
76
SetTabBarMode(TabBarMode tabBarMode)77 void TabsModelImpl::SetTabBarMode(TabBarMode tabBarMode)
78 {
79 auto component = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
80 CHECK_NULL_VOID(component);
81 auto tabBar = component->GetTabBarChild();
82 CHECK_NULL_VOID(tabBar);
83 tabBar->SetMode(tabBarMode);
84 }
85
SetTabBarWidth(const Dimension & tabBarWidth)86 void TabsModelImpl::SetTabBarWidth(const Dimension& tabBarWidth)
87 {
88 auto component = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
89 CHECK_NULL_VOID(component);
90 auto tabBar = component->GetTabBarChild();
91 CHECK_NULL_VOID(tabBar);
92 auto box = AceType::DynamicCast<BoxComponent>(tabBar->GetParent().Upgrade());
93 CHECK_NULL_VOID(box);
94 box->SetWidth(tabBarWidth);
95 }
96
SetTabBarHeight(const Dimension & tabBarHeight)97 void TabsModelImpl::SetTabBarHeight(const Dimension& tabBarHeight)
98 {
99 auto component = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
100 CHECK_NULL_VOID(component);
101 auto tabBar = component->GetTabBarChild();
102 CHECK_NULL_VOID(tabBar);
103 auto box = AceType::DynamicCast<BoxComponent>(tabBar->GetParent().Upgrade());
104 CHECK_NULL_VOID(box);
105 box->SetHeight(tabBarHeight);
106 }
107
SetIsVertical(bool isVertical)108 void TabsModelImpl::SetIsVertical(bool isVertical)
109 {
110 auto tabsComponent = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
111 CHECK_NULL_VOID(tabsComponent);
112 if (isVertical) {
113 tabsComponent->SetDirection(FlexDirection::ROW);
114 } else {
115 tabsComponent->SetDirection(FlexDirection::COLUMN);
116 }
117 auto tabBar = tabsComponent->GetTabBarChild();
118 if (tabBar) {
119 tabBar->SetVertical(isVertical);
120 }
121 auto tabContent = tabsComponent->GetTabContentChild();
122 if (tabContent) {
123 tabContent->SetVertical(isVertical);
124 }
125 }
126
SetScrollable(bool scrollable)127 void TabsModelImpl::SetScrollable(bool scrollable)
128 {
129 auto component = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
130 CHECK_NULL_VOID(component);
131 auto tabContent = component->GetTabContentChild();
132 CHECK_NULL_VOID(tabContent);
133 tabContent->SetScrollable(scrollable);
134 }
135
SetAnimationDuration(float duration)136 void TabsModelImpl::SetAnimationDuration(float duration)
137 {
138 auto component = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
139 CHECK_NULL_VOID(component);
140 auto tabContent = component->GetTabContentChild();
141 CHECK_NULL_VOID(tabContent);
142 tabContent->SetScrollDuration(duration);
143 }
144
SetOnChange(std::function<void (const BaseEventInfo *)> && onChange)145 void TabsModelImpl::SetOnChange(std::function<void(const BaseEventInfo*)>&& onChange)
146 {
147 auto component = AceType::DynamicCast<V2::TabsComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
148 CHECK_NULL_VOID(component);
149 auto tabContent = component->GetTabContentChild();
150 CHECK_NULL_VOID(tabContent);
151 tabContent->SetChangeEventId(EventMarker(std::move(onChange)));
152 }
153
GetTheme() const154 RefPtr<TabTheme> TabsModelImpl::GetTheme() const
155 {
156 auto pipelineContext = PipelineContext::GetCurrentContext();
157 CHECK_NULL_RETURN(pipelineContext, nullptr);
158 auto themeManager = pipelineContext->GetThemeManager();
159 CHECK_NULL_RETURN(themeManager, nullptr);
160 return themeManager->GetTheme<TabTheme>();
161 }
162
163 } // namespace OHOS::Ace::Framework
164