1 /* 2 * Copyright (c) 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_PATTERNS_SWITCH_SWITCH_MODIFIER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_MODIFIER_H 18 19 #include <vector> 20 21 #include "base/geometry/ng/offset_t.h" 22 #include "core/animation/spring_curve.h" 23 #include "core/common/container.h" 24 #include "core/components_ng/base/modifier.h" 25 #include "core/components_ng/pattern/radio/radio_modifier.h" 26 #include "core/components_ng/pattern/toggle/switch_paint_property.h" 27 #include "core/components_ng/property/property.h" 28 #include "core/components_ng/render/animation_utils.h" 29 #include "core/components_ng/render/canvas_image.h" 30 #include "core/components_ng/render/drawing.h" 31 #include "core/components_ng/render/paint_wrapper.h" 32 namespace OHOS::Ace::NG { 33 class SwitchModifier : public ContentModifier { 34 DECLARE_ACE_TYPE(SwitchModifier, ContentModifier); 35 36 public: 37 SwitchModifier(bool isSelect, const Color& boardColor, float mainDelta); 38 ~SwitchModifier() override = default; 39 onDraw(DrawingContext & context)40 void onDraw(DrawingContext& context) override 41 { 42 RSCanvas canvas = context.canvas; 43 PaintSwitch(canvas, offset_->Get(), size_->Get()); 44 } 45 UpdateAnimatableProperty()46 void UpdateAnimatableProperty() 47 { 48 AnimationOption option = AnimationOption(); 49 option.SetDuration(hoverDuration_); 50 option.SetCurve(Curves::FRICTION); 51 AnimationUtils::Animate(option, [&]() { 52 animateHoverColor_->Set(isHover_->Get() ? LinearColor(hoverColor_) : LinearColor(Color::TRANSPARENT)); 53 }); 54 55 option.SetDuration(colorAnimationDuration_); 56 option.SetCurve(Curves::FAST_OUT_SLOW_IN); 57 AnimationUtils::Animate(option, [&]() { 58 animatableBoardColor_->Set(isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactiveColor_)); 59 }); 60 } 61 62 void InitializeParam(); 63 void PaintSwitch(RSCanvas& canvas, const OffsetF& contentOffset, const SizeF& contentSize); 64 void DrawHoverBoard(RSCanvas& canvas, const OffsetF& offset) const; 65 float GetSwitchWidth(const SizeF& contentSize) const; 66 SetUserActiveColor(const Color & color)67 void SetUserActiveColor(const Color& color) 68 { 69 userActiveColor_ = color; 70 } 71 SetUserPointColor(const Color & color)72 void SetUserPointColor(const Color& color) 73 { 74 userPointColor_ = color; 75 } 76 SetEnabled(bool enabled)77 void SetEnabled(bool enabled) 78 { 79 if (enabled_) { 80 enabled_->Set(enabled); 81 } 82 } 83 SetIsHover(bool isHover)84 void SetIsHover(bool isHover) 85 { 86 if (isHover_) { 87 isHover_->Set(isHover); 88 } 89 } 90 SetIsSelect(bool isSelect)91 void SetIsSelect(bool isSelect) 92 { 93 if (isSelect_) { 94 isSelect_->Set(isSelect); 95 } 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 SetOffset(OffsetF & offset)108 void SetOffset(OffsetF& offset) 109 { 110 if (offset_) { 111 offset_->Set(offset); 112 } 113 } 114 SetSize(SizeF & size)115 void SetSize(SizeF& size) 116 { 117 if (size_) { 118 size_->Set(size); 119 } 120 } 121 SetMainDelta(float mainDelta)122 void SetMainDelta(float mainDelta) 123 { 124 if (mainDelta_) { 125 mainDelta_->Set(mainDelta); 126 } 127 } 128 129 private: 130 float actualWidth_ = 0.0f; 131 float actualHeight_ = 0.0f; 132 float pointRadius_ = 0.0f; 133 const Dimension radiusGap_ = 2.0_vp; 134 Color clickEffectColor_; 135 Color hoverColor_; 136 Color activeColor_; 137 Color inactiveColor_; 138 Color pointColor_; 139 Color userActiveColor_; 140 Color userPointColor_; 141 Dimension hoverRadius_ = 8.0_vp; 142 float hoverDuration_ = 0.0f; 143 float hoverToTouchDuration_ = 0.0f; 144 float touchDuration_ = 0.0f; 145 float colorAnimationDuration_ = 0.0f; 146 147 OffsetF hotZoneOffset_; 148 SizeF hotZoneSize_; 149 150 RefPtr<AnimatablePropertyColor> animatableBoardColor_; 151 RefPtr<AnimatablePropertyColor> animateHoverColor_; 152 RefPtr<PropertyFloat> mainDelta_; 153 RefPtr<PropertyBool> isSelect_; 154 RefPtr<PropertyBool> isHover_; 155 RefPtr<PropertyOffsetF> offset_; 156 RefPtr<PropertySizeF> size_; 157 RefPtr<PropertyBool> enabled_; 158 159 ACE_DISALLOW_COPY_AND_MOVE(SwitchModifier); 160 }; 161 } // namespace OHOS::Ace::NG 162 163 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_MODIFIER_H 164