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_row_bridge.h"
16 
17 #include "base/geometry/dimension.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19 
20 namespace OHOS::Ace::NG {
SetAlignItems(ArkUIRuntimeCallInfo * runtimeCallInfo)21 ArkUINativeModuleValue RowBridge::SetAlignItems(ArkUIRuntimeCallInfo* runtimeCallInfo)
22 {
23     EcmaVM* vm = runtimeCallInfo->GetVM();
24     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
25     Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(0);
26     Local<JSValueRef> alignItemsArg = runtimeCallInfo->GetCallArgRef(1);
27     auto nativeNode = nodePtr(nativeNodeArg->ToNativePointer(vm)->Value());
28     if (alignItemsArg->IsNumber()) {
29         int32_t alignItems = alignItemsArg->Int32Value(vm);
30         if ((alignItems == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
31             (alignItems == static_cast<int32_t>(FlexAlign::FLEX_END)) ||
32             (alignItems == static_cast<int32_t>(FlexAlign::CENTER)) ||
33             (alignItems == static_cast<int32_t>(FlexAlign::STRETCH))) {
34             GetArkUINodeModifiers()->getRowModifier()->setRowAlignItems(nativeNode, alignItems);
35         } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
36             GetArkUINodeModifiers()->getRowModifier()->resetRowAlignItems(nativeNode);
37         }
38     } else {
39         GetArkUINodeModifiers()->getRowModifier()->resetRowAlignItems(nativeNode);
40     }
41     return panda::JSValueRef::Undefined(vm);
42 }
43 
ResetAlignItems(ArkUIRuntimeCallInfo * runtimeCallInfo)44 ArkUINativeModuleValue RowBridge::ResetAlignItems(ArkUIRuntimeCallInfo* runtimeCallInfo)
45 {
46     EcmaVM* vm = runtimeCallInfo->GetVM();
47     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
48     Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(0);
49     auto nativeNode = nodePtr(nativeNodeArg->ToNativePointer(vm)->Value());
50     GetArkUINodeModifiers()->getRowModifier()->resetRowAlignItems(nativeNode);
51     return panda::JSValueRef::Undefined(vm);
52 }
53 
SetJustifyContent(ArkUIRuntimeCallInfo * runtimeCallInfo)54 ArkUINativeModuleValue RowBridge::SetJustifyContent(ArkUIRuntimeCallInfo* runtimeCallInfo)
55 {
56     EcmaVM* vm = runtimeCallInfo->GetVM();
57     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
58     Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(0);
59     Local<JSValueRef> justifyContentArg = runtimeCallInfo->GetCallArgRef(1);
60     auto nativeNode = nodePtr(nativeNodeArg->ToNativePointer(vm)->Value());
61     if (justifyContentArg->IsNumber()) {
62         int32_t justifyContent = justifyContentArg->Int32Value(vm);
63         if ((justifyContent == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
64             (justifyContent == static_cast<int32_t>(FlexAlign::FLEX_END)) ||
65             (justifyContent == static_cast<int32_t>(FlexAlign::CENTER)) ||
66             (justifyContent == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) ||
67             (justifyContent == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) ||
68             (justifyContent == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) {
69             GetArkUINodeModifiers()->getRowModifier()->setRowJustifyContent(nativeNode, justifyContent);
70         } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
71             GetArkUINodeModifiers()->getRowModifier()->resetRowJustifyContent(nativeNode);
72         }
73     } else {
74         GetArkUINodeModifiers()->getRowModifier()->resetRowJustifyContent(nativeNode);
75     }
76     return panda::JSValueRef::Undefined(vm);
77 }
78 
ResetJustifyContent(ArkUIRuntimeCallInfo * runtimeCallInfo)79 ArkUINativeModuleValue RowBridge::ResetJustifyContent(ArkUIRuntimeCallInfo* runtimeCallInfo)
80 {
81     EcmaVM* vm = runtimeCallInfo->GetVM();
82     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
83     Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(0);
84     auto nativeNode = nodePtr(nativeNodeArg->ToNativePointer(vm)->Value());
85     GetArkUINodeModifiers()->getRowModifier()->resetRowJustifyContent(nativeNode);
86     return panda::JSValueRef::Undefined(vm);
87 }
88 
SetSpace(ArkUIRuntimeCallInfo * runtimeCallInfo)89 ArkUINativeModuleValue RowBridge::SetSpace(ArkUIRuntimeCallInfo* runtimeCallInfo)
90 {
91     EcmaVM* vm = runtimeCallInfo->GetVM();
92     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
93     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
94     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
95     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
96     CalcDimension space;
97     ArkTSUtils::ParseJsDimensionVp(vm, secondArg, space, false);
98     if (LessNotEqual(space.Value(), 0.0)) {
99         GetArkUINodeModifiers()->getRowModifier()->resetRowSpace(nativeNode);
100         return panda::JSValueRef::Undefined(vm);
101     }
102     GetArkUINodeModifiers()->getRowModifier()->setRowSpace(nativeNode, space.Value(), static_cast<int>(space.Unit()));
103     return panda::JSValueRef::Undefined(vm);
104 }
105 
ResetSpace(ArkUIRuntimeCallInfo * runtimeCallInfo)106 ArkUINativeModuleValue RowBridge::ResetSpace(ArkUIRuntimeCallInfo* runtimeCallInfo)
107 {
108     EcmaVM* vm = runtimeCallInfo->GetVM();
109     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
110     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
111     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
112     GetArkUINodeModifiers()->getRowModifier()->resetRowSpace(nativeNode);
113     return panda::JSValueRef::Undefined(vm);
114 }
115 
SetReverse(ArkUIRuntimeCallInfo * runtimeCallInfo)116 ArkUINativeModuleValue RowBridge::SetReverse(ArkUIRuntimeCallInfo* runtimeCallInfo)
117 {
118     EcmaVM* vm = runtimeCallInfo->GetVM();
119     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
120     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
121     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
122     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
123     auto hasValue = secondArg->IsBoolean();
124     if (!hasValue) {
125         GetArkUINodeModifiers()->getRowModifier()->setRowReverse(nativeNode, true);
126         return panda::JSValueRef::Undefined(vm);
127     }
128     auto isReverse = secondArg->ToBoolean(vm)->Value();
129     GetArkUINodeModifiers()->getRowModifier()->setRowReverse(nativeNode, isReverse);
130     return panda::JSValueRef::Undefined(vm);
131 }
132 
ResetReverse(ArkUIRuntimeCallInfo * runtimeCallInfo)133 ArkUINativeModuleValue RowBridge::ResetReverse(ArkUIRuntimeCallInfo* runtimeCallInfo)
134 {
135     EcmaVM* vm = runtimeCallInfo->GetVM();
136     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
137     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
138     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
139     GetArkUINodeModifiers()->getRowModifier()->resetRowReverse(nativeNode);
140     return panda::JSValueRef::Undefined(vm);
141 }
142 
143 } // namespace OHOS::Ace::NG
144