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_CONTENT_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TAB_BAR_TAB_CONTENT_ELEMENT_H
18 
19 #include <list>
20 #include <unordered_map>
21 
22 #include "core/components/tab_bar/tab_controller.h"
23 #include "core/focus/focus_node.h"
24 #include "core/pipeline/base/component_group_element.h"
25 
26 namespace OHOS::Ace {
27 
28 class TabContentElement : public ComponentGroupElement, public FocusGroup {
29     DECLARE_ACE_TYPE(TabContentElement, ComponentGroupElement, FocusGroup);
30 
31 public:
32     explicit TabContentElement(const std::list<RefPtr<Component>>& contents);
33     ~TabContentElement() override = default;
34 
35     void PerformBuild() override;
36     void Update() override;
37     void ChangeByContent(int32_t index);
38     virtual void ChangeByBar(int32_t index, bool isFromController = false);
39     virtual void PrepareContent(int32_t index);
40     void IndicatorByContent(double percent, int32_t newIndex, bool needChange);
41     void ChangeDispatch(int32_t index);
42     RefPtr<Element> GetTabContentChild(int32_t index) const;
43 
44     void OnFocus() override;
45     bool RequestNextFocus(bool vertical, bool reverse, const Rect& rect) override;
46     bool IsFocusable() const override;
47 
GetTabController()48     RefPtr<TabController> GetTabController() const
49     {
50         return controller_;
51     }
52 
53 protected:
54     RefPtr<RenderNode> CreateRenderNode() override;
55     void UpdateLastFocusNode();
56     RefPtr<FocusNode> GetCurrentFocusNode() const;
57 
58     std::list<RefPtr<Component>> contents_;
59     std::unordered_map<int32_t, RefPtr<Element>> childMap_;
60     std::unordered_map<int32_t, RefPtr<FocusNode>> focusChildMap_;
61     std::unordered_set<int32_t> focusIndexMap_;
62     RefPtr<TabController> controller_;
63 
64     // new content index, requested by tab bar
65     int32_t newBarIndex_ = -1;
66     // new content index, requested by drag
67     int32_t newIndex_ = -1;
68     int32_t lastIndex_ = -1;
69     bool fromController_ = false;
70     bool newComponentBuild_ = false;
71 };
72 
73 } // namespace OHOS::Ace
74 
75 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TAB_BAR_TAB_CONTENT_ELEMENT_H
76