• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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_SCROLL_INNER_SCROLL_BAR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_INNER_SCROLL_BAR_H
18 
19 #include <cmath>
20 
21 #include "base/geometry/dimension.h"
22 #include "base/geometry/offset.h"
23 #include "base/geometry/rect.h"
24 #include "base/utils/utils.h"
25 #include "core/animation/friction_motion.h"
26 #include "core/components/common/layout/constants.h"
27 #include "core/components/common/properties/color.h"
28 #include "core/components/common/properties/edge.h"
29 #include "core/components/scroll/scroll_bar_theme.h"
30 #include "core/components_ng/base/frame_scene_status.h"
31 #include "core/components_ng/event/input_event.h"
32 #include "core/components_ng/event/touch_event.h"
33 #include "core/components_ng/gestures/recognizers/pan_recognizer.h"
34 #include "core/components_ng/pattern/scroll/inner/scroll_bar_overlay_modifier.h"
35 #include "core/components_ng/pattern/scrollable/scrollable_properties.h"
36 #include "core/components_ng/property/border_property.h"
37 
38 namespace OHOS::Ace::NG {
39 
40 constexpr double FACTOR_HALF = 0.5;
41 constexpr double DEFAULT_TOPANGLE = 60.0;
42 constexpr double DEFAULT_BOTTOMANGLE = 120.0;
43 constexpr double DEFAULT_MINANGLE = 10.0;
44 constexpr double STRAIGHT_ANGLE = 180.0;
45 constexpr double BAR_FRICTION = 0.9;
46 constexpr Color PRESSED_BLEND_COLOR = Color(0x19000000);
47 using DragFRCSceneCallback = std::function<void(double velocity, NG::SceneStatus sceneStatus)>;
48 
49 class ScrollBar final : public AceType {
50     DECLARE_ACE_TYPE(ScrollBar, AceType);
51 
52 public:
53     ScrollBar();
54     ScrollBar(DisplayMode displayMode, ShapeMode shapeMode = ShapeMode::RECT,
55         PositionMode positionMode = PositionMode::RIGHT);
56     ~ScrollBar() override = default;
57 
GetShapeMode()58     ShapeMode GetShapeMode() const
59     {
60         return shapeMode_;
61     }
GetDisplayMode()62     DisplayMode GetDisplayMode() const
63     {
64         return displayMode_;
65     }
GetPositionMode()66     PositionMode GetPositionMode() const
67     {
68         return positionMode_;
69     }
SetPadding(const Edge & padding)70     void SetPadding(const Edge& padding)
71     {
72         padding_ = padding;
73     }
GetPadding()74     const Edge& GetPadding() const
75     {
76         return padding_;
77     }
SetBackgroundColor(const Color & backgroundColor)78     void SetBackgroundColor(const Color& backgroundColor)
79     {
80         backgroundColor_ = backgroundColor;
81     }
GetBackgroundColor()82     const Color& GetBackgroundColor() const
83     {
84         return backgroundColor_;
85     }
SetForegroundColor(const Color & foregroundColor)86     void SetForegroundColor(const Color& foregroundColor)
87     {
88         foregroundColor_ = foregroundColor;
89     }
GetTopAngle()90     double GetTopAngle() const
91     {
92         return topAngle_;
93     }
GetBottomAngle()94     double GetBottomAngle() const
95     {
96         return bottomAngle_;
97     }
GetTrickStartAngle()98     double GetTrickStartAngle() const
99     {
100         return trickStartAngle_;
101     }
GetTrickSweepAngle()102     double GetTrickSweepAngle() const
103     {
104         return trickSweepAngle_;
105     }
SetMinHeight(const Dimension & minHeight)106     void SetMinHeight(const Dimension& minHeight)
107     {
108         minHeight_ = minHeight;
109     }
GetMinHeight()110     const Dimension& GetMinHeight() const
111     {
112         return minHeight_;
113     }
SetMinDynamicHeight(const Dimension & minDynamicHeight)114     void SetMinDynamicHeight(const Dimension& minDynamicHeight)
115     {
116         minDynamicHeight_ = minDynamicHeight;
117     }
GetMinDynamicHeight()118     const Dimension& GetMinDynamicHeight() const
119     {
120         return minDynamicHeight_;
121     }
SetInactiveWidth(const Dimension & inactiveWidth)122     void SetInactiveWidth(const Dimension& inactiveWidth)
123     {
124         inactiveWidth_ = inactiveWidth;
125     }
SetActiveWidth(const Dimension & activeWidth)126     void SetActiveWidth(const Dimension& activeWidth)
127     {
128         activeWidth_ = activeWidth;
129     }
GetActiveWidth()130     const Dimension& GetActiveWidth() const
131     {
132         return activeWidth_;
133     }
GetActiveRect()134     const Rect& GetActiveRect() const
135     {
136         return activeRect_;
137     }
SetTouchWidth(const Dimension & touchWidth)138     void SetTouchWidth(const Dimension& touchWidth)
139     {
140         touchWidth_ = touchWidth;
141     }
GetTouchWidth()142     const Dimension& GetTouchWidth() const
143     {
144         return touchWidth_;
145     }
GetBarRect()146     const Rect& GetBarRect() const
147     {
148         return barRect_;
149     }
IsScrollable()150     bool IsScrollable() const
151     {
152         return isScrollable_;
153     }
GetPositionModeUpdate()154     bool GetPositionModeUpdate() const
155     {
156         return positionModeUpdate_;
157     }
158 
SetShapeMode(ShapeMode shapeMode)159     void SetShapeMode(ShapeMode shapeMode)
160     {
161         shapeMode_ = shapeMode;
162     }
GetOutBoundary()163     double GetOutBoundary() const
164     {
165         return outBoundary_;
166     }
SetOutBoundary(double outBoundary)167     void SetOutBoundary(double outBoundary)
168     {
169         outBoundary_ = outBoundary;
170     }
SetIsOutOfBoundary(bool isOutOfBoundary)171     void SetIsOutOfBoundary(bool isOutOfBoundary)
172     {
173         isOutOfBoundary_ = isOutOfBoundary;
174     }
SetPosition(const Dimension & position)175     void SetPosition(const Dimension& position)
176     {
177         position_ = position;
178     }
GetPosition()179     const Dimension& GetPosition() const
180     {
181         return position_;
182     }
SetPressed(bool press)183     void SetPressed(bool press)
184     {
185         isPressed_ = press;
186     }
IsPressed()187     bool IsPressed() const
188     {
189         return isPressed_;
190     }
SetHover(bool hover)191     void SetHover(bool hover)
192     {
193         isHover_ = hover;
194     }
IsHover()195     bool IsHover() const
196     {
197         return isHover_;
198     }
GetOpacityAnimationType()199     OpacityAnimationType GetOpacityAnimationType() const
200     {
201         return opacityAnimationType_;
202     }
SetOpacityAnimationType(OpacityAnimationType opacityAnimationType)203     void SetOpacityAnimationType(OpacityAnimationType opacityAnimationType)
204     {
205         opacityAnimationType_ = opacityAnimationType;
206     }
GetHoverAnimationType()207     HoverAnimationType GetHoverAnimationType() const
208     {
209         return hoverAnimationType_;
210     }
SetHoverAnimationType(HoverAnimationType hoverAnimationType)211     void SetHoverAnimationType(HoverAnimationType hoverAnimationType)
212     {
213         hoverAnimationType_ = hoverAnimationType;
214     }
GetNeedAdaptAnimation()215     bool GetNeedAdaptAnimation() const
216     {
217         return needAdaptAnimation_;
218     }
SetMarkNeedRenderFunc(std::function<void ()> && func)219     void SetMarkNeedRenderFunc(std::function<void()>&& func)
220     {
221         markNeedRenderFunc_ = func;
222     }
GetTouchEvent()223     RefPtr<TouchEventImpl> GetTouchEvent()
224     {
225         return touchEvent_;
226     }
GetMouseEvent()227     RefPtr<InputEvent> GetMouseEvent()
228     {
229         return mouseEvent_;
230     }
GetHoverEvent()231     RefPtr<InputEvent> GetHoverEvent() const
232     {
233         return hoverEvent_;
234     }
SetIsUserNormalWidth(bool isUserNormalWidth)235     void SetIsUserNormalWidth(bool isUserNormalWidth)
236     {
237         isUserNormalWidth_ = isUserNormalWidth;
238     }
GetIsUserNormalWidth()239     bool GetIsUserNormalWidth() const
240     {
241         return isUserNormalWidth_;
242     }
SetStartReservedHeight(const Dimension & startReservedHeight)243     void SetStartReservedHeight(const Dimension& startReservedHeight)
244     {
245         startReservedHeight_ = startReservedHeight;
246     }
GetStartReservedHeight()247     const Dimension& GetStartReservedHeight() const
248     {
249         return startReservedHeight_;
250     }
SetEndReservedHeight(const Dimension & endReservedHeight)251     void SetEndReservedHeight(const Dimension& endReservedHeight)
252     {
253         endReservedHeight_ = endReservedHeight;
254     }
GetEndReservedHeight()255     const Dimension& GetEndReservedHeight() const
256     {
257         return endReservedHeight_;
258     }
SetHostBorderRadius(const BorderRadiusProperty & hostBorderRadius)259     void SetHostBorderRadius(const BorderRadiusProperty& hostBorderRadius)
260     {
261         hostBorderRadius_ = hostBorderRadius;
262     }
GetHostBorderRadius()263     const BorderRadiusProperty& GetHostBorderRadius() const
264     {
265         return hostBorderRadius_;
266     }
SetScrollPositionCallback(ScrollPositionCallback && callback)267     void SetScrollPositionCallback(ScrollPositionCallback&& callback)
268     {
269         scrollPositionCallback_ = std::move(callback);
270     }
GetScrollPositionCallback()271     const ScrollPositionCallback& GetScrollPositionCallback() const
272     {
273         return scrollPositionCallback_;
274     }
SetScrollEndCallback(ScrollEndCallback && scrollEndCallback)275     void SetScrollEndCallback(ScrollEndCallback&& scrollEndCallback)
276     {
277         scrollEndCallback_ = std::move(scrollEndCallback);
278     }
GetScrollEndCallback()279     const ScrollEndCallback& GetScrollEndCallback() const
280     {
281         return scrollEndCallback_;
282     }
SetCalePredictSnapOffsetCallback(CalePredictSnapOffsetCallback && calePredictSnapOffsetCallback)283     void SetCalePredictSnapOffsetCallback(CalePredictSnapOffsetCallback&& calePredictSnapOffsetCallback)
284     {
285         calePredictSnapOffsetCallback_ = std::move(calePredictSnapOffsetCallback);
286     }
GetCalePredictSnapOffsetCallback()287     const CalePredictSnapOffsetCallback& GetCalePredictSnapOffsetCallback() const
288     {
289         return calePredictSnapOffsetCallback_;
290     }
SetStartScrollSnapMotionCallback(StartScrollSnapMotionCallback && startScrollSnapMotionCallback)291     void SetStartScrollSnapMotionCallback(StartScrollSnapMotionCallback&& startScrollSnapMotionCallback)
292     {
293         startScrollSnapMotionCallback_ = std::move(startScrollSnapMotionCallback);
294     }
GetStartScrollSnapMotionCallback()295     const StartScrollSnapMotionCallback& GetStartScrollSnapMotionCallback() const
296     {
297         return startScrollSnapMotionCallback_;
298     }
SetDragFRCSceneCallback(DragFRCSceneCallback && dragFRCSceneCallback)299     void SetDragFRCSceneCallback(DragFRCSceneCallback&& dragFRCSceneCallback)
300     {
301         dragFRCSceneCallback_ = std::move(dragFRCSceneCallback);
302     }
SetDragStartPosition(float position)303     void SetDragStartPosition(float position)
304     {
305         dragStartPosition_ = position;
306     }
SetDragEndPosition(float position)307     void SetDragEndPosition(float position)
308     {
309         dragEndPosition_ = position;
310     }
GetDragOffset()311     float GetDragOffset()
312     {
313         return dragEndPosition_ - dragStartPosition_;
314     }
IsReverse()315     bool IsReverse()
316     {
317         return isReverse_;
318     }
SetAxis(Axis axis)319     void SetAxis(Axis axis)
320     {
321         axis_ = axis;
322     }
323 
324     void OnCollectTouchTarget(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl,
325         TouchTestResult& result, const RefPtr<FrameNode>& frameNode, const RefPtr<TargetComponent>& targetComponent,
326         ResponseLinkResult& responseLinkResult);
327     bool InBarTouchRegion(const Point& point) const;
328     bool InBarHoverRegion(const Point& point) const;
329     bool InBarRectRegion(const Point& point) const;
330     bool NeedScrollBar() const;
331     bool NeedPaint() const;
332     void UpdateScrollBarRegion(
333         const Offset& offset, const Size& size, const Offset& lastOffset, double estimatedHeight);
334     double GetNormalWidthToPx() const;
335     float CalcPatternOffset(float scrollBarOffset) const;
336     Color GetForegroundColor() const;
337     void SetHoverWidth(const RefPtr<ScrollBarTheme>& theme);
338     void SetNormalWidth(const Dimension& normalWidth);
339     void SetScrollable(bool isScrollable);
340     void SetPositionMode(PositionMode positionMode);
341     void SetDisplayMode(DisplayMode displayMode);
342     void PlayScrollBarDisappearAnimation();
343     void PlayScrollBarAppearAnimation();
344     void PlayScrollBarGrowAnimation();
345     void PlayScrollBarShrinkAnimation();
346     void PlayScrollBarAdaptAnimation();
347     void MarkNeedRender();
348     void SetGestureEvent();
349     void SetMouseEvent();
350     void SetHoverEvent();
351     void FlushBarWidth();
352     void CalcReservedHeight();
353     void ScheduleDisappearDelayTask();
354     float GetMainOffset(const Offset& offset) const;
355     void SetReverse(bool reverse);
356     Axis GetPanDirection() const;
357     // infos for dump
358     void AddScrollBarLayoutInfo();
359     void GetShapeModeDumpInfo();
360     void GetPositionModeDumpInfo();
361     void GetAxisDumpInfo();
362     void GetPanDirectionDumpInfo();
363     void DumpAdvanceInfo();
364     void StopFlingAnimation();
365 
366 protected:
367     void InitTheme();
368 
369 private:
370     void SetBarRegion(const Offset& offset, const Size& size);
371     void SetRectTrickRegion(const Offset& offset, const Size& size, const Offset& lastOffset, double mainScrollExtent);
372     void SetRoundTrickRegion(const Offset& offset, const Size& size, const Offset& lastOffset, double mainScrollExtent);
373     void UpdateActiveRectSize(double activeSize);
374     void UpdateActiveRectOffset(double activeMainOffset);
375     double NormalizeToPx(const Dimension& dimension) const;
376     void InitPanRecognizer();
377     void HandleDragStart(const GestureEvent& info);
378     void HandleDragUpdate(const GestureEvent& info);
379     void HandleDragEnd(const GestureEvent& info);
380     void ProcessFrictionMotion(double value);
381     void ProcessFrictionMotionStop();
382 
383     DisplayMode displayMode_ = DisplayMode::AUTO;
384     ShapeMode shapeMode_ = ShapeMode::RECT;
385     PositionMode positionMode_ = PositionMode::RIGHT;
386     BorderRadiusProperty hostBorderRadius_;
387     Edge padding_;
388     Color backgroundColor_;
389     Color foregroundColor_;
390     Rect touchRegion_;
391     Rect hoverRegion_;
392     Rect barRect_;
393     Rect activeRect_;
394     Dimension minHeight_;           // this is min static height
395     Dimension minDynamicHeight_;    // this is min dynamic height when on the top or bottom
396     Dimension startReservedHeight_; // this is reservedHeight on the start
397     Dimension endReservedHeight_;   // this is reservedHeight on the end
398     Dimension inactiveWidth_;
399     Dimension activeWidth_;
400     Dimension normalWidth_; // user-set width of the scrollbar
401     Dimension themeNormalWidth_;
402     Dimension touchWidth_;
403     Dimension hoverWidth_;
404 
405     Dimension position_;
406 
407     double trickStartAngle_ = 0.0;
408     double trickSweepAngle_ = 0.0;
409     double topAngle_ = DEFAULT_TOPANGLE;
410     double bottomAngle_ = DEFAULT_BOTTOMANGLE;
411     double minAngle_ = DEFAULT_MINANGLE;
412     double outBoundary_ = 0.0;
413     double offsetScale_ = 1.0f;
414     double scrollableOffset_ = 0.0;
415     double barRegionSize_ = 0.0;
416     double friction_ = BAR_FRICTION;
417     double frictionPosition_ = 0.0;
418     float dragStartPosition_ = 0.0f;
419     float dragEndPosition_ = 0.0f;
420 
421     bool isScrollable_ = false;
422 
423     bool isPressed_ = false;
424     bool isDriving_ = false; // false: scroll driving; true: bar driving
425     bool isHover_ = false;
426     bool isOutOfBoundary_ = false; // whether bar in the spring state
427     bool positionModeUpdate_ = false;
428     bool normalWidthUpdate_ = false;
429     bool isUserNormalWidth_ = false;
430     bool needAdaptAnimation_ = false;
431     bool isReverse_ = false;
432     bool isReverseUpdate_ = false;
433 
434     Offset paintOffset_;
435     Size viewPortSize_;
436     Offset lastOffset_;
437     double estimatedHeight_ = 0.0;
438     RefPtr<TouchEventImpl> touchEvent_;
439     RefPtr<InputEvent> mouseEvent_;
440     RefPtr<InputEvent> hoverEvent_;
441     RefPtr<PanRecognizer> panRecognizer_;
442     RefPtr<Animator> frictionController_;
443     RefPtr<FrictionMotion> frictionMotion_;
444     std::function<void()> markNeedRenderFunc_;
445     ScrollPositionCallback scrollPositionCallback_;
446     ScrollEndCallback scrollEndCallback_;
447     CalePredictSnapOffsetCallback calePredictSnapOffsetCallback_;
448     StartScrollSnapMotionCallback startScrollSnapMotionCallback_;
449     OpacityAnimationType opacityAnimationType_ = OpacityAnimationType::NONE;
450     HoverAnimationType hoverAnimationType_ = HoverAnimationType::NONE;
451     CancelableCallback<void()> disappearDelayTask_;
452     Axis axis_ = Axis::VERTICAL;
453 
454     DragFRCSceneCallback dragFRCSceneCallback_;
455 
456     // dump info
457     std::list<InnerScrollBarLayoutInfo> innerScrollBarLayoutInfos_;
458     bool needAddLayoutInfo = false;
459 };
460 
461 } // namespace OHOS::Ace::NG
462 
463 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_INNER_SCROLL_BAR_H
464