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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TAB_BAR_TAB_CONTROLLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TAB_BAR_TAB_CONTROLLER_H
18 
19 #include <map>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/event/ace_event_helper.h"
23 #include "core/pipeline/base/element.h"
24 
25 namespace OHOS::Ace {
26 using TabBarChangeListener = std::function<void(int32_t)>;
27 
28 class ACE_FORCE_EXPORT TabController : public AceType {
29     DECLARE_ACE_TYPE(TabController, AceType);
30 
31 public:
32     explicit TabController(int32_t id);
33     ~TabController() override = default;
34 
35     static RefPtr<TabController> GetController(int32_t id);
36 
37     int32_t GetId() const;
38     int32_t GetIndex() const;
39 
SetTotalCount(int32_t totalCount)40     void SetTotalCount(int32_t totalCount)
41     {
42         totalCount_ = totalCount;
43     }
GetTotalCount()44     int32_t GetTotalCount() const
45     {
46         return totalCount_;
47     }
48 
GetInitialIndex()49     int32_t GetInitialIndex() const
50     {
51         return initialIndex_;
52     }
53 
GetPendingIndex()54     int32_t GetPendingIndex() const
55     {
56         return pendingIndex_;
57     }
58 
59     void ValidateIndex(int32_t maxIndex);
60     void SetPageReady(bool ready);
61     void SetIndex(int32_t index);
62     void SetInitialIndex(int32_t index);
63     void SetPendingIndex(int32_t index);
64     void SetIndexWithoutChangeContent(int32_t index);
65     void SetIndexByController(int32_t index, bool blockEvent = true);
66     void SetIndicatorByScrollContent(double percent, int32_t newIndex, bool needChange);
67     void SetIndexByScrollContent(int32_t index);
68     void SetContentElement(const RefPtr<Element>& contentElement);
69     void SetBarElement(const RefPtr<Element>& barElement);
70     void ChangeDispatch(int32_t index);
71     bool IsIndexDefined() const;
72 
OnTabBarChanged(int32_t index)73     void OnTabBarChanged(int32_t index)
74     {
75         if (tabBarChangeListener_) {
76             tabBarChangeListener_(index);
77         }
78     }
SetTabBarChangeListener(const TabBarChangeListener & listener)79     void SetTabBarChangeListener(const TabBarChangeListener& listener)
80     {
81         tabBarChangeListener_ = listener;
82     }
83 
84 private:
85     int32_t id_ = -1;
86     int32_t index_ = 0;
87     int32_t initialIndex_ = -1;
88     int32_t pendingIndex_ = -1;
89     int32_t totalCount_ = 0;
90     bool pageReady_ = false;
91     bool indexDefined_ = false;
92     WeakPtr<Element> contentElement_;
93     WeakPtr<Element> barElement_;
94     TabBarChangeListener tabBarChangeListener_;
95 };
96 } // namespace OHOS::Ace
97 
98 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TAB_BAR_TAB_CONTROLLER_H
99