• 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_PATTERNS_SLIDER_SLIDER_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SLIDER_SLIDER_PATTERN_H
18 
19 #include <cstddef>
20 
21 #include "core/components_ng/pattern/pattern.h"
22 #include "core/components_ng/pattern/slider/slider_content_modifier.h"
23 #include "core/components_ng/pattern/slider/slider_event_hub.h"
24 #include "core/components_ng/pattern/slider/slider_layout_algorithm.h"
25 #include "core/components_ng/pattern/slider/slider_layout_property.h"
26 #include "core/components_ng/pattern/slider/slider_paint_method.h"
27 #include "core/components_ng/pattern/slider/slider_paint_property.h"
28 
29 namespace OHOS::Ace::NG {
30 class SliderPattern : public Pattern {
31     DECLARE_ACE_TYPE(SliderPattern, Pattern);
32 
33 public:
34     SliderPattern() = default;
35     ~SliderPattern() override = default;
36 
CreateNodePaintMethod()37     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
38     {
39         if (!IsSliderVisible()) {
40             return nullptr;
41         }
42         auto paintParameters = UpdateContentParameters();
43         if (!sliderContentModifier_) {
44             sliderContentModifier_ =
45                 AceType::MakeRefPtr<SliderContentModifier>(paintParameters, [weak = WeakClaim(this)]() {
46                     auto pattern = weak.Upgrade();
47                     CHECK_NULL_VOID(pattern);
48                     pattern->LayoutImageNode();
49                 });
50         }
51         SliderPaintMethod::TipParameters tipParameters { bubbleFlag_,
52             GetBubbleVertexPosition(circleCenter_, trackThickness_, blockSize_) };
53         if (!sliderTipModifier_ && bubbleFlag_) {
54             sliderTipModifier_ = AceType::MakeRefPtr<SliderTipModifier>([weak = WeakClaim(this)]() {
55                 auto pattern = weak.Upgrade();
56                 CHECK_NULL_RETURN(pattern, OffsetF());
57                 auto blockCenter = pattern->GetBlockCenter();
58                 auto trackThickness = pattern->sliderContentModifier_->GetTrackThickness();
59                 auto blockSize = pattern->sliderContentModifier_->GetBlockSize();
60                 return pattern->GetBubbleVertexPosition(blockCenter, trackThickness, blockSize);
61             });
62         }
63         return MakeRefPtr<SliderPaintMethod>(
64             sliderContentModifier_, paintParameters, sliderLength_, borderBlank_, sliderTipModifier_, tipParameters);
65     }
66 
CreateLayoutProperty()67     RefPtr<LayoutProperty> CreateLayoutProperty() override
68     {
69         return MakeRefPtr<SliderLayoutProperty>();
70     }
71 
CreatePaintProperty()72     RefPtr<PaintProperty> CreatePaintProperty() override
73     {
74         return MakeRefPtr<SliderPaintProperty>();
75     }
76 
CreateLayoutAlgorithm()77     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
78     {
79         return MakeRefPtr<SliderLayoutAlgorithm>();
80     }
81 
82     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override;
83 
CreateEventHub()84     RefPtr<EventHub> CreateEventHub() override
85     {
86         return MakeRefPtr<SliderEventHub>();
87     }
88 
GetFocusPattern()89     FocusPattern GetFocusPattern() const override
90     {
91         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION };
92     }
93 
GetBlockCenter()94     const OffsetF& GetBlockCenter() const
95     {
96         return circleCenter_;
97     }
98 
GetAnimatableBlockCenter()99     OffsetF GetAnimatableBlockCenter() const
100     {
101         if (sliderContentModifier_ != nullptr) {
102             auto blockCenter = sliderContentModifier_->GetBlockCenter();
103             return { blockCenter.GetX(), blockCenter.GetY() };
104         }
105         return {};
106     }
107 
GetValueRatio()108     float GetValueRatio() const
109     {
110         return valueRatio_;
111     }
112 
113     std::string ProvideRestoreInfo() override;
114     void OnRestoreInfo(const std::string& restoreInfo) override;
115 
116     void UpdateValue(float value);
117     void OnVisibleChange(bool isVisible) override;
118 private:
119     void OnAttachToFrameNode() override;
120     void OnModifyDone() override;
121     void CancelExceptionValue(float& min, float& max, float& step);
122     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
123 
124     void CreateParagraphFunc();
125     void CreateParagraphAndLayout(
126         const TextStyle& textStyle, const std::string& content, const LayoutConstraintF& contentConstraint);
127     bool CreateParagraph(const TextStyle& textStyle, std::string content);
128     void UpdateCircleCenterOffset();
129     void UpdateTipsValue();
130     void UpdateBubbleSizeAndLayout();
131     void UpdateBubble();
132     void InitializeBubble();
133 
134     bool AtMousePanArea(const Offset& offsetInFrame);
135     bool AtTouchPanArea(const Offset& offsetInFrame);
136     bool AtPanArea(const Offset& offset, const SourceType& sourceType);
137 
138     void UpdateMarkDirtyNode(const PropertyChangeFlag& Flag);
139     Axis GetDirection() const;
140 
141     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
142     void HandleTouchEvent(const TouchEventInfo& info);
143     void InitMouseEvent(const RefPtr<InputEventHub>& inputEventHub);
144     void HandleMouseEvent(const MouseInfo& info);
145     void HandleHoverEvent(bool isHover);
146     void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
147     void HandlingGestureStart(const GestureEvent& info);
148     void HandlingGestureEvent(const GestureEvent& info);
149     void HandledGestureEvent();
150 
151     void UpdateValueByLocalLocation(const std::optional<Offset>& localLocation);
152     void FireChangeEvent(int32_t mode);
153 
154     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
155     void GetInnerFocusPaintRect(RoundRect& paintRect);
156     void GetOutsetInnerFocusPaintRect(RoundRect& paintRect);
157     void GetInsetInnerFocusPaintRect(RoundRect& paintRect);
158     bool OnKeyEvent(const KeyEvent& event);
159     void PaintFocusState();
160     bool MoveStep(int32_t stepCount);
161 
162     bool IsSliderVisible();
163     void RegisterVisibleAreaChange();
164     void OnWindowHide() override;
165     void OnWindowShow() override;
166     void StartAnimation();
167     void StopAnimation();
168 
169     void OpenTranslateAnimation();
170     void CloseTranslateAnimation();
171     SliderContentModifier::Parameters UpdateContentParameters();
172     void GetSelectPosition(SliderContentModifier::Parameters& parameters, float centerWidth, const OffsetF& offset);
173     void GetBackgroundPosition(SliderContentModifier::Parameters& parameters, float centerWidth, const OffsetF& offset);
174     void GetCirclePosition(SliderContentModifier::Parameters& parameters, float centerWidth, const OffsetF& offset);
175     void UpdateBlock();
176     void LayoutImageNode();
177     OffsetF GetBubbleVertexPosition(const OffsetF& blockCenter, float trackThickness, const SizeF& blockSize);
178     void SetAccessibilityAction();
179 
180     Axis direction_ = Axis::HORIZONTAL;
181     enum SliderChangeMode { Begin = 0, Moving = 1, End = 2, Click = 3 };
182     float value_ = 0.0f;
183     bool showTips_ = false;
184     bool hotFlag_ = false; // whether the mouse is hovering over the slider
185     bool valueChangeFlag_ = false;
186     bool mouseHoverFlag_ = false;
187     bool mousePressedFlag_ = false;
188     bool axisFlag_ = false; // Wheel operation flag
189     bool focusFlag_ = false;
190     bool panMoveFlag_ = false;
191     bool hasVisibleChangeRegistered_ = false;
192     bool isVisibleArea_ = true;
193     bool isVisible_ = true;
194     bool isShow_ = true;
195 
196     float stepRatio_ = 1.0f / 100.0f;
197     float valueRatio_ = 0.0f;
198     float sliderLength_ = 0.0f;
199     float borderBlank_ = 0.0f;
200     float hotBlockShadowWidth_ = 0.0f;
201     OffsetF circleCenter_ = { 0.0f, 0.0f }; // Relative to the content area
202 
203     float trackThickness_ = 0.0f;
204     SizeF blockHotSize_;
205     SizeF blockSize_;
206 
207     RefPtr<TouchEventImpl> touchEvent_;
208     RefPtr<ClickEvent> clickListener_;
209     RefPtr<PanEvent> panEvent_;
210     RefPtr<InputEvent> mouseEvent_;
211     RefPtr<InputEvent> hoverEvent_;
212 
213     RefPtr<SliderContentModifier> sliderContentModifier_;
214 
215     // tip Parameters
216     bool bubbleFlag_ = false;
217     RefPtr<SliderTipModifier> sliderTipModifier_;
218 
219     RefPtr<FrameNode> imageFrameNode_;
220 
221     ACE_DISALLOW_COPY_AND_MOVE(SliderPattern);
222 };
223 } // namespace OHOS::Ace::NG
224 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SLIDER_SLIDER_PATTERN_H
225