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