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 { 27 DECLARE_ACE_TYPE(CheckBoxPaintMethod, NodePaintMethod) 28 29 public: 30 CheckBoxPaintMethod() = default; 31 32 ~CheckBoxPaintMethod() override = default; 33 GetContentModifier(PaintWrapper * paintWrapper)34 RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override 35 { 36 if (!checkboxModifier_) { 37 auto size = paintWrapper->GetContentSize(); 38 auto offset = paintWrapper->GetContentOffset(); 39 auto pipeline = PipelineBase::GetCurrentContextSafely(); 40 CHECK_NULL_RETURN(pipeline, nullptr); 41 auto checkBoxTheme = pipeline->GetTheme<CheckboxTheme>(); 42 CHECK_NULL_RETURN(checkBoxTheme, nullptr); 43 auto paintProperty = DynamicCast<CheckBoxPaintProperty>(paintWrapper->GetPaintProperty()); 44 auto isSelect = paintProperty->GetCheckBoxSelectValue(false); 45 auto boardColor = isSelect ? paintProperty->GetCheckBoxSelectedColorValue(checkBoxTheme->GetActiveColor()) 46 : checkBoxTheme->GetInactivePointColor(); 47 auto checkColor = isSelect ? checkBoxTheme->GetPointColor() : Color::TRANSPARENT; 48 auto borderColor = isSelect ? Color::TRANSPARENT : checkBoxTheme->GetInactiveColor(); 49 auto shadowColor = isSelect ? checkBoxTheme->GetShadowColor() : Color::TRANSPARENT; 50 float strokePaintSize = size.Width(); 51 auto checkStroke = static_cast<float>(checkBoxTheme->GetCheckStroke().ConvertToPx()); 52 if (paintProperty->HasCheckBoxCheckMarkWidth()) { 53 checkStroke = static_cast<float>(paintProperty->GetCheckBoxCheckMarkWidthValue().ConvertToPx()); 54 auto strokeLimitByMarkSize = strokePaintSize * CHECKBOX_MARK_STROKEWIDTH_LIMIT_RATIO; 55 if (checkStroke > strokeLimitByMarkSize) { 56 checkStroke = strokeLimitByMarkSize; 57 } 58 } 59 auto strokeSize = size.Width(); 60 if (paintProperty->HasCheckBoxCheckMarkSize()) { 61 if (paintProperty->GetCheckBoxCheckMarkSizeValue().ConvertToPx() >= 0) { 62 strokePaintSize = paintProperty->GetCheckBoxCheckMarkSizeValue().ConvertToPx(); 63 } 64 if (strokePaintSize > size.Width()) { 65 strokePaintSize = size.Width(); 66 } 67 } 68 checkboxModifier_ = AceType::MakeRefPtr<CheckBoxModifier>( 69 isSelect, boardColor, checkColor, borderColor, shadowColor, size, offset, checkStroke, strokeSize); 70 } 71 return checkboxModifier_; 72 } 73 UpdateCheckboxColors(const RefPtr<CheckBoxPaintProperty> & paintProperty)74 void UpdateCheckboxColors(const RefPtr<CheckBoxPaintProperty>& paintProperty) 75 { 76 if (paintProperty->HasCheckBoxSelectedColor()) { 77 checkboxModifier_->SetUserActiveColor(paintProperty->GetCheckBoxSelectedColorValue()); 78 } 79 if (paintProperty->HasCheckBoxSelectedStyle()) { 80 checkboxModifier_->SetCheckboxStyle(paintProperty->GetCheckBoxSelectedStyleValue()); 81 } 82 if (paintProperty->HasCheckBoxUnSelectedColor()) { 83 checkboxModifier_->SetInActiveColor(paintProperty->GetCheckBoxUnSelectedColorValue()); 84 } 85 if (paintProperty->HasCheckBoxCheckMarkColor()) { 86 checkboxModifier_->SetPointColor(paintProperty->GetCheckBoxCheckMarkColorValue()); 87 } 88 } 89 UpdateContentModifier(PaintWrapper * paintWrapper)90 void UpdateContentModifier(PaintWrapper* paintWrapper) override 91 { 92 CHECK_NULL_VOID(checkboxModifier_); 93 checkboxModifier_->InitializeParam(); 94 CHECK_NULL_VOID(paintWrapper); 95 auto size = paintWrapper->GetContentSize(); 96 float strokePaintSize = size.Width(); 97 auto paintProperty = DynamicCast<CheckBoxPaintProperty>(paintWrapper->GetPaintProperty()); 98 if (paintProperty->GetCheckBoxSelect().has_value()) { 99 checkboxModifier_->SetIsSelect(paintProperty->GetCheckBoxSelectValue()); 100 } 101 UpdateCheckboxColors(paintProperty); 102 if (paintProperty->HasCheckBoxCheckMarkSize()) { 103 if (paintProperty->GetCheckBoxCheckMarkSizeValue().ConvertToPx() >= 0) { 104 strokePaintSize = paintProperty->GetCheckBoxCheckMarkSizeValue().ConvertToPx(); 105 } 106 if (strokePaintSize > size.Width()) { 107 strokePaintSize = size.Width(); 108 } 109 } 110 checkboxModifier_->SetStrokeSize(strokePaintSize); 111 if (paintProperty->HasCheckBoxCheckMarkWidth()) { 112 auto strokeWidth = paintProperty->GetCheckBoxCheckMarkWidthValue().ConvertToPx(); 113 auto strokeLimitByMarkSize = strokePaintSize * CHECKBOX_MARK_STROKEWIDTH_LIMIT_RATIO; 114 if (strokeWidth > strokeLimitByMarkSize) { 115 strokeWidth = strokeLimitByMarkSize; 116 } 117 checkboxModifier_->SetStrokeWidth(strokeWidth); 118 } 119 120 checkboxModifier_->SetSize(size); 121 auto offset = paintWrapper->GetContentOffset(); 122 checkboxModifier_->SetOffset(offset); 123 checkboxModifier_->SetEnabled(enabled_); 124 checkboxModifier_->SetTouchHoverAnimationType(touchHoverType_); 125 checkboxModifier_->UpdateAnimatableProperty(); 126 checkboxModifier_->SetCheckboxStyle(checkBoxStyle_); 127 checkboxModifier_->SetUseContentModifier(useContentModifier_); 128 checkboxModifier_->SetHasBuilder(hasBuilder_); 129 auto pipeline = PipelineBase::GetCurrentContext(); 130 CHECK_NULL_VOID(pipeline); 131 auto checkboxTheme = pipeline->GetTheme<CheckboxTheme>(); 132 auto horizontalPadding = checkboxTheme->GetHotZoneHorizontalPadding().ConvertToPx(); 133 auto verticalPadding = checkboxTheme->GetHotZoneVerticalPadding().ConvertToPx(); 134 float boundsRectOriginX = offset.GetX() - horizontalPadding; 135 float boundsRectOriginY = offset.GetY() - verticalPadding; 136 float boundsRectWidth = size.Width() + 2 * horizontalPadding; 137 float boundsRectHeight = size.Height() + 2 * verticalPadding; 138 RectF boundsRect(boundsRectOriginX, boundsRectOriginY, boundsRectWidth, boundsRectHeight); 139 checkboxModifier_->SetBoundsRect(boundsRect); 140 SetHoverEffectType(paintProperty); 141 } 142 SetHoverEffectType(const RefPtr<CheckBoxPaintProperty> & checkBoxPaintProperty)143 void SetHoverEffectType(const RefPtr<CheckBoxPaintProperty>& checkBoxPaintProperty) 144 { 145 auto host = checkBoxPaintProperty->GetHost(); 146 CHECK_NULL_VOID(host); 147 auto eventHub = host->GetEventHub<EventHub>(); 148 CHECK_NULL_VOID(eventHub); 149 auto inputEventHub = eventHub->GetInputEventHub(); 150 HoverEffectType hoverEffectType = HoverEffectType::AUTO; 151 if (inputEventHub) { 152 hoverEffectType = inputEventHub->GetHoverEffect(); 153 if (HoverEffectType::UNKNOWN == hoverEffectType || HoverEffectType::OPACITY == hoverEffectType) { 154 hoverEffectType = HoverEffectType::AUTO; 155 } 156 if (checkboxModifier_) { 157 checkboxModifier_->SetHoverEffectType(hoverEffectType); 158 } 159 } 160 } SetHotZoneOffset(OffsetF & hotZoneOffset)161 void SetHotZoneOffset(OffsetF& hotZoneOffset) 162 { 163 hotZoneOffset_ = hotZoneOffset; 164 } 165 SetHotZoneSize(SizeF & hotZoneSize)166 void SetHotZoneSize(SizeF& hotZoneSize) 167 { 168 hotZoneSize_ = hotZoneSize; 169 } 170 SetEnabled(bool enabled)171 void SetEnabled(bool enabled) 172 { 173 enabled_ = enabled; 174 } 175 SetTotalScale(float totalScale)176 void SetTotalScale(float totalScale) 177 { 178 totalScale_ = totalScale; 179 } 180 SetPointScale(float pointScale)181 void SetPointScale(float pointScale) 182 { 183 pointScale_ = pointScale; 184 } 185 SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)186 void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType) 187 { 188 touchHoverType_ = touchHoverType; 189 } 190 SetCheckboxStyle(CheckBoxStyle checkBoxStyle)191 void SetCheckboxStyle(CheckBoxStyle checkBoxStyle) 192 { 193 checkBoxStyle_ = checkBoxStyle; 194 } 195 SetUseContentModifier(bool useContentModifier)196 void SetUseContentModifier(bool useContentModifier) 197 { 198 useContentModifier_ = useContentModifier; 199 } 200 SetHasBuilder(bool hasBuilder)201 void SetHasBuilder(bool hasBuilder) 202 { 203 hasBuilder_ = hasBuilder; 204 } 205 GetCheckBoxModifier()206 RefPtr<CheckBoxModifier> GetCheckBoxModifier() 207 { 208 return checkboxModifier_; 209 } 210 211 private: 212 bool enabled_ = true; 213 float totalScale_ = 1.0f; 214 float pointScale_ = 0.5f; 215 bool hasBuilder_ = false; 216 bool useContentModifier_ = false; 217 CheckBoxStyle checkBoxStyle_ = CheckBoxStyle::CIRCULAR_STYLE; 218 OffsetF hotZoneOffset_; 219 SizeF hotZoneSize_; 220 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 221 222 RefPtr<CheckBoxModifier> checkboxModifier_; 223 }; 224 } // namespace OHOS::Ace::NG 225 226 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PAINTS_CHECKBOX_CHECKBOX_PAINT_METHOD_H 227