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) {
69 return;
70 }
71 if (args[0]->IsFunction()) {
72 auto onScroll = [execCtx = args.GetExecutionContext(), func = JSRef<JSFunc>::Cast(args[0])](
73 const CalcDimension& scrollOffset, const ScrollState& scrollState) {
74 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
75 auto params = ConvertToJSValues(scrollOffset, scrollState);
76 func->Call(JSRef<JSObject>(), params.size(), params.data());
77 };
78 NG::ScrollableModelNG::SetOnDidScroll(std::move(onScroll));
79 } else {
80 NG::ScrollableModelNG::SetOnDidScroll(nullptr);
81 }
82 }
83
SetFadingEdge(const JSCallbackInfo & info)84 void JSScrollableBase::SetFadingEdge(const JSCallbackInfo& info)
85 {
86 bool fadingEdge = false;
87 CalcDimension fadingEdgeLength = Dimension(32.0, DimensionUnit::VP); // 32.0: default fading edge length
88 if (info.Length() >= 1) {
89 ParseJsBool(info[0], fadingEdge);
90 }
91 if (info.Length() == 2 && info[1]->IsObject()) { /* 2: parameter count */
92 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[1]);
93 JSViewAbstract::ParseLengthMetricsToDimension(obj->GetProperty("fadingEdgeLength"), fadingEdgeLength);
94 if (fadingEdgeLength.Value() < 0) {
95 fadingEdgeLength = Dimension(32.0, DimensionUnit::VP); // 32.0: default fading edge length
96 }
97 }
98 NG::ScrollableModelNG::SetFadingEdge(fadingEdge, fadingEdgeLength);
99 }
100
SetDigitalCrownSensitivity(const JSCallbackInfo & info)101 void JSScrollableBase::SetDigitalCrownSensitivity(const JSCallbackInfo& info)
102 {
103 #ifdef SUPPORT_DIGITAL_CROWN
104 if (info.Length() < 1 || info[0]->IsNull() || !info[0]->IsNumber()) {
105 NG::ScrollableModelNG::SetDigitalCrownSensitivity(
106 static_cast<CrownSensitivity>(static_cast<int32_t>(CrownSensitivity::MEDIUM)));
107 return;
108 }
109 auto sensitivity = info[0]->ToNumber<int32_t>();
110 if (sensitivity < 0 || sensitivity > static_cast<int32_t>(CrownSensitivity::HIGH)) {
111 NG::ScrollableModelNG::SetDigitalCrownSensitivity(
112 static_cast<CrownSensitivity>(static_cast<int32_t>(CrownSensitivity::MEDIUM)));
113 return;
114 }
115 NG::ScrollableModelNG::SetDigitalCrownSensitivity(static_cast<CrownSensitivity>(sensitivity));
116 #endif
117 }
118
JSBind(BindingTarget globalObj)119 void JSScrollableBase::JSBind(BindingTarget globalObj)
120 {
121 MethodOptions opt = MethodOptions::NONE;
122 JSClass<JSScrollableBase>::Declare("JSContainerBase");
123 JSClass<JSScrollableBase>::StaticMethod("flingSpeedLimit", &JSScrollableBase::JSFlingSpeedLimit, opt);
124 JSClass<JSScrollableBase>::StaticMethod("onWillScroll", &JSScrollableBase::JsOnWillScroll);
125 JSClass<JSScrollableBase>::StaticMethod("onDidScroll", &JSScrollableBase::JsOnDidScroll);
126 JSClass<JSScrollableBase>::StaticMethod("fadingEdge", &JSScrollableBase::SetFadingEdge);
127 JSClass<JSScrollableBase>::StaticMethod("clipContent", &JSScrollableBase::JSClipContent);
128 JSClass<JSScrollableBase>::StaticMethod("digitalCrownSensitivity", &JSScrollableBase::SetDigitalCrownSensitivity);
129 JSClass<JSScrollableBase>::StaticMethod("backToTop", &JSScrollableBase::JSBackToTop);
130 JSClass<JSScrollableBase>::InheritAndBind<JSContainerBase>(globalObj);
131 }
132
JSClipContent(const JSCallbackInfo & info)133 void JSScrollableBase::JSClipContent(const JSCallbackInfo& info)
134 {
135 if (info.Length() != 1) {
136 return;
137 }
138 if (info[0]->IsNumber()) {
139 auto mode = static_cast<NG::ContentClipMode>(info[0]->ToNumber<int32_t>());
140 if (mode >= NG::ContentClipMode::CONTENT_ONLY && mode <= NG::ContentClipMode::SAFE_AREA) {
141 NG::ScrollableModelNG::SetContentClip(mode, nullptr);
142 return;
143 }
144 } else if (info[0]->IsObject()) {
145 auto* clipShape = JSRef<JSObject>::Cast(info[0])->Unwrap<JSShapeAbstract>();
146 if (clipShape) {
147 NG::ScrollableModelNG::SetContentClip(
148 NG::ContentClipMode::CUSTOM, AceType::DynamicCast<ShapeRect>(clipShape->GetBasicShape()));
149 return;
150 }
151 }
152 // default
153 NG::ScrollableModelNG::SetContentClip(NG::ContentClipMode::DEFAULT, nullptr);
154 }
155
JSBackToTop(const JSCallbackInfo & info)156 void JSScrollableBase::JSBackToTop(const JSCallbackInfo& info)
157 {
158 if (info.Length() < 1) {
159 return;
160 }
161 if (info[0]->IsBoolean()) {
162 NG::ScrollableModelNG::SetBackToTop(info[0]->ToBoolean());
163 } else {
164 NG::ScrollableModelNG::ResetBackToTop();
165 }
166 }
167 } // namespace OHOS::Ace::Framework
168