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