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 
16 #include "waterflow_section_option.h"
17 
18 #include <cstdint>
19 #include <vector>
20 
21 #include "native_type.h"
22 
23 #include "base/utils/utils.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 // 创建函数
OH_ArkUI_WaterFlowSectionOption_Create()30 ArkUI_WaterFlowSectionOption* OH_ArkUI_WaterFlowSectionOption_Create()
31 {
32     // 分配 ArkUI_WaterFlowSectionOption 对象的内存
33     ArkUI_WaterFlowSectionOption* waterFlowSectionOption = new ArkUI_WaterFlowSectionOption();
34     waterFlowSectionOption->sections.resize(1);
35     // 此时 sections 已经是空的 std::vector<Section>,无需额外操作
36     return waterFlowSectionOption;
37 }
38 
39 // 释放函数
OH_ArkUI_WaterFlowSectionOption_Dispose(ArkUI_WaterFlowSectionOption * option)40 void OH_ArkUI_WaterFlowSectionOption_Dispose(ArkUI_WaterFlowSectionOption* option)
41 {
42     if (option != nullptr) {
43         delete option;
44         option = nullptr;
45     }
46 }
47 
OH_ArkUI_WaterFlowSectionOption_SetSize(ArkUI_WaterFlowSectionOption * option,int32_t size)48 void OH_ArkUI_WaterFlowSectionOption_SetSize(ArkUI_WaterFlowSectionOption* option, int32_t size)
49 {
50     CHECK_NULL_VOID(option);
51     if (size < 0) {
52         return;
53     }
54     option->sections.resize(size);
55 }
56 
OH_ArkUI_WaterFlowSectionOption_SetItemCount(ArkUI_WaterFlowSectionOption * option,int32_t index,int32_t itemCount)57 void OH_ArkUI_WaterFlowSectionOption_SetItemCount(
58     ArkUI_WaterFlowSectionOption* option, int32_t index, int32_t itemCount)
59 {
60     CHECK_NULL_VOID(option);
61     if (index < 0) {
62         return;
63     }
64     if (itemCount < 0) {
65         return;
66     }
67     auto size = static_cast<int32_t>(option->sections.size());
68     if (size == 0 || size <= index + 1) {
69         option->sections.resize(static_cast<uint32_t>(index + 1));
70         option->sections[index].itemsCount = itemCount;
71     } else {
72         option->sections[index].itemsCount = itemCount;
73     }
74 }
75 
OH_ArkUI_WaterFlowSectionOption_SetCrossCount(ArkUI_WaterFlowSectionOption * option,int32_t index,int32_t crossCount)76 void OH_ArkUI_WaterFlowSectionOption_SetCrossCount(
77     ArkUI_WaterFlowSectionOption* option, int32_t index, int32_t crossCount)
78 {
79     CHECK_NULL_VOID(option);
80     if (index < 0) {
81         return;
82     }
83     if (crossCount <= 0) {
84         crossCount = 1;
85     }
86     auto size = static_cast<int32_t>(option->sections.size());
87     if (size == 0 || size <= index + 1) {
88         option->sections.resize(static_cast<uint32_t>(index + 1));
89         option->sections[index].crossCount = crossCount;
90     } else {
91         option->sections[index].crossCount = crossCount;
92     }
93 }
94 
OH_ArkUI_WaterFlowSectionOption_SetColumnGap(ArkUI_WaterFlowSectionOption * option,int32_t index,float columnGap)95 void OH_ArkUI_WaterFlowSectionOption_SetColumnGap(ArkUI_WaterFlowSectionOption* option, int32_t index, float columnGap)
96 {
97     CHECK_NULL_VOID(option);
98     if (index < 0) {
99         return;
100     }
101     if (columnGap < 0) {
102         columnGap = 0.0;
103     }
104     auto size = static_cast<int32_t>(option->sections.size());
105     if (size == 0 || size <= index + 1) {
106         option->sections.resize(static_cast<uint32_t>(index + 1));
107         option->sections[index].columnsGap = columnGap;
108     } else {
109         option->sections[index].columnsGap = columnGap;
110     }
111 }
112 
OH_ArkUI_WaterFlowSectionOption_SetRowGap(ArkUI_WaterFlowSectionOption * option,int32_t index,float rowGap)113 void OH_ArkUI_WaterFlowSectionOption_SetRowGap(ArkUI_WaterFlowSectionOption* option, int32_t index, float rowGap)
114 {
115     CHECK_NULL_VOID(option);
116     if (index < 0) {
117         return;
118     }
119     if (rowGap < 0) {
120         rowGap = 0.0;
121     }
122     auto size = static_cast<int32_t>(option->sections.size());
123     if (size == 0 || size <= index + 1) {
124         option->sections.resize(static_cast<uint32_t>(index + 1));
125         option->sections[index].rowsGap = rowGap;
126     } else {
127         option->sections[index].rowsGap = rowGap;
128     }
129 }
130 
OH_ArkUI_WaterFlowSectionOption_SetMargin(ArkUI_WaterFlowSectionOption * option,int32_t index,float marginTop,float marginRight,float marginBottom,float marginLeft)131 void OH_ArkUI_WaterFlowSectionOption_SetMargin(ArkUI_WaterFlowSectionOption* option, int32_t index, float marginTop,
132     float marginRight, float marginBottom, float marginLeft)
133 {
134     CHECK_NULL_VOID(option);
135     if (index < 0) {
136         return;
137     }
138     auto size = static_cast<int32_t>(option->sections.size());
139     if (size == 0 || size <= index + 1) {
140         option->sections.resize(static_cast<uint32_t>(index + 1));
141         option->sections[index].margin[0] = marginTop;
142         option->sections[index].margin[1] = marginRight;
143         option->sections[index].margin[2] = marginBottom;
144         option->sections[index].margin[3] = marginLeft;
145     } else {
146         option->sections[index].margin[0] = marginTop;
147         option->sections[index].margin[1] = marginRight;
148         option->sections[index].margin[2] = marginBottom;
149         option->sections[index].margin[3] = marginLeft;
150     }
151 }
152 
OH_ArkUI_WaterFlowSectionOption_GetSize(ArkUI_WaterFlowSectionOption * option)153 int32_t OH_ArkUI_WaterFlowSectionOption_GetSize(ArkUI_WaterFlowSectionOption* option)
154 {
155     CHECK_NULL_RETURN(option, -1);
156     return static_cast<int32_t>(option->sections.size());
157 }
158 
OH_ArkUI_WaterFlowSectionOption_GetItemCount(ArkUI_WaterFlowSectionOption * option,int32_t index)159 int32_t OH_ArkUI_WaterFlowSectionOption_GetItemCount(ArkUI_WaterFlowSectionOption* option, int32_t index)
160 {
161     CHECK_NULL_RETURN(option, -1);
162     auto size = static_cast<int32_t>(option->sections.size());
163     if (size == 0 || size < index + 1) {
164         return 0;
165     }
166     return option->sections[index].itemsCount;
167 }
168 
OH_ArkUI_WaterFlowSectionOption_GetCrossCount(ArkUI_WaterFlowSectionOption * option,int32_t index)169 int32_t OH_ArkUI_WaterFlowSectionOption_GetCrossCount(ArkUI_WaterFlowSectionOption* option, int32_t index)
170 {
171     CHECK_NULL_RETURN(option, -1);
172     auto size = static_cast<int32_t>(option->sections.size());
173     if (size == 0 || size < index + 1) {
174         return 0;
175     }
176     return option->sections[index].crossCount;
177 }
178 
OH_ArkUI_WaterFlowSectionOption_GetColumnGap(ArkUI_WaterFlowSectionOption * option,int32_t index)179 float OH_ArkUI_WaterFlowSectionOption_GetColumnGap(ArkUI_WaterFlowSectionOption* option, int32_t index)
180 {
181     CHECK_NULL_RETURN(option, 0.0f);
182     auto size = static_cast<int32_t>(option->sections.size());
183     if (size == 0 || size < index + 1) {
184         return 0.0f;
185     }
186     return option->sections[index].columnsGap;
187 }
188 
OH_ArkUI_WaterFlowSectionOption_GetRowGap(ArkUI_WaterFlowSectionOption * option,int32_t index)189 float OH_ArkUI_WaterFlowSectionOption_GetRowGap(ArkUI_WaterFlowSectionOption* option, int32_t index)
190 {
191     CHECK_NULL_RETURN(option, 0.0f);
192     auto size = static_cast<int32_t>(option->sections.size());
193     if (size == 0 || size < index + 1) {
194         return 0.0f;
195     }
196     return option->sections[index].rowsGap;
197 }
198 
OH_ArkUI_WaterFlowSectionOption_GetMargin(ArkUI_WaterFlowSectionOption * option,int32_t index)199 ArkUI_Margin OH_ArkUI_WaterFlowSectionOption_GetMargin(ArkUI_WaterFlowSectionOption* option, int32_t index)
200 {
201     ArkUI_Margin margin = { 0.0, 0.0, 0.0, 0.0 };
202     CHECK_NULL_RETURN(option, margin);
203     auto size = static_cast<int32_t>(option->sections.size());
204     if (size == 0 || size < index + 1) {
205         return margin;
206     }
207     margin.top = option->sections[index].margin[0];
208     margin.right = option->sections[index].margin[1];
209     margin.bottom = option->sections[index].margin[2];
210     margin.left = option->sections[index].margin[3];
211     return margin;
212 }
213 
OH_ArkUI_WaterFlowSectionOption_RegisterGetItemMainSizeCallbackByIndex(ArkUI_WaterFlowSectionOption * option,int32_t index,float (* callback)(int32_t itemIndex))214 void OH_ArkUI_WaterFlowSectionOption_RegisterGetItemMainSizeCallbackByIndex(
215     ArkUI_WaterFlowSectionOption* option, int32_t index, float (*callback)(int32_t itemIndex))
216 {
217     CHECK_NULL_VOID(option);
218     auto size = static_cast<int32_t>(option->sections.size());
219     if (size == 0 || size < index + 1) {
220         return;
221     }
222     option->sections[index].onGetItemMainSizeByIndex = reinterpret_cast<void*>(callback);
223 }
224 
OH_ArkUI_WaterFlowSectionOption_RegisterGetItemMainSizeCallbackByIndexWithUserData(ArkUI_WaterFlowSectionOption * option,int32_t index,void * userData,float (* callback)(int32_t itemIndex,void * extraParams))225 void OH_ArkUI_WaterFlowSectionOption_RegisterGetItemMainSizeCallbackByIndexWithUserData(
226     ArkUI_WaterFlowSectionOption* option, int32_t index, void* userData,
227     float (*callback)(int32_t itemIndex, void* extraParams))
228 {
229     CHECK_NULL_VOID(option);
230     auto size = static_cast<int32_t>(option->sections.size());
231     if (size == 0 || size < index + 1) {
232         return;
233     }
234     option->sections[index].onGetItemMainSizeByIndex = reinterpret_cast<void*>(callback);
235     option->sections[index].userData = userData;
236 }
237 
238 #ifdef __cplusplus
239 };
240 #endif
241