1 /*
2  * Copyright (c) 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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_flex_bridge.h"
16 
17 #include "bridge/declarative_frontend/engine/jsi/jsi_types.h"
18 #include "core/components/common/layout/constants.h"
19 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
20 
21 namespace OHOS::Ace::NG {
SetFlexInitialize(ArkUIRuntimeCallInfo * runtimeCallInfo)22 ArkUINativeModuleValue FlexBridge::SetFlexInitialize(ArkUIRuntimeCallInfo* runtimeCallInfo)
23 {
24     EcmaVM* vm = runtimeCallInfo->GetVM();
25     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
26     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
27     Local<JSValueRef> directionArg = runtimeCallInfo->GetCallArgRef(1);
28     Local<JSValueRef> wrapArg = runtimeCallInfo->GetCallArgRef(2);
29     Local<JSValueRef> justifyContentArg = runtimeCallInfo->GetCallArgRef(3);
30     Local<JSValueRef> alignItemsArg = runtimeCallInfo->GetCallArgRef(4);
31     Local<JSValueRef> alignContentArg = runtimeCallInfo->GetCallArgRef(5);
32     Local<JSValueRef> mainArg = runtimeCallInfo->GetCallArgRef(6);
33     Local<JSValueRef> crossArg = runtimeCallInfo->GetCallArgRef(7);
34     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
35 
36     int32_t options[5] = { 0, 0, 0, 1, 0 };
37     if (!directionArg->IsNull() && directionArg->IsNumber()) {
38         options[0] = directionArg->Int32Value(vm);
39     }
40     if (!wrapArg->IsNull() && wrapArg->IsNumber()) {
41         options[1] = wrapArg->Int32Value(vm);
42     }
43     if (!justifyContentArg->IsNull() && justifyContentArg->IsNumber()) {
44         options[2] = justifyContentArg->Int32Value(vm);
45     }
46     if (!alignItemsArg->IsNull() && alignItemsArg->IsNumber()) {
47         options[3] = alignItemsArg->Int32Value(vm);
48     }
49     if (!alignContentArg->IsNull() && alignContentArg->IsNumber()) {
50         options[4] = alignContentArg->Int32Value(vm);
51     }
52     GetArkUINodeModifiers()->getFlexModifier()->setFlexOptions(nativeNode, options, 5);
53     if (options[1] != 0) {
54         if (!crossArg->IsNull() && !crossArg->IsUndefined()) {
55             CalcDimension crossValue;
56             if (crossArg->IsObject(vm) && ArkTSUtils::ParseJsLengthMetrics(vm, crossArg, crossValue)) {
57                 GetArkUINodeModifiers()->getFlexModifier()->setFlexCrossSpace(
58                     nativeNode, crossValue.Value(), static_cast<int8_t>(crossValue.Unit()));
59             }
60         }
61     }
62     if (!mainArg->IsNull() && !mainArg->IsUndefined()) {
63         CalcDimension mainValue;
64         if (crossArg->IsObject(vm) && ArkTSUtils::ParseJsLengthMetrics(vm, crossArg, mainValue)) {
65             GetArkUINodeModifiers()->getFlexModifier()->setFlexMainSpace(
66                 nativeNode, mainValue.Value(), static_cast<int8_t>(mainValue.Unit()));
67         }
68     }
69     return panda::JSValueRef::Undefined(vm);
70 }
71 
ResetFlexInitialize(ArkUIRuntimeCallInfo * runtimeCallInfo)72 ArkUINativeModuleValue FlexBridge::ResetFlexInitialize(ArkUIRuntimeCallInfo* runtimeCallInfo)
73 {
74     EcmaVM* vm = runtimeCallInfo->GetVM();
75     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
76     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
77     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
78     GetArkUINodeModifiers()->getFlexModifier()->resetFlexOptions(nativeNode);
79     return panda::JSValueRef::Undefined(vm);
80 }
81 }
82