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_V2_INDEXER_INDEXER_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INDEXER_INDEXER_COMPONENT_H
18 
19 #include "base/i18n/localization.h"
20 #include "base/utils/string_utils.h"
21 #include "base/utils/system_properties.h"
22 #include "core/components_v2/indexer/indexer_item_component.h"
23 #include "core/components_v2/indexer/popup_list_component.h"
24 #include "core/pipeline/base/component_group.h"
25 
26 namespace OHOS::Ace::V2 {
27 // common data
28 inline constexpr int32_t INDEXER_INVALID_INDEX = -1;
29 inline constexpr int32_t INDEXER_ITEM_MAX_COUNT = 29; // [indexer], default max count
30 inline constexpr uint8_t DEFAULT_OPACITY = 255;
31 inline constexpr uint8_t ZERO_OPACITY = 0;
32 inline const std::u16string INDEXER_STR_DOT = StringUtils::Str8ToStr16("•");
33 inline const std::u16string INDEXER_STR_DOT_EX = StringUtils::Str8ToStr16(".");
34 inline const std::u16string INDEXER_STR_SHARP = StringUtils::Str8ToStr16("#");
35 
36 inline constexpr float KEYFRAME_BEGIN = 0.0;
37 inline constexpr float KEYFRAME_HALF = 0.5;
38 inline constexpr float KEYFRAME_END = 1.0;
39 inline constexpr double DEFAULT_OPACITY_IN_PERCENT = 1.0;
40 inline constexpr double NINETY_OPACITY_IN_PERCENT = 0.9;
41 inline constexpr double ZERO_OPACITY_IN_PERCENT = 0.0;
42 // data for list mode
43 inline constexpr double INDEXER_LIST_ITEM_TEXT_SIZE = 12.0; // list mode, font size (FP)
44 inline constexpr uint32_t INDEXER_LIST_COLOR = 0x99000000;
45 inline constexpr uint32_t INDEXER_LIST_ACTIVE_COLOR = 0xFF254FF7;
46 inline constexpr double INDEXER_DEFAULT_PADDING_X = 10.0;
47 inline constexpr double INDEXER_DEFAULT_PADDING_Y = 16.0;
48 inline constexpr double BUBBLE_BOX_SIZE = 56.0;
49 inline constexpr double BUBBLE_BOX_RADIUS = 12.0;
50 inline constexpr double ZERO_RADIUS = 0.0;
51 inline constexpr double BUBBLE_FONT_SIZE = 24.0;
52 inline constexpr Dimension BUBBLE_POSITION_X = 48.0_vp;
53 inline constexpr Dimension BUBBLE_POSITION_Y = 96.0_vp;
54 inline constexpr uint32_t BUBBLE_FONT_COLOR = 0xFF254FF7;
55 inline constexpr uint32_t BUBBLE_BG_COLOR = 0xFFF1F3F5;
56 inline constexpr double INDEXER_ITEM_SIZE = 24.0;      // circle mode, item size (VP)
57 inline constexpr double INDEXER_ITEM_TEXT_SIZE = 12.0; // circle, mode font size (VP)
58 // data for circle mode
59 inline constexpr int32_t INDEXER_COLLAPSE_ITEM_COUNT = 4;
60 inline constexpr double INDEXER_CIRCLE_ITEM_SIZE = 24.0;          // circle mode, item size (VP)
61 inline constexpr double INDEXER_CIRCLE_ITEM_TEXT_SIZE = 13.0;     // circle, mode font size (VP)
62 inline constexpr double INDEXER_CIRCLE_ITEM_SHADOW_RADIUS = 27.0; // circle mode, shadow radius (VP)
63 inline constexpr double BUBBLE_BOX_SIZE_CIRCLE = 46.0;
64 inline constexpr double BUBBLE_FONT_SIZE_CIRCLE = 19.0;
65 inline constexpr uint32_t INDEXER_ACTIVE_BG_COLOR = 0x1F0A59F7;
66 
67 enum class AlignStyle {
68     LEFT = 0,
69     RIGHT,
70     START,
71     END,
72 };
73 
74 class ACE_EXPORT IndexerComponent : public ComponentGroup {
75     DECLARE_ACE_TYPE(IndexerComponent, ComponentGroup);
76 
77 public:
78     using OnRequestPopupDataFunc = std::function<std::vector<std::string>(std::shared_ptr<IndexerEventInfo>)>;
79 
IndexerComponent(const std::vector<std::string> & label,int32_t selectedIndex)80     IndexerComponent(const std::vector<std::string>& label, int32_t selectedIndex)
81         : selectedIndex_(selectedIndex)
82     {
83         indexerLabel_ = GetU16StrVector(label);
84         itemSize_ = Dimension(INDEXER_ITEM_SIZE, DimensionUnit::VP);
85         selectedBgColor_ = Color(INDEXER_ACTIVE_BG_COLOR);
86 
87         BuildIndexerAlphabet();
88         InitIndexerItemStyle();
89         BuildIndexerItems();
90         BuildPopupList();
91     }
92 
93     ~IndexerComponent() override = default;
94     RefPtr<Element> CreateElement() override;
95     RefPtr<RenderNode> CreateRenderNode() override;
96 
IsBubbleEnabled()97     bool IsBubbleEnabled() const
98     {
99         return bubbleEnabled_;
100     }
101 
SetBubbleEnabled(bool enable)102     void SetBubbleEnabled(bool enable)
103     {
104         bubbleEnabled_ = enable;
105     }
106 
HasCollapseItem()107     bool HasCollapseItem() const
108     {
109         return hasCollapseItem_;
110     }
111 
GetBubbleTextComponent()112     const RefPtr<TextComponent>& GetBubbleTextComponent() const
113     {
114         return bubbleText_;
115     }
116 
GetNonItemCount()117     int32_t GetNonItemCount() const
118     {
119         return nonItemCount_;
120     }
121 
GetMaxShowCount()122     int32_t GetMaxShowCount() const
123     {
124         return maxShowCount_;
125     }
126 
GetItemSize()127     const Dimension& GetItemSize() const
128     {
129         return itemSize_;
130     }
131 
SetItemSize(const Dimension & size)132     void SetItemSize(const Dimension& size)
133     {
134         itemSize_ = size;
135         UpdateTextStyle();
136     }
137 
GetItemCount()138     int32_t GetItemCount() const
139     {
140         return itemCount_;
141     }
142 
GetNormalTextStyle()143     const TextStyle& GetNormalTextStyle() const
144     {
145         return normalStyle_;
146     }
147 
SetNormalTextStyle(const TextStyle & textStyle)148     void SetNormalTextStyle(const TextStyle& textStyle)
149     {
150         normalStyle_ = textStyle;
151         UpdateTextStyle();
152     }
153 
GetActiveTextStyle()154     const TextStyle& GetActiveTextStyle() const
155     {
156         return activeStyle_;
157     }
158 
SetActiveTextStyle(const TextStyle & textStyle)159     void SetActiveTextStyle(const TextStyle& textStyle)
160     {
161         activeStyle_ = textStyle;
162         UpdateTextStyle();
163     }
164 
GetBubbleTextStyle()165     const TextStyle& GetBubbleTextStyle() const
166     {
167         return bubbleStyle_;
168     }
169 
SetBubbleTextStyle(const TextStyle & textStyle)170     void SetBubbleTextStyle(const TextStyle& textStyle)
171     {
172         bubbleStyle_ = textStyle;
173         bubbleText_->SetTextStyle(bubbleStyle_);
174     }
175 
GetU16StrVector(const std::vector<std::string> & str)176     std::vector<std::u16string> GetU16StrVector(const std::vector<std::string>& str)
177     {
178         std::vector<std::u16string> alphabet;
179         for (const auto& item : str) {
180             alphabet.push_back(StringUtils::Str8ToStr16(item));
181         }
182         return alphabet;
183     }
184 
GetIndexerItemsComponents()185     std::list<RefPtr<IndexerItemComponent>> GetIndexerItemsComponents() const
186     {
187         return listItem_;
188     }
189 
SetSelectedEvent(const EventMarker & event)190     void SetSelectedEvent(const EventMarker& event)
191     {
192         selectedEvent_ = event;
193     }
194 
GetSelectedEvent()195     const EventMarker& GetSelectedEvent() const
196     {
197         return selectedEvent_;
198     }
199 
GetSelectedIndex()200     int32_t GetSelectedIndex() const
201     {
202         return selectedIndex_;
203     }
204 
SetSelectedIndex(int32_t index)205     void SetSelectedIndex(int32_t index)
206     {
207         selectedIndex_ = index;
208     }
209 
SetSelectedBackgroundColor(const Color & bgColor)210     void SetSelectedBackgroundColor(const Color& bgColor)
211     {
212         selectedBgColor_ = bgColor;
213         UpdateTextStyle();
214     }
215 
GetSelectedBackgroundColor()216     const Color& GetSelectedBackgroundColor()
217     {
218         return selectedBgColor_;
219     }
220 
SetBubbleBackgroundColor(const Color & bgColor)221     void SetBubbleBackgroundColor(const Color& bgColor)
222     {
223         if (!bubbleBack_) {
224             bubbleBack_ = AceType::MakeRefPtr<Decoration>();
225         }
226         bubbleBack_->SetBackgroundColor(bgColor.BlendOpacity(NINETY_OPACITY_IN_PERCENT));
227     }
228 
GetBubbleBackgroundColor()229     const Color& GetBubbleBackgroundColor()
230     {
231         return bubbleBack_->GetBackgroundColor();
232     }
233 
SetAlignStyle(AlignStyle align)234     void SetAlignStyle(AlignStyle align)
235     {
236         alignStyle_ = align;
237     }
238 
GetAlignStyle()239     AlignStyle GetAlignStyle()
240     {
241         return alignStyle_;
242     }
243 
SetRequestPopupDataFunc(const OnRequestPopupDataFunc & func)244     void SetRequestPopupDataFunc (const OnRequestPopupDataFunc& func)
245     {
246         requestPopupDataEvent_ = func;
247 
248         // if implement OnRequestPopupData function, enable the popup list.
249         popupListEnabled_ = true;
250     }
251 
GetRequestPopupDataFunc()252     const OnRequestPopupDataFunc& GetRequestPopupDataFunc() const
253     {
254         return requestPopupDataEvent_;
255     }
256 
SetPopupListEnabled(bool enable)257     void SetPopupListEnabled(bool enable)
258     {
259         popupListEnabled_ = enable;
260     }
261 
IsPopupListEnabled()262     bool IsPopupListEnabled()
263     {
264         return popupListEnabled_;
265     }
266 
SetPopupSelectedEvent(const EventMarker & event)267     void SetPopupSelectedEvent(const EventMarker& event)
268     {
269         if (popupList_) {
270             popupList_->SetPopupSelectedEvent(event);
271         } else {
272             LOGW("Not popup list component");
273         }
274     }
275 
GetArrayValue()276     std::vector<std::string> GetArrayValue() const
277     {
278         return valueArray_;
279     }
280 
GetLabel()281     std::vector<std::u16string> GetLabel() const
282     {
283         return indexerLabel_;
284     }
285 
286 protected:
287     // init data
288     void FormatLabelAlphabet();
289     void BuildIndexerAlphabet();
290 
291     void InitIndexerItemStyle();
292     void BuildIndexerItems();
293     void BuildBubbleBox();
294     void BuildPopupList();
295     void BuildTextItem(const std::u16string& strSection, const std::u16string& strLabel, int32_t itemType = 0);
296     void UpdateTextStyle();
297 
298     std::vector<std::u16string> indexerLabel_;  // user input sections
299     std::vector<std::u16string> sectionsLocal_; // actual sections, exclude dot
300     std::vector<std::u16string> labelLocal_;    // sections shown on the screen with dot inside
301     std::vector<std::string> valueArray_;
302     TextStyle normalStyle_;
303     TextStyle activeStyle_;
304     TextStyle bubbleStyle_;
305     Color selectedBgColor_;
306     AlignStyle alignStyle_ = AlignStyle::RIGHT;
307 
308     std::list<RefPtr<IndexerItemComponent>> listItem_;
309     RefPtr<Decoration> bubbleBack_;
310     RefPtr<TextComponent> bubbleText_;
311     RefPtr<PopupListComponent> popupList_;
312 
313     int32_t itemCount_ = INDEXER_ITEM_MAX_COUNT;    // actual indexer item
314     int32_t maxShowCount_ = INDEXER_ITEM_MAX_COUNT; // max item shown on screen
315     int32_t nonItemCount_ = 0;                      // not indexer item count such as arc and bubbleBox
316     int32_t selectedIndex_ = 0;
317     // item size
318     Dimension itemSize_;
319     bool hasCollapseItem_ = false;
320     bool bubbleEnabled_ = false;
321     bool popupListEnabled_ = false;
322 
323 private:
324     EventMarker selectedEvent_;
325     OnRequestPopupDataFunc requestPopupDataEvent_;
326 };
327 } // namespace OHOS::Ace::V2
328 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INDEXER_INDEXER_COMPONENT_H
329