• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_scrollable_base.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 #include "core/components_ng/pattern/scrollable/scrollable_model_ng.h"
20 
21 namespace OHOS::Ace::Framework {
JSFlingSpeedLimit(const JSCallbackInfo & info)22 void JSScrollableBase::JSFlingSpeedLimit(const JSCallbackInfo& info)
23 {
24     double max = -1.0;
25     if (!JSViewAbstract::ParseJsDouble(info[0], max)) {
26         return;
27     }
28     NG::ScrollableModelNG::SetMaxFlingSpeed(max);
29 }
30 
JsOnWillScroll(const JSCallbackInfo & args)31 void JSScrollableBase::JsOnWillScroll(const JSCallbackInfo& args)
32 {
33     if (args.Length() <= 0) {
34         return;
35     }
36     if (args[0]->IsFunction()) {
37         auto onScroll = [execCtx = args.GetExecutionContext(), func = JSRef<JSFunc>::Cast(args[0])](
38                             const CalcDimension& scrollOffset, const ScrollState& scrollState,
39                             ScrollSource scrollSource) {
40             auto params = ConvertToJSValues(scrollOffset, scrollState, scrollSource);
41             ScrollFrameResult scrollRes { .offset = scrollOffset };
42             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, scrollRes);
43             auto result = func->Call(JSRef<JSObject>(), params.size(), params.data());
44             if (result.IsEmpty()) {
45                 return scrollRes;
46             }
47 
48             if (!result->IsObject()) {
49                 return scrollRes;
50             }
51 
52             auto resObj = JSRef<JSObject>::Cast(result);
53             auto dxRemainValue = resObj->GetProperty("offsetRemain");
54             if (dxRemainValue->IsNumber()) {
55                 scrollRes.offset = Dimension(dxRemainValue->ToNumber<float>(), DimensionUnit::VP);
56             }
57             return scrollRes;
58         };
59         NG::ScrollableModelNG::SetOnWillScroll(std::move(onScroll));
60     } else {
61         NG::ScrollableModelNG::SetOnWillScroll(nullptr);
62     }
63 }
64 
JsOnDidScroll(const JSCallbackInfo & args)65 void JSScrollableBase::JsOnDidScroll(const JSCallbackInfo& args)
66 {
67     if (args.Length() > 0 && args[0]->IsFunction()) {
68         auto onScroll = [execCtx = args.GetExecutionContext(), func = JSRef<JSFunc>::Cast(args[0])](
69                             const CalcDimension& scrollOffset, const ScrollState& scrollState) {
70             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
71             auto params = ConvertToJSValues(scrollOffset, scrollState);
72             func->Call(JSRef<JSObject>(), params.size(), params.data());
73         };
74         NG::ScrollableModelNG::SetOnDidScroll(std::move(onScroll));
75     }
76 }
77 
JSBind(BindingTarget globalObj)78 void JSScrollableBase::JSBind(BindingTarget globalObj)
79 {
80     MethodOptions opt = MethodOptions::NONE;
81     JSClass<JSScrollableBase>::Declare("JSContainerBase");
82     JSClass<JSScrollableBase>::StaticMethod("flingSpeedLimit", &JSScrollableBase::JSFlingSpeedLimit, opt);
83     JSClass<JSScrollableBase>::StaticMethod("onWillScroll", &JSScrollableBase::JsOnWillScroll);
84     JSClass<JSScrollableBase>::StaticMethod("onDidScroll", &JSScrollableBase::JsOnDidScroll);
85     JSClass<JSScrollableBase>::InheritAndBind<JSContainerBase>(globalObj);
86 }
87 } // namespace OHOS::Ace::Framework
88