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