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_shape_abstract.h"
19 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
20 #include "core/components_ng/pattern/scrollable/scrollable_model_ng.h"
21
22 namespace OHOS::Ace::Framework {
JSFlingSpeedLimit(const JSCallbackInfo & info)23 void JSScrollableBase::JSFlingSpeedLimit(const JSCallbackInfo& info)
24 {
25 double max = -1.0;
26 if (!JSViewAbstract::ParseJsDouble(info[0], max)) {
27 return;
28 }
29 NG::ScrollableModelNG::SetMaxFlingSpeed(max);
30 }
31
JsOnWillScroll(const JSCallbackInfo & args)32 void JSScrollableBase::JsOnWillScroll(const JSCallbackInfo& args)
33 {
34 if (args.Length() <= 0) {
35 return;
36 }
37 if (args[0]->IsFunction()) {
38 auto onScroll = [execCtx = args.GetExecutionContext(), func = JSRef<JSFunc>::Cast(args[0])](
39 const CalcDimension& scrollOffset, const ScrollState& scrollState,
40 ScrollSource scrollSource) {
41 auto params = ConvertToJSValues(scrollOffset, scrollState, scrollSource);
42 ScrollFrameResult scrollRes { .offset = scrollOffset };
43 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, scrollRes);
44 auto result = func->Call(JSRef<JSObject>(), params.size(), params.data());
45 if (result.IsEmpty()) {
46 return scrollRes;
47 }
48
49 if (!result->IsObject()) {
50 return scrollRes;
51 }
52
53 auto resObj = JSRef<JSObject>::Cast(result);
54 auto dxRemainValue = resObj->GetProperty("offsetRemain");
55 if (dxRemainValue->IsNumber()) {
56 scrollRes.offset = Dimension(dxRemainValue->ToNumber<float>(), DimensionUnit::VP);
57 }
58 return scrollRes;
59 };
60 NG::ScrollableModelNG::SetOnWillScroll(std::move(onScroll));
61 } else {
62 NG::ScrollableModelNG::SetOnWillScroll(nullptr);
63 }
64 }
65
JsOnDidScroll(const JSCallbackInfo & args)66 void JSScrollableBase::JsOnDidScroll(const JSCallbackInfo& args)
67 {
68 if (args.Length() > 0 && args[0]->IsFunction()) {
69 auto onScroll = [execCtx = args.GetExecutionContext(), func = JSRef<JSFunc>::Cast(args[0])](
70 const CalcDimension& scrollOffset, const ScrollState& scrollState) {
71 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
72 auto params = ConvertToJSValues(scrollOffset, scrollState);
73 func->Call(JSRef<JSObject>(), params.size(), params.data());
74 };
75 NG::ScrollableModelNG::SetOnDidScroll(std::move(onScroll));
76 }
77 }
78
SetFadingEdge(const JSCallbackInfo & info)79 void JSScrollableBase::SetFadingEdge(const JSCallbackInfo& info)
80 {
81 bool fadingEdge = false;
82 CalcDimension fadingEdgeLength = Dimension(32.0, DimensionUnit::VP); // 32.0: default fading edge length
83 if (info.Length() >= 1) {
84 ParseJsBool(info[0], fadingEdge);
85 }
86 if (info.Length() == 2 && info[1]->IsObject()) { /* 2: parameter count */
87 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[1]);
88 JSViewAbstract::ParseLengthMetricsToDimension(obj->GetProperty("fadingEdgeLength"), fadingEdgeLength);
89 if (fadingEdgeLength.Value() < 0) {
90 fadingEdgeLength = Dimension(32.0, DimensionUnit::VP); // 32.0: default fading edge length
91 }
92 }
93 NG::ScrollableModelNG::SetFadingEdge(fadingEdge, fadingEdgeLength);
94 }
95
JSBind(BindingTarget globalObj)96 void JSScrollableBase::JSBind(BindingTarget globalObj)
97 {
98 MethodOptions opt = MethodOptions::NONE;
99 JSClass<JSScrollableBase>::Declare("JSContainerBase");
100 JSClass<JSScrollableBase>::StaticMethod("flingSpeedLimit", &JSScrollableBase::JSFlingSpeedLimit, opt);
101 JSClass<JSScrollableBase>::StaticMethod("onWillScroll", &JSScrollableBase::JsOnWillScroll);
102 JSClass<JSScrollableBase>::StaticMethod("onDidScroll", &JSScrollableBase::JsOnDidScroll);
103 JSClass<JSScrollableBase>::StaticMethod("fadingEdge", &JSScrollableBase::SetFadingEdge);
104 JSClass<JSScrollableBase>::StaticMethod("clipContent", &JSScrollableBase::JSClipContent);
105 JSClass<JSScrollableBase>::InheritAndBind<JSContainerBase>(globalObj);
106 }
107
JSClipContent(const JSCallbackInfo & info)108 void JSScrollableBase::JSClipContent(const JSCallbackInfo& info)
109 {
110 if (info.Length() != 1) {
111 return;
112 }
113 if (info[0]->IsNumber()) {
114 auto mode = static_cast<NG::ContentClipMode>(info[0]->ToNumber<int32_t>());
115 if (mode >= NG::ContentClipMode::CONTENT_ONLY && mode <= NG::ContentClipMode::SAFE_AREA) {
116 NG::ScrollableModelNG::SetContentClip(mode, nullptr);
117 return;
118 }
119 } else if (info[0]->IsObject()) {
120 auto* clipShape = JSRef<JSObject>::Cast(info[0])->Unwrap<JSShapeAbstract>();
121 if (clipShape) {
122 NG::ScrollableModelNG::SetContentClip(
123 NG::ContentClipMode::CUSTOM, AceType::DynamicCast<ShapeRect>(clipShape->GetBasicShape()));
124 return;
125 }
126 }
127 // default
128 NG::ScrollableModelNG::SetContentClip(NG::ContentClipMode::DEFAULT, nullptr);
129 }
130 } // namespace OHOS::Ace::Framework
131