1 /*
2 * Copyright (c) 2021-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 "core/components/tab_bar/tab_controller.h"
17
18 #include "core/common/container.h"
19 #include "core/components/tab_bar/render_tab_bar.h"
20 #include "core/components/tab_bar/tab_bar_element.h"
21 #include "core/components/tab_bar/tab_content_element.h"
22
23 namespace OHOS::Ace {
24
GetController(int32_t id)25 RefPtr<TabController> TabController::GetController(int32_t id)
26 {
27 return AceType::MakeRefPtr<TabController>(id);
28 }
29
TabController(int32_t id)30 TabController::TabController(int32_t id)
31 {
32 id_ = id;
33 }
34
ValidateIndex(int32_t maxIndex)35 void TabController::ValidateIndex(int32_t maxIndex)
36 {
37 if (pageReady_ && index_ > maxIndex) {
38 index_ = 0;
39 if (barElement_.Upgrade()) {
40 auto tabBar = AceType::DynamicCast<TabBarElement>(barElement_.Upgrade());
41 if (tabBar) {
42 tabBar->UpdateIndex(0);
43 }
44 }
45 }
46 }
47
SetPageReady(bool ready)48 void TabController::SetPageReady(bool ready)
49 {
50 pageReady_ = ready;
51 }
52
SetIndex(int32_t index)53 void TabController::SetIndex(int32_t index)
54 {
55 // There can be different tab at the same index
56 if ((index_ == index && !Container::IsCurrentUsePartialUpdate()) || index < 0) {
57 LOGD("SetIndex: Input index is not valid, %{public}d, %{public}d", index_, index);
58 return;
59 }
60 indexDefined_ = true;
61
62 index_ = index;
63 if (contentElement_.Upgrade()) {
64 auto tabContent = AceType::DynamicCast<TabContentElement>(contentElement_.Upgrade());
65 if (tabContent) {
66 tabContent->ChangeByBar(index);
67 }
68 }
69 }
70
SetInitialIndex(int32_t index)71 void TabController::SetInitialIndex(int32_t index)
72 {
73 if (initialIndex_ == index || index < 0) {
74 LOGD("SetInitialIndex: Input index is not valid, %{public}d, %{public}d", initialIndex_, index);
75 return;
76 }
77 initialIndex_ = index;
78 }
79
SetIndexWithoutChangeContent(int32_t index)80 void TabController::SetIndexWithoutChangeContent(int32_t index)
81 {
82 if (index_ == index || index < 0) {
83 LOGD("SetIndexWithoutChangeContent: Input index is not valid, %{public}d, %{public}d", index_, index);
84 return;
85 }
86 indexDefined_ = true;
87 index_ = index;
88 }
89
SetPendingIndex(int32_t index)90 void TabController::SetPendingIndex(int32_t index)
91 {
92 if (pendingIndex_ == index || index < 0) {
93 LOGD("SetPendingIndex: Input index is not valid, %{public}d, %{public}d", pendingIndex_, index);
94 return;
95 }
96 pendingIndex_ = index;
97 indexDefined_ = false;
98 }
99
SetIndexByController(int32_t index,bool blockEvent)100 void TabController::SetIndexByController(int32_t index, bool blockEvent)
101 {
102 if (index_ == index || index < 0) {
103 LOGD("SetIndexByController: Input index is not valid, %{public}d, %{public}d", index_, index);
104 return;
105 }
106 if (index >= totalCount_) {
107 LOGD("index is large than total, %{public}d, %{public}d", index, totalCount_);
108 SetPendingIndex(index);
109 return;
110 }
111 indexDefined_ = true;
112 if (barElement_.Upgrade()) {
113 auto tabBar = AceType::DynamicCast<TabBarElement>(barElement_.Upgrade());
114 if (tabBar) {
115 auto renderTabBar = AceType::DynamicCast<RenderTabBar>(tabBar->GetRenderNode());
116 if (renderTabBar && renderTabBar->GetTabsSize() < index) {
117 LOGW("Input index is not valid.");
118 return;
119 }
120 tabBar->UpdateIndex(index);
121 }
122 }
123
124 index_ = index;
125 if (contentElement_.Upgrade()) {
126 auto tabContent = AceType::DynamicCast<TabContentElement>(contentElement_.Upgrade());
127 if (tabContent) {
128 tabContent->ChangeByBar(index, blockEvent);
129 }
130 }
131 }
132
ChangeDispatch(int32_t index)133 void TabController::ChangeDispatch(int32_t index)
134 {
135 if (contentElement_.Upgrade()) {
136 LOGD("tab controller dispatch domChange event");
137 auto tabContent = AceType::DynamicCast<TabContentElement>(contentElement_.Upgrade());
138 if (tabContent) {
139 tabContent->ChangeDispatch(index);
140 }
141 }
142 }
143
SetIndicatorByScrollContent(double percent,int32_t newIndex,bool needChange)144 void TabController::SetIndicatorByScrollContent(double percent, int32_t newIndex, bool needChange)
145 {
146 if (barElement_.Upgrade()) {
147 auto tabBar = AceType::DynamicCast<TabBarElement>(barElement_.Upgrade());
148 if (tabBar) {
149 tabBar->UpdateScrollIndicator(percent, newIndex, needChange);
150 }
151 }
152 }
153
SetIndexByScrollContent(int32_t index)154 void TabController::SetIndexByScrollContent(int32_t index)
155 {
156 if (index_ == index || index < 0) {
157 LOGD("SetIndexByScrollContent: Input index is not valid, %{public}d, %{public}d", index_, index);
158 return;
159 }
160 indexDefined_ = true;
161 index_ = index;
162 if (barElement_.Upgrade()) {
163 auto tabBar = AceType::DynamicCast<TabBarElement>(barElement_.Upgrade());
164 if (tabBar) {
165 tabBar->UpdateIndex(index);
166 }
167 }
168 }
169
SetContentElement(const RefPtr<Element> & contentElement)170 void TabController::SetContentElement(const RefPtr<Element>& contentElement)
171 {
172 contentElement_ = contentElement;
173 }
174
SetBarElement(const RefPtr<Element> & barElement)175 void TabController::SetBarElement(const RefPtr<Element>& barElement)
176 {
177 barElement_ = barElement;
178 }
179
GetId() const180 int32_t TabController::GetId() const
181 {
182 return id_;
183 }
184
GetIndex() const185 int32_t TabController::GetIndex() const
186 {
187 return index_;
188 }
189
IsIndexDefined() const190 bool TabController::IsIndexDefined() const
191 {
192 return indexDefined_;
193 }
194
195 } // namespace OHOS::Ace
196