1 /* 2 * Copyright (c) 2022 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_ng/pattern/toggle/switch_modifier.h" 20 #include "core/components_ng/pattern/toggle/switch_paint_property.h" 21 #include "core/components_ng/render/canvas.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_->SetUserPointColor(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_->SetIsHover(isHover_); 59 switchModifier_->SetMainDelta(mainDelta_); 60 switchModifier_->UpdateAnimatableProperty(); 61 } 62 SetHotZoneOffset(OffsetF & hotZoneOffset)63 void SetHotZoneOffset(OffsetF& hotZoneOffset) 64 { 65 hotZoneOffset_ = hotZoneOffset; 66 } 67 SetHotZoneSize(SizeF & hotZoneSize)68 void SetHotZoneSize(SizeF& hotZoneSize) 69 { 70 hotZoneSize_ = hotZoneSize; 71 } 72 SetHoverPercent(float hoverPercent)73 void SetHoverPercent(float hoverPercent) 74 { 75 hoverPercent_ = hoverPercent; 76 } 77 SetEnabled(bool enabled)78 void SetEnabled(bool enabled) 79 { 80 enabled_ = enabled; 81 } 82 SetMainDelta(float mainDelta)83 void SetMainDelta(float mainDelta) 84 { 85 mainDelta_ = mainDelta; 86 } 87 SetIsSelect(bool isSelect)88 void SetIsSelect(bool isSelect) 89 { 90 isSelect_ = isSelect; 91 } 92 SetIsHover(bool isHover)93 void SetIsHover(bool isHover) 94 { 95 isHover_ = isHover; 96 } 97 98 private: 99 float mainDelta_ = 0.0f; 100 float hoverPercent_ = 0.0f; 101 const Dimension radiusGap_ = 2.0_vp; 102 bool enabled_ = true; 103 bool isSelect_ = true; 104 Color clickEffectColor_ = Color::WHITE; 105 Color hoverColor_ = Color::WHITE; 106 Dimension hoverRadius_ = 8.0_vp; 107 108 bool isHover_ = false; 109 OffsetF hotZoneOffset_; 110 SizeF hotZoneSize_; 111 112 RefPtr<SwitchModifier> switchModifier_; 113 114 ACE_DISALLOW_COPY_AND_MOVE(SwitchPaintMethod); 115 }; 116 } // namespace OHOS::Ace::NG 117 118 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWITCH_SWITCH_PAINT_METHOD_H 119