• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CHECKABLE_CHECKABLE_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_COMPONENT_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/utils/label_target.h"
21 #include "core/components/checkable/checkable_theme.h"
22 #include "core/components/declaration/common/attribute.h"
23 #include "core/components_v2/common/common_def.h"
24 #include "core/pipeline/base/render_component.h"
25 #include "core/pipeline/base/render_node.h"
26 #ifndef WEARABLE_PRODUCT
27 #include "core/event/multimodal/multimodal_properties.h"
28 #endif
29 
30 namespace OHOS::Ace {
31 
32 enum class CheckableType {
33     RADIO,
34     CHECKBOX,
35     SWITCH,
36     UNKNOWN,
37 };
38 
39 enum class CheckableStatus {
40     ALL,
41     PART,
42     NONE,
43 };
44 
45 template<class T>
46 class CheckableValue {
47 public:
48     CheckableValue() = default;
CheckableValue(T value)49     explicit CheckableValue(T value) : value_(value) {}
50     virtual ~CheckableValue() = default;
51 
GetValue()52     T GetValue() const
53     {
54         return value_;
55     }
56 
SetValue(T value)57     void SetValue(T value)
58     {
59         value_ = value;
60     }
61 
62 private:
63     T value_;
64 };
65 
66 class ACE_EXPORT CheckableComponent : public RenderComponent {
67     DECLARE_ACE_TYPE(CheckableComponent, RenderComponent);
68 
69 public:
70     CheckableComponent(CheckableType type, const RefPtr<CheckableTheme>& theme);
71     ~CheckableComponent() override = default;
72 
73     RefPtr<RenderNode> CreateRenderNode() override;
74     RefPtr<Element> CreateElement() override;
75 
76     void ApplyTheme(const RefPtr<CheckableTheme>& theme);
77 
GetChangeEvent()78     EventMarker& GetChangeEvent()
79     {
80         return changeEvent_;
81     }
82 
SetChangeEvent(const EventMarker & changeEvent)83     void SetChangeEvent(const EventMarker& changeEvent)
84     {
85         changeEvent_ = changeEvent;
86     }
87 
GetClickEvent()88     const EventMarker& GetClickEvent() const
89     {
90         return clickEvent_;
91     }
92 
SetClickEvent(const EventMarker & clickEvent)93     void SetClickEvent(const EventMarker& clickEvent)
94     {
95         clickEvent_ = clickEvent;
96     }
97 
GetDomChangeEvent()98     const EventMarker& GetDomChangeEvent() const
99     {
100         return domChangeEvent_;
101     }
102 
SetDomChangeEvent(const EventMarker & domChangeEvent)103     void SetDomChangeEvent(const EventMarker& domChangeEvent)
104     {
105         domChangeEvent_ = domChangeEvent;
106     }
107 
GetWidth()108     const Dimension& GetWidth() const
109     {
110         return width_;
111     }
112 
GetHeight()113     const Dimension& GetHeight() const
114     {
115         return height_;
116     }
117 
SetWidth(const Dimension & width)118     void SetWidth(const Dimension& width)
119     {
120         width_ = width;
121     }
122 
SetHeight(const Dimension & height)123     void SetHeight(const Dimension& height)
124     {
125         height_ = height;
126     }
127 
SetHorizontalPadding(const Dimension & hotZoneHorizontalPadding)128     void SetHorizontalPadding(const Dimension& hotZoneHorizontalPadding)
129     {
130         hotZoneHorizontalPadding_ = hotZoneHorizontalPadding;
131     }
132 
GetHotZoneHorizontalPadding()133     const Dimension& GetHotZoneHorizontalPadding() const
134     {
135         return hotZoneHorizontalPadding_;
136     }
137 
SetHotZoneVerticalPadding(const Dimension & hotZoneVerticalPadding)138     void SetHotZoneVerticalPadding(const Dimension& hotZoneVerticalPadding)
139     {
140         hotZoneVerticalPadding_ = hotZoneVerticalPadding;
141     }
142 
GetHotZoneVerticalPadding()143     const Dimension& GetHotZoneVerticalPadding() const
144     {
145         return hotZoneVerticalPadding_;
146     }
147 
SetAspectRatio(double aspectRatio)148     void SetAspectRatio(double aspectRatio)
149     {
150         aspectRatio_ = aspectRatio;
151     }
152 
GetAspectRatio()153     double GetAspectRatio() const
154     {
155         return aspectRatio_;
156     }
157 
GetRadioInnerSizeRatio()158     double GetRadioInnerSizeRatio() const
159     {
160         return radioInnerSizeRatio_;
161     }
162 
IsBackgroundSolid()163     bool IsBackgroundSolid() const
164     {
165         return backgroundSolid_;
166     }
167 
SetBackgroundSolid(bool backgroundSolid)168     void SetBackgroundSolid(bool backgroundSolid)
169     {
170         backgroundSolid_ = backgroundSolid;
171     }
172 
IsDisabled()173     bool IsDisabled() const
174     {
175         return disabled_;
176     }
177 
SetDisabled(bool disabled)178     void SetDisabled(bool disabled)
179     {
180         disabled_ = disabled;
181     }
182 
GetPointColor()183     const Color& GetPointColor() const
184     {
185         return pointColor_;
186     }
187 
SetPointColor(const Color & pointColor)188     void SetPointColor(const Color& pointColor)
189     {
190         pointColor_ = pointColor;
191     }
192 
GetActiveColor()193     const Color& GetActiveColor() const
194     {
195         return activeColor_;
196     }
197 
SetActiveColor(const Color & activeColor)198     void SetActiveColor(const Color& activeColor)
199     {
200         activeColor_ = activeColor;
201     }
202 
GetInactiveColor()203     const Color& GetInactiveColor() const
204     {
205         return inactiveColor_;
206     }
207 
SetInactiveColor(const Color & inactiveColor)208     void SetInactiveColor(const Color& inactiveColor)
209     {
210         inactiveColor_ = inactiveColor;
211     }
212 
GetFocusColor()213     const Color& GetFocusColor() const
214     {
215         return focusColor_;
216     }
217 
SetFocusColor(const Color & focusColor)218     void SetFocusColor(const Color& focusColor)
219     {
220         focusColor_ = focusColor;
221     }
222 
SetDefaultWidth(const Dimension & defaultWidth)223     void SetDefaultWidth(const Dimension& defaultWidth)
224     {
225         defaultWidth_ = defaultWidth;
226     }
227 
GetDefaultWidth()228     const Dimension& GetDefaultWidth() const
229     {
230         return defaultWidth_;
231     }
232 
SetDefaultHeight(const Dimension & defaultHeight)233     void SetDefaultHeight(const Dimension& defaultHeight)
234     {
235         defaultHeight_ = defaultHeight;
236     }
237 
GetDefaultHeight()238     const Dimension& GetDefaultHeight() const
239     {
240         return defaultHeight_;
241     }
242 
SetNeedFocus(bool needFocus)243     void SetNeedFocus(bool needFocus)
244     {
245         needFocus_ = needFocus;
246     }
247 
GetNeedFocus()248     bool GetNeedFocus() const
249     {
250         return needFocus_;
251     }
252 
SetHoverColor(const Color & hoverColor)253     void SetHoverColor(const Color& hoverColor)
254     {
255         hoverColor_ = hoverColor;
256     }
257 
GetHoverColor()258     const Color& GetHoverColor() const
259     {
260         return hoverColor_;
261     }
262 
GetInactivePointColor()263     const Color& GetInactivePointColor() const
264     {
265         return inactivePointColor_;
266     }
267 
GetShadowColor()268     const Color& GetShadowColor() const
269     {
270         return shadowColor_;
271     }
272 
GetShadowWidth()273     const Dimension& GetShadowWidth() const
274     {
275         return shadowWidth_;
276     }
277 
GetHoverRadius()278     const Dimension& GetHoverRadius() const
279     {
280         return hoverRadius_;
281     }
282 
GetBorderWidth()283     const Dimension& GetBorderWidth() const
284     {
285         return borderWidth_;
286     }
287 
288     ACE_DEFINE_COMPONENT_EVENT(OnChange, void(bool));
289     ACE_DEFINE_COMPONENT_EVENT(OnClick, void());
290 
291 protected:
292     CheckableType checkableType_ = CheckableType::UNKNOWN;
293     Dimension width_;
294     Dimension height_;
295     Dimension hotZoneHorizontalPadding_;
296     Dimension hotZoneVerticalPadding_;
297     Dimension defaultWidth_;
298     Dimension defaultHeight_;
299     Dimension shadowWidth_;
300     Dimension hoverRadius_;
301     Dimension borderWidth_;
302     bool backgroundSolid_ = true;
303     bool disabled_ = false;
304     bool needFocus_ = true;
305     Color pointColor_;
306     Color activeColor_;
307     Color inactiveColor_;
308     Color inactivePointColor_;
309     Color focusColor_;
310     Color hoverColor_;
311     Color shadowColor_;
312     EventMarker changeEvent_;
313     EventMarker clickEvent_;
314     EventMarker domChangeEvent_;
315     double aspectRatio_ = 1.0;
316     double radioInnerSizeRatio_ = 0.5;
317 };
318 
319 class ACE_EXPORT CheckboxGroupResult : public BaseEventInfo {
320     DECLARE_RELATIONSHIP_OF_CLASSES(CheckboxGroupResult, BaseEventInfo);
321 
322 public:
CheckboxGroupResult(const std::vector<std::string> & vec,int32_t status)323     CheckboxGroupResult(const std::vector<std::string>& vec, int32_t status) :
324         BaseEventInfo("CheckboxGroupResult"), status_(status) {
325         for (int i = 0; i < static_cast<int32_t>(vec.size()); ++i) {
326             nameVector_.push_back(vec[i]);
327         }
328     }
329 
330     ~CheckboxGroupResult() = default;
331 
GetNameList()332     const std::vector<std::string>& GetNameList() const
333     {
334         return nameVector_;
335     }
336 
GetStatus()337     int32_t GetStatus() const
338     {
339         return status_;
340     }
341 
342 private:
343     std::vector<std::string> nameVector_;
344     int32_t status_ = 0;
345 };
346 
347 class ACE_EXPORT CheckboxComponent : public CheckableComponent, public CheckableValue<bool>, public LabelTarget {
348     DECLARE_ACE_TYPE(CheckboxComponent, CheckableComponent, LabelTarget);
349 
350 public:
351     explicit CheckboxComponent(const RefPtr<CheckboxTheme>& theme);
352     ~CheckboxComponent() override = default;
SetGroupName(const std::string & groupName)353     void SetGroupName(const std::string& groupName)
354     {
355         groupName_ = groupName;
356     }
357 
GetGroupName()358     const std::string& GetGroupName() const
359     {
360         return groupName_;
361     }
362 
SetGroupValue(const CheckableStatus value)363     void SetGroupValue(const CheckableStatus value)
364     {
365         groupValue_ = value;
366     }
367 
GetGroupValue()368     CheckableStatus GetGroupValue() const
369     {
370         return groupValue_;
371     }
372 
SetGroup(const RefPtr<CheckboxComponent> & groupComponent)373     void SetGroup(const RefPtr<CheckboxComponent>& groupComponent)
374     {
375         group_ = groupComponent;
376     }
377 
GetGroup()378     RefPtr<CheckboxComponent> GetGroup() const
379     {
380         return group_;
381     }
382 
SetCheckboxName(const std::string & checkboxName)383     void SetCheckboxName(const std::string& checkboxName)
384     {
385         checkboxName_ = checkboxName;
386     }
387 
GetCheckboxName()388     const std::string& GetCheckboxName() const
389     {
390         return checkboxName_;
391     }
392 
SetBelongGroup(const std::string & group)393     void SetBelongGroup(const std::string& group)
394     {
395         belongGroup_ = group;
396     }
397 
GetBelongGroup()398     const std::string& GetBelongGroup() const
399     {
400         return belongGroup_;
401     }
402 
SetGroupValueUpdateHandler(const std::function<void (CheckableStatus)> & value)403     void SetGroupValueUpdateHandler(const std::function<void(CheckableStatus)>& value)
404     {
405         groupValueUpdateHandler_ = value;
406     }
407 
UpdateRenderChecked(CheckableStatus checked)408     void UpdateRenderChecked(CheckableStatus checked)
409     {
410         if (groupValueUpdateHandler_) {
411             groupValueUpdateHandler_(checked);
412         }
413     }
414 
AddCheckbox(RefPtr<CheckboxComponent> checkboxComponent)415     void AddCheckbox(RefPtr<CheckboxComponent> checkboxComponent)
416     {
417         checkboxList_.push_back(checkboxComponent);
418     }
419 
SetItemValueUpdateHandler(const std::function<void (bool)> & value)420     void SetItemValueUpdateHandler(const std::function<void(bool)>& value)
421     {
422         itemValueUpdateHandler_ = value;
423     }
424 
UpdateItemChecked(bool checked)425     void UpdateItemChecked(bool checked)
426     {
427         if (itemValueUpdateHandler_) {
428             itemValueUpdateHandler_(checked);
429         }
430     }
431 
SetMember(bool isAllSelect)432     void SetMember(bool isAllSelect)
433     {
434         for (auto& item : checkboxList_) {
435             if (item->GetValue() == isAllSelect) {
436                 continue;
437             }
438             item->SetValue(isAllSelect);
439             item->UpdateItemChecked(isAllSelect);
440         }
441     }
442 
SetGroupStatus()443     void SetGroupStatus()
444     {
445         size_t count = 0;
446         for (auto& item : checkboxList_) {
447             if (item->GetValue()) {
448                 ++count;
449             }
450         }
451 
452         if (count == checkboxList_.size()) {
453             UpdateRenderChecked(CheckableStatus::ALL);
454         } else if (count ==0) {
455             UpdateRenderChecked(CheckableStatus::NONE);
456         } else {
457             UpdateRenderChecked(CheckableStatus::PART);
458         }
459     }
460 
GetCheckboxList()461     std::list<RefPtr<CheckboxComponent>> GetCheckboxList()
462     {
463         return checkboxList_;
464     }
465 
SetOnGroupChange(const EventMarker & OnGroupChange)466     void SetOnGroupChange(const EventMarker& OnGroupChange)
467     {
468         OnGroupChange_ = OnGroupChange;
469     }
470 
GetOnGroupChange()471     const EventMarker& GetOnGroupChange() const
472     {
473         return OnGroupChange_;
474     }
475 
GetSelectedCheckBoxName(std::vector<std::string> & result)476     void GetSelectedCheckBoxName(std::vector<std::string>& result)
477     {
478         for (auto& item : checkboxList_) {
479             if (item->GetValue()) {
480                 result.push_back(item->GetCheckboxName());
481             }
482         }
483     }
484 
GetUngroupedCheckboxs()485     static std::unordered_map<std::string, std::list<WeakPtr<CheckboxComponent>>>& GetUngroupedCheckboxs()
486     {
487         return ungroupedCheckboxs_;
488     }
489 
GetCheckboxGroupComponent()490     static std::unordered_map<std::string, RefPtr<CheckboxComponent>>& GetCheckboxGroupComponent()
491     {
492         return checkboxGroups_;
493     }
494 
495 private:
496     CheckableStatus groupValue_ = CheckableStatus::NONE;
497     std::list<RefPtr<CheckboxComponent>> checkboxList_;
498     RefPtr<CheckboxComponent> group_;
499     std::string groupName_ = "";
500     std::string checkboxName_ = "";
501     std::string belongGroup_ = "";
502     std::function<void(bool)> itemValueUpdateHandler_;
503     std::function<void(CheckableStatus)> groupValueUpdateHandler_;
504     EventMarker OnGroupChange_;
505     static std::unordered_map<std::string, std::list<WeakPtr<CheckboxComponent>>> ungroupedCheckboxs_;
506     static std::unordered_map<std::string, RefPtr<CheckboxComponent>> checkboxGroups_;
507 };
508 
509 class ACE_EXPORT SwitchComponent : public CheckableComponent, public CheckableValue<bool> {
510     DECLARE_ACE_TYPE(SwitchComponent, CheckableComponent);
511 
512 public:
513     explicit SwitchComponent(const RefPtr<SwitchTheme>& theme);
514     ~SwitchComponent() override = default;
515 
SetTextStyle(const TextStyle & textStyle)516     void SetTextStyle(const TextStyle& textStyle)
517     {
518         textStyle_ = textStyle;
519     }
520 
GetTextStyle()521     const TextStyle& GetTextStyle() const
522     {
523         return textStyle_;
524     }
525 
GetTextOn()526     const std::string& GetTextOn() const
527     {
528         return textOn_;
529     }
530 
SetTextOn(const std::string & textOn)531     void SetTextOn(const std::string& textOn)
532     {
533         textOn_ = textOn;
534     }
535 
GetTextOff()536     const std::string& GetTextOff() const
537     {
538         return textOff_;
539     }
540 
SetTextOff(const std::string & textOff)541     void SetTextOff(const std::string& textOff)
542     {
543         textOff_ = textOff;
544     }
545 
GetShowText()546     bool GetShowText() const
547     {
548         return showText_;
549     }
550 
SetShowText(bool showText)551     void SetShowText(bool showText)
552     {
553         showText_ = showText;
554     }
555 
GetTextColorOn()556     const Color& GetTextColorOn() const
557     {
558         return textColorOn_;
559     }
560 
SetTextColorOn(const Color & textColorOn)561     void SetTextColorOn(const Color& textColorOn)
562     {
563         textColorOn_ = textColorOn;
564     }
565 
GetTextColorOff()566     const Color& GetTextColorOff() const
567     {
568         return textColorOff_;
569     }
570 
SetTextColorOff(const Color & textColorOff)571     void SetTextColorOff(const Color& textColorOff)
572     {
573         textColorOff_ = textColorOff;
574     }
575 
GetTextPadding()576     const Dimension& GetTextPadding() const
577     {
578         return textPadding_;
579     }
580 
SetTextPadding(const Dimension & textPadding)581     void SetTextPadding(const Dimension& textPadding)
582     {
583         textPadding_ = textPadding;
584     }
585 
586 #ifndef WEARABLE_PRODUCT
GetMultimodalProperties()587     const CommonMultimodalAttribute& GetMultimodalProperties() const
588     {
589         return multimodalProperties_;
590     }
591 
SetMultimodalProperties(const CommonMultimodalAttribute & multimodalProperties)592     void SetMultimodalProperties(const CommonMultimodalAttribute& multimodalProperties)
593     {
594         multimodalProperties_ = multimodalProperties;
595     }
596 #endif
597 
598 private:
599     std::string textOn_ = "On";
600     std::string textOff_ = "Off";
601     Color textColorOn_ = Color::BLACK;
602     Color textColorOff_ = Color::BLACK;
603     bool showText_ = false;
604     TextStyle textStyle_;
605 #ifndef WEARABLE_PRODUCT
606     CommonMultimodalAttribute multimodalProperties_;
607 #endif
608     Dimension textPadding_ { 0, DimensionUnit::PX };
609 };
610 
611 template<class VALUE_TYPE>
612 class ACE_EXPORT RadioComponent : public CheckableComponent, public CheckableValue<VALUE_TYPE>, public LabelTarget {
613     DECLARE_ACE_TYPE(RadioComponent<VALUE_TYPE>, CheckableComponent, LabelTarget);
614 
615 public:
RadioComponent(const RefPtr<RadioTheme> & theme)616     explicit RadioComponent(const RefPtr<RadioTheme>& theme) : CheckableComponent(CheckableType::RADIO, theme) {}
617     ~RadioComponent() override = default;
618 
GetGroupValue()619     VALUE_TYPE GetGroupValue() const
620     {
621         return groupValue_;
622     }
623 
SetGroupValue(VALUE_TYPE groupValue)624     void SetGroupValue(VALUE_TYPE groupValue)
625     {
626         groupValue_ = groupValue;
627     }
628 
GetOriginChecked()629     bool GetOriginChecked()
630     {
631         return originChecked_;
632     }
633 
SetOriginChecked(bool checked)634     void SetOriginChecked(bool checked)
635     {
636         originChecked_ = checked;
637     }
638 
GetGroupValueChangedListener()639     const std::function<void(VALUE_TYPE)>& GetGroupValueChangedListener() const
640     {
641         return groupValueChangedListener_;
642     }
643 
SetGroupValueChangedListener(const std::function<void (VALUE_TYPE)> & groupValueChangedListener)644     void SetGroupValueChangedListener(const std::function<void(VALUE_TYPE)>& groupValueChangedListener)
645     {
646         groupValueChangedListener_ = groupValueChangedListener;
647     }
648 
SetGroupValueUpdateHandler(const std::function<void (VALUE_TYPE)> & groupValueUpdateHandler)649     void SetGroupValueUpdateHandler(const std::function<void(VALUE_TYPE)>& groupValueUpdateHandler)
650     {
651         groupValueUpdateHandler_ = groupValueUpdateHandler;
652     }
653 
UpdateGroupValue(VALUE_TYPE groupValue)654     void UpdateGroupValue(VALUE_TYPE groupValue)
655     {
656         if (groupValueUpdateHandler_) {
657             groupValueUpdateHandler_(groupValue);
658         }
659     }
660 
GetGroupName()661     const std::string& GetGroupName() const
662     {
663         return groupName_;
664     }
SetGroupName(const std::string & groupName)665     void SetGroupName(const std::string& groupName)
666     {
667         groupName_ = groupName;
668     }
669 
670 private:
671     VALUE_TYPE groupValue_;
672     std::string groupName_;
673     std::function<void(VALUE_TYPE)> groupValueChangedListener_;
674     std::function<void(VALUE_TYPE)> groupValueUpdateHandler_;
675     bool originChecked_ = false;
676 };
677 
678 } // namespace OHOS::Ace
679 
680 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_COMPONENT_H
681