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_PATTERN_SWITCH_SWITCH_PAINT_METHOD_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWITCH_SWITCH_PAINT_METHOD_H 18 19 #include "core/components/checkable/checkable_theme.h" 20 #include "core/components_ng/pattern/toggle/switch_modifier.h" 21 #include "core/components_ng/pattern/toggle/switch_paint_property.h" 22 #include "core/components_ng/render/canvas_image.h" 23 #include "core/components_ng/render/node_paint_method.h" 24 #include "core/components_ng/render/paint_wrapper.h" 25 #include "core/components_ng/render/render_context.h" 26 27 namespace OHOS::Ace::NG { 28 class ACE_EXPORT SwitchPaintMethod : public NodePaintMethod { DECLARE_ACE_TYPE(SwitchPaintMethod,NodePaintMethod)29 DECLARE_ACE_TYPE(SwitchPaintMethod, NodePaintMethod) 30 public: 31 explicit SwitchPaintMethod(const RefPtr<SwitchModifier>& switchModifier) : switchModifier_(switchModifier) {} 32 33 ~SwitchPaintMethod() override = default; 34 GetContentModifier(PaintWrapper * paintWrapper)35 RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override 36 { 37 CHECK_NULL_RETURN(switchModifier_, nullptr); 38 return switchModifier_; 39 } 40 UpdateContentModifier(PaintWrapper * paintWrapper)41 void UpdateContentModifier(PaintWrapper* paintWrapper) override 42 { 43 CHECK_NULL_VOID(switchModifier_); 44 switchModifier_->InitializeParam(); 45 auto paintProperty = DynamicCast<SwitchPaintProperty>(paintWrapper->GetPaintProperty()); 46 if (paintProperty->HasSelectedColor()) { 47 switchModifier_->SetUserActiveColor(paintProperty->GetSelectedColor().value()); 48 } 49 if (paintProperty->HasSwitchPointColor()) { 50 switchModifier_->SetPointColor(paintProperty->GetSwitchPointColor().value()); 51 } 52 auto size = paintWrapper->GetContentSize(); 53 auto offset = paintWrapper->GetContentOffset(); 54 switchModifier_->SetSize(size); 55 switchModifier_->SetOffset(offset); 56 switchModifier_->SetEnabled(enabled_); 57 switchModifier_->SetIsSelect(isSelect_); 58 switchModifier_->SetDirection(direction_); 59 switchModifier_->SetTouchHoverAnimationType(touchHoverType_); 60 switchModifier_->SetDragOffsetX(dragOffsetX_); 61 switchModifier_->SetIsDragEvent(isDragEvent_); 62 switchModifier_->SetShowHoverEffect(showHoverEffect_); 63 switchModifier_->UpdateAnimatableProperty(); 64 auto pipeline = PipelineBase::GetCurrentContext(); 65 CHECK_NULL_VOID(pipeline); 66 auto switchTheme = pipeline->GetTheme<SwitchTheme>(); 67 auto horizontalPadding = switchTheme->GetHotZoneHorizontalPadding().ConvertToPx(); 68 auto verticalPadding = switchTheme->GetHotZoneVerticalPadding().ConvertToPx(); 69 float boundsRectOriginX = offset.GetX() - horizontalPadding; 70 float boundsRectOriginY = offset.GetY() - verticalPadding; 71 float boundsRectWidth = size.Width() + 2 * horizontalPadding; 72 float boundsRectHeight = size.Height() + 2 * verticalPadding; 73 RectF boundsRect(boundsRectOriginX, boundsRectOriginY, boundsRectWidth, boundsRectHeight); 74 switchModifier_->SetBoundsRect(boundsRect); 75 } 76 SetHotZoneOffset(OffsetF & hotZoneOffset)77 void SetHotZoneOffset(OffsetF& hotZoneOffset) 78 { 79 hotZoneOffset_ = hotZoneOffset; 80 } 81 SetHotZoneSize(SizeF & hotZoneSize)82 void SetHotZoneSize(SizeF& hotZoneSize) 83 { 84 hotZoneSize_ = hotZoneSize; 85 } 86 SetHoverPercent(float hoverPercent)87 void SetHoverPercent(float hoverPercent) 88 { 89 hoverPercent_ = hoverPercent; 90 } 91 SetEnabled(bool enabled)92 void SetEnabled(bool enabled) 93 { 94 enabled_ = enabled; 95 } 96 SetDragOffsetX(float dragOffsetX)97 void SetDragOffsetX(float dragOffsetX) 98 { 99 dragOffsetX_ = dragOffsetX; 100 } 101 SetIsSelect(bool isSelect)102 void SetIsSelect(bool isSelect) 103 { 104 isSelect_ = isSelect; 105 } 106 SetIsHover(bool isHover)107 void SetIsHover(bool isHover) 108 { 109 isHover_ = isHover; 110 } 111 SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)112 void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType) 113 { 114 touchHoverType_ = touchHoverType; 115 } 116 SetIsDragEvent(bool isDragEvent)117 void SetIsDragEvent(bool isDragEvent) 118 { 119 isDragEvent_ = isDragEvent; 120 } 121 SetShowHoverEffect(bool showHoverEffect)122 void SetShowHoverEffect(bool showHoverEffect) 123 { 124 showHoverEffect_ = showHoverEffect; 125 } 126 SetDirection(TextDirection direction)127 void SetDirection(TextDirection direction) 128 { 129 direction_ = direction; 130 } 131 132 private: 133 float dragOffsetX_ = 0.0f; 134 float hoverPercent_ = 0.0f; 135 const Dimension radiusGap_ = 2.0_vp; 136 bool enabled_ = true; 137 bool isSelect_ = true; 138 Color clickEffectColor_ = Color::WHITE; 139 Color hoverColor_ = Color::WHITE; 140 Dimension hoverRadius_ = 8.0_vp; 141 bool showHoverEffect_ = true; 142 143 bool isHover_ = false; 144 OffsetF hotZoneOffset_; 145 SizeF hotZoneSize_; 146 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 147 TextDirection direction_ = TextDirection::AUTO; 148 bool isDragEvent_ = false; 149 150 RefPtr<SwitchModifier> switchModifier_; 151 152 ACE_DISALLOW_COPY_AND_MOVE(SwitchPaintMethod); 153 }; 154 } // namespace OHOS::Ace::NG 155 156 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWITCH_SWITCH_PAINT_METHOD_H 157