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 "core/interfaces/native/node/row_modifier.h"
16 #include "core/components/common/layout/constants.h"
17 #include "core/pipeline/base/element_register.h"
18 #include "core/components_ng/base/frame_node.h"
19 #include "core/components_ng/pattern/linear_layout/row_model_ng.h"
20 
21 namespace OHOS::Ace::NG {
22 constexpr FlexAlign DEFAULT_ROW_JUSTIFY_CONTENT = FlexAlign::FLEX_START;
23 constexpr FlexAlign DEFAULT_ROW_ALIGN_ITEMS = FlexAlign::CENTER;
24 const int32_t ERROR_INT_CODE = -1;
25 
SetRowJustifyContent(ArkUINodeHandle node,int32_t flexAlign)26 void SetRowJustifyContent(ArkUINodeHandle node, int32_t flexAlign)
27 {
28     auto *frameNode = reinterpret_cast<FrameNode *>(node);
29     CHECK_NULL_VOID(frameNode);
30     RowModelNG::SetJustifyContent(frameNode, static_cast<FlexAlign>(flexAlign));
31 }
32 
ResetRowJustifyContent(ArkUINodeHandle node)33 void ResetRowJustifyContent(ArkUINodeHandle node)
34 {
35     auto *frameNode = reinterpret_cast<FrameNode *>(node);
36     CHECK_NULL_VOID(frameNode);
37     RowModelNG::SetJustifyContent(frameNode, DEFAULT_ROW_JUSTIFY_CONTENT);
38 }
39 
SetRowAlignItems(ArkUINodeHandle node,int32_t verticalAlign)40 void SetRowAlignItems(ArkUINodeHandle node, int32_t verticalAlign)
41 {
42     auto *frameNode = reinterpret_cast<FrameNode *>(node);
43     CHECK_NULL_VOID(frameNode);
44     RowModelNG::SetAlignItems(frameNode, static_cast<FlexAlign>(verticalAlign));
45 }
46 
ResetRowAlignItems(ArkUINodeHandle node)47 void ResetRowAlignItems(ArkUINodeHandle node)
48 {
49     auto *frameNode = reinterpret_cast<FrameNode *>(node);
50     CHECK_NULL_VOID(frameNode);
51     RowModelNG::SetAlignItems(frameNode, DEFAULT_ROW_ALIGN_ITEMS);
52 }
53 
GetRowJustifyContent(ArkUINodeHandle node)54 ArkUI_Int32 GetRowJustifyContent(ArkUINodeHandle node)
55 {
56     auto* frameNode = reinterpret_cast<FrameNode*>(node);
57     CHECK_NULL_RETURN(frameNode, ERROR_INT_CODE);
58     return static_cast<ArkUI_Int32>(RowModelNG::GetJustifyContent(frameNode));
59 }
60 
GetRowAlignItems(ArkUINodeHandle node)61 ArkUI_Int32 GetRowAlignItems(ArkUINodeHandle node)
62 {
63     auto* frameNode = reinterpret_cast<FrameNode*>(node);
64     CHECK_NULL_RETURN(frameNode, ERROR_INT_CODE);
65     return static_cast<ArkUI_Int32>(RowModelNG::GetAlignItems(frameNode));
66 }
67 
SetRowSpace(ArkUINodeHandle node,ArkUI_Float32 value,ArkUI_Int32 unit)68 void SetRowSpace(ArkUINodeHandle node, ArkUI_Float32 value, ArkUI_Int32 unit)
69 {
70     auto* frameNode = reinterpret_cast<FrameNode*>(node);
71     CHECK_NULL_VOID(frameNode);
72     const auto space = CalcDimension(value, static_cast<OHOS::Ace::DimensionUnit>(unit));
73     RowModelNG::SetSpace(frameNode, space);
74 }
75 
ResetRowSpace(ArkUINodeHandle node)76 void ResetRowSpace(ArkUINodeHandle node)
77 {
78     auto* frameNode = reinterpret_cast<FrameNode*>(node);
79     CHECK_NULL_VOID(frameNode);
80     const auto space = CalcDimension(0.0, DimensionUnit::PX);
81     RowModelNG::SetSpace(frameNode, space);
82 }
83 
SetRowReverse(ArkUINodeHandle node,ArkUI_Bool value)84 void SetRowReverse(ArkUINodeHandle node, ArkUI_Bool value)
85 {
86     auto* frameNode = reinterpret_cast<FrameNode*>(node);
87     CHECK_NULL_VOID(frameNode);
88     RowModelNG::SetIsReverse(frameNode, value);
89 }
90 
ResetRowReverse(ArkUINodeHandle node)91 void ResetRowReverse(ArkUINodeHandle node)
92 {
93     auto* frameNode = reinterpret_cast<FrameNode*>(node);
94     CHECK_NULL_VOID(frameNode);
95     RowModelNG::SetIsReverse(frameNode, false);
96 }
97 
98 namespace NodeModifier {
GetRowModifier()99 const ArkUIRowModifier* GetRowModifier()
100 {
101     static const ArkUIRowModifier modifier = {
102         SetRowJustifyContent, ResetRowJustifyContent, SetRowAlignItems,
103         ResetRowAlignItems, GetRowJustifyContent, GetRowAlignItems,
104         SetRowSpace, ResetRowSpace,
105         SetRowReverse,
106         ResetRowReverse,
107     };
108     return &modifier;
109 }
110 
GetCJUIRowModifier()111 const CJUIRowModifier* GetCJUIRowModifier()
112 {
113     static const CJUIRowModifier modifier = {
114         SetRowJustifyContent, ResetRowJustifyContent, SetRowAlignItems,
115         ResetRowAlignItems, GetRowJustifyContent, GetRowAlignItems,
116         SetRowSpace, ResetRowSpace,
117     };
118     return &modifier;
119 }
120 }
121 }