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 #include "core/components_v2/indexer/indexer_component.h"
17 
18 #include "core/components/arc/arc_component.h"
19 #include "core/components/text/text_component.h"
20 #include "core/components_v2/indexer/indexer_element.h"
21 #include "core/components_v2/indexer/render_indexer.h"
22 #include "core/components_v2/indexer/popup_list_component.h"
23 
24 namespace OHOS::Ace::V2 {
CreateElement()25 RefPtr<Element> IndexerComponent::CreateElement()
26 {
27     return AceType::MakeRefPtr<IndexerElement>();
28 }
29 
CreateRenderNode()30 RefPtr<RenderNode> IndexerComponent::CreateRenderNode()
31 {
32     RefPtr<RenderIndexer> renderNode =  AceType::MakeRefPtr<RenderIndexer>();
33     renderNode->SetFocusIndex(selectedIndex_);
34     return renderNode;
35 }
36 
FormatLabelAlphabet()37 void IndexerComponent::FormatLabelAlphabet()
38 {
39     for (auto& item : indexerLabel_) {
40         if (item == INDEXER_STR_DOT_EX) {
41             item = INDEXER_STR_DOT;
42         }
43     }
44 }
45 
BuildIndexerAlphabet()46 void IndexerComponent::BuildIndexerAlphabet()
47 {
48     std::vector<std::u16string> alphabet = Localization::GetInstance()->GetIndexAlphabet(); // get alphabet
49     if (alphabet.empty()) {
50         LOGE("fail to build indexer alphabet due to alphabet is empty");
51         return;
52     }
53     FormatLabelAlphabet();
54 
55     sectionsLocal_.clear();
56     labelLocal_.clear();
57 
58     for (size_t i = 0; i < indexerLabel_.size(); ++i) {
59         if (indexerLabel_[i] != INDEXER_STR_DOT) {
60             sectionsLocal_.push_back(indexerLabel_[i]);
61             labelLocal_.push_back(indexerLabel_[i]);
62         }
63     }
64 }
65 
InitIndexerItemStyle()66 void IndexerComponent::InitIndexerItemStyle()
67 {
68     // Indexer item style when binding list item is not current.
69     normalStyle_.SetFontSize(Dimension(INDEXER_LIST_ITEM_TEXT_SIZE, DimensionUnit::FP));
70     normalStyle_.SetFontWeight(FontWeight::W400);
71     normalStyle_.SetTextColor(Color(INDEXER_LIST_COLOR));
72 
73     // Indexer item style when binding list item is current.
74     activeStyle_.SetFontSize(Dimension(INDEXER_LIST_ITEM_TEXT_SIZE, DimensionUnit::FP));
75     activeStyle_.SetFontWeight(FontWeight::W500);
76     activeStyle_.SetTextColor(Color(INDEXER_LIST_ACTIVE_COLOR));
77 
78     bubbleStyle_.SetTextAlign(TextAlign::CENTER);
79     bubbleStyle_.SetFontSize(Dimension(BUBBLE_FONT_SIZE, DimensionUnit::VP));
80     bubbleStyle_.SetTextColor(Color(BUBBLE_FONT_COLOR));
81 }
82 
BuildBubbleBox()83 void IndexerComponent::BuildBubbleBox()
84 {
85     RefPtr<BoxComponent> bubble = AceType::MakeRefPtr<BoxComponent>();
86     bubble->SetFlex(BoxFlex::FLEX_NO);
87     bubble->SetAlignment(Alignment::CENTER);
88     bubble->SetWidth(BUBBLE_BOX_SIZE, DimensionUnit::VP);
89     bubble->SetHeight(BUBBLE_BOX_SIZE, DimensionUnit::VP);
90 
91     if (!bubbleBack_) {
92         bubbleBack_ = AceType::MakeRefPtr<Decoration>();
93     }
94     Radius radius = Radius(Dimension(BUBBLE_BOX_RADIUS, DimensionUnit::VP));
95     Border border;
96     border.SetBorderRadius(radius);
97     bubbleBack_->SetBorder(border);
98     bubbleBack_->SetBackgroundColor(Color(BUBBLE_BG_COLOR).BlendOpacity(NINETY_OPACITY_IN_PERCENT));
99     bubble->SetBackDecoration(bubbleBack_);
100 
101     if (!bubbleText_) {
102         bubbleText_ = AceType::MakeRefPtr<TextComponent>(StringUtils::Str16ToStr8(INDEXER_STR_SHARP));
103     }
104     bubbleText_->SetTextStyle(bubbleStyle_);
105     bubble->SetChild(bubbleText_);
106     bubble->SetEnableDebugBoundary(true);
107     RefPtr<DisplayComponent> displayComponent = AceType::MakeRefPtr<DisplayComponent>(bubble);
108     displayComponent->SetOpacity(ZERO_OPACITY);
109 
110     AppendChild(displayComponent);
111     nonItemCount_++;
112 }
113 
BuildPopupList()114 void IndexerComponent::BuildPopupList()
115 {
116     if (!popupList_) {
117         popupList_ = AceType::MakeRefPtr<PopupListComponent>();
118     }
119     RefPtr<DisplayComponent> displayComponent = AceType::MakeRefPtr<DisplayComponent>(popupList_);
120     displayComponent->SetOpacity(ZERO_OPACITY);
121 
122     AppendChild(displayComponent);
123     nonItemCount_++;
124 }
125 
BuildTextItem(const std::u16string & strSection,const std::u16string & strLabel,int32_t itemType)126 void IndexerComponent::BuildTextItem(const std::u16string& strSection, const std::u16string& strLabel, int32_t itemType)
127 {
128     RefPtr<IndexerItemComponent> textItem =
129         AceType::MakeRefPtr<IndexerItemComponent>(strSection, strLabel, itemSize_, false);
130     textItem->SetNormalTextStyle(normalStyle_);
131     textItem->SetActiveTextStyle(activeStyle_);
132     textItem->SetTextStyle(false);
133     textItem->SetItemType(itemType);
134     textItem->SetSectionIndex(itemCount_);
135     textItem->SetSelectedBackgroundColor(selectedBgColor_);
136     listItem_.push_back(textItem);
137     AppendChild(textItem);
138     ++itemCount_;
139 }
140 
BuildIndexerItems()141 void IndexerComponent::BuildIndexerItems()
142 {
143     uint32_t length = labelLocal_.size();
144     if (length == 0) {
145         LOGE("[indexer] invalid section string");
146         return;
147     }
148 
149     BuildBubbleBox();
150     itemCount_ = 0;
151 
152     for (uint32_t i = 0; i < length; ++i) {
153         std::u16string strItem = labelLocal_[i];
154         BuildTextItem(sectionsLocal_[i], strItem);
155     }
156 
157     LOGI("[indexer] BuildIndexerItems, itemCount_:%{public}d", itemCount_);
158 }
159 
UpdateTextStyle()160 void IndexerComponent::UpdateTextStyle()
161 {
162     for (auto& item : GetChildren()) {
163         RefPtr<IndexerItemComponent> textItem = AceType::DynamicCast<IndexerItemComponent>(item);
164         if (textItem) {
165             textItem->SetNormalTextStyle(normalStyle_);
166             textItem->SetActiveTextStyle(activeStyle_);
167             textItem->SetTextStyle(false);
168             textItem->SetSelectedBackgroundColor(selectedBgColor_);
169             textItem->SetItemSize(itemSize_);
170             textItem->UpdateSize();
171         }
172     }
173 }
174 } // namespace OHOS::Ace::V2