• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "bridge/declarative_frontend/jsview/models/scroll_model_impl.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "base/utils/utils.h"
20 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
21 
22 namespace OHOS::Ace::Framework {
23 namespace {
24 const std::vector<DisplayMode> DISPLAY_MODE = { DisplayMode::OFF, DisplayMode::AUTO, DisplayMode::ON };
25 } // namespace
26 
Create()27 void ScrollModelImpl::Create()
28 {
29     RefPtr<Component> child;
30     auto scrollComponent = AceType::MakeRefPtr<OHOS::Ace::ScrollComponent>(child);
31     ViewStackProcessor::GetInstance()->ClaimElementId(scrollComponent);
32     auto positionController = AceType::MakeRefPtr<ScrollPositionController>();
33     scrollComponent->SetScrollPositionController(positionController);
34     ViewStackProcessor::GetInstance()->Push(scrollComponent);
35 }
36 
GetOrCreateController()37 RefPtr<ScrollControllerBase> ScrollModelImpl::GetOrCreateController()
38 {
39     auto component = AceType::DynamicCast<ScrollComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
40     CHECK_NULL_RETURN(component, nullptr);
41     if (!component->GetScrollPositionController()) {
42         component->SetScrollPositionController(AceType::MakeRefPtr<ScrollPositionController>());
43     }
44     return component->GetScrollPositionController();
45 }
46 
CreateScrollBarProxy()47 RefPtr<ScrollProxy> ScrollModelImpl::CreateScrollBarProxy()
48 {
49     return AceType::MakeRefPtr<ScrollBarProxy>();
50 }
51 
SetAxis(Axis axis)52 void ScrollModelImpl::SetAxis(Axis axis)
53 {
54     JSViewSetProperty(&ScrollComponent::SetAxisDirection, axis);
55 }
56 
SetOnScrollBegin(OnScrollBeginEvent && event)57 void ScrollModelImpl::SetOnScrollBegin(OnScrollBeginEvent&& event)
58 {
59     JSViewSetProperty(&ScrollComponent::SetOnScrollBegin, std::move(event));
60 }
61 
SetOnScrollFrameBegin(OnScrollFrameBeginEvent && event)62 void ScrollModelImpl::SetOnScrollFrameBegin(OnScrollFrameBeginEvent&& event)
63 {
64 }
65 
SetOnScroll(NG::ScrollEvent && event)66 void ScrollModelImpl::SetOnScroll(NG::ScrollEvent&& event)
67 {
68     auto onScroll = EventMarker([scrollEvent = std::move(event)](const BaseEventInfo* info) {
69         auto eventInfo = TypeInfoHelper::DynamicCast<ScrollEventInfo>(info);
70         if (!eventInfo || !scrollEvent) {
71             return;
72         }
73         Dimension x(eventInfo->GetScrollX(), DimensionUnit::VP);
74         Dimension y(eventInfo->GetScrollY(), DimensionUnit::VP);
75         scrollEvent(x, y);
76     });
77     JSViewSetProperty(&ScrollComponent::SetOnScroll, std::move(onScroll));
78 }
79 
SetOnScrollEdge(NG::ScrollEdgeEvent && event)80 void ScrollModelImpl::SetOnScrollEdge(NG::ScrollEdgeEvent&& event)
81 {
82     auto onScrollEdge = EventMarker([scrollEvent = std::move(event)](const BaseEventInfo* info) {
83         auto eventInfo = TypeInfoHelper::DynamicCast<ScrollEventInfo>(info);
84         if (!eventInfo || !scrollEvent) {
85             return;
86         }
87         if (eventInfo->GetType() == ScrollEvent::SCROLL_TOP) {
88             scrollEvent(NG::ScrollEdge::TOP);
89         } else if (eventInfo->GetType() == ScrollEvent::SCROLL_BOTTOM) {
90             scrollEvent(NG::ScrollEdge::BOTTOM);
91         } else {
92             LOGW("EventType is not support: %{public}d", static_cast<int32_t>(eventInfo->GetType()));
93         }
94     });
95     JSViewSetProperty(&ScrollComponent::SetOnScrollEdge, std::move(onScrollEdge));
96 }
97 
SetOnScrollEnd(NG::ScrollEndEvent && event)98 void ScrollModelImpl::SetOnScrollEnd(NG::ScrollEndEvent&& event)
99 {
100     auto onScrollEnd = EventMarker([scrollEvent = std::move(event)](const BaseEventInfo* info) {
101         if (scrollEvent) {
102             scrollEvent();
103         }
104     });
105     JSViewSetProperty(&ScrollComponent::SetOnScrollEnd, std::move(onScrollEnd));
106 }
107 
SetScrollBarProxy(const RefPtr<ScrollProxy> & proxy)108 void ScrollModelImpl::SetScrollBarProxy(const RefPtr<ScrollProxy>& proxy)
109 {
110     auto scrollBarProxy = AceType::DynamicCast<ScrollBarProxy>(proxy);
111     JSViewSetProperty(&ScrollComponent::SetScrollBarProxy, scrollBarProxy);
112 }
113 
InitScrollBar(const RefPtr<ScrollBarTheme> & theme,const std::pair<bool,Color> & color,const std::pair<bool,Dimension> & width,EdgeEffect effect)114 void ScrollModelImpl::InitScrollBar(const RefPtr<ScrollBarTheme>& theme, const std::pair<bool, Color>& color,
115     const std::pair<bool, Dimension>& width, EdgeEffect effect)
116 {
117     auto component = AceType::DynamicCast<ScrollComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
118     CHECK_NULL_VOID(component);
119     component->InitScrollBar(theme, color, width, effect);
120 }
121 
SetDisplayMode(int displayMode)122 void ScrollModelImpl::SetDisplayMode(int displayMode)
123 {
124     if (displayMode >= 0 && displayMode < static_cast<int32_t>(DISPLAY_MODE.size())) {
125         JSViewSetProperty(&ScrollComponent::SetDisplayMode, DISPLAY_MODE[displayMode]);
126     }
127 }
128 
SetScrollBarWidth(const Dimension & dimension)129 void ScrollModelImpl::SetScrollBarWidth(const Dimension& dimension)
130 {
131     JSViewSetProperty(&ScrollComponent::SetScrollBarWidth, dimension);
132 }
133 
SetScrollBarColor(const Color & color)134 void ScrollModelImpl::SetScrollBarColor(const Color& color)
135 {
136     JSViewSetProperty(&ScrollComponent::SetScrollBarColor, color);
137 }
138 
SetEdgeEffect(EdgeEffect edgeEffect,bool alwaysEnabled,EffectEdge effectEdge)139 void ScrollModelImpl::SetEdgeEffect(EdgeEffect edgeEffect, bool alwaysEnabled, EffectEdge effectEdge)
140 {
141     auto scrollComponent = AceType::DynamicCast<ScrollComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
142     CHECK_NULL_VOID(scrollComponent);
143     RefPtr<ScrollEdgeEffect> scrollEdgeEffect;
144     if (edgeEffect == EdgeEffect::SPRING) {
145         scrollEdgeEffect = AceType::MakeRefPtr<ScrollSpringEffect>();
146     } else if (edgeEffect == EdgeEffect::FADE) {
147         scrollEdgeEffect = AceType::MakeRefPtr<ScrollFadeEffect>(Color::GRAY);
148     } else {
149         scrollEdgeEffect = AceType::MakeRefPtr<ScrollEdgeEffect>(EdgeEffect::NONE);
150     }
151     scrollComponent->SetScrollEffect(scrollEdgeEffect);
152 }
153 
SetHasWidth(bool hasWidth)154 void ScrollModelImpl::SetHasWidth(bool hasWidth)
155 {
156     JSViewSetProperty(&ScrollComponent::SetHasWidth, hasWidth);
157 }
158 
SetHasHeight(bool hasHeight)159 void ScrollModelImpl::SetHasHeight(bool hasHeight)
160 {
161     JSViewSetProperty(&ScrollComponent::SetHasHeight, hasHeight);
162 }
163 
164 } // namespace OHOS::Ace::Framework
165