1 /*
2 * Copyright (c) 2023 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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_loading_progress_bridge.h"
16 #include "core/components_ng/base/frame_node.h"
17 #include "core/components_ng/pattern/loading_progress/loading_progress_model_ng.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19
20 using namespace OHOS::Ace::Framework;
21
22 namespace OHOS::Ace::NG {
23 namespace {
24 constexpr int NUM_0 = 0;
25 constexpr int NUM_1 = 1;
26 const char* LOADINGPROGRESS_NODEPTR_OF_UINODE = "nodePtr_";
27 } // namespace
SetColor(ArkUIRuntimeCallInfo * runtimeCallInfo)28 ArkUINativeModuleValue LoadingProgressBridge::SetColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
29 {
30 EcmaVM* vm = runtimeCallInfo->GetVM();
31 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
32 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
33 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(NUM_1);
34 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
35 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
36 Color color;
37 RefPtr<ResourceObject> colorResObj;
38 auto nodeInfo = ArkTSUtils::MakeNativeNodeInfo(nativeNode);
39 if (!ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color, colorResObj, nodeInfo)) {
40 GetArkUINodeModifiers()->getLoadingProgressModifier()->resetColor(nativeNode);
41 } else {
42 auto colorRawPtr = AceType::RawPtr(colorResObj);
43 GetArkUINodeModifiers()->getLoadingProgressModifier()->setColorPtr(nativeNode, color.GetValue(), colorRawPtr);
44 }
45 return panda::JSValueRef::Undefined(vm);
46 }
47
ResetColor(ArkUIRuntimeCallInfo * runtimeCallInfo)48 ArkUINativeModuleValue LoadingProgressBridge::ResetColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
49 {
50 EcmaVM* vm = runtimeCallInfo->GetVM();
51 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
52 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
53 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
54 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
55 GetArkUINodeModifiers()->getLoadingProgressModifier()->resetColor(nativeNode);
56 return panda::JSValueRef::Undefined(vm);
57 }
58
SetEnableLoading(ArkUIRuntimeCallInfo * runtimeCallInfo)59 ArkUINativeModuleValue LoadingProgressBridge::SetEnableLoading(ArkUIRuntimeCallInfo* runtimeCallInfo)
60 {
61 EcmaVM* vm = runtimeCallInfo->GetVM();
62 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
63 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
64 Local<JSValueRef> enableLoadingArg = runtimeCallInfo->GetCallArgRef(NUM_1);
65 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
66 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
67 bool boolValue = enableLoadingArg->ToBoolean(vm)->Value();
68 GetArkUINodeModifiers()->getLoadingProgressModifier()->setEnableLoading(nativeNode, boolValue);
69 return panda::JSValueRef::Undefined(vm);
70 }
71
ResetEnableLoading(ArkUIRuntimeCallInfo * runtimeCallInfo)72 ArkUINativeModuleValue LoadingProgressBridge::ResetEnableLoading(ArkUIRuntimeCallInfo* runtimeCallInfo)
73 {
74 EcmaVM* vm = runtimeCallInfo->GetVM();
75 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
76 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
77 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
78 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
79 GetArkUINodeModifiers()->getLoadingProgressModifier()->resetEnableLoading(nativeNode);
80 return panda::JSValueRef::Undefined(vm);
81 }
82
SetForegroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)83 ArkUINativeModuleValue LoadingProgressBridge::SetForegroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
84 {
85 EcmaVM *vm = runtimeCallInfo->GetVM();
86 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
87 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
88 auto colorArg = runtimeCallInfo->GetCallArgRef(1);
89 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
90 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
91
92 ForegroundColorStrategy strategy;
93 if (ArkTSUtils::ParseJsColorStrategy(vm, colorArg, strategy)) {
94 auto strategyInt = static_cast<uint32_t>(ForegroundColorStrategy::INVERT);
95 GetArkUINodeModifiers()->getCommonModifier()->setForegroundColor(nativeNode, false, strategyInt, nullptr);
96 return panda::JSValueRef::Undefined(vm);
97 }
98 Color foregroundColor;
99 RefPtr<ResourceObject> foregroundColorResObj;
100 auto nodeInfo = ArkTSUtils::MakeNativeNodeInfo(nativeNode);
101 if (!ArkTSUtils::ParseJsColorAlpha(vm, colorArg, foregroundColor, foregroundColorResObj, nodeInfo)) {
102 GetArkUINodeModifiers()->getLoadingProgressModifier()->resetForegroundColor(nativeNode);
103 } else {
104 auto foregroundColorRawPtr = AceType::RawPtr(foregroundColorResObj);
105 GetArkUINodeModifiers()->getLoadingProgressModifier()->setForegroundColorPtr(
106 nativeNode, foregroundColor.GetValue(), foregroundColorRawPtr);
107 }
108 return panda::JSValueRef::Undefined(vm);
109 }
110
ResetForegroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)111 ArkUINativeModuleValue LoadingProgressBridge::ResetForegroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
112 {
113 EcmaVM* vm = runtimeCallInfo->GetVM();
114 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
115 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
116 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
117 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
118 GetArkUINodeModifiers()->getLoadingProgressModifier()->resetForegroundColor(nativeNode);
119 return panda::JSValueRef::Undefined(vm);
120 }
121
SetContentModifierBuilder(ArkUIRuntimeCallInfo * runtimeCallInfo)122 ArkUINativeModuleValue LoadingProgressBridge::SetContentModifierBuilder(ArkUIRuntimeCallInfo* runtimeCallInfo)
123 {
124 EcmaVM* vm = runtimeCallInfo->GetVM();
125 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
126 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
127 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
128 CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
129 auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
130 if (!secondArg->IsObject(vm)) {
131 LoadingProgressModelNG::SetBuilderFunc(frameNode, nullptr);
132 return panda::JSValueRef::Undefined(vm);
133 }
134 panda::CopyableGlobal<panda::ObjectRef> obj(vm, secondArg);
135 LoadingProgressModelNG::SetBuilderFunc(frameNode,
136 [vm, frameNode, obj = std::move(obj), containerId = Container::CurrentId()](
137 LoadingProgressConfiguration config) -> RefPtr<FrameNode> {
138 ContainerScope scope(containerId);
139 auto context = ArkTSUtils::GetContext(vm);
140 CHECK_EQUAL_RETURN(context->IsUndefined(), true, nullptr);
141 const char* keysOfLoadingprogress[] = { "enableLoading", "enabled"};
142 Local<JSValueRef> valuesOfLoadingprogress[] = { panda::BooleanRef::New(vm, config.enableloading_),
143 panda::BooleanRef::New(vm, config.enabled_)};
144 auto loadingprogress = panda::ObjectRef::NewWithNamedProperties(vm,
145 ArraySize(keysOfLoadingprogress), keysOfLoadingprogress, valuesOfLoadingprogress);
146 loadingprogress->SetNativePointerFieldCount(vm, 1);
147 loadingprogress->SetNativePointerField(vm, 0, static_cast<void*>(frameNode));
148 panda::Local<panda::JSValueRef> params[2] = { context, loadingprogress };
149 LocalScope pandaScope(vm);
150 panda::TryCatch trycatch(vm);
151 auto jsObject = obj.ToLocal();
152 auto makeFunc = jsObject->Get(vm, panda::StringRef::NewFromUtf8(vm, "makeContentModifierNode"));
153 CHECK_EQUAL_RETURN(makeFunc->IsFunction(vm), false, nullptr);
154 panda::Local<panda::FunctionRef> func = makeFunc;
155 auto result = func->Call(vm, jsObject, params, 2);
156 JSNApi::ExecutePendingJob(vm);
157 CHECK_EQUAL_RETURN(result.IsEmpty() || trycatch.HasCaught() || !result->IsObject(vm), true, nullptr);
158 auto resultObj = result->ToObject(vm);
159 panda::Local<panda::JSValueRef> nodeptr =
160 resultObj->Get(vm, panda::StringRef::NewFromUtf8(vm, LOADINGPROGRESS_NODEPTR_OF_UINODE));
161 CHECK_EQUAL_RETURN(nodeptr.IsEmpty() || nodeptr->IsUndefined() || nodeptr->IsNull(), true, nullptr);
162 CHECK_NULL_RETURN(nodeptr->IsNativePointer(vm), nullptr);
163 auto* frameNode = reinterpret_cast<FrameNode*>(nodeptr->ToNativePointer(vm)->Value());
164 CHECK_NULL_RETURN(frameNode, nullptr);
165 return AceType::Claim(frameNode);
166 });
167 return panda::JSValueRef::Undefined(vm);
168 }
169 } // namespace OHOS::Ace::NG
170