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