1 /*
2 * Copyright (c) 2021 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 "frameworks/bridge/declarative_frontend/engine/quickjs/modules/qjs_curves_module.h"
17
18 #include "base/log/log.h"
19 #include "frameworks/bridge/declarative_frontend/engine/quickjs/qjs_declarative_engine.h"
20 #include "frameworks/bridge/js_frontend/engine/common/js_constants.h"
21 #include "frameworks/bridge/js_frontend/engine/quickjs/qjs_utils.h"
22
23 namespace OHOS::Ace::Framework {
24
25 namespace {
26 constexpr size_t RESPONSIVE_SPRING_MOTION_PARAMS_SIZE = 3;
GetJsStringVal(JSContext * ctx,JSValueConst value)27 std::string GetJsStringVal(JSContext* ctx, JSValueConst value)
28 {
29 std::string val;
30 if (JS_IsString(value)) {
31 ScopedString curveJsonStr(ctx, value);
32 val = curveJsonStr.get();
33 }
34 return val;
35 }
36
CurvesInterpolate(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)37 JSValue CurvesInterpolate(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
38 {
39 QJSHandleScope handleScope(ctx);
40 std::string curveString = GetJsStringVal(ctx, QJSUtils::GetPropertyStr(ctx, value, "__curveString"));
41 double time;
42 if (argc == 0) {
43 return JS_NULL;
44 }
45 JS_ToFloat64(ctx, &time, argv[0]);
46 auto animationCurve = CreateCurve(curveString, false);
47 if (!animationCurve) {
48 LOGW("created animationCurve is null, curveString:%{public}s", curveString.c_str());
49 return JS_NULL;
50 }
51 double curveValue = animationCurve->Move(time);
52 JSValue curveNum = JS_NewFloat64(ctx, curveValue);
53 return curveNum;
54 }
55
CurvesInitInternal(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)56 JSValue CurvesInitInternal(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
57 {
58 JS_SetPropertyStr(ctx, value, CURVE_INTERPOLATE, JS_NewCFunction(ctx, CurvesInterpolate, CURVE_INTERPOLATE, 1));
59 if ((!argv) || ((argc != 1) && (argc != 0))) {
60 LOGE("CurvesInit args count is invalid");
61 return JS_NULL;
62 }
63 RefPtr<Curve> curve;
64 std::string curveString;
65 if (argc == 1) {
66 ScopedString curveJsonStr(ctx, argv[0]);
67 curveString = curveJsonStr.get();
68 } else {
69 curveString = "linear";
70 }
71 curve = CreateCurve(curveString);
72 JS_SetPropertyStr(ctx, value, "__curveString", JS_NewString(ctx, curveString.c_str()));
73 if (Container::IsCurrentUseNewPipeline()) {
74 JS_DupValue(ctx, value);
75 return value;
76 }
77 auto* instance = static_cast<QJSDeclarativeEngineInstance*>(JS_GetContextOpaque(ctx));
78 if (instance == nullptr) {
79 LOGE("Can not cast Context to QJSDeclarativeEngineInstance object.");
80 return JS_NULL;
81 }
82 auto page = instance->GetRunningPage(ctx);
83 if (page == nullptr) {
84 LOGE("page is nullptr");
85 return JS_NULL;
86 }
87 int32_t pageId = page->GetPageId();
88 JS_SetPropertyStr(ctx, value, "__pageId", JS_NewInt32(ctx, pageId));
89 JS_DupValue(ctx, value);
90 return value;
91 }
92
CurvesInit(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)93 JSValue CurvesInit(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
94 {
95 return CurvesInitInternal(ctx, value, argc, argv);
96 }
97
InitCurve(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)98 JSValue InitCurve(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
99 {
100 return CurvesInitInternal(ctx, value, argc, argv);
101 }
102
CreateSpringCurve(JSContext * ctx,JSValueConst & value,int32_t argc,JSValueConst * argv,RefPtr<Curve> & curve)103 bool CreateSpringCurve(JSContext* ctx, JSValueConst& value, int32_t argc,
104 JSValueConst* argv, RefPtr<Curve>& curve)
105 {
106 if (argc != 4) {
107 LOGE("Spring curve: the number of parameters is illegal");
108 return false;
109 }
110 double x0;
111 JS_ToFloat64(ctx, &x0, argv[0]);
112 double y0;
113 JS_ToFloat64(ctx, &y0, argv[1]);
114 double x1;
115 JS_ToFloat64(ctx, &x1, argv[2]);
116 double y1;
117 JS_ToFloat64(ctx, &y1, argv[3]);
118 if (y0 > 0 && x1 > 0 && y1 > 0) {
119 curve = AceType::MakeRefPtr<SpringCurve>(x0, y0, x1, y1);
120 return true;
121 } else {
122 LOGE("Spring curve: the value of the parameters are illegal");
123 return false;
124 }
125 }
126
CreateCubicCurve(JSContext * ctx,JSValueConst & value,int32_t argc,JSValueConst * argv,RefPtr<Curve> & curve)127 bool CreateCubicCurve(JSContext* ctx, JSValueConst& value, int32_t argc,
128 JSValueConst* argv, RefPtr<Curve>& curve)
129 {
130 if (argc != 4) {
131 LOGE("Cubic curve: the number of parameters is illegal");
132 return false;
133 }
134 double x0;
135 JS_ToFloat64(ctx, &x0, argv[0]);
136 double y0;
137 JS_ToFloat64(ctx, &y0, argv[1]);
138 double x1;
139 JS_ToFloat64(ctx, &x1, argv[2]);
140 double y1;
141 JS_ToFloat64(ctx, &y1, argv[3]);
142 curve = AceType::MakeRefPtr<CubicCurve>(x0, y0, x1, y1);
143 return true;
144 }
145
CreateStepsCurve(JSContext * ctx,JSValueConst & value,int32_t argc,JSValueConst * argv,RefPtr<Curve> & curve)146 bool CreateStepsCurve(JSContext* ctx, JSValueConst& value, int32_t argc,
147 JSValueConst* argv, RefPtr<Curve>& curve)
148 {
149 if (!argv || (argc != 1 && argc != 2)) {
150 LOGE("Steps curve: the number of parameters is illegal");
151 return false;
152 }
153 int32_t stepSize;
154 if (argc == 2) {
155 JS_ToInt32(ctx, &stepSize, argv[0]);
156 if (stepSize < 0) {
157 LOGE("Steps curve: When two parameters, the value of the stepSize is illegal");
158 return false;
159 }
160 bool isEnd = JS_ToBool(ctx, argv[1]);
161 if (isEnd) {
162 curve = AceType::MakeRefPtr<StepsCurve>(stepSize, StepsCurvePosition::END);
163 } else {
164 curve = AceType::MakeRefPtr<StepsCurve>(stepSize, StepsCurvePosition::START);
165 }
166 } else {
167 JS_ToInt32(ctx, &stepSize, argv[0]);
168 if (stepSize < 0) {
169 LOGE("Steps curve: When one parameter, the value of the stepSize is illegal");
170 return false;
171 }
172 curve = AceType::MakeRefPtr<StepsCurve>(stepSize);
173 }
174 return true;
175 }
176
CreateSpringMotionCurve(JSContext * ctx,JSValueConst & value,int32_t argc,JSValueConst * argv,RefPtr<Curve> & curve)177 bool CreateSpringMotionCurve(JSContext* ctx, JSValueConst& value, int32_t argc,
178 JSValueConst* argv, RefPtr<Curve>& curve)
179 {
180 if (!argv || argc > static_cast<int32_t>(RESPONSIVE_SPRING_MOTION_PARAMS_SIZE)) {
181 LOGW("SpringMotionCurve: the number of parameters is illegal");
182 return false;
183 }
184 double response = ResponsiveSpringMotion::DEFAULT_SPRING_MOTION_RESPONSE;
185 double dampingRatio = ResponsiveSpringMotion::DEFAULT_SPRING_MOTION_DAMPING_RATIO;
186 double blendDuration = ResponsiveSpringMotion::DEFAULT_SPRING_MOTION_BLEND_DURATION;
187 if (argc > 0) {
188 JS_ToFloat64(ctx, &response, argv[0]);
189 }
190 if (argc > 1) {
191 JS_ToFloat64(ctx, &dampingRatio, argv[1]);
192 }
193 if (argc > static_cast<int32_t>(RESPONSIVE_SPRING_MOTION_PARAMS_SIZE) - 1) {
194 JS_ToFloat64(ctx, &blendDuration, argv[RESPONSIVE_SPRING_MOTION_PARAMS_SIZE - 1]);
195 }
196 curve = AceType::MakeRefPtr<ResponsiveSpringMotion>(static_cast<float>(response), static_cast<float>(dampingRatio),
197 static_cast<float>(blendDuration));
198 return true;
199 }
200
CreateResponsiveSpringMotionCurve(JSContext * ctx,JSValueConst & value,int32_t argc,JSValueConst * argv,RefPtr<Curve> & curve)201 bool CreateResponsiveSpringMotionCurve(JSContext* ctx, JSValueConst& value, int32_t argc,
202 JSValueConst* argv, RefPtr<Curve>& curve)
203 {
204 if (!argv || argc > static_cast<int32_t>(RESPONSIVE_SPRING_MOTION_PARAMS_SIZE)) {
205 LOGW("ResponsiveSpringMotionCurve: the number of parameters is illegal");
206 return false;
207 }
208 double response = ResponsiveSpringMotion::DEFAULT_RESPONSIVE_SPRING_MOTION_RESPONSE;
209 double dampingRatio = ResponsiveSpringMotion::DEFAULT_RESPONSIVE_SPRING_MOTION_DAMPING_RATIO;
210 double blendDuration = ResponsiveSpringMotion::DEFAULT_RESPONSIVE_SPRING_MOTION_BLEND_DURATION;
211 if (argc > 0) {
212 JS_ToFloat64(ctx, &response, argv[0]);
213 }
214 if (argc > 1) {
215 JS_ToFloat64(ctx, &dampingRatio, argv[1]);
216 }
217 if (argc > static_cast<int32_t>(RESPONSIVE_SPRING_MOTION_PARAMS_SIZE) - 1) {
218 JS_ToFloat64(ctx, &blendDuration, argv[RESPONSIVE_SPRING_MOTION_PARAMS_SIZE - 1]);
219 }
220 curve = AceType::MakeRefPtr<ResponsiveSpringMotion>(static_cast<float>(response), static_cast<float>(dampingRatio),
221 static_cast<float>(blendDuration));
222 return true;
223 }
224
ParseCurves(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv,std::string & curveString)225 JSValue ParseCurves(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv, std::string& curveString)
226 {
227 JS_SetPropertyStr(ctx, value, CURVE_INTERPOLATE, JS_NewCFunction(ctx, CurvesInterpolate, CURVE_INTERPOLATE, 1));
228 RefPtr<Curve> curve;
229 bool curveCreated;
230 if (curveString == CURVES_SPRING || curveString == SPRING_CURVE) {
231 curveCreated = CreateSpringCurve(ctx, value, argc, argv, curve);
232 } else if (curveString == CURVES_CUBIC_BEZIER || curveString == CUBIC_BEZIER_CURVE) {
233 curveCreated = CreateCubicCurve(ctx, value, argc, argv, curve);
234 } else if (curveString == CURVES_STEPS || curveString == STEPS_CURVE) {
235 curveCreated = CreateStepsCurve(ctx, value, argc, argv, curve);
236 } else if (curveString == SPRING_MOTION) {
237 curveCreated = CreateSpringMotionCurve(ctx, value, argc, argv, curve);
238 } else if (curveString == RESPONSIVE_SPRING_MOTION) {
239 curveCreated = CreateResponsiveSpringMotionCurve(ctx, value, argc, argv, curve);
240 } else {
241 return JS_NULL;
242 }
243 if (!curveCreated) {
244 return JS_NULL;
245 }
246 auto customCurve = curve->ToString();
247 JS_SetPropertyStr(ctx, value, "__curveString", JS_NewString(ctx, curveString.c_str()));
248 if (Container::IsCurrentUseNewPipeline()) {
249 JS_DupValue(ctx, value);
250 return value;
251 }
252 auto* instance = static_cast<QJSDeclarativeEngineInstance*>(JS_GetContextOpaque(ctx));
253 if (instance == nullptr) {
254 LOGE("Can not cast Context to QJSDeclarativeEngineInstance object.");
255 return JS_NULL;
256 }
257 auto page = instance->GetRunningPage(ctx);
258 if (page == nullptr) {
259 LOGE("page is nullptr");
260 return JS_NULL;
261 }
262 int32_t pageId = page->GetPageId();
263 JS_SetPropertyStr(ctx, value, "__pageId", JS_NewInt32(ctx, pageId));
264 JS_DupValue(ctx, value);
265 return value;
266 }
267
CurvesBezier(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)268 JSValue CurvesBezier(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
269 {
270 std::string curveString(CURVES_CUBIC_BEZIER);
271 return ParseCurves(ctx, value, argc, argv, curveString);
272 }
273
BezierCurve(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)274 JSValue BezierCurve(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
275 {
276 std::string curveString(CUBIC_BEZIER_CURVE);
277 return ParseCurves(ctx, value, argc, argv, curveString);
278 }
279
CurvesSpring(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)280 JSValue CurvesSpring(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
281 {
282 std::string curveString(CURVES_SPRING);
283 return ParseCurves(ctx, value, argc, argv, curveString);
284 }
285
SpringCurve(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)286 JSValue SpringCurve(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
287 {
288 std::string curveString(SPRING_CURVE);
289 return ParseCurves(ctx, value, argc, argv, curveString);
290 }
291
CurvesSteps(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)292 JSValue CurvesSteps(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
293 {
294 std::string curveString(CURVES_STEPS);
295 return ParseCurves(ctx, value, argc, argv, curveString);
296 }
297
StepsCurve(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)298 JSValue StepsCurve(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
299 {
300 std::string curveString(STEPS_CURVE);
301 return ParseCurves(ctx, value, argc, argv, curveString);
302 }
303
SpringMotionCurve(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)304 JSValue SpringMotionCurve(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
305 {
306 std::string curveString(SPRING_MOTION);
307 return ParseCurves(ctx, value, argc, argv, curveString);
308 }
309
ResponsiveSpringMotionCurve(JSContext * ctx,JSValueConst value,int32_t argc,JSValueConst * argv)310 JSValue ResponsiveSpringMotionCurve(JSContext* ctx, JSValueConst value, int32_t argc, JSValueConst* argv)
311 {
312 std::string curveString(RESPONSIVE_SPRING_MOTION);
313 return ParseCurves(ctx, value, argc, argv, curveString);
314 }
315 } // namespace
316
InitCurvesModule(JSContext * ctx,JSValue & moduleObj)317 void InitCurvesModule(JSContext* ctx, JSValue& moduleObj)
318 {
319 JS_SetPropertyStr(ctx, moduleObj, CURVES_INIT, JS_NewCFunction(ctx, CurvesInit, CURVES_INIT, 1));
320 JS_SetPropertyStr(ctx, moduleObj, INIT_CURVE, JS_NewCFunction(ctx, InitCurve, INIT_CURVE, 1));
321 JS_SetPropertyStr(ctx, moduleObj, CURVES_CUBIC_BEZIER, JS_NewCFunction(ctx, CurvesBezier, CURVES_CUBIC_BEZIER, 4));
322 JS_SetPropertyStr(ctx, moduleObj, CUBIC_BEZIER_CURVE, JS_NewCFunction(ctx, BezierCurve, CUBIC_BEZIER_CURVE, 4));
323 JS_SetPropertyStr(ctx, moduleObj, CURVES_SPRING, JS_NewCFunction(ctx, CurvesSpring, CURVES_SPRING, 4));
324 JS_SetPropertyStr(ctx, moduleObj, SPRING_CURVE, JS_NewCFunction(ctx, SpringCurve, SPRING_CURVE, 4));
325 JS_SetPropertyStr(ctx, moduleObj, CURVES_STEPS, JS_NewCFunction(ctx, CurvesSteps, CURVES_STEPS, 2));
326 JS_SetPropertyStr(ctx, moduleObj, STEPS_CURVE, JS_NewCFunction(ctx, StepsCurve, STEPS_CURVE, 2));
327 JS_SetPropertyStr(ctx, moduleObj, SPRING_MOTION, JS_NewCFunction(ctx, SpringMotionCurve,
328 SPRING_MOTION, RESPONSIVE_SPRING_MOTION_PARAMS_SIZE));
329 JS_SetPropertyStr(ctx, moduleObj, RESPONSIVE_SPRING_MOTION, JS_NewCFunction(ctx, ResponsiveSpringMotionCurve,
330 RESPONSIVE_SPRING_MOTION, RESPONSIVE_SPRING_MOTION_PARAMS_SIZE));
331 }
332 } // namespace OHOS::Ace::Framework