• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PATTERN_SLIDER_SLIDER_CONTENT_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SLIDER_SLIDER_CONTENT_MODIFIER_H
18 
19 #include "base/memory/referenced.h"
20 #include "base/utils/utils.h"
21 #include "core/components/common/properties/animation_option.h"
22 #include "core/components/slider/slider_theme.h"
23 #include "core/components_ng/base/modifier.h"
24 #include "core/components_ng/pattern/slider/slider_paint_property.h"
25 #include "core/components_ng/render/animation_utils.h"
26 #include "core/components_ng/render/drawing.h"
27 #include "core/pipeline/pipeline_base.h"
28 
29 namespace OHOS::Ace::NG {
30 class SliderContentModifier : public ContentModifier {
31     DECLARE_ACE_TYPE(SliderContentModifier, ContentModifier);
32 
33 public:
34     struct Parameters {
35         float trackThickness = 0.0f;
36         float blockDiameter = 0.0f;
37         float stepRatio = 0.0f;
38         float hotCircleShadowWidth = 0.0f;
39         bool mouseHoverFlag_ = false;
40         bool mousePressedFlag_ = false;
41         PointF selectStart;
42         PointF selectEnd;
43         PointF backStart;
44         PointF backEnd;
45         PointF circleCenter;
46         Color selectColor;
47         Color trackBackgroundColor;
48         Color blockColor;
49     };
50 
51     explicit SliderContentModifier(const Parameters& parameters);
52     ~SliderContentModifier() override = default;
53 
54     void onDraw(DrawingContext& context) override;
55 
56     void DrawBackground(DrawingContext& context);
57     void DrawStep(DrawingContext& context);
58     void DrawSelect(DrawingContext& context);
59     void DrawDefaultBlock(DrawingContext& context);
60     void DrawHoverOrPress(DrawingContext& context);
61     void DrawShadow(DrawingContext& context);
62 
UpdateThemeColor()63     void UpdateThemeColor()
64     {
65         auto pipeline = PipelineBase::GetCurrentContext();
66         CHECK_NULL_VOID(pipeline);
67         auto sliderTheme = pipeline->GetTheme<SliderTheme>();
68         CHECK_NULL_VOID(sliderTheme);
69         blockOuterEdgeColor_ = sliderTheme->GetBlockOuterEdgeColor();
70         stepSize_ = static_cast<float>(sliderTheme->GetMarkerSize().ConvertToPx());
71         stepColor_ = sliderTheme->GetMarkerColor();
72     }
73 
74     void UpdateData(const Parameters& parameters);
75     void JudgeNeedAimate(const RefPtr<SliderPaintProperty>& property);
76 
SetTrackThickness(float trackThickness)77     void SetTrackThickness(float trackThickness)
78     {
79         if (trackThickness_) {
80             trackThickness_->Set(trackThickness);
81         }
82     }
83 
SetTrackBackgroundColor(Color color)84     void SetTrackBackgroundColor(Color color)
85     {
86         if (trackBackgroundColor_) {
87             trackBackgroundColor_->Set(LinearColor(color));
88         }
89     }
90 
SetSelectColor(Color color)91     void SetSelectColor(Color color)
92     {
93         if (selectColor_) {
94             selectColor_->Set(LinearColor(color));
95         }
96     }
97 
SetBlockColor(Color color)98     void SetBlockColor(Color color)
99     {
100         if (blockColor_) {
101             blockColor_->Set(LinearColor(color));
102         }
103     }
104 
105     void SetBoardColor();
106 
SetBackgroundSize(const PointF & start,const PointF & end)107     void SetBackgroundSize(const PointF& start, const PointF& end)
108     {
109         if (backStart_) {
110             backStart_->Set(start - PointF());
111         }
112         if (backEnd_) {
113             backEnd_->Set(end - PointF());
114         }
115     }
116 
117     void SetSelectSize(const PointF& start, const PointF& end);
118 
119     void SetCircleCenter(const PointF& center);
120 
SetBlockDiameter(float blockDiameter)121     void SetBlockDiameter(float blockDiameter)
122     {
123         if (blockDiameter_) {
124             blockDiameter_->Set(blockDiameter);
125         }
126     }
127 
SetStepRatio(float stepRatio)128     void SetStepRatio(float stepRatio)
129     {
130         if (stepRatio_) {
131             stepRatio_->Set(stepRatio);
132         }
133     }
134 
SetNotAnimated()135     void SetNotAnimated()
136     {
137         needAnimate_ = false;
138     }
139 
SetAnimated()140     void SetAnimated()
141     {
142         needAnimate_ = true;
143     }
144 
SetShowSteps(bool showSteps)145     void SetShowSteps(bool showSteps)
146     {
147         if (isShowStep_) {
148             isShowStep_->Set(showSteps);
149         }
150     }
151 
SetDirection(Axis axis)152     void SetDirection(Axis axis)
153     {
154         directionAxis_ = axis;
155     }
156 
157 private:
158     // animatable property
159     RefPtr<AnimatablePropertyOffsetF> selectStart_;
160     RefPtr<AnimatablePropertyOffsetF> selectEnd_;
161     RefPtr<AnimatablePropertyOffsetF> backStart_;
162     RefPtr<AnimatablePropertyOffsetF> backEnd_;
163     RefPtr<AnimatablePropertyOffsetF> circleCenter_;
164     RefPtr<AnimatablePropertyFloat> trackThickness_;
165     RefPtr<AnimatablePropertyColor> trackBackgroundColor_;
166     RefPtr<AnimatablePropertyColor> selectColor_;
167     RefPtr<AnimatablePropertyColor> blockColor_;
168     RefPtr<AnimatablePropertyColor> boardColor_;
169     // non-animatable property
170     RefPtr<PropertyFloat> blockDiameter_;
171     RefPtr<PropertyFloat> stepRatio_;
172     RefPtr<PropertyBool> isShowStep_;
173     // others
174     bool mouseHoverFlag_ = false;
175     bool mousePressedFlag_ = false;
176     bool reverse_ = false;
177     Axis directionAxis_ = Axis::HORIZONTAL;
178     bool needAnimate_ = false; // Translate Animation on-off
179     float hotCircleShadowWidth_ = 0.0f;
180     Color blockOuterEdgeColor_;
181     float stepSize_ = 0.0f;
182     Color stepColor_;
183     ACE_DISALLOW_COPY_AND_MOVE(SliderContentModifier);
184 };
185 
186 } // namespace OHOS::Ace::NG
187 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SLIDER_SLIDER_CONTENT_MODIFIER_H
188