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