• 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         auto size = paintWrapper->GetContentSize();
48         auto offset = paintWrapper->GetContentOffset();
49         float strokePaintSize = size.Width();
50         if (paintProperty->GetCheckBoxGroupSelectedColor().has_value()) {
51             checkboxGroupModifier_->SetActiveColor(paintProperty->GetCheckBoxGroupSelectedColor().value());
52         }
53         if (paintProperty->GetCheckBoxGroupUnSelectedColor().has_value()) {
54             checkboxGroupModifier_->SetInactiveColor(paintProperty->GetCheckBoxGroupUnSelectedColor().value());
55         }
56         if (paintProperty->GetCheckBoxGroupCheckMarkColor().has_value()) {
57             checkboxGroupModifier_->SetPointColor(paintProperty->GetCheckBoxGroupCheckMarkColor().value());
58         }
59         if (paintProperty->GetCheckBoxGroupCheckMarkSize().has_value()) {
60             if (paintProperty->GetCheckBoxGroupCheckMarkSizeValue().ConvertToPx() >= 0) {
61                 strokePaintSize = paintProperty->GetCheckBoxGroupCheckMarkSizeValue().ConvertToPx();
62             }
63             if (strokePaintSize > size.Width()) {
64                 strokePaintSize = size.Width();
65             }
66         }
67         checkboxGroupModifier_->SetCheckMarkPaintSize(strokePaintSize);
68         if (paintProperty->GetCheckBoxGroupCheckMarkWidth().has_value()) {
69             auto strokeWidth = paintProperty->GetCheckBoxGroupCheckMarkWidthValue().ConvertToPx();
70             auto strokeLimitByMarkSize = strokePaintSize * CHECKBOXGROUP_MARK_STROKEWIDTH_LIMIT_RATIO;
71             if (strokeWidth > strokeLimitByMarkSize) {
72                 strokeWidth = strokeLimitByMarkSize;
73             }
74             checkboxGroupModifier_->SetCheckStroke(strokeWidth);
75         }
76         auto selectStatus = paintProperty->GetSelectStatus();
77 
78         checkboxGroupModifier_->SetEnabled(enabled_);
79         checkboxGroupModifier_->SetUiStatus(uiStatus_);
80         checkboxGroupModifier_->SetSelectStatus(selectStatus);
81         checkboxGroupModifier_->SetOffset(offset);
82         checkboxGroupModifier_->SetSize(size);
83         checkboxGroupModifier_->SetTouchHoverAnimationType(touchHoverType_);
84         checkboxGroupModifier_->UpdateAnimatableProperty();
85         auto pipeline = PipelineBase::GetCurrentContext();
86         CHECK_NULL_VOID(pipeline);
87         auto checkboxTheme = pipeline->GetTheme<CheckboxTheme>();
88         auto horizontalPadding = checkboxTheme->GetHotZoneHorizontalPadding().ConvertToPx();
89         auto verticalPadding = checkboxTheme->GetHotZoneVerticalPadding().ConvertToPx();
90         float boundsRectOriginX = offset.GetX() - horizontalPadding;
91         float boundsRectOriginY = offset.GetY() - verticalPadding;
92         float boundsRectWidth = size.Width() + 2 * horizontalPadding;
93         float boundsRectHeight = size.Height() + 2 * verticalPadding;
94         RectF boundsRect(boundsRectOriginX, boundsRectOriginY, boundsRectWidth, boundsRectHeight);
95         checkboxGroupModifier_->SetBoundsRect(boundsRect);
96         checkboxGroupModifier_->SetInactivePointColor(checkboxTheme->GetInactivePointColor());
97     }
98 
SetEnabled(bool enabled)99     void SetEnabled(bool enabled)
100     {
101         enabled_ = enabled;
102     }
103 
SetUiStatus(const UIStatus uiStatus)104     void SetUiStatus(const UIStatus uiStatus)
105     {
106         uiStatus_ = uiStatus;
107     }
108 
SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)109     void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)
110     {
111         touchHoverType_ = touchHoverType;
112     }
113 
SetHotZoneOffset(OffsetF & hotZoneOffset)114     void SetHotZoneOffset(OffsetF& hotZoneOffset)
115     {
116         hotZoneOffset_ = hotZoneOffset;
117     }
118 
SetHotZoneSize(SizeF & hotZoneSize)119     void SetHotZoneSize(SizeF& hotZoneSize)
120     {
121         hotZoneSize_ = hotZoneSize;
122     }
123 
124 private:
125     bool enabled_ = true;
126     UIStatus uiStatus_ = UIStatus::UNSELECTED;
127     OffsetF hotZoneOffset_;
128     SizeF hotZoneSize_;
129     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
130     RefPtr<CheckBoxGroupModifier> checkboxGroupModifier_;
131 };
132 } // namespace OHOS::Ace::NG
133 
134 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_CHECKBOXGROUP_CHECKBOXGROUP_PAINT_METHOD_H
135