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