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