1 /*
2 * Copyright (c) 2021-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 "frameworks/bridge/declarative_frontend/jsview/js_xcomponent.h"
17
18 #include "base/log/ace_scoring_log.h"
19 #include "base/memory/referenced.h"
20 #include "base/utils/utils.h"
21 #include "bridge/common/utils/engine_helper.h"
22 #include "bridge/declarative_frontend/engine/js_converter.h"
23 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
24 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
25 #include "bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
26 #include "bridge/declarative_frontend/jsview/models/xcomponent_model_impl.h"
27 #include "core/components/common/layout/constants.h"
28 #include "core/components_ng/base/view_stack_processor.h"
29 #include "core/components_ng/pattern/xcomponent/xcomponent_model.h"
30 #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h"
31 #include "frameworks/core/components_ng/base/view_abstract_model.h"
32 #include "frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h"
33
34 namespace OHOS::Ace {
35 namespace {
ConvertToXComponentType(const std::string& type)36 XComponentType ConvertToXComponentType(const std::string& type)
37 {
38 if (type == "surface") {
39 return XComponentType::SURFACE;
40 }
41 if (type == "component") {
42 return XComponentType::COMPONENT;
43 }
44 if (type == "node") {
45 return XComponentType::NODE;
46 }
47 return XComponentType::SURFACE;
48 }
49 } // namespace
50
GetInstance()51 XComponentModel* XComponentModel::GetInstance()
52 {
53 #ifdef NG_BUILD
54 static NG::XComponentModelNG instance;
55 return &instance;
56 #else
57 if (Container::IsCurrentUseNewPipeline()) {
58 static NG::XComponentModelNG instance;
59 return &instance;
60 } else {
61 static Framework::XComponentModelImpl instance;
62 return &instance;
63 }
64 #endif
65 }
66 } // namespace OHOS::Ace
67
68 namespace OHOS::Ace::Framework {
SetControllerCallback(const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)69 void SetControllerCallback(const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
70 {
71 WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
72 auto jsCreatedFunc = object->GetProperty("onSurfaceCreated");
73 if (jsCreatedFunc->IsFunction()) {
74 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsCreatedFunc));
75 auto onSurfaceCreated = [execCtx, func = std::move(jsFunc), node = targetNode](const std::string& surfaceId) {
76 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
77 ACE_SCORING_EVENT("XComponentController.onSurfaceCreated");
78 PipelineContext::SetCallBackNode(node);
79 auto jsVal = JSRef<JSVal>::Make(ToJSValue(surfaceId));
80 func->ExecuteJS(1, &jsVal);
81 };
82 XComponentModel::GetInstance()->SetControllerOnCreated(std::move(onSurfaceCreated));
83 }
84 auto jsChangedFunc = object->GetProperty("onSurfaceChanged");
85 if (jsChangedFunc->IsFunction()) {
86 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsChangedFunc));
87 auto onSurfaceChanged = [execCtx, func = std::move(jsFunc), node = targetNode](
88 const std::string& surfaceId, const NG::RectF& rect) {
89 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
90 ACE_SCORING_EVENT("XComponentController.onSurfaceChanged");
91 PipelineContext::SetCallBackNode(node);
92 JSRef<JSObject> rectObj = JSRef<JSObject>::New();
93 rectObj->SetProperty("offsetX", rect.Left());
94 rectObj->SetProperty("offsetY", rect.Top());
95 rectObj->SetProperty("surfaceWidth", rect.Width());
96 rectObj->SetProperty("surfaceHeight", rect.Height());
97 auto jsSurfaceId = JSRef<JSVal>::Make(ToJSValue(surfaceId));
98 JSRef<JSVal> params[2] = { jsSurfaceId, rectObj };
99 func->ExecuteJS(2, params);
100 };
101 XComponentModel::GetInstance()->SetControllerOnChanged(std::move(onSurfaceChanged));
102 }
103 auto jsDestroyedFunc = object->GetProperty("onSurfaceDestroyed");
104 if (jsDestroyedFunc->IsFunction()) {
105 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsDestroyedFunc));
106 auto onSurfaceDestroyed = [execCtx, func = std::move(jsFunc), node = targetNode](const std::string& surfaceId) {
107 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
108 ACE_SCORING_EVENT("XComponentController.onSurfaceDestroyed");
109 PipelineContext::SetCallBackNode(node);
110 auto jsVal = JSRef<JSVal>::Make(ToJSValue(surfaceId));
111 func->ExecuteJS(1, &jsVal);
112 };
113 XComponentModel::GetInstance()->SetControllerOnDestroyed(std::move(onSurfaceDestroyed));
114 }
115 }
116
GetXComponentController( const JSRef<JSObject>& controller, std::optional<std::string>& id, const JsiExecutionContext& execCtx)117 std::shared_ptr<InnerXComponentController> GetXComponentController(
118 const JSRef<JSObject>& controller, std::optional<std::string>& id, const JsiExecutionContext& execCtx)
119 {
120 std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
121 auto* jsXComponentController = controller->Unwrap<JSXComponentController>();
122 if (jsXComponentController) {
123 jsXComponentController->SetInstanceId(Container::CurrentId());
124 if (id.has_value()) {
125 XComponentClient::GetInstance().AddControllerToJSXComponentControllersMap(
126 id.value(), jsXComponentController);
127 }
128 xcomponentController = jsXComponentController->GetController();
129 }
130 return xcomponentController;
131 }
132
JSBind(BindingTarget globalObj)133 void JSXComponent::JSBind(BindingTarget globalObj)
134 {
135 JSClass<JSXComponent>::Declare("XComponent");
136 JSClass<JSXComponent>::StaticMethod("create", &JSXComponent::Create);
137 JSClass<JSXComponent>::StaticMethod("onLoad", &JSXComponent::JsOnLoad);
138 JSClass<JSXComponent>::StaticMethod("onDestroy", &JSXComponent::JsOnDestroy);
139 JSClass<JSXComponent>::StaticMethod("onAppear", &JSXComponent::JsOnAppear);
140 JSClass<JSXComponent>::StaticMethod("onDisAppear", &JSXComponent::JsOnDisAppear);
141 JSClass<JSXComponent>::StaticMethod("onAttach", &JSXComponent::JsOnAttach);
142 JSClass<JSXComponent>::StaticMethod("onDetach", &JSXComponent::JsOnDetach);
143
144 JSClass<JSXComponent>::StaticMethod("onTouch", &JSXComponent::JsOnTouch);
145 JSClass<JSXComponent>::StaticMethod("onClick", &JSXComponent::JsOnClick);
146 JSClass<JSXComponent>::StaticMethod("onKeyEvent", &JSXComponent::JsOnKeyEvent);
147 JSClass<JSXComponent>::StaticMethod("onMouse", &JSXComponent::JsOnMouse);
148 JSClass<JSXComponent>::StaticMethod("onHover", &JSXComponent::JsOnHover);
149 JSClass<JSXComponent>::StaticMethod("onFocus", &JSXComponent::JsOnFocus);
150 JSClass<JSXComponent>::StaticMethod("onBlur", &JSXComponent::JsOnBlur);
151
152 JSClass<JSXComponent>::StaticMethod("backgroundColor", &JSXComponent::JsBackgroundColor);
153 JSClass<JSXComponent>::StaticMethod("backgroundImage", &JSXComponent::JsBackgroundImage);
154 JSClass<JSXComponent>::StaticMethod("backgroundImageSize", &JSXComponent::JsBackgroundImageSize);
155 JSClass<JSXComponent>::StaticMethod("backgroundImagePosition", &JSXComponent::JsBackgroundImagePosition);
156 JSClass<JSXComponent>::StaticMethod("opacity", &JSXComponent::JsOpacity);
157 JSClass<JSXComponent>::StaticMethod("blur", &JSXComponent::JsBlur);
158 JSClass<JSXComponent>::StaticMethod("backdropBlur", &JSXComponent::JsBackdropBlur);
159 JSClass<JSXComponent>::StaticMethod("grayscale", &JSXComponent::JsGrayscale);
160 JSClass<JSXComponent>::StaticMethod("brightness", &JSXComponent::JsBrightness);
161 JSClass<JSXComponent>::StaticMethod("saturate", &JSXComponent::JsSaturate);
162 JSClass<JSXComponent>::StaticMethod("contrast", &JSXComponent::JsContrast);
163 JSClass<JSXComponent>::StaticMethod("invert", &JSXComponent::JsInvert);
164 JSClass<JSXComponent>::StaticMethod("sepia", &JSXComponent::JsSepia);
165 JSClass<JSXComponent>::StaticMethod("hueRotate", &JSXComponent::JsHueRotate);
166 JSClass<JSXComponent>::StaticMethod("colorBlend", &JSXComponent::JsColorBlend);
167 JSClass<JSXComponent>::StaticMethod("sphericalEffect", &JSXComponent::JsSphericalEffect);
168 JSClass<JSXComponent>::StaticMethod("lightUpEffect", &JSXComponent::JsLightUpEffect);
169 JSClass<JSXComponent>::StaticMethod("pixelStretchEffect", &JSXComponent::JsPixelStretchEffect);
170 JSClass<JSXComponent>::StaticMethod("linearGradientBlur", &JSXComponent::JsLinearGradientBlur);
171 JSClass<JSXComponent>::StaticMethod("enableAnalyzer", &JSXComponent::JsEnableAnalyzer);
172 JSClass<JSXComponent>::StaticMethod("renderFit", &JSXComponent::JsRenderFit);
173 JSClass<JSXComponent>::StaticMethod("enableSecure", &JSXComponent::JsEnableSecure);
174
175 JSClass<JSXComponent>::InheritAndBind<JSContainerBase>(globalObj);
176 }
177
Create(const JSCallbackInfo& info)178 void JSXComponent::Create(const JSCallbackInfo& info)
179 {
180 if (info.Length() < 1 || !info[0]->IsObject()) {
181 return;
182 }
183 auto paramObject = JSRef<JSObject>::Cast(info[0]);
184 auto id = paramObject->GetProperty("id");
185
186 auto type = paramObject->GetProperty("type");
187 auto libraryNameValue = paramObject->GetProperty("libraryname");
188 std::optional<std::string> idOpt = std::nullopt;
189 std::optional<std::string> libraryNameOpt = std::nullopt;
190 if (id->IsString()) {
191 idOpt = id->ToString();
192 }
193 if (libraryNameValue->IsString()) {
194 libraryNameOpt = libraryNameValue->ToString();
195 }
196 auto controller = paramObject->GetProperty("controller");
197 auto aiOptions = paramObject->GetProperty("imageAIOptions");
198 std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
199 JSRef<JSObject> controllerObj;
200 if (controller->IsObject()) {
201 controllerObj = JSRef<JSObject>::Cast(controller);
202 xcomponentController = GetXComponentController(controllerObj, idOpt, info.GetExecutionContext());
203 }
204 XComponentType xcomponentType = XComponentType::SURFACE;
205 if (type->IsString()) {
206 xcomponentType = ConvertToXComponentType(type->ToString());
207 } else if (type->IsNumber()) {
208 xcomponentType = static_cast<XComponentType>(type->ToNumber<int32_t>());
209 }
210 XComponentModel::GetInstance()->Create(idOpt, xcomponentType, libraryNameOpt, xcomponentController);
211 if (!libraryNameOpt.has_value() && xcomponentController && !controllerObj->IsUndefined()) {
212 SetControllerCallback(controllerObj, info.GetExecutionContext());
213 }
214
215 auto detachCallback = [](const std::string& xcomponentId) {
216 XComponentClient::GetInstance().DeleteControllerFromJSXComponentControllersMap(xcomponentId);
217 XComponentClient::GetInstance().DeleteFromJsValMapById(xcomponentId);
218 };
219 XComponentModel::GetInstance()->SetDetachCallback(std::move(detachCallback));
220
221 if (info.Length() > 1 && info[1]->IsString()) {
222 auto soPath = info[1]->ToString();
223 XComponentModel::GetInstance()->SetSoPath(soPath);
224 }
225
226 if (aiOptions->IsObject()) {
227 auto engine = EngineHelper::GetCurrentEngine();
228 CHECK_NULL_VOID(engine);
229 NativeEngine* nativeEngine = engine->GetNativeEngine();
230 CHECK_NULL_VOID(nativeEngine);
231 panda::Local<JsiValue> value = aiOptions.Get().GetLocalHandle();
232 JSValueWrapper valueWrapper = value;
233 ScopeRAII scope(reinterpret_cast<napi_env>(nativeEngine));
234 napi_value optionsValue = nativeEngine->ValueToNapiValue(valueWrapper);
235 XComponentModel::GetInstance()->SetImageAIOptions(optionsValue);
236 }
237 }
238
Create(const XComponentParams& params)239 void* JSXComponent::Create(const XComponentParams& params)
240 {
241 std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
242 if (params.controller) {
243 xcomponentController = params.controller->GetController();
244 }
245 auto frameNode = AceType::DynamicCast<NG::FrameNode>(XComponentModel::GetInstance()->Create(params.elmtId,
246 static_cast<float>(params.width), static_cast<float>(params.height), params.xcomponentId,
247 static_cast<XComponentType>(params.xcomponentType), params.libraryName, xcomponentController));
248 frameNode->SetIsArkTsFrameNode(true);
249 auto pattern = frameNode->GetPattern<NG::XComponentPattern>();
250 CHECK_NULL_RETURN(pattern, nullptr);
251 pattern->SetRenderType(static_cast<NodeRenderType>(params.renderType));
252 pattern->SetExportTextureSurfaceId(params.surfaceId);
253
254 auto container = Container::Current();
255 CHECK_NULL_RETURN(container, nullptr);
256 auto pipelineContext = AceType::DynamicCast<NG::PipelineContext>(container->GetPipelineContext());
257 CHECK_NULL_RETURN(pipelineContext, nullptr);
258 auto taskExecutor = pipelineContext->GetTaskExecutor();
259 CHECK_NULL_RETURN(taskExecutor, nullptr);
260 auto* jsXComponent = new JSXComponent();
261 jsXComponent->SetFrameNode(frameNode);
262 taskExecutor->PostTask(
263 [weak = AceType::WeakClaim(AceType::RawPtr(frameNode))]() {
264 auto frameNode = weak.Upgrade();
265 CHECK_NULL_VOID(frameNode);
266 auto xcPattern = frameNode->GetPattern<NG::XComponentPattern>();
267 CHECK_NULL_VOID(xcPattern);
268 xcPattern->XComponentSizeInit();
269 xcPattern->SetXcomponentInit(true);
270 },
271 TaskExecutor::TaskType::JS, "ArkUIXComponentCreate");
272
273 return jsXComponent;
274 }
275
ChangeRenderType(int32_t renderType)276 bool JSXComponent::ChangeRenderType(int32_t renderType)
277 {
278 auto xcFrameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
279 CHECK_NULL_RETURN(xcFrameNode, false);
280 auto pattern = xcFrameNode->GetPattern<NG::XComponentPattern>();
281 CHECK_NULL_RETURN(pattern, false);
282 return pattern->ChangeRenderType(static_cast<NodeRenderType>(renderType));
283 }
284
JsOnLoad(const JSCallbackInfo& args)285 void JSXComponent::JsOnLoad(const JSCallbackInfo& args)
286 {
287 if (args.Length() < 1 || !args[0]->IsFunction()) {
288 return;
289 }
290 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
291 WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
292 auto onLoad = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
293 const std::string& xcomponentId) {
294 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
295 ACE_SCORING_EVENT("XComponent.onLoad");
296 PipelineContext::SetCallBackNode(node);
297 std::vector<std::string> keys = { "load", xcomponentId };
298 func->ExecuteNew(keys, "");
299 };
300 XComponentModel::GetInstance()->SetOnLoad(std::move(onLoad));
301 }
302
RegisterOnCreate(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func)303 void JSXComponent::RegisterOnCreate(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func)
304 {
305 auto frameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
306 CHECK_NULL_VOID(frameNode);
307
308 if (!func->IsFunction(execCtx.vm_)) {
309 return;
310 }
311
312 auto jsFunc = panda::Global<panda::FunctionRef>(execCtx.vm_, Local<panda::FunctionRef>(func));
313 auto onLoad = [execCtx, funcRef = std::move(jsFunc), node = AceType::WeakClaim(AceType::RawPtr(frameNode))](
314 const std::string& xcomponentId) {
315 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
316 ACE_SCORING_EVENT("XComponentNode.onCreate");
317 PipelineContext::SetCallBackNode(node);
318 std::vector<Local<JSValueRef>> argv;
319 JSRef<JSVal> jsVal;
320 if (XComponentClient::GetInstance().GetJSVal(xcomponentId, jsVal)) {
321 argv.emplace_back(jsVal->GetLocalHandle());
322 }
323 funcRef->Call(execCtx.vm_, JSNApi::GetGlobalObject(execCtx.vm_), argv.data(), argv.size());
324 };
325 XComponentModel::GetInstance()->RegisterOnCreate(frameNode, std::move(onLoad));
326 }
327
RegisterOnDestroy(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func)328 void JSXComponent::RegisterOnDestroy(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func)
329 {
330 auto frameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
331 CHECK_NULL_VOID(frameNode);
332
333 if (!func->IsFunction(execCtx.vm_)) {
334 return;
335 }
336
337 auto jsFunc = panda::Global<panda::FunctionRef>(execCtx.vm_, Local<panda::FunctionRef>(func));
338 auto onDestroy = [execCtx, funcRef = std::move(jsFunc), node = AceType::WeakClaim(AceType::RawPtr(frameNode))]() {
339 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
340 ACE_SCORING_EVENT("XComponentNode.onDestroy");
341 PipelineContext::SetCallBackNode(node);
342 funcRef->Call(execCtx.vm_, JSNApi::GetGlobalObject(execCtx.vm_), nullptr, 0);
343 };
344 XComponentModel::GetInstance()->RegisterOnDestroy(frameNode, std::move(onDestroy));
345 }
346
JsOnDestroy(const JSCallbackInfo& args)347 void JSXComponent::JsOnDestroy(const JSCallbackInfo& args)
348 {
349 if (args.Length() < 1 || !args[0]->IsFunction()) {
350 return;
351 }
352 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
353 WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
354 auto onDestroy = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode]() {
355 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
356 ACE_SCORING_EVENT("XComponent.onDestroy");
357 PipelineContext::SetCallBackNode(node);
358 std::vector<std::string> keys = { "destroy" };
359 func->Execute(keys, "");
360 };
361 XComponentModel::GetInstance()->SetOnDestroy(std::move(onDestroy));
362 }
363
JsOnAppear(const JSCallbackInfo& args)364 void JSXComponent::JsOnAppear(const JSCallbackInfo& args)
365 {
366 auto type = XComponentModel::GetInstance()->GetType();
367 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
368 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
369 return;
370 }
371 JSInteractableView::JsOnAppear(args);
372 }
373
JsOnDisAppear(const JSCallbackInfo& args)374 void JSXComponent::JsOnDisAppear(const JSCallbackInfo& args)
375 {
376 auto type = XComponentModel::GetInstance()->GetType();
377 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
378 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
379 return;
380 }
381 JSInteractableView::JsOnDisAppear(args);
382 }
383
JsOnAttach(const JSCallbackInfo& args)384 void JSXComponent::JsOnAttach(const JSCallbackInfo& args)
385 {
386 auto type = XComponentModel::GetInstance()->GetType();
387 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
388 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
389 return;
390 }
391 JSInteractableView::JsOnAttach(args);
392 }
393
JsOnDetach(const JSCallbackInfo& args)394 void JSXComponent::JsOnDetach(const JSCallbackInfo& args)
395 {
396 auto type = XComponentModel::GetInstance()->GetType();
397 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
398 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
399 return;
400 }
401 JSInteractableView::JsOnDetach(args);
402 }
403
JsOnTouch(const JSCallbackInfo& args)404 void JSXComponent::JsOnTouch(const JSCallbackInfo& args)
405 {
406 auto type = XComponentModel::GetInstance()->GetType();
407 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
408 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
409 return;
410 }
411 JSInteractableView::JsOnTouch(args);
412 }
413
JsOnClick(const JSCallbackInfo& args)414 void JSXComponent::JsOnClick(const JSCallbackInfo& args)
415 {
416 auto type = XComponentModel::GetInstance()->GetType();
417 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
418 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
419 return;
420 }
421 JSViewAbstract::JsOnClick(args);
422 }
423
JsOnKeyEvent(const JSCallbackInfo& args)424 void JSXComponent::JsOnKeyEvent(const JSCallbackInfo& args)
425 {
426 auto type = XComponentModel::GetInstance()->GetType();
427 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
428 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
429 return;
430 }
431 JSViewAbstract::JsOnKeyEvent(args);
432 }
433
JsOnMouse(const JSCallbackInfo& args)434 void JSXComponent::JsOnMouse(const JSCallbackInfo& args)
435 {
436 auto type = XComponentModel::GetInstance()->GetType();
437 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
438 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
439 return;
440 }
441 JSViewAbstract::JsOnMouse(args);
442 }
443
JsOnHover(const JSCallbackInfo& args)444 void JSXComponent::JsOnHover(const JSCallbackInfo& args)
445 {
446 auto type = XComponentModel::GetInstance()->GetType();
447 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
448 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
449 return;
450 }
451 JSViewAbstract::JsOnHover(args);
452 }
453
454
JsOnFocus(const JSCallbackInfo& args)455 void JSXComponent::JsOnFocus(const JSCallbackInfo& args)
456 {
457 auto type = XComponentModel::GetInstance()->GetType();
458 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
459 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
460 return;
461 }
462 JSViewAbstract::JsOnFocus(args);
463 }
464
JsOnBlur(const JSCallbackInfo& args)465 void JSXComponent::JsOnBlur(const JSCallbackInfo& args)
466 {
467 auto type = XComponentModel::GetInstance()->GetType();
468 auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
469 if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
470 return;
471 }
472 JSViewAbstract::JsOnBlur(args);
473 }
474
JsBackgroundColor(const JSCallbackInfo& args)475 void JSXComponent::JsBackgroundColor(const JSCallbackInfo& args)
476 {
477 auto type = XComponentModel::GetInstance()->GetType();
478 if (!XComponentModel::IsBackGroundColorAvailable(type)) {
479 return;
480 }
481 if (args.Length() < 1) {
482 return;
483 }
484 Color backgroundColor;
485 if (!ParseJsColor(args[0], backgroundColor)) {
486 backgroundColor = (type == XComponentType::SURFACE) ? Color::BLACK : Color::TRANSPARENT;
487 }
488 ViewAbstractModel::GetInstance()->SetBackgroundColor(backgroundColor);
489 }
490
JsBackgroundImage(const JSCallbackInfo& args)491 void JSXComponent::JsBackgroundImage(const JSCallbackInfo& args)
492 {
493 auto type = XComponentModel::GetInstance()->GetType();
494 if (type != XComponentType::NODE) {
495 return;
496 }
497 JSViewAbstract::JsBackgroundImage(args);
498 }
499
JsBackgroundImageSize(const JSCallbackInfo& args)500 void JSXComponent::JsBackgroundImageSize(const JSCallbackInfo& args)
501 {
502 auto type = XComponentModel::GetInstance()->GetType();
503 if (type != XComponentType::NODE) {
504 return;
505 }
506 JSViewAbstract::JsBackgroundImageSize(args);
507 }
508
JsBackgroundImagePosition(const JSCallbackInfo& args)509 void JSXComponent::JsBackgroundImagePosition(const JSCallbackInfo& args)
510 {
511 auto type = XComponentModel::GetInstance()->GetType();
512 if (type != XComponentType::NODE) {
513 return;
514 }
515 JSViewAbstract::JsBackgroundImagePosition(args);
516 }
517
518
JsOpacity(const JSCallbackInfo& args)519 void JSXComponent::JsOpacity(const JSCallbackInfo& args)
520 {
521 auto type = XComponentModel::GetInstance()->GetType();
522 if (type == XComponentType::SURFACE || type == XComponentType::COMPONENT) {
523 return;
524 }
525 JSViewAbstract::JsOpacity(args);
526 }
527
JsBlur(const JSCallbackInfo& args)528 void JSXComponent::JsBlur(const JSCallbackInfo& args)
529 {
530 auto type = XComponentModel::GetInstance()->GetType();
531 if (type != XComponentType::NODE) {
532 return;
533 }
534 JSViewAbstract::JsBlur(args);
535 }
536
JsBackdropBlur(const JSCallbackInfo& args)537 void JSXComponent::JsBackdropBlur(const JSCallbackInfo& args)
538 {
539 auto type = XComponentModel::GetInstance()->GetType();
540 if (type != XComponentType::NODE) {
541 return;
542 }
543 JSViewAbstract::JsBackdropBlur(args);
544 }
545
JsGrayscale(const JSCallbackInfo& args)546 void JSXComponent::JsGrayscale(const JSCallbackInfo& args)
547 {
548 auto type = XComponentModel::GetInstance()->GetType();
549 if (type != XComponentType::NODE) {
550 return;
551 }
552 // JSViewAbstract::JsGrayscale(args);
553 }
554
JsBrightness(const JSCallbackInfo& args)555 void JSXComponent::JsBrightness(const JSCallbackInfo& args)
556 {
557 auto type = XComponentModel::GetInstance()->GetType();
558 if (type != XComponentType::NODE) {
559 return;
560 }
561 JSViewAbstract::JsBrightness(args);
562 }
563
JsSaturate(const JSCallbackInfo& args)564 void JSXComponent::JsSaturate(const JSCallbackInfo& args)
565 {
566 auto type = XComponentModel::GetInstance()->GetType();
567 if (type != XComponentType::NODE) {
568 return;
569 }
570 JSViewAbstract::JsSaturate(args);
571 }
572
JsContrast(const JSCallbackInfo& args)573 void JSXComponent::JsContrast(const JSCallbackInfo& args)
574 {
575 auto type = XComponentModel::GetInstance()->GetType();
576 if (type != XComponentType::NODE) {
577 return;
578 }
579 JSViewAbstract::JsContrast(args);
580 }
581
JsInvert(const JSCallbackInfo& args)582 void JSXComponent::JsInvert(const JSCallbackInfo& args)
583 {
584 auto type = XComponentModel::GetInstance()->GetType();
585 if (type != XComponentType::NODE) {
586 return;
587 }
588 JSViewAbstract::JsInvert(args);
589 }
590
JsSepia(const JSCallbackInfo& args)591 void JSXComponent::JsSepia(const JSCallbackInfo& args)
592 {
593 auto type = XComponentModel::GetInstance()->GetType();
594 if (type != XComponentType::NODE) {
595 return;
596 }
597 JSViewAbstract::JsSepia(args);
598 }
599
JsHueRotate(const JSCallbackInfo& args)600 void JSXComponent::JsHueRotate(const JSCallbackInfo& args)
601 {
602 auto type = XComponentModel::GetInstance()->GetType();
603 if (type != XComponentType::NODE) {
604 return;
605 }
606 JSViewAbstract::JsHueRotate(args);
607 }
608
JsColorBlend(const JSCallbackInfo& args)609 void JSXComponent::JsColorBlend(const JSCallbackInfo& args)
610 {
611 auto type = XComponentModel::GetInstance()->GetType();
612 if (type != XComponentType::NODE) {
613 return;
614 }
615 JSViewAbstract::JsColorBlend(args);
616 }
617
JsSphericalEffect(const JSCallbackInfo& args)618 void JSXComponent::JsSphericalEffect(const JSCallbackInfo& args)
619 {
620 auto type = XComponentModel::GetInstance()->GetType();
621 if (type != XComponentType::NODE) {
622 return;
623 }
624 JSViewAbstract::JsSphericalEffect(args);
625 }
626
JsLightUpEffect(const JSCallbackInfo& args)627 void JSXComponent::JsLightUpEffect(const JSCallbackInfo& args)
628 {
629 auto type = XComponentModel::GetInstance()->GetType();
630 if (type != XComponentType::NODE) {
631 return;
632 }
633 JSViewAbstract::JsLightUpEffect(args);
634 }
635
JsPixelStretchEffect(const JSCallbackInfo& args)636 void JSXComponent::JsPixelStretchEffect(const JSCallbackInfo& args)
637 {
638 auto type = XComponentModel::GetInstance()->GetType();
639 if (type != XComponentType::NODE) {
640 return;
641 }
642 JSViewAbstract::JsPixelStretchEffect(args);
643 }
644
JsLinearGradientBlur(const JSCallbackInfo& args)645 void JSXComponent::JsLinearGradientBlur(const JSCallbackInfo& args)
646 {
647 auto type = XComponentModel::GetInstance()->GetType();
648 if (type != XComponentType::NODE) {
649 return;
650 }
651 JSViewAbstract::JsLinearGradientBlur(args);
652 }
653
JsEnableAnalyzer(bool enable)654 void JSXComponent::JsEnableAnalyzer(bool enable)
655 {
656 auto type = XComponentModel::GetInstance()->GetType();
657 if (type == XComponentType::COMPONENT || type == XComponentType::NODE) {
658 return;
659 }
660 XComponentModel::GetInstance()->EnableAnalyzer(enable);
661 }
662
JsRenderFit(const JSCallbackInfo& args)663 void JSXComponent::JsRenderFit(const JSCallbackInfo& args)
664 {
665 auto type = XComponentModel::GetInstance()->GetType();
666 if (type == XComponentType::COMPONENT || type == XComponentType::NODE || args.Length() != 1) {
667 return;
668 }
669 if (type == XComponentType::TEXTURE) {
670 JSViewAbstract::JSRenderFit(args);
671 return;
672 }
673
674 // set RenderFit on SurfaceNode when type is SURFACE
675 RenderFit renderFit = RenderFit::RESIZE_FILL;
676 if (args[0]->IsNumber()) {
677 int32_t fitNumber = args[0]->ToNumber<int32_t>();
678 if (fitNumber >= static_cast<int32_t>(RenderFit::CENTER) &&
679 fitNumber <= static_cast<int32_t>(RenderFit::RESIZE_COVER_BOTTOM_RIGHT)) {
680 renderFit = static_cast<RenderFit>(fitNumber);
681 }
682 }
683 XComponentModel::GetInstance()->SetRenderFit(renderFit);
684 }
685
JsEnableSecure(const JSCallbackInfo& args)686 void JSXComponent::JsEnableSecure(const JSCallbackInfo& args)
687 {
688 auto type = XComponentModel::GetInstance()->GetType();
689 if (type != XComponentType::SURFACE || args.Length() != 1) {
690 return;
691 }
692 // set isSecure on SurfaceNode when type is SURFACE
693 if (args[0]->IsBoolean()) {
694 bool isSecure = args[0]->ToBoolean();
695 XComponentModel::GetInstance()->EnableSecure(isSecure);
696 }
697 }
698 } // namespace OHOS::Ace::Framework
699