• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bridge/declarative_frontend/jsview/js_scroller.h"
17 
18 #include "base/geometry/axis.h"
19 #include "base/utils/linear_map.h"
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
22 #include "core/animation/curves.h"
23 #include "core/components/common/layout/align_declaration.h"
24 
25 namespace OHOS::Ace::Framework {
26 namespace {
27 
28 constexpr Axis DIRECTION_TABLE[] = { Axis::VERTICAL, Axis::HORIZONTAL };
29 
30 constexpr AlignDeclaration::Edge EDGE_TABLE[] = {
31     AlignDeclaration::Edge::TOP,
32     AlignDeclaration::Edge::CENTER,
33     AlignDeclaration::Edge::BOTTOM,
34     AlignDeclaration::Edge::BASELINE,
35     AlignDeclaration::Edge::START,
36     AlignDeclaration::Edge::MIDDLE,
37     AlignDeclaration::Edge::END,
38 };
39 
40 // corresponding to EDGE_TABLE[]
41 constexpr ScrollEdgeType EDGE_TYPE_TABLE[] = { ScrollEdgeType::SCROLL_TOP, ScrollEdgeType::SCROLL_NONE,
42     ScrollEdgeType::SCROLL_BOTTOM, ScrollEdgeType::SCROLL_NONE, ScrollEdgeType::SCROLL_TOP, ScrollEdgeType::SCROLL_NONE,
43     ScrollEdgeType::SCROLL_BOTTOM };
44 
45 const LinearMapNode<RefPtr<Curve>> CURVE_MAP[] = {
46     { "ease", Curves::EASE },
47     { "ease-in", Curves::EASE_IN },
48     { "ease-in-out", Curves::EASE_IN_OUT },
49     { "ease-out", Curves::EASE_OUT },
50     { "friction", Curves::FRICTION },
51     { "linear", Curves::LINEAR },
52 };
53 
54 } // namespace
55 
JSBind(BindingTarget globalObj)56 void JSScroller::JSBind(BindingTarget globalObj)
57 {
58     JSClass<JSScroller>::Declare("Scroller");
59     JSClass<JSScroller>::CustomMethod("scrollTo", &JSScroller::ScrollTo);
60     JSClass<JSScroller>::CustomMethod("scrollEdge", &JSScroller::ScrollEdge);
61     JSClass<JSScroller>::CustomMethod("scrollPage", &JSScroller::ScrollPage);
62     JSClass<JSScroller>::CustomMethod("currentOffset", &JSScroller::CurrentOffset);
63     JSClass<JSScroller>::CustomMethod("scrollToIndex", &JSScroller::ScrollToIndex);
64     JSClass<JSScroller>::Bind(globalObj, JSScroller::Constructor, JSScroller::Destructor);
65 }
66 
Constructor(const JSCallbackInfo & args)67 void JSScroller::Constructor(const JSCallbackInfo& args)
68 {
69     auto scroller = Referenced::MakeRefPtr<JSScroller>();
70     scroller->IncRefCount();
71     args.SetReturnValue(Referenced::RawPtr(scroller));
72 }
73 
Destructor(JSScroller * scroller)74 void JSScroller::Destructor(JSScroller* scroller)
75 {
76     if (scroller != nullptr) {
77         scroller->DecRefCount();
78     }
79 }
80 
ScrollTo(const JSCallbackInfo & args)81 void JSScroller::ScrollTo(const JSCallbackInfo& args)
82 {
83     if (args.Length() < 1 || !args[0]->IsObject()) {
84         LOGW("Invalid params");
85         return;
86     }
87 
88     JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
89     Dimension xOffset;
90     Dimension yOffset;
91     if (!ConvertFromJSValue(obj->GetProperty("xOffset"), xOffset) ||
92         !ConvertFromJSValue(obj->GetProperty("yOffset"), yOffset)) {
93         LOGW("Failed to parse param 'xOffset' or 'yOffset'");
94         return;
95     }
96 
97     double duration = 0.0;
98     RefPtr<Curve> curve = Curves::EASE;
99     auto animationValue = obj->GetProperty("animation");
100     if (animationValue->IsObject()) {
101         auto animationObj = JSRef<JSObject>::Cast(animationValue);
102         ConvertFromJSValue(animationObj->GetProperty("duration"), duration);
103 
104         std::string curveName;
105         if (ConvertFromJSValue(animationObj->GetProperty("curve"), curveName)) {
106             auto index = BinarySearchFindIndex(CURVE_MAP, ArraySize(CURVE_MAP), curveName.c_str());
107             if (index >= 0) {
108                 curve = CURVE_MAP[index].value;
109             }
110         }
111     }
112 
113     if (GreatNotEqual(duration, 0.0)) {
114         LOGD("ScrollTo(%lf, %lf, %lf)", xOffset.Value(), yOffset.Value(), duration);
115     } else {
116         LOGD("ScrollTo(%lf, %lf)", xOffset.Value(), yOffset.Value());
117     }
118     if (!controller_) {
119         LOGE("controller_ is nullptr");
120         return;
121     }
122     auto direction = controller_->GetScrollDirection();
123     auto position = direction == Axis::VERTICAL ? yOffset : xOffset;
124     controller_->AnimateTo(position, duration, curve);
125 }
126 
ScrollEdge(const JSCallbackInfo & args)127 void JSScroller::ScrollEdge(const JSCallbackInfo& args)
128 {
129     AlignDeclaration::Edge edge = AlignDeclaration::Edge::AUTO;
130     if (args.Length() < 1 || !ConvertFromJSValue(args[0], EDGE_TABLE, edge)) {
131         LOGW("Invalid params");
132         return;
133     }
134     if (!controller_) {
135         LOGE("controller_ is nullptr");
136         return;
137     }
138     LOGD("ScrollEdge(%{public}d)", static_cast<int32_t>(edge));
139     ScrollEdgeType edgeType = EDGE_TYPE_TABLE[static_cast<int32_t>(edge)];
140     controller_->ScrollToEdge(edgeType, true);
141 }
142 
ScrollToIndex(const JSCallbackInfo & args)143 void JSScroller::ScrollToIndex(const JSCallbackInfo& args)
144 {
145     int32_t index = 0;
146     if (args.Length() < 1 || !ConvertFromJSValue(args[0], index)) {
147         LOGW("Invalid params");
148         return;
149     }
150     if (!controller_) {
151         LOGE("controller_ is nullptr");
152         return;
153     }
154     controller_->JumpTo(index, SCROLL_FROM_JUMP);
155 }
156 
ScrollPage(const JSCallbackInfo & args)157 void JSScroller::ScrollPage(const JSCallbackInfo& args)
158 {
159     if (args.Length() < 1 || !args[0]->IsObject()) {
160         LOGW("Invalid params");
161         return;
162     }
163 
164     auto obj = JSRef<JSObject>::Cast(args[0]);
165     bool next = true;
166     if (!ConvertFromJSValue(obj->GetProperty("next"), next)) {
167         LOGW("Failed to parse param 'next'");
168         return;
169     }
170 
171     Axis direction = Axis::NONE;
172     ConvertFromJSValue(obj->GetProperty("direction"), DIRECTION_TABLE, direction);
173     if (!controller_) {
174         LOGE("controller_ is nullptr");
175         return;
176     }
177     LOGD("ScrollPage(%{public}s, %{public}d)", next ? "true" : "false", static_cast<int32_t>(direction));
178     controller_->ScrollPage(!next, true);
179 }
180 
CurrentOffset(const JSCallbackInfo & args)181 void JSScroller::CurrentOffset(const JSCallbackInfo& args)
182 {
183     LOGD("CurrentOffset()");
184     if (!controller_) {
185         LOGE("controller_ is nullptr");
186         return;
187     }
188     auto retObj = JSRef<JSObject>::New();
189     auto offset = controller_->GetCurrentOffset();
190     retObj->SetProperty("xOffset", offset.GetX());
191     retObj->SetProperty("yOffset", offset.GetY());
192     args.SetReturnValue(retObj);
193 }
194 
195 } // namespace OHOS::Ace::Framework
196