• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         CHECK_NULL_VOID(paintProperty);
48         auto size = paintWrapper->GetContentSize();
49         auto offset = paintWrapper->GetContentOffset();
50         float strokePaintSize = size.Width();
51         SetModifierContentType(paintProperty);
52         if (paintProperty->GetCheckBoxGroupCheckMarkSize().has_value()) {
53             if (paintProperty->GetCheckBoxGroupCheckMarkSizeValue().ConvertToPx() >= 0) {
54                 strokePaintSize = paintProperty->GetCheckBoxGroupCheckMarkSizeValue().ConvertToPx();
55             }
56             if (strokePaintSize > size.Width()) {
57                 strokePaintSize = size.Width();
58             }
59         }
60         checkboxGroupModifier_->SetCheckMarkPaintSize(strokePaintSize);
61         if (paintProperty->GetCheckBoxGroupCheckMarkWidth().has_value()) {
62             auto strokeWidth = paintProperty->GetCheckBoxGroupCheckMarkWidthValue().ConvertToPx();
63             auto strokeLimitByMarkSize = strokePaintSize * CHECKBOXGROUP_MARK_STROKEWIDTH_LIMIT_RATIO;
64             if (strokeWidth > strokeLimitByMarkSize) {
65                 strokeWidth = strokeLimitByMarkSize;
66             }
67             checkboxGroupModifier_->SetCheckStroke(strokeWidth);
68         }
69         auto selectStatus = paintProperty->GetSelectStatus();
70 
71         checkboxGroupModifier_->SetEnabled(enabled_);
72         checkboxGroupModifier_->SetUiStatus(uiStatus_);
73         checkboxGroupModifier_->SetSelectStatus(selectStatus);
74         checkboxGroupModifier_->SetOffset(offset);
75         checkboxGroupModifier_->SetSize(size);
76         checkboxGroupModifier_->SetTouchHoverAnimationType(touchHoverType_);
77         checkboxGroupModifier_->UpdateAnimatableProperty();
78         auto pipeline = PipelineBase::GetCurrentContext();
79         CHECK_NULL_VOID(pipeline);
80         auto host = paintWrapper->GetRenderContext() ? paintWrapper->GetRenderContext()->GetHost() : nullptr;
81         auto checkboxTheme = pipeline->GetTheme<CheckboxTheme>(host ? host->GetThemeScopeId() : 0);
82         CHECK_NULL_VOID(checkboxTheme);
83         auto horizontalPadding = checkboxTheme->GetHotZoneHorizontalPadding().ConvertToPx();
84         auto verticalPadding = checkboxTheme->GetHotZoneVerticalPadding().ConvertToPx();
85         float boundsRectOriginX = offset.GetX() - horizontalPadding;
86         float boundsRectOriginY = offset.GetY() - verticalPadding;
87         float boundsRectWidth = size.Width() + 2 * horizontalPadding;
88         float boundsRectHeight = size.Height() + 2 * verticalPadding;
89         RectF boundsRect(boundsRectOriginX, boundsRectOriginY, boundsRectWidth, boundsRectHeight);
90         checkboxGroupModifier_->SetBoundsRect(boundsRect);
91         SetHoverEffectType(paintProperty);
92         checkboxGroupModifier_->SetInactivePointColor(checkboxTheme->GetInactivePointColor());
93     }
94 
SetModifierContentType(const RefPtr<CheckBoxGroupPaintProperty> & paintProperty)95     void SetModifierContentType(const RefPtr<CheckBoxGroupPaintProperty>& paintProperty)
96     {
97         CHECK_NULL_VOID(paintProperty);
98         if (paintProperty->GetCheckBoxGroupSelectedColor().has_value()) {
99             checkboxGroupModifier_->SetActiveColor(paintProperty->GetCheckBoxGroupSelectedColor().value());
100         }
101         if (paintProperty->GetCheckBoxGroupUnSelectedColor().has_value()) {
102             checkboxGroupModifier_->SetInactiveColor(paintProperty->GetCheckBoxGroupUnSelectedColor().value());
103         }
104         if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
105             if (paintProperty->HasCheckBoxGroupSelectedStyle()) {
106                 checkboxGroupModifier_->SetCheckboxGroupStyle(paintProperty->GetCheckBoxGroupSelectedStyleValue());
107             }
108         }
109         if (paintProperty->GetCheckBoxGroupCheckMarkColor().has_value()) {
110             checkboxGroupModifier_->SetPointColor(paintProperty->GetCheckBoxGroupCheckMarkColor().value());
111         }
112     }
113 
SetHoverEffectType(const RefPtr<CheckBoxGroupPaintProperty> & checkBoxgroupPaintProperty)114     void SetHoverEffectType(const RefPtr<CheckBoxGroupPaintProperty>& checkBoxgroupPaintProperty)
115     {
116         auto host = checkBoxgroupPaintProperty->GetHost();
117         CHECK_NULL_VOID(host);
118         auto eventHub = host->GetEventHub<EventHub>();
119         CHECK_NULL_VOID(eventHub);
120         auto inputEventHub = eventHub->GetInputEventHub();
121         HoverEffectType hoverEffectType = HoverEffectType::AUTO;
122         if (inputEventHub) {
123             hoverEffectType = inputEventHub->GetHoverEffect();
124             if (HoverEffectType::UNKNOWN == hoverEffectType || HoverEffectType::OPACITY == hoverEffectType) {
125                 hoverEffectType = HoverEffectType::AUTO;
126             }
127             if (checkboxGroupModifier_) {
128                 checkboxGroupModifier_->SetHoverEffectType(hoverEffectType);
129             }
130         }
131     }
132 
SetEnabled(bool enabled)133     void SetEnabled(bool enabled)
134     {
135         enabled_ = enabled;
136     }
137 
SetUiStatus(const UIStatus uiStatus)138     void SetUiStatus(const UIStatus uiStatus)
139     {
140         uiStatus_ = uiStatus;
141     }
142 
SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)143     void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)
144     {
145         touchHoverType_ = touchHoverType;
146     }
147 
SetHotZoneOffset(OffsetF & hotZoneOffset)148     void SetHotZoneOffset(OffsetF& hotZoneOffset)
149     {
150         hotZoneOffset_ = hotZoneOffset;
151     }
152 
SetHotZoneSize(SizeF & hotZoneSize)153     void SetHotZoneSize(SizeF& hotZoneSize)
154     {
155         hotZoneSize_ = hotZoneSize;
156     }
157 
158 private:
159     bool enabled_ = true;
160     UIStatus uiStatus_ = UIStatus::UNSELECTED;
161     OffsetF hotZoneOffset_;
162     SizeF hotZoneSize_;
163     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
164     RefPtr<CheckBoxGroupModifier> checkboxGroupModifier_;
165 };
166 } // namespace OHOS::Ace::NG
167 
168 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_CHECKBOXGROUP_CHECKBOXGROUP_PAINT_METHOD_H
169