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 }; 71 72 class ACE_EXPORT IndexerComponent : public ComponentGroup { 73 DECLARE_ACE_TYPE(IndexerComponent, ComponentGroup); 74 75 public: 76 using OnRequestPopupDataFunc = std::function<std::vector<std::string>(std::shared_ptr<IndexerEventInfo>)>; 77 IndexerComponent(const std::vector<std::string> & label,int32_t selectedIndex)78 IndexerComponent(const std::vector<std::string>& label, int32_t selectedIndex) 79 : selectedIndex_(selectedIndex) 80 { 81 indexerLabel_ = GetU16StrVector(label); 82 itemSize_ = Dimension(INDEXER_ITEM_SIZE, DimensionUnit::VP); 83 selectedBgColor_ = Color(INDEXER_ACTIVE_BG_COLOR); 84 85 BuildIndexerAlphabet(); 86 InitIndexerItemStyle(); 87 BuildIndexerItems(); 88 BuildPopupList(); 89 } 90 91 ~IndexerComponent() override = default; 92 RefPtr<Element> CreateElement() override; 93 RefPtr<RenderNode> CreateRenderNode() override; 94 IsBubbleEnabled()95 bool IsBubbleEnabled() const 96 { 97 return bubbleEnabled_; 98 } 99 SetBubbleEnabled(bool enable)100 void SetBubbleEnabled(bool enable) 101 { 102 bubbleEnabled_ = enable; 103 } 104 HasCollapseItem()105 bool HasCollapseItem() const 106 { 107 return hasCollapseItem_; 108 } 109 GetBubbleTextComponent()110 const RefPtr<TextComponent>& GetBubbleTextComponent() const 111 { 112 return bubbleText_; 113 } 114 GetNonItemCount()115 int32_t GetNonItemCount() const 116 { 117 return nonItemCount_; 118 } 119 GetMaxShowCount()120 int32_t GetMaxShowCount() const 121 { 122 return maxShowCount_; 123 } 124 GetItemSize()125 const Dimension& GetItemSize() const 126 { 127 return itemSize_; 128 } 129 SetItemSize(const Dimension & size)130 void SetItemSize(const Dimension& size) 131 { 132 itemSize_ = size; 133 UpdateTextStyle(); 134 } 135 GetItemCount()136 int32_t GetItemCount() const 137 { 138 return itemCount_; 139 } 140 GetNormalTextStyle()141 const TextStyle& GetNormalTextStyle() const 142 { 143 return normalStyle_; 144 } 145 SetNormalTextStyle(const TextStyle & textStyle)146 void SetNormalTextStyle(const TextStyle& textStyle) 147 { 148 normalStyle_ = textStyle; 149 UpdateTextStyle(); 150 } 151 GetActiveTextStyle()152 const TextStyle& GetActiveTextStyle() const 153 { 154 return activeStyle_; 155 } 156 SetActiveTextStyle(const TextStyle & textStyle)157 void SetActiveTextStyle(const TextStyle& textStyle) 158 { 159 activeStyle_ = textStyle; 160 UpdateTextStyle(); 161 } 162 GetBubbleTextStyle()163 const TextStyle& GetBubbleTextStyle() const 164 { 165 return bubbleStyle_; 166 } 167 SetBubbleTextStyle(const TextStyle & textStyle)168 void SetBubbleTextStyle(const TextStyle& textStyle) 169 { 170 bubbleStyle_ = textStyle; 171 bubbleText_->SetTextStyle(bubbleStyle_); 172 } 173 GetU16StrVector(const std::vector<std::string> & str)174 std::vector<std::u16string> GetU16StrVector(const std::vector<std::string>& str) 175 { 176 std::vector<std::u16string> alphabet; 177 for (const auto& item : str) { 178 alphabet.push_back(StringUtils::Str8ToStr16(item)); 179 } 180 return alphabet; 181 } 182 GetIndexerItemsComponents()183 std::list<RefPtr<IndexerItemComponent>> GetIndexerItemsComponents() const 184 { 185 return listItem_; 186 } 187 SetSelectedEvent(const EventMarker & event)188 void SetSelectedEvent(const EventMarker& event) 189 { 190 selectedEvent_ = event; 191 } 192 GetSelectedEvent()193 const EventMarker& GetSelectedEvent() const 194 { 195 return selectedEvent_; 196 } 197 GetSelectedIndex()198 int32_t GetSelectedIndex() const 199 { 200 return selectedIndex_; 201 } 202 SetSelectedIndex(int32_t index)203 void SetSelectedIndex(int32_t index) 204 { 205 selectedIndex_ = index; 206 } 207 SetSelectedBackgroundColor(const Color & bgColor)208 void SetSelectedBackgroundColor(const Color& bgColor) 209 { 210 selectedBgColor_ = bgColor; 211 UpdateTextStyle(); 212 } 213 GetSelectedBackgroundColor()214 const Color& GetSelectedBackgroundColor() 215 { 216 return selectedBgColor_; 217 } 218 SetBubbleBackgroundColor(const Color & bgColor)219 void SetBubbleBackgroundColor(const Color& bgColor) 220 { 221 if (!bubbleBack_) { 222 bubbleBack_ = AceType::MakeRefPtr<Decoration>(); 223 } 224 bubbleBack_->SetBackgroundColor(bgColor.BlendOpacity(NINETY_OPACITY_IN_PERCENT)); 225 } 226 GetBubbleBackgroundColor()227 const Color& GetBubbleBackgroundColor() 228 { 229 return bubbleBack_->GetBackgroundColor(); 230 } 231 SetAlignStyle(AlignStyle align)232 void SetAlignStyle(AlignStyle align) 233 { 234 alignStyle_ = align; 235 } 236 GetAlignStyle()237 AlignStyle GetAlignStyle() 238 { 239 return alignStyle_; 240 } 241 SetRequestPopupDataFunc(const OnRequestPopupDataFunc & func)242 void SetRequestPopupDataFunc (const OnRequestPopupDataFunc& func) 243 { 244 requestPopupDataEvent_ = func; 245 246 // if implement OnRequestPopupData function, enable the popup list. 247 popupListEnabled_ = true; 248 } 249 GetRequestPopupDataFunc()250 const OnRequestPopupDataFunc& GetRequestPopupDataFunc() const 251 { 252 return requestPopupDataEvent_; 253 } 254 SetPopupListEnabled(bool enable)255 void SetPopupListEnabled(bool enable) 256 { 257 popupListEnabled_ = enable; 258 } 259 IsPopupListEnabled()260 bool IsPopupListEnabled() 261 { 262 return popupListEnabled_; 263 } 264 SetPopupSelectedEvent(const EventMarker & event)265 void SetPopupSelectedEvent(const EventMarker& event) 266 { 267 if (popupList_) { 268 popupList_->SetPopupSelectedEvent(event); 269 } else { 270 LOGW("Not popup list component"); 271 } 272 } 273 GetArrayValue()274 std::vector<std::string> GetArrayValue() const 275 { 276 return valueArray_; 277 } 278 GetLabel()279 std::vector<std::u16string> GetLabel() const 280 { 281 return indexerLabel_; 282 } 283 284 protected: 285 // init data 286 void FormatLabelAlphabet(); 287 void BuildIndexerAlphabet(); 288 289 void InitIndexerItemStyle(); 290 void BuildIndexerItems(); 291 void BuildBubbleBox(); 292 void BuildPopupList(); 293 void BuildTextItem(const std::u16string& strSection, const std::u16string& strLabel, int32_t itemType = 0); 294 void UpdateTextStyle(); 295 296 std::vector<std::u16string> indexerLabel_; // user input sections 297 std::vector<std::u16string> sectionsLocal_; // actual sections, exclude dot 298 std::vector<std::u16string> labelLocal_; // sections shown on the screen with dot inside 299 std::vector<std::string> valueArray_; 300 TextStyle normalStyle_; 301 TextStyle activeStyle_; 302 TextStyle bubbleStyle_; 303 Color selectedBgColor_; 304 AlignStyle alignStyle_ = AlignStyle::RIGHT; 305 306 std::list<RefPtr<IndexerItemComponent>> listItem_; 307 RefPtr<Decoration> bubbleBack_; 308 RefPtr<TextComponent> bubbleText_; 309 RefPtr<PopupListComponent> popupList_; 310 311 int32_t itemCount_ = INDEXER_ITEM_MAX_COUNT; // actual indexer item 312 int32_t maxShowCount_ = INDEXER_ITEM_MAX_COUNT; // max item shown on screen 313 int32_t nonItemCount_ = 0; // not indexer item count such as arc and bubbleBox 314 int32_t selectedIndex_ = 0; 315 // item size 316 Dimension itemSize_; 317 bool hasCollapseItem_ = false; 318 bool bubbleEnabled_ = false; 319 bool popupListEnabled_ = false; 320 321 private: 322 EventMarker selectedEvent_; 323 OnRequestPopupDataFunc requestPopupDataEvent_; 324 }; 325 } // namespace OHOS::Ace::V2 326 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INDEXER_INDEXER_COMPONENT_H 327