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