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_CHECKABLE_RENDER_CHECKBOX_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_RENDER_CHECKBOX_H
18 
19 #include "core/animation/animator.h"
20 #include "core/animation/curve_animation.h"
21 #include "core/components/checkable/checkable_component.h"
22 #include "core/components/checkable/render_checkable.h"
23 
24 namespace OHOS::Ace {
25 
26 class RenderCheckbox : public RenderCheckable {
27     DECLARE_ACE_TYPE(RenderCheckbox, RenderCheckable);
28 
29 public:
30     RenderCheckbox() = default;
31     ~RenderCheckbox() override = default;
32 
33     static RefPtr<RenderNode> Create();
34     void Update(const RefPtr<Component>& component) override;
35     void HandleClick() override;
36     bool UpdateItemValue(const bool itemValue);
37     bool UpdateGroupValue(const CheckableStatus groupValue);
SetChecked(bool checked)38     void SetChecked(bool checked)
39     {
40         checked_ = checked;
41     }
42 
GetCheckBoxValue()43     bool GetCheckBoxValue()
44     {
45         if (component_) {
46             return component_->GetValue();
47         }
48         return false;
49     }
50 
GetBelongGroup()51     std::string GetBelongGroup() const
52     {
53         if (component_) {
54             return component_->GetBelongGroup();
55         }
56         return "";
57     }
58 
GetCheckboxName()59     std::string GetCheckboxName() const
60     {
61         if (component_) {
62             return component_->GetCheckboxName();
63         }
64         return "";
65     }
66 
GetGroupName()67     std::string GetGroupName() const
68     {
69         if (component_) {
70             return component_->GetGroupName();
71         }
72         return "";
73     }
74 
GetSelectedColor()75     const Color& GetSelectedColor() const
76     {
77         if (component_) {
78             return component_->GetActiveColor();
79         }
80         return Color::TRANSPARENT;
81     }
82 
UpdateUIStatus()83     void UpdateUIStatus() override
84     {
85         if (!(component_->GetGroupName().empty())) {
86             auto value = component_->GetGroupValue();
87             if (value == CheckableStatus::ALL) {
88                 uiStatus_ = UIStatus::SELECTED;
89             } else if (value == CheckableStatus::PART) {
90                 uiStatus_ = UIStatus::PART;
91             } else if (value == CheckableStatus::NONE) {
92                 uiStatus_ = UIStatus::UNSELECTED;
93             }
94         } else if (!(component_->GetCheckboxName().empty())) {
95             bool isCheck = component_->GetValue();
96             uiStatus_ = isCheck ? UIStatus::SELECTED :
97                 ((onFocus_ && needFocus_) ? UIStatus::FOCUS : UIStatus::UNSELECTED);
98         } else {
99             uiStatus_ = checked_ ? UIStatus::SELECTED : UIStatus::UNSELECTED;
100         }
101     }
102 
GetCheckboxComponent()103     RefPtr<CheckboxComponent> GetCheckboxComponent() const
104     {
105         return component_;
106     }
107 
108     std::string ProvideRestoreInfo() override;
109 
110 protected:
111     void UpdateAnimation();
112     void OnAnimationStop();
113     void UpdateCheckBoxShape(double value);
114     void UpdateAccessibilityAttr();
115     void UpdateGroupStatus();
116 
117     enum class SelectStatus {
118         ALL = 0,
119         PART,
120         NONE,
121     };
122 
123     // animation control
124     RefPtr<Animator> controller_;
125     RefPtr<CurveAnimation<double>> translate_;
126 
127     SelectStatus status_ = SelectStatus::ALL;
128     SelectStatus lastStatus_ = SelectStatus::ALL;
129     double shapeScale_ = 1.0;
130     RefPtr<CheckboxComponent> component_;
131     bool isGroup_ = false;
132     bool selectAll_ = false;
133     bool firstUpdate_ = true;
134     std::function<void(bool)> groupValueChangedListener_;
135     std::function<void(const std::shared_ptr<BaseEventInfo>&)> onGroupChange_;
136 
137 private:
138     void SetAccessibilityClickImpl();
139     void ApplyRestoreInfo();
140 };
141 
142 } // namespace OHOS::Ace
143 
144 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_RENDER_CHECKBOX_H
145