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_list_item_group_bridge.h"
16
17 #include "core/interfaces/native/node/api.h"
18 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19
20 namespace OHOS::Ace::NG {
21 constexpr int32_t NODE_INDEX = 0;
22 constexpr int32_t STROKE_WIDTH_INDEX = 1;
23 constexpr int32_t COLOR_INDEX = 2;
24 constexpr int32_t START_MARGIN_INDEX = 3;
25 constexpr int32_t END_MARGIN_INDEX = 4;
26
27 constexpr int32_t ARG_GROUP_LENGTH = 3;
28
SetDivider(ArkUIRuntimeCallInfo * runtimeCallInfo)29 ArkUINativeModuleValue ListeItemGroupBridege::SetDivider(ArkUIRuntimeCallInfo* runtimeCallInfo)
30 {
31 EcmaVM* vm = runtimeCallInfo->GetVM();
32 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
33 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NODE_INDEX);
34 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
35 Local<JSValueRef> dividerStrokeWidthArgs = runtimeCallInfo->GetCallArgRef(STROKE_WIDTH_INDEX);
36 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(COLOR_INDEX);
37 Local<JSValueRef> dividerStartMarginArgs = runtimeCallInfo->GetCallArgRef(START_MARGIN_INDEX);
38 Local<JSValueRef> dividerEndMarginArgs = runtimeCallInfo->GetCallArgRef(END_MARGIN_INDEX);
39 if (dividerStrokeWidthArgs->IsUndefined() && dividerStartMarginArgs->IsUndefined() &&
40 dividerEndMarginArgs->IsUndefined() && colorArg->IsUndefined()) {
41 GetArkUIInternalNodeAPI()->GetListItemGroupModifier().ListItemGroupResetDivider(nativeNode);
42 return panda::JSValueRef::Undefined(vm);
43 }
44
45 CalcDimension dividerStrokeWidth;
46 CalcDimension dividerStartMargin;
47 CalcDimension dividerEndMargin;
48 uint32_t color;
49 auto* frameNode = reinterpret_cast<FrameNode*>(nativeNode);
50 auto context = frameNode->GetContext();
51 auto themeManager = context->GetThemeManager();
52 CHECK_NULL_RETURN(themeManager, panda::NativePointerRef::New(vm, nullptr));
53 auto listTheme = themeManager->GetTheme<ListTheme>();
54 CHECK_NULL_RETURN(listTheme, panda::NativePointerRef::New(vm, nullptr));
55
56 if (!ArkTSUtils::ParseJsDimensionVp(vm, dividerStrokeWidthArgs, dividerStrokeWidth, true) ||
57 LessNotEqual(dividerStrokeWidth.Value(), 0.0f) || dividerStrokeWidth.Unit() == DimensionUnit::PERCENT) {
58 dividerStrokeWidth.Reset();
59 }
60
61 Color colorObj;
62 if (!ArkTSUtils::ParseJsColorAlpha(vm, colorArg, colorObj)) {
63 color = listTheme->GetDividerColor().GetValue();
64 } else {
65 color = colorObj.GetValue();
66 }
67 if (!ArkTSUtils::ParseJsDimensionVp(vm, dividerStartMarginArgs, dividerStartMargin) ||
68 LessNotEqual(dividerStartMargin.Value(), 0.0f) || dividerStartMargin.Unit() == DimensionUnit::PERCENT) {
69 dividerStartMargin.Reset();
70 }
71 if (!ArkTSUtils::ParseJsDimensionVp(vm, dividerEndMarginArgs, dividerEndMargin) ||
72 LessNotEqual(dividerEndMargin.Value(), 0.0f) || dividerEndMargin.Unit() == DimensionUnit::PERCENT) {
73 dividerEndMargin.Reset();
74 }
75 uint32_t size = ARG_GROUP_LENGTH;
76 double values[size];
77 int32_t units[size];
78 values[NODE_INDEX] = dividerStrokeWidth.Value();
79 values[STROKE_WIDTH_INDEX] = dividerStartMargin.Value();
80 values[COLOR_INDEX] = dividerEndMargin.Value();
81 units[NODE_INDEX] = static_cast<int32_t>(dividerStrokeWidth.Unit());
82 units[STROKE_WIDTH_INDEX] = static_cast<int32_t>(dividerStartMargin.Unit());
83 units[COLOR_INDEX] = static_cast<int32_t>(dividerEndMargin.Unit());
84 GetArkUIInternalNodeAPI()->GetListItemGroupModifier().ListItemGroupSetDivider(
85 nativeNode, color, values, units, size);
86
87 return panda::JSValueRef::Undefined(vm);
88 }
ResetDivider(ArkUIRuntimeCallInfo * runtimeCallInfo)89 ArkUINativeModuleValue ListeItemGroupBridege::ResetDivider(ArkUIRuntimeCallInfo* runtimeCallInfo)
90 {
91 EcmaVM* vm = runtimeCallInfo->GetVM();
92 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
93 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NODE_INDEX);
94 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
95 GetArkUIInternalNodeAPI()->GetListItemGroupModifier().ListItemGroupResetDivider(nativeNode);
96 return panda::JSValueRef::Undefined(vm);
97 }
98 } // namespace OHOS::Ace::NG