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