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_OPTION_OPTION_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_OPTION_OPTION_COMPONENT_H 18 19 #include "core/accessibility/accessibility_manager.h" 20 #include "core/components/box/box_component.h" 21 #include "core/components/common/properties/color.h" 22 #include "core/components/flex/flex_component.h" 23 #include "core/components/image/image_component.h" 24 #include "core/components/select/select_theme.h" 25 #include "core/components/text/text_component.h" 26 #include "core/pipeline/base/component_group.h" 27 28 namespace OHOS::Ace { 29 30 class ACE_EXPORT OptionComponent : public ComponentGroup { 31 DECLARE_ACE_TYPE(OptionComponent, ComponentGroup); 32 33 public: 34 OptionComponent() = default; 35 OptionComponent(const RefPtr<SelectTheme>& theme); 36 ~OptionComponent() override = default; 37 38 RefPtr<RenderNode> CreateRenderNode() override; 39 RefPtr<Element> CreateElement() override; 40 41 void InitTheme(const RefPtr<ThemeManager>& themeManager); 42 GetIcon()43 const RefPtr<ImageComponent>& GetIcon() const 44 { 45 return icon_; 46 } SetIcon(const RefPtr<ImageComponent> & icon)47 void SetIcon(const RefPtr<ImageComponent>& icon) 48 { 49 icon_ = icon; 50 } 51 GetText()52 const RefPtr<TextComponent>& GetText() const 53 { 54 return text_; 55 } SetText(const RefPtr<TextComponent> & text)56 void SetText(const RefPtr<TextComponent>& text) 57 { 58 text_ = text; 59 CheckOptionModify(); 60 } 61 SetTheme(const RefPtr<SelectTheme> & theme)62 void SetTheme(const RefPtr<SelectTheme>& theme) 63 { 64 SetClickedColor(theme->GetClickedColor()); 65 SetSelectedColor(theme->GetSelectedColor()); 66 SetFontColor(theme->GetFontColor()); 67 SetFontSize(theme->GetFontSize()); 68 SetFontWeight(theme->GetFontWeight()); 69 SetFontFamily(theme->GetFontFamily()); 70 SetTextDecoration(theme->GetTextDecoration()); 71 SetAllowScale(theme->IsAllowScale()); 72 } 73 GetSelected()74 bool GetSelected() const 75 { 76 if (!selectable_) { 77 return false; 78 } 79 return selected_; 80 } SetSelected(bool selected)81 void SetSelected(bool selected) 82 { 83 if (!selectable_) { 84 selected_ = false; 85 } else { 86 selected_ = selected; 87 } 88 } 89 GetClicked()90 bool GetClicked() const 91 { 92 return clicked_; 93 } SetClicked(bool clicked)94 void SetClicked(bool clicked) 95 { 96 clicked_ = clicked; 97 } 98 GetFocused()99 bool GetFocused() const 100 { 101 return focused_; 102 } SetFocused(bool value)103 void SetFocused(bool value) 104 { 105 focused_ = value; 106 } 107 GetFocusable()108 bool GetFocusable() const 109 { 110 return focusable_; 111 } SetFocusable(bool value)112 void SetFocusable(bool value) 113 { 114 focusable_ = value; 115 } 116 GetVisible()117 bool GetVisible() const 118 { 119 return visible_; 120 } SetVisible(bool value)121 void SetVisible(bool value) 122 { 123 visible_ = value; 124 } 125 GetValue()126 std::string GetValue() const 127 { 128 return value_; 129 } SetValue(const std::string & value)130 void SetValue(const std::string& value) 131 { 132 value_ = value; 133 } 134 GetClickedColor()135 const Color& GetClickedColor() const 136 { 137 return theme_->GetClickedColor(); 138 } SetClickedColor(const Color & clickedColor)139 void SetClickedColor(const Color& clickedColor) 140 { 141 theme_->SetClickedColor(clickedColor); 142 } 143 GetSelectedColor()144 const Color& GetSelectedColor() const 145 { 146 return theme_->GetSelectedColor(); 147 } SetSelectedColor(const Color & selectedColor)148 void SetSelectedColor(const Color& selectedColor) 149 { 150 theme_->SetSelectedColor(selectedColor); 151 } 152 GetFontColor()153 const Color& GetFontColor() const 154 { 155 return theme_->GetFontColor(); 156 } SetFontColor(const Color & fontColor)157 void SetFontColor(const Color& fontColor) 158 { 159 theme_->SetFontColor(fontColor); 160 } 161 GetFontWeight()162 FontWeight GetFontWeight() const 163 { 164 return theme_->GetFontWeight(); 165 } SetFontWeight(FontWeight fontWeight)166 void SetFontWeight(FontWeight fontWeight) 167 { 168 theme_->SetFontWeight(fontWeight); 169 } 170 GetFontFamily()171 const std::string& GetFontFamily() const 172 { 173 return theme_->GetFontFamily(); 174 } SetFontFamily(const std::string & fontFamily)175 void SetFontFamily(const std::string& fontFamily) 176 { 177 theme_->SetFontFamily(fontFamily); 178 } 179 GetTextDecoration()180 TextDecoration GetTextDecoration() const 181 { 182 return theme_->GetTextDecoration(); 183 } SetTextDecoration(TextDecoration textDecoration)184 void SetTextDecoration(TextDecoration textDecoration) 185 { 186 theme_->SetTextDecoration(textDecoration); 187 } 188 GetFontSize()189 const Dimension& GetFontSize() const 190 { 191 return theme_->GetFontSize(); 192 } SetFontSize(const Dimension & fontSize)193 void SetFontSize(const Dimension& fontSize) 194 { 195 theme_->SetFontSize(fontSize); 196 } 197 GetClickedCallback()198 const std::function<void(std::size_t)>& GetClickedCallback() const 199 { 200 return clickedCallback_; 201 } 202 SetClickedCallback(const std::function<void (std::size_t)> & clickedCallback)203 void SetClickedCallback(const std::function<void(std::size_t)>& clickedCallback) 204 { 205 clickedCallback_ = clickedCallback; 206 } 207 SetModifiedCallback(const std::function<void (std::size_t)> & value)208 void SetModifiedCallback(const std::function<void(std::size_t)>& value) 209 { 210 modifiedCallback_ = value; 211 } 212 GetIndex()213 std::size_t GetIndex() const 214 { 215 return index_; 216 } SetIndex(std::size_t index)217 void SetIndex(std::size_t index) 218 { 219 index_ = index; 220 } 221 GetSelectable()222 bool GetSelectable() const 223 { 224 return selectable_; 225 } SetSelectable(bool selectable)226 void SetSelectable(bool selectable) 227 { 228 selectable_ = selectable; 229 if (!selectable) { 230 selected_ = false; 231 } 232 } 233 GetShowInNavigationBar()234 ShowInNavigationBar GetShowInNavigationBar() const 235 { 236 return showInNavigationBar_; 237 } 238 SetShowInNavigationBar(ShowInNavigationBar showInNavigationBar)239 void SetShowInNavigationBar(ShowInNavigationBar showInNavigationBar) 240 { 241 showInNavigationBar_ = showInNavigationBar; 242 } 243 244 bool Initialize(const RefPtr<AccessibilityManager>& manager); 245 GetId()246 const ComposeId& GetId() const 247 { 248 return id_; 249 } 250 SetId(const ComposeId & id)251 void SetId(const ComposeId& id) 252 { 253 id_ = id; 254 } 255 IsAllowScale()256 bool IsAllowScale() const 257 { 258 return theme_->IsAllowScale(); 259 } 260 SetAllowScale(bool allowScale)261 void SetAllowScale(bool allowScale) 262 { 263 theme_->SetAllowScale(allowScale); 264 } 265 GetTheme()266 const RefPtr<SelectTheme>& GetTheme() const 267 { 268 return theme_; 269 } 270 GetNode()271 const RefPtr<AccessibilityNode> GetNode() const 272 { 273 return node_; 274 } 275 SetNode(const RefPtr<AccessibilityNode> & value)276 void SetNode(const RefPtr<AccessibilityNode>& value) 277 { 278 node_ = value; 279 } 280 GetParentId()281 int32_t GetParentId() const 282 { 283 return parentId_; 284 } 285 SetParentId(int32_t value)286 void SetParentId(int32_t value) 287 { 288 parentId_ = value; 289 } 290 SetDisabled(bool disable)291 void SetDisabled(bool disable) 292 { 293 disabled_ = disable; 294 } 295 GetDisabled()296 bool GetDisabled() const 297 { 298 return disabled_; 299 } 300 SetClickEvent(const EventMarker & clickEvent)301 void SetClickEvent(const EventMarker& clickEvent) 302 { 303 clickEvent_ = clickEvent; 304 } 305 GetClickEvent()306 const EventMarker& GetClickEvent() const 307 { 308 return clickEvent_; 309 } 310 SetClickEventForToolBarItem(const EventMarker & clickEventForToolBarItem)311 void SetClickEventForToolBarItem(const EventMarker& clickEventForToolBarItem) 312 { 313 clickEventForToolBarItem_ = clickEventForToolBarItem; 314 } 315 GetClickEventForToolBarItem()316 const EventMarker& GetClickEventForToolBarItem() const 317 { 318 return clickEventForToolBarItem_; 319 } 320 321 // used for inspector node in PC preview SetAttr(const std::vector<std::pair<std::string,std::string>> & attrs)322 void SetAttr(const std::vector<std::pair<std::string, std::string>>& attrs) 323 { 324 attrs_ = attrs; 325 } 326 327 // used for inspector node in PC preview SetStyle(const std::vector<std::pair<std::string,std::string>> & styles)328 void SetStyle(const std::vector<std::pair<std::string, std::string>>& styles) 329 { 330 styles_ = styles; 331 } 332 GetCustomizedCallback()333 const std::function<void()>& GetCustomizedCallback() 334 { 335 return customizedCallback_; 336 } 337 SetCustomizedCallback(const std::function<void ()> & onClickFunc)338 void SetCustomizedCallback(const std::function<void()>& onClickFunc) 339 { 340 customizedCallback_ = onClickFunc; 341 } 342 SetCustomComponent(const RefPtr<Component> & component)343 void SetCustomComponent(const RefPtr<Component>& component) 344 { 345 customComponent_ = component; 346 } 347 GetCustomComponent()348 const RefPtr<Component>& GetCustomComponent() const 349 { 350 return customComponent_; 351 } 352 GetTextStyle()353 const TextStyle& GetTextStyle() const 354 { 355 return textStyle_; 356 } SetTextStyle(const TextStyle & style)357 void SetTextStyle(const TextStyle& style) 358 { 359 textStyle_ = style; 360 } 361 GetSelectedTextStyle()362 const TextStyle& GetSelectedTextStyle() const 363 { 364 return selectedTextStyle_; 365 } SetSelectedTextStyle(const TextStyle & style)366 void SetSelectedTextStyle(const TextStyle& style) 367 { 368 selectedTextStyle_ = style; 369 } 370 GetBackgroundColor()371 const Color& GetBackgroundColor() const 372 { 373 return backgroundColor_; 374 } SetBackgroundColor(const Color & backgroundColor)375 void SetBackgroundColor(const Color& backgroundColor) 376 { 377 backgroundColor_ = backgroundColor; 378 } 379 GetSelectedBackgroundColor()380 const Color& GetSelectedBackgroundColor() const 381 { 382 return selectedBackgroundColor_; 383 } SetSelectedBackgroundColor(const Color & backgroundColor)384 void SetSelectedBackgroundColor(const Color& backgroundColor) 385 { 386 selectedBackgroundColor_ = backgroundColor; 387 } 388 389 private: 390 // used for inspector node in PC preview GetAttr()391 const std::vector<std::pair<std::string, std::string>> GetAttr() 392 { 393 return attrs_; 394 } 395 396 // used for inspector node in PC preview GetStyle()397 const std::vector<std::pair<std::string, std::string>> GetStyle() 398 { 399 return styles_; 400 } 401 402 void CheckOptionModify(); 403 404 // used for inspector node in PC preview 405 std::vector<std::pair<std::string, std::string>> attrs_; 406 std::vector<std::pair<std::string, std::string>> styles_; 407 RefPtr<ImageComponent> icon_; 408 RefPtr<TextComponent> text_; 409 RefPtr<Component> customComponent_; 410 bool selected_ = false; 411 bool clicked_ = false; 412 bool focused_ = false; 413 bool selectable_ = true; 414 std::string value_; 415 ShowInNavigationBar showInNavigationBar_ = ShowInNavigationBar::SHOW; 416 RefPtr<SelectTheme> theme_; 417 std::size_t index_ = SELECT_INVALID_INDEX; 418 std::function<void(std::size_t)> clickedCallback_; 419 std::function<void(std::size_t)> modifiedCallback_; 420 std::function<void()> customizedCallback_; 421 422 std::string lastText_; 423 ComposeId id_ = "-1"; 424 EventMarker clickEvent_; 425 EventMarker clickEventForToolBarItem_; 426 427 TextStyle textStyle_; 428 TextStyle selectedTextStyle_; 429 Color backgroundColor_ = Color::TRANSPARENT; 430 Color selectedBackgroundColor_; 431 432 RefPtr<AccessibilityNode> node_; 433 int32_t parentId_ = -1; 434 bool disabled_ = false; 435 bool visible_ = true; 436 bool focusable_ = true; 437 }; 438 439 } // namespace OHOS::Ace 440 441 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_OPTION_OPTION_COMPONENT_H 442