1 /*
2  * Copyright (c) 2021-2022 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_BASE_PROPERTIES_SCROLL_BAR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_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/components/common/properties/color.h"
26 #include "core/components/common/properties/edge.h"
27 #include "core/components/scroll/scroll_bar_controller.h"
28 #include "core/components/scroll/scroll_position_controller.h"
29 
30 namespace OHOS::Ace {
31 
32 constexpr double FACTOR_HALF = 0.5;
33 constexpr double DEFAULT_TOPANGLE = 60.0;
34 constexpr double DEFAULT_BOTTOMANGLE = 120.0;
35 constexpr double DEFAULT_MINANGLE = 10.0;
36 constexpr double STRAIGHT_ANGLE = 180.0;
37 constexpr Color PRESSED_BLEND_COLOR = Color(0x19000000);
38 
39 class ScrollBar final : public AceType {
40     DECLARE_ACE_TYPE(ScrollBar, AceType);
41 
42 public:
43     ScrollBar() = default;
44     explicit ScrollBar(
45         DisplayMode displayMode, ShapeMode shapeMode = ShapeMode::RECT, PositionMode positionMode = PositionMode::RIGHT)
46         : displayMode_(displayMode), shapeMode_(shapeMode), positionMode_(positionMode) {}
47     ~ScrollBar() override = default;
48 
49     bool InBarRegion(const Point& point) const;
50     bool NeedScrollBar() const;
51     bool NeedPaint() const;
52     void UpdateScrollBarRegion(
53         const Offset& offset, const Size& size, const Offset& lastOffset, double estimatedHeight);
54     double GetNormalWidthToPx() const;
55     void InitScrollBar(const WeakPtr<RenderNode>& scroll, const WeakPtr<PipelineContext>& context);
56     void SetCallBack(const ScrollBarPositionCallback& callback, const ScrollBarEndCallback& barEndCallback,
57         const ScrollBarEventCallback& scrollEndCallback);
58     void HandleScrollBarEnd();
59     void AddScrollBarController(const Offset& coordinateOffset, TouchTestResult& result);
60     void SetActive(bool isActive);
61     bool IsActive() const;
62     Size GetRootSize() const;
63 
64     void Reset(const RefPtr<ScrollBar>& scrollBar = nullptr);
65 
GetShapeMode()66     ShapeMode GetShapeMode() const
67     {
68         return shapeMode_;
69     }
70 
GetDisplayMode()71     DisplayMode GetDisplayMode() const
72     {
73         return displayMode_;
74     }
75 
GetPositionMode()76     PositionMode GetPositionMode() const
77     {
78         return positionMode_;
79     }
80 
SetPadding(const Edge & padding)81     void SetPadding(const Edge& padding)
82     {
83         padding_ = padding;
84     }
85 
GetPadding()86     const Edge& GetPadding() const
87     {
88         return padding_;
89     }
90 
SetBackgroundColor(const Color & backgroundColor)91     void SetBackgroundColor(const Color& backgroundColor)
92     {
93         backgroundColor_ = backgroundColor;
94     }
95 
GetBackgroundColor()96     const Color& GetBackgroundColor() const
97     {
98         return backgroundColor_;
99     }
100 
SetForegroundColor(const Color & foregroundColor)101     void SetForegroundColor(const Color& foregroundColor)
102     {
103         foregroundColor_ = foregroundColor;
104     }
105 
GetForegroundColor()106     Color GetForegroundColor() const
107     {
108         return IsPressed() ? foregroundColor_.BlendColor(PRESSED_BLEND_COLOR) : foregroundColor_;
109     }
110 
GetTopAngle()111     double GetTopAngle() const
112     {
113         return topAngle_;
114     }
115 
GetBottomAngle()116     double GetBottomAngle() const
117     {
118         return bottomAngle_;
119     }
120 
GetTrickStartAngle()121     double GetTrickStartAngle() const
122     {
123         return trickStartAngle_;
124     }
125 
GetTrickSweepAngle()126     double GetTrickSweepAngle() const
127     {
128         return trickSweepAngle_;
129     }
130 
SetMinHeight(const Dimension & minHeight)131     void SetMinHeight(const Dimension& minHeight)
132     {
133         minHeight_ = minHeight;
134     }
135 
GetMinHeight()136     const Dimension& GetMinHeight() const
137     {
138         return minHeight_;
139     }
140 
SetMinDynamicHeight(const Dimension & minDynamicHeight)141     void SetMinDynamicHeight(const Dimension& minDynamicHeight)
142     {
143         minDynamicHeight_ = minDynamicHeight;
144     }
145 
GetMinDynamicHeight()146     const Dimension& GetMinDynamicHeight() const
147     {
148         return minDynamicHeight_;
149     }
150 
SetReservedHeight(const Dimension & height)151     void SetReservedHeight(const Dimension& height)
152     {
153         reservedHeight_ = height;
154     }
155 
GetReservedHeight()156     const Dimension& GetReservedHeight() const
157     {
158         return reservedHeight_;
159     }
160 
SetInactiveWidth(const Dimension & inactiveWidth)161     void SetInactiveWidth(const Dimension& inactiveWidth)
162     {
163         inactiveWidth_ = inactiveWidth;
164     }
165 
GetInactiveWidth()166     const Dimension& GetInactiveWidth() const
167     {
168         return inactiveWidth_;
169     }
170 
SetActiveWidth(const Dimension & activeWidth)171     void SetActiveWidth(const Dimension& activeWidth)
172     {
173         activeWidth_ = activeWidth;
174     }
175 
GetActiveWidth()176     const Dimension& GetActiveWidth() const
177     {
178         return activeWidth_;
179     }
180 
SetNormalWidth(const Dimension & normalWidth)181     void SetNormalWidth(const Dimension& normalWidth)
182     {
183         normalWidth_ = normalWidth;
184     }
185 
GetNormalWidth()186     const Dimension& GetNormalWidth() const
187     {
188         return normalWidth_;
189     }
190 
GetActiveRect()191     const Rect& GetActiveRect() const
192     {
193         return activeRect_;
194     }
195 
SetTouchWidth(const Dimension & touchWidth)196     void SetTouchWidth(const Dimension& touchWidth)
197     {
198         touchWidth_ = touchWidth;
199     }
200 
GetTouchWidth()201     const Dimension& GetTouchWidth() const
202     {
203         return touchWidth_;
204     }
205 
GetBarRect()206     const Rect& GetBarRect() const
207     {
208         return barRect_;
209     }
210 
SetScrollable(bool isScrollable)211     void SetScrollable(bool isScrollable)
212     {
213         isScrollable_ = isScrollable;
214     }
215 
IsScrollable()216     bool IsScrollable() const
217     {
218         return isScrollable_;
219     }
220 
SetPositionMode(PositionMode positionMode)221     void SetPositionMode(PositionMode positionMode)
222     {
223         positionMode_ = positionMode;
224     }
225 
SetShapeMode(ShapeMode shapeMode)226     void SetShapeMode(ShapeMode shapeMode)
227     {
228         shapeMode_ = shapeMode;
229     }
230 
SetDisplayMode(DisplayMode displayMode)231     void SetDisplayMode(DisplayMode displayMode)
232     {
233         displayMode_ = displayMode;
234     }
235 
SetOutBoundary(double outBoundary)236     void SetOutBoundary(double outBoundary)
237     {
238         outBoundary_ = outBoundary;
239     }
240 
GetController()241     RefPtr<ScrollBarController> GetController()
242     {
243         return barController_;
244     }
245 
SetPosition(const Dimension & position)246     void SetPosition(const Dimension& position)
247     {
248         position_ = position;
249     }
250 
GetPosition()251     const Dimension& GetPosition() const
252     {
253         return position_;
254     }
255 
SetScrollBarController(RefPtr<ScrollBarController> controller)256     void SetScrollBarController(RefPtr<ScrollBarController> controller)
257     {
258         barController_ = std::move(controller);
259     }
260 
IsPressed()261     bool IsPressed() const
262     {
263         return barController_ ?  barController_->IsPressed() : false;
264     }
265 
SetIsHover(bool isInBarRegion)266     void SetIsHover(bool isInBarRegion) const
267     {
268         if (barController_) {
269             barController_->SetIsHover(isInBarRegion);
270         }
271     }
272 
273 private:
274     void SetBarRegion(const Offset& offset, const Size& size);
275     void SetRectTrickRegion(const Offset& offset, const Size& size, const Offset& lastOffset, double mainScrollExtent);
276     void SetRoundTrickRegion(const Offset& offset, const Size& size, const Offset& lastOffset, double mainScrollExtent);
277     double NormalizeToPx(const Dimension& dimension) const;
278 
279     DisplayMode displayMode_ = DisplayMode::OFF;
280     ShapeMode shapeMode_ = ShapeMode::RECT;
281     PositionMode positionMode_ = PositionMode::RIGHT;
282     Edge padding_;
283     Color backgroundColor_;
284     Color foregroundColor_;
285     Rect touchRegion_;
286     Rect barRect_;
287     Rect activeRect_;
288     Dimension minHeight_; // this is min static height
289     Dimension minDynamicHeight_; // this is min dynamic height when on the top or bottom
290     Dimension reservedHeight_; // this is reservedHeight on the bottom
291     Dimension inactiveWidth_;
292     Dimension activeWidth_;
293     Dimension normalWidth_;
294     Dimension touchWidth_;
295 
296     Dimension position_;
297 
298     double trickStartAngle_ = 0.0;
299     double trickSweepAngle_ = 0.0;
300     double topAngle_ = DEFAULT_TOPANGLE;
301     double bottomAngle_ = DEFAULT_BOTTOMANGLE;
302     double minAngle_ = DEFAULT_MINANGLE;
303     double outBoundary_ = 0.0;
304 
305     bool isScrollable_ = false;
306 
307     WeakPtr<PipelineContext> pipelineContext_;
308     RefPtr<ScrollBarController> barController_;
309 };
310 
311 } // namespace OHOS::Ace
312 
313 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_SCROLL_BAR_H
314