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_CHECKBOX_CHECKBOX_PAINT_METHOD_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_CHECKBOX_CHECKBOX_PAINT_METHOD_H 18 19 #include "base/memory/ace_type.h" 20 #include "base/utils/macros.h" 21 #include "core/components_ng/pattern/checkbox/checkbox_modifier.h" 22 #include "core/components_ng/pattern/checkbox/checkbox_paint_property.h" 23 #include "core/components_ng/render/node_paint_method.h" 24 namespace OHOS::Ace::NG { 25 constexpr float CHECKBOX_MARK_STROKEWIDTH_LIMIT_RATIO = 0.25f; 26 class CheckBoxPaintMethod : public NodePaintMethod { DECLARE_ACE_TYPE(CheckBoxPaintMethod,NodePaintMethod)27 DECLARE_ACE_TYPE(CheckBoxPaintMethod, NodePaintMethod) 28 29 public: 30 explicit CheckBoxPaintMethod(const RefPtr<CheckBoxModifier>& checkboxModifier) : checkboxModifier_(checkboxModifier) 31 {} 32 33 ~CheckBoxPaintMethod() override = default; 34 GetContentModifier(PaintWrapper * paintWrapper)35 RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override 36 { 37 CHECK_NULL_RETURN(checkboxModifier_, nullptr); 38 return checkboxModifier_; 39 } 40 UpdateContentModifier(PaintWrapper * paintWrapper)41 void UpdateContentModifier(PaintWrapper* paintWrapper) override 42 { 43 CHECK_NULL_VOID(checkboxModifier_); 44 checkboxModifier_->InitializeParam(); 45 CHECK_NULL_VOID(paintWrapper); 46 auto size = paintWrapper->GetContentSize(); 47 auto offset = paintWrapper->GetContentOffset(); 48 float strokePaintSize = size.Width(); 49 auto paintProperty = DynamicCast<CheckBoxPaintProperty>(paintWrapper->GetPaintProperty()); 50 if (paintProperty->GetCheckBoxSelect().has_value()) { 51 checkboxModifier_->SetIsSelect(paintProperty->GetCheckBoxSelectValue()); 52 } 53 if (paintProperty->HasCheckBoxSelectedColor()) { 54 checkboxModifier_->SetUserActiveColor(paintProperty->GetCheckBoxSelectedColorValue()); 55 } 56 if (paintProperty->HasCheckBoxUnSelectedColor()) { 57 checkboxModifier_->SetInActiveColor(paintProperty->GetCheckBoxUnSelectedColorValue()); 58 } 59 if (paintProperty->HasCheckBoxCheckMarkColor()) { 60 checkboxModifier_->SetPointColor(paintProperty->GetCheckBoxCheckMarkColorValue()); 61 } 62 if (paintProperty->HasCheckBoxCheckMarkSize()) { 63 if (paintProperty->GetCheckBoxCheckMarkSizeValue().ConvertToPx() >= 0) { 64 strokePaintSize = paintProperty->GetCheckBoxCheckMarkSizeValue().ConvertToPx(); 65 } 66 if (strokePaintSize > size.Width()) { 67 strokePaintSize = size.Width(); 68 } 69 } 70 checkboxModifier_->SetStrokeSize(strokePaintSize); 71 if (paintProperty->HasCheckBoxCheckMarkWidth()) { 72 auto strokeWidth = paintProperty->GetCheckBoxCheckMarkWidthValue().ConvertToPx(); 73 auto strokeLimitByMarkSize = strokePaintSize * CHECKBOX_MARK_STROKEWIDTH_LIMIT_RATIO; 74 if (strokeWidth > strokeLimitByMarkSize) { 75 strokeWidth = strokeLimitByMarkSize; 76 } 77 checkboxModifier_->SetStrokeWidth(strokeWidth); 78 } 79 80 checkboxModifier_->SetSize(size); 81 checkboxModifier_->SetOffset(offset); 82 checkboxModifier_->SetEnabled(enabled_); 83 checkboxModifier_->SetTouchHoverAnimationType(touchHoverType_); 84 checkboxModifier_->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 checkboxModifier_->SetBoundsRect(boundsRect); 96 } 97 SetHotZoneOffset(OffsetF & hotZoneOffset)98 void SetHotZoneOffset(OffsetF& hotZoneOffset) 99 { 100 hotZoneOffset_ = hotZoneOffset; 101 } 102 SetHotZoneSize(SizeF & hotZoneSize)103 void SetHotZoneSize(SizeF& hotZoneSize) 104 { 105 hotZoneSize_ = hotZoneSize; 106 } 107 SetEnabled(bool enabled)108 void SetEnabled(bool enabled) 109 { 110 enabled_ = enabled; 111 } 112 SetTotalScale(float totalScale)113 void SetTotalScale(float totalScale) 114 { 115 totalScale_ = totalScale; 116 } 117 SetPointScale(float pointScale)118 void SetPointScale(float pointScale) 119 { 120 pointScale_ = pointScale; 121 } 122 SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)123 void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType) 124 { 125 touchHoverType_ = touchHoverType; 126 } 127 128 private: 129 bool enabled_ = true; 130 float totalScale_ = 1.0f; 131 float pointScale_ = 0.5f; 132 OffsetF hotZoneOffset_; 133 SizeF hotZoneSize_; 134 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 135 136 RefPtr<CheckBoxModifier> checkboxModifier_; 137 }; 138 } // namespace OHOS::Ace::NG 139 140 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_CHECKBOX_CHECKBOX_PAINT_METHOD_H 141