• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_->SetTouchHoverAnimationType(touchHoverType_);
59         switchModifier_->SetDragOffsetX(dragOffsetX_);
60         switchModifier_->SetIsDragEvent(isDragEvent_);
61         switchModifier_->UpdateAnimatableProperty();
62         auto pipeline = PipelineBase::GetCurrentContext();
63         CHECK_NULL_VOID(pipeline);
64         auto switchTheme = pipeline->GetTheme<SwitchTheme>();
65         auto horizontalPadding = switchTheme->GetHotZoneHorizontalPadding().ConvertToPx();
66         auto verticalPadding = switchTheme->GetHotZoneVerticalPadding().ConvertToPx();
67         float boundsRectOriginX = offset.GetX() - horizontalPadding;
68         float boundsRectOriginY = offset.GetY() - verticalPadding;
69         float boundsRectWidth = size.Width() + 2 * horizontalPadding;
70         float boundsRectHeight = size.Height() + 2 * verticalPadding;
71         RectF boundsRect(boundsRectOriginX, boundsRectOriginY, boundsRectWidth, boundsRectHeight);
72         switchModifier_->SetBoundsRect(boundsRect);
73     }
74 
SetHotZoneOffset(OffsetF & hotZoneOffset)75     void SetHotZoneOffset(OffsetF& hotZoneOffset)
76     {
77         hotZoneOffset_ = hotZoneOffset;
78     }
79 
SetHotZoneSize(SizeF & hotZoneSize)80     void SetHotZoneSize(SizeF& hotZoneSize)
81     {
82         hotZoneSize_ = hotZoneSize;
83     }
84 
SetHoverPercent(float hoverPercent)85     void SetHoverPercent(float hoverPercent)
86     {
87         hoverPercent_ = hoverPercent;
88     }
89 
SetEnabled(bool enabled)90     void SetEnabled(bool enabled)
91     {
92         enabled_ = enabled;
93     }
94 
SetDragOffsetX(float dragOffsetX)95     void SetDragOffsetX(float dragOffsetX)
96     {
97         dragOffsetX_ = dragOffsetX;
98     }
99 
SetIsSelect(bool isSelect)100     void SetIsSelect(bool isSelect)
101     {
102         isSelect_ = isSelect;
103     }
104 
SetIsHover(bool isHover)105     void SetIsHover(bool isHover)
106     {
107         isHover_ = isHover;
108     }
109 
SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)110     void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)
111     {
112         touchHoverType_ = touchHoverType;
113     }
114 
SetIsDragEvent(bool isDragEvent)115     void SetIsDragEvent(bool isDragEvent)
116     {
117         isDragEvent_ = isDragEvent;
118     }
119 
120 private:
121     float dragOffsetX_ = 0.0f;
122     float hoverPercent_ = 0.0f;
123     const Dimension radiusGap_ = 2.0_vp;
124     bool enabled_ = true;
125     bool isSelect_ = true;
126     Color clickEffectColor_ = Color::WHITE;
127     Color hoverColor_ = Color::WHITE;
128     Dimension hoverRadius_ = 8.0_vp;
129 
130     bool isHover_ = false;
131     OffsetF hotZoneOffset_;
132     SizeF hotZoneSize_;
133     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
134     bool isDragEvent_ = false;
135 
136     RefPtr<SwitchModifier> switchModifier_;
137 
138     ACE_DISALLOW_COPY_AND_MOVE(SwitchPaintMethod);
139 };
140 } // namespace OHOS::Ace::NG
141 
142 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWITCH_SWITCH_PAINT_METHOD_H
143