1 /*
2  * Copyright (c) 2021 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_INDEXER_INDEXER_ITEM_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_INDEXER_INDEXER_ITEM_COMPONENT_H
18 
19 #include "base/utils/string_utils.h"
20 #include "core/components/box/box_component.h"
21 #include "core/components/image/image_component.h"
22 #include "core/components/text/text_component.h"
23 
24 namespace OHOS::Ace {
25 
26 class IndexerItemComponent : public SoleChildComponent {
27     DECLARE_ACE_TYPE(IndexerItemComponent, SoleChildComponent);
28 
29 public:
IndexerItemComponent()30     IndexerItemComponent()
31     {
32         strSection_ = StringUtils::Str8ToStr16("");
33         strLabel_ = StringUtils::Str8ToStr16("");
34         BuildItem();
35     }
36 
37     IndexerItemComponent(
38         const std::u16string& strSection, const std::u16string& strLabel, bool circleMode, bool rotate = false)
39         : strSection_(strSection), strLabel_(strLabel), circleMode_(circleMode), rotate_(rotate)
40     {
41         BuildItem();
42     }
43 
44     IndexerItemComponent(const std::u16string& strSection, const std::u16string& strLabel, const Dimension& itemSize,
45         bool circleMode, bool rotate = false)
46         : strSection_(strSection), strLabel_(strLabel), itemSize_(itemSize), circleMode_(circleMode), rotate_(rotate)
47     {
48         BuildItem();
49     }
50 
51     ~IndexerItemComponent() override = default;
52     RefPtr<RenderNode> CreateRenderNode() override;
53 
54     void BuildItem();
55 
56     bool IsCorrectItem(const std::string& indexKey) const;
57     uint32_t AddIndexKey(const std::string& indexKey);
58     bool RemoveIndexKey(const std::string& indexKey);
59     void PrintIndexerItem() const;
60 
GetSectionStr()61     const std::u16string& GetSectionStr() const
62     {
63         return strSection_;
64     }
65 
GetLabelStr()66     const std::u16string& GetLabelStr() const
67     {
68         return strLabel_;
69     }
70 
GetSectionIndex()71     int32_t GetSectionIndex() const
72     {
73         return sectionIndex_;
74     }
75 
SetSectionIndex(int32_t index)76     void SetSectionIndex(int32_t index)
77     {
78         sectionIndex_ = index;
79     }
80 
GetKeyCount()81     uint32_t GetKeyCount() const
82     {
83         return keyCount_;
84     }
85 
SetPosition(int32_t position)86     void SetPosition(int32_t position)
87     {
88         position_ = position;
89     }
90 
GetPosition()91     int32_t GetPosition() const
92     {
93         return position_;
94     }
95 
96     void SetTextStyle(bool active);
97 
GetTextComponent()98     const RefPtr<TextComponent>& GetTextComponent() const
99     {
100         return text_;
101     }
102 
GetItemSize()103     const Dimension& GetItemSize() const
104     {
105         return itemSize_;
106     }
107 
SetItemSize(const Dimension & size)108     void SetItemSize(const Dimension& size)
109     {
110         itemSize_ = size;
111     }
112 
GetNormalTextStyle()113     const TextStyle& GetNormalTextStyle() const
114     {
115         return normalStyle_;
116     }
117 
SetNormalTextStyle(const TextStyle & textStyle)118     void SetNormalTextStyle(const TextStyle& textStyle)
119     {
120         normalStyle_ = textStyle;
121     }
122 
GetActiveTextStyle()123     const TextStyle& GetActiveTextStyle() const
124     {
125         return activeStyle_;
126     }
127 
SetActiveTextStyle(const TextStyle & textStyle)128     void SetActiveTextStyle(const TextStyle& textStyle)
129     {
130         activeStyle_ = textStyle;
131     }
132 
GetCircleMode()133     bool GetCircleMode() const
134     {
135         return circleMode_;
136     }
137 
SetCircleMode(bool circleMode)138     void SetCircleMode(bool circleMode)
139     {
140         circleMode_ = circleMode;
141     }
142 
GetRotateFlag()143     bool GetRotateFlag() const
144     {
145         return rotate_;
146     }
147 
148     void MarkItemPrimary(bool isPrimary = true)
149     {
150         isPrimary_ = isPrimary;
151     }
152 
IsItemPrimary()153     bool IsItemPrimary() const
154     {
155         return isPrimary_;
156     }
157 
GetItemType()158     int32_t GetItemType() const
159     {
160         return itemType_;
161     }
162 
SetItemType(int32_t type)163     void SetItemType(int32_t type)
164     {
165         itemType_ = type;
166     }
167 
168 private:
169     uint32_t AddIndexKeyForSharp(const std::string& indexKey);
170     bool RemoveIndexKeyForSharp(const std::string& indexKey);
171 
172     std::u16string strSection_;
173     std::u16string strLabel_; // string show in the section head
174     Dimension itemSize_;      // item size
175     bool circleMode_ = true;
176     bool rotate_ = false;
177     std::list<std::string> indexKey_;
178     int32_t sectionIndex_ = 0;
179     uint32_t keyCount_ = 0;
180     int32_t position_ = 0;
181     int32_t itemType_ = 0;
182     RefPtr<TextComponent> text_;
183     RefPtr<ImageComponent> image_;
184     RefPtr<BoxComponent> box_;
185     TextStyle normalStyle_;
186     TextStyle activeStyle_;
187     bool isPrimary_ = false;
188 };
189 
190 } // namespace OHOS::Ace
191 
192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_INDEXER_INDEXER_ITEM_COMPONENT_H
193