1 /*
2  * Copyright (c) 2022-2023 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_NG_PAINTS_CHECKBOXGROUP_CHECKBOXGROUP_PAINT_METHOD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_CHECKBOXGROUP_CHECKBOXGROUP_PAINT_METHOD_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_modifier.h"
21 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_paint_property.h"
22 #include "core/components_ng/render/node_paint_method.h"
23 
24 namespace OHOS::Ace::NG {
25 constexpr float CHECKBOXGROUP_MARK_STROKEWIDTH_LIMIT_RATIO = 0.25f;
26 
27 class CheckBoxGroupPaintMethod : public NodePaintMethod {
DECLARE_ACE_TYPE(CheckBoxGroupPaintMethod,NodePaintMethod)28     DECLARE_ACE_TYPE(CheckBoxGroupPaintMethod, NodePaintMethod)
29 
30 public:
31     explicit CheckBoxGroupPaintMethod(const RefPtr<CheckBoxGroupModifier>& checkboxGroupModifier)
32         : checkboxGroupModifier_(checkboxGroupModifier) {};
33 
34     ~CheckBoxGroupPaintMethod() override = default;
35 
GetContentModifier(PaintWrapper *)36     RefPtr<Modifier> GetContentModifier(PaintWrapper* /*paintWrapper*/) override
37     {
38         CHECK_NULL_RETURN(checkboxGroupModifier_, nullptr);
39         return checkboxGroupModifier_;
40     }
41 
UpdateContentModifier(PaintWrapper * paintWrapper)42     void UpdateContentModifier(PaintWrapper* paintWrapper) override
43     {
44         CHECK_NULL_VOID(checkboxGroupModifier_);
45         CHECK_NULL_VOID(paintWrapper);
46         auto paintProperty = DynamicCast<CheckBoxGroupPaintProperty>(paintWrapper->GetPaintProperty());
47         auto size = paintWrapper->GetContentSize();
48         auto offset = paintWrapper->GetContentOffset();
49         float strokePaintSize = size.Width();
50         SetModifierContentType(paintProperty);
51         if (paintProperty->GetCheckBoxGroupCheckMarkSize().has_value()) {
52             if (paintProperty->GetCheckBoxGroupCheckMarkSizeValue().ConvertToPx() >= 0) {
53                 strokePaintSize = paintProperty->GetCheckBoxGroupCheckMarkSizeValue().ConvertToPx();
54             }
55             if (strokePaintSize > size.Width()) {
56                 strokePaintSize = size.Width();
57             }
58         }
59         checkboxGroupModifier_->SetCheckMarkPaintSize(strokePaintSize);
60         if (paintProperty->GetCheckBoxGroupCheckMarkWidth().has_value()) {
61             auto strokeWidth = paintProperty->GetCheckBoxGroupCheckMarkWidthValue().ConvertToPx();
62             auto strokeLimitByMarkSize = strokePaintSize * CHECKBOXGROUP_MARK_STROKEWIDTH_LIMIT_RATIO;
63             if (strokeWidth > strokeLimitByMarkSize) {
64                 strokeWidth = strokeLimitByMarkSize;
65             }
66             checkboxGroupModifier_->SetCheckStroke(strokeWidth);
67         }
68         auto selectStatus = paintProperty->GetSelectStatus();
69 
70         checkboxGroupModifier_->SetEnabled(enabled_);
71         checkboxGroupModifier_->SetUiStatus(uiStatus_);
72         checkboxGroupModifier_->SetSelectStatus(selectStatus);
73         checkboxGroupModifier_->SetOffset(offset);
74         checkboxGroupModifier_->SetSize(size);
75         checkboxGroupModifier_->SetTouchHoverAnimationType(touchHoverType_);
76         checkboxGroupModifier_->UpdateAnimatableProperty();
77         auto pipeline = PipelineBase::GetCurrentContext();
78         CHECK_NULL_VOID(pipeline);
79         auto checkboxTheme = pipeline->GetTheme<CheckboxTheme>();
80         auto horizontalPadding = checkboxTheme->GetHotZoneHorizontalPadding().ConvertToPx();
81         auto verticalPadding = checkboxTheme->GetHotZoneVerticalPadding().ConvertToPx();
82         float boundsRectOriginX = offset.GetX() - horizontalPadding;
83         float boundsRectOriginY = offset.GetY() - verticalPadding;
84         float boundsRectWidth = size.Width() + 2 * horizontalPadding;
85         float boundsRectHeight = size.Height() + 2 * verticalPadding;
86         RectF boundsRect(boundsRectOriginX, boundsRectOriginY, boundsRectWidth, boundsRectHeight);
87         checkboxGroupModifier_->SetBoundsRect(boundsRect);
88         SetHoverEffectType(paintProperty);
89         checkboxGroupModifier_->SetInactivePointColor(checkboxTheme->GetInactivePointColor());
90     }
91 
SetModifierContentType(const RefPtr<CheckBoxGroupPaintProperty> & paintProperty)92     void SetModifierContentType(const RefPtr<CheckBoxGroupPaintProperty>& paintProperty)
93     {
94         CHECK_NULL_VOID(paintProperty);
95         if (paintProperty->GetCheckBoxGroupSelectedColor().has_value()) {
96             checkboxGroupModifier_->SetActiveColor(paintProperty->GetCheckBoxGroupSelectedColor().value());
97         }
98         if (paintProperty->GetCheckBoxGroupUnSelectedColor().has_value()) {
99             checkboxGroupModifier_->SetInactiveColor(paintProperty->GetCheckBoxGroupUnSelectedColor().value());
100         }
101         if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
102             if (paintProperty->HasCheckBoxGroupSelectedStyle()) {
103                 checkboxGroupModifier_->SetCheckboxGroupStyle(paintProperty->GetCheckBoxGroupSelectedStyleValue());
104             }
105         }
106         if (paintProperty->GetCheckBoxGroupCheckMarkColor().has_value()) {
107             checkboxGroupModifier_->SetPointColor(paintProperty->GetCheckBoxGroupCheckMarkColor().value());
108         }
109     }
110 
SetHoverEffectType(const RefPtr<CheckBoxGroupPaintProperty> & checkBoxgroupPaintProperty)111     void SetHoverEffectType(const RefPtr<CheckBoxGroupPaintProperty>& checkBoxgroupPaintProperty)
112     {
113         auto host = checkBoxgroupPaintProperty->GetHost();
114         CHECK_NULL_VOID(host);
115         auto eventHub = host->GetEventHub<EventHub>();
116         CHECK_NULL_VOID(eventHub);
117         auto inputEventHub = eventHub->GetInputEventHub();
118         HoverEffectType hoverEffectType = HoverEffectType::AUTO;
119         if (inputEventHub) {
120             hoverEffectType = inputEventHub->GetHoverEffect();
121             if (HoverEffectType::UNKNOWN == hoverEffectType || HoverEffectType::OPACITY == hoverEffectType) {
122                 hoverEffectType = HoverEffectType::AUTO;
123             }
124             if (checkboxGroupModifier_) {
125                 checkboxGroupModifier_->SetHoverEffectType(hoverEffectType);
126             }
127         }
128     }
129 
SetEnabled(bool enabled)130     void SetEnabled(bool enabled)
131     {
132         enabled_ = enabled;
133     }
134 
SetUiStatus(const UIStatus uiStatus)135     void SetUiStatus(const UIStatus uiStatus)
136     {
137         uiStatus_ = uiStatus;
138     }
139 
SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)140     void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)
141     {
142         touchHoverType_ = touchHoverType;
143     }
144 
SetHotZoneOffset(OffsetF & hotZoneOffset)145     void SetHotZoneOffset(OffsetF& hotZoneOffset)
146     {
147         hotZoneOffset_ = hotZoneOffset;
148     }
149 
SetHotZoneSize(SizeF & hotZoneSize)150     void SetHotZoneSize(SizeF& hotZoneSize)
151     {
152         hotZoneSize_ = hotZoneSize;
153     }
154 
155 private:
156     bool enabled_ = true;
157     UIStatus uiStatus_ = UIStatus::UNSELECTED;
158     OffsetF hotZoneOffset_;
159     SizeF hotZoneSize_;
160     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
161     RefPtr<CheckBoxGroupModifier> checkboxGroupModifier_;
162 };
163 } // namespace OHOS::Ace::NG
164 
165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_CHECKBOXGROUP_CHECKBOXGROUP_PAINT_METHOD_H
166