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_SCROLL_SCROLL_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_COMPONENT_H 18 19 #include "base/geometry/axis.h" 20 #include "core/components/common/properties/edge.h" 21 #include "core/components/common/properties/scroll_bar.h" 22 #include "core/components/scroll/scroll_bar_theme.h" 23 #include "core/components/scroll/scroll_edge_effect.h" 24 #include "core/components/scroll/scroll_fade_effect.h" 25 #include "core/components/scroll/scroll_position_controller.h" 26 #include "core/components/scroll/scroll_spring_effect.h" 27 #include "core/components/scroll_bar/scroll_bar_proxy.h" 28 #include "core/pipeline/base/element.h" 29 #include "core/pipeline/base/render_node.h" 30 #include "core/pipeline/base/sole_child_component.h" 31 32 namespace OHOS::Ace { 33 34 class ACE_EXPORT ScrollComponent : public SoleChildComponent { 35 DECLARE_ACE_TYPE(ScrollComponent, SoleChildComponent); 36 37 public: 38 explicit ScrollComponent(const RefPtr<Component>& child); 39 ~ScrollComponent() override = default; 40 41 RefPtr<Element> CreateElement() override; 42 RefPtr<RenderNode> CreateRenderNode() override; 43 44 void InitScrollBar(const RefPtr<ScrollBarTheme>& theme, const std::pair<bool, Color>& scrollBarColor, 45 const std::pair<bool, Dimension>& scrollBarWidth, EdgeEffect edgeEffect); 46 SetAxisDirection(Axis axisDirection)47 void SetAxisDirection(Axis axisDirection) 48 { 49 if (axisDirection == Axis::FREE) { 50 LOGE("Invalid direction: %{public}d", axisDirection); 51 return; 52 } 53 axisDirection_ = axisDirection; 54 } 55 GetAxisDirection()56 Axis GetAxisDirection() const 57 { 58 return axisDirection_; 59 } 60 SetEnable(bool enable)61 void SetEnable(bool enable) 62 { 63 enable_ = enable; 64 } 65 GetEnable()66 bool GetEnable() const 67 { 68 return enable_; 69 } 70 SetPadding(const Edge & padding)71 void SetPadding(const Edge& padding) 72 { 73 if (padding.IsValid()) { 74 padding_ = padding; 75 } else { 76 LOGE("Invalid padding input."); 77 } 78 } 79 GetPadding()80 const Edge& GetPadding() const 81 { 82 return padding_; 83 } 84 SetScrollPositionController(const RefPtr<ScrollPositionController> & positionController)85 void SetScrollPositionController(const RefPtr<ScrollPositionController>& positionController) 86 { 87 positionController_ = positionController; 88 } 89 GetScrollPositionController()90 RefPtr<ScrollPositionController> GetScrollPositionController() const 91 { 92 return positionController_; 93 } 94 SetScrollPage(bool scrollPage)95 void SetScrollPage(bool scrollPage) 96 { 97 scrollPage_ = scrollPage; 98 } 99 GetScrollPage()100 bool GetScrollPage() const 101 { 102 return scrollPage_; 103 } 104 SetScrollBar(const RefPtr<ScrollBar> & scrollBar)105 void SetScrollBar(const RefPtr<ScrollBar>& scrollBar) 106 { 107 scrollBar_ = scrollBar; 108 } 109 GetScrollBar()110 RefPtr<ScrollBar> GetScrollBar() const 111 { 112 return scrollBar_; 113 } 114 GetScrollEffect()115 const RefPtr<ScrollEdgeEffect>& GetScrollEffect() const 116 { 117 return scrollEffect_; 118 } 119 SetScrollEffect(const RefPtr<ScrollEdgeEffect> & scrollEffect)120 void SetScrollEffect(const RefPtr<ScrollEdgeEffect>& scrollEffect) 121 { 122 isEffectSetted_ = true; 123 scrollEffect_ = scrollEffect; 124 } 125 IsEffectSetted()126 bool IsEffectSetted() const 127 { 128 return isEffectSetted_; 129 } 130 SetOnReachStart(const EventMarker & onReachStart)131 void SetOnReachStart(const EventMarker& onReachStart) 132 { 133 onReachStart_ = onReachStart; 134 } 135 SetOnReachEnd(const EventMarker & onReachEnd)136 void SetOnReachEnd(const EventMarker& onReachEnd) 137 { 138 onReachEnd_ = onReachEnd; 139 } 140 SetOnReachTop(const EventMarker & onReachTop)141 void SetOnReachTop(const EventMarker& onReachTop) 142 { 143 onReachTop_ = onReachTop; 144 } 145 SetOnReachBottom(const EventMarker & onReachBottom)146 void SetOnReachBottom(const EventMarker& onReachBottom) 147 { 148 onReachBottom_ = onReachBottom; 149 } 150 GetOnReachStart()151 const EventMarker& GetOnReachStart() const 152 { 153 return onReachStart_; 154 } 155 GetOnReachEnd()156 const EventMarker& GetOnReachEnd() const 157 { 158 return onReachEnd_; 159 } 160 GetOnReachTop()161 const EventMarker& GetOnReachTop() const 162 { 163 return onReachTop_; 164 } 165 GetOnReachBottom()166 const EventMarker& GetOnReachBottom() const 167 { 168 return onReachBottom_; 169 } 170 GetOnScroll()171 const EventMarker& GetOnScroll() const 172 { 173 return onScroll_; 174 } 175 SetOnScroll(const EventMarker & onScroll)176 void SetOnScroll(const EventMarker& onScroll) 177 { 178 onScroll_ = onScroll; 179 } 180 GetOnScrollEdge()181 const EventMarker& GetOnScrollEdge() const 182 { 183 return onScrollEdge_; 184 } 185 SetOnScrollEdge(const EventMarker & onScrollEdge)186 void SetOnScrollEdge(const EventMarker& onScrollEdge) 187 { 188 onScrollEdge_ = onScrollEdge; 189 } 190 GetOnScrollEnd()191 const EventMarker& GetOnScrollEnd() const 192 { 193 return onScrollEnd_; 194 } 195 SetOnScrollEnd(const EventMarker & onScrollEnd)196 void SetOnScrollEnd(const EventMarker& onScrollEnd) 197 { 198 onScrollEnd_ = onScrollEnd; 199 } 200 SetDisplayMode(DisplayMode displayMode)201 void SetDisplayMode(DisplayMode displayMode) 202 { 203 if (!scrollBar_) { 204 return; 205 } 206 scrollBar_->SetDisplayMode(displayMode); 207 } 208 SetScrollBarColor(const Color & scrollBarColor)209 void SetScrollBarColor(const Color& scrollBarColor) 210 { 211 if (!scrollBar_) { 212 return; 213 } 214 scrollBar_->SetForegroundColor(scrollBarColor); 215 } 216 SetScrollBarWidth(const Dimension & scrollBarWidth)217 void SetScrollBarWidth(const Dimension& scrollBarWidth) 218 { 219 if (!scrollBar_) { 220 return; 221 } 222 scrollBar_->SetInactiveWidth(scrollBarWidth); 223 scrollBar_->SetNormalWidth(scrollBarWidth); 224 scrollBar_->SetActiveWidth(scrollBarWidth); 225 scrollBar_->SetTouchWidth(scrollBarWidth); 226 } 227 SetScrollBarProxy(const RefPtr<ScrollBarProxy> & scrollBarProxy)228 void SetScrollBarProxy(const RefPtr<ScrollBarProxy>& scrollBarProxy) 229 { 230 scrollBarProxy_ = scrollBarProxy; 231 } 232 GetScrollBarProxy()233 const RefPtr<ScrollBarProxy>& GetScrollBarProxy() const 234 { 235 return scrollBarProxy_; 236 } 237 SetOnScrollBegin(const ScrollBeginCallback & onScrollBegin)238 void SetOnScrollBegin(const ScrollBeginCallback& onScrollBegin) 239 { 240 onScrollBegin_ = onScrollBegin; 241 } 242 GetOnScrollBegin()243 const ScrollBeginCallback& GetOnScrollBegin() const 244 { 245 return onScrollBegin_; 246 } 247 SetHasWidth(const bool & hasWidth)248 void SetHasWidth(const bool& hasWidth) 249 { 250 hasWidth_ = hasWidth; 251 } 252 GetHasWidth()253 const bool& GetHasWidth() const 254 { 255 return hasWidth_; 256 } 257 SetHasHeight(const bool & hasHeight)258 void SetHasHeight(const bool& hasHeight) 259 { 260 hasHeight_ = hasHeight; 261 } 262 GetHasHeight()263 const bool& GetHasHeight() const 264 { 265 return hasHeight_; 266 } 267 268 private: 269 Edge padding_; 270 Axis axisDirection_ = Axis::VERTICAL; 271 RefPtr<ScrollPositionController> positionController_; 272 bool scrollPage_ = false; 273 RefPtr<ScrollBar> scrollBar_; 274 bool enable_ = true; 275 bool isEffectSetted_ = false; 276 RefPtr<ScrollEdgeEffect> scrollEffect_; 277 RefPtr<ScrollBarProxy> scrollBarProxy_; 278 279 // for scroll reaching the edge event 280 EventMarker onReachStart_; 281 EventMarker onReachEnd_; 282 EventMarker onReachTop_; 283 EventMarker onReachBottom_; 284 285 EventMarker onScroll_; 286 EventMarker onScrollEdge_; 287 EventMarker onScrollEnd_; 288 289 ScrollBeginCallback onScrollBegin_; 290 291 bool hasWidth_ = false; 292 bool hasHeight_ = false; 293 }; 294 295 } // namespace OHOS::Ace 296 297 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_COMPONENT_H 298