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 #include "core/components/scroll/scroll_component.h"
17
18 #include "core/components/scroll/render_single_child_scroll.h"
19 #include "core/components/scroll/scroll_element.h"
20
21 namespace OHOS::Ace {
22
ScrollComponent(const RefPtr<Component> & child)23 ScrollComponent::ScrollComponent(const RefPtr<Component>& child) : SoleChildComponent(child) {}
24
CreateElement()25 RefPtr<Element> ScrollComponent::CreateElement()
26 {
27 return AceType::MakeRefPtr<ScrollElement>();
28 }
29
CreateRenderNode()30 RefPtr<RenderNode> ScrollComponent::CreateRenderNode()
31 {
32 return RenderSingleChildScroll::Create();
33 }
34
InitScrollBar(const RefPtr<ScrollBarTheme> & theme,const std::pair<bool,Color> & scrollBarColor,const std::pair<bool,Dimension> & scrollBarWidth,EdgeEffect edgeEffect)35 void ScrollComponent::InitScrollBar(const RefPtr<ScrollBarTheme>& theme, const std::pair<bool, Color>& scrollBarColor,
36 const std::pair<bool, Dimension>& scrollBarWidth, EdgeEffect edgeEffect)
37 {
38 if (!positionController_) {
39 positionController_ = AceType::MakeRefPtr<ScrollPositionController>();
40 }
41 if (edgeEffect == EdgeEffect::SPRING) {
42 SetScrollEffect(AceType::MakeRefPtr<ScrollSpringEffect>());
43 } else if (edgeEffect == EdgeEffect::FADE) {
44 SetScrollEffect(AceType::MakeRefPtr<ScrollFadeEffect>(Color::GRAY));
45 } else {
46 SetScrollEffect(AceType::MakeRefPtr<ScrollEdgeEffect>(EdgeEffect::NONE));
47 }
48
49 if (!theme) {
50 return;
51 }
52
53 scrollBar_ = AceType::MakeRefPtr<ScrollBar>(DisplayMode::AUTO, theme->GetShapeMode());
54 if (scrollBarWidth.first) {
55 const auto& width = scrollBarWidth.second;
56 scrollBar_->SetInactiveWidth(width);
57 scrollBar_->SetNormalWidth(width);
58 scrollBar_->SetActiveWidth(width);
59 scrollBar_->SetTouchWidth(width);
60 } else {
61 scrollBar_->SetInactiveWidth(theme->GetNormalWidth());
62 scrollBar_->SetNormalWidth(theme->GetNormalWidth());
63 scrollBar_->SetActiveWidth(theme->GetActiveWidth());
64 scrollBar_->SetTouchWidth(theme->GetTouchWidth());
65 }
66 scrollBar_->SetReservedHeight(theme->GetReservedHeight());
67 scrollBar_->SetMinHeight(theme->GetMinHeight());
68 scrollBar_->SetMinDynamicHeight(theme->GetMinDynamicHeight());
69 scrollBar_->SetBackgroundColor(theme->GetBackgroundColor());
70 scrollBar_->SetForegroundColor(scrollBarColor.first ? scrollBarColor.second : theme->GetForegroundColor());
71 scrollBar_->SetPadding(theme->GetPadding());
72 scrollBar_->SetScrollable(true);
73 }
74
75 } // namespace OHOS::Ace