• 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 #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_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 SetIndexByScrollContent(int32_t index);
67     void SetContentElement(const RefPtr<Element>& contentElement);
68     void SetBarElement(const RefPtr<Element>& barElement);
69     void ChangeDispatch(int32_t index);
70     bool IsIndexDefined() const;
71 
OnTabBarChanged(int32_t index)72     void OnTabBarChanged(int32_t index)
73     {
74         if (tabBarChangeListener_) {
75             tabBarChangeListener_(index);
76         }
77     }
SetTabBarChangeListener(const TabBarChangeListener & listener)78     void SetTabBarChangeListener(const TabBarChangeListener& listener)
79     {
80         tabBarChangeListener_ = listener;
81     }
82 
83 private:
84     int32_t id_ = -1;
85     int32_t index_ = 0;
86     int32_t initialIndex_ = -1;
87     int32_t pendingIndex_ = -1;
88     int32_t totalCount_ = 0;
89     bool pageReady_ = false;
90     bool indexDefined_ = false;
91     WeakPtr<Element> contentElement_;
92     WeakPtr<Element> barElement_;
93     TabBarChangeListener tabBarChangeListener_;
94 };
95 } // namespace OHOS::Ace
96 
97 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TAB_BAR_TAB_CONTROLLER_H
98