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/node_checkbox_modifier.h"
16
17 #include "core/components/common/layout/constants.h"
18 #include "core/components_ng/base/frame_node.h"
19 #include "core/components_ng/base/view_abstract.h"
20 #include "core/components_ng/pattern/checkbox/checkbox_model_ng.h"
21 #include "core/interfaces/arkoala/arkoala_api.h"
22 #include "core/pipeline/base/element_register.h"
23 #include "frameworks/core/components/checkable/checkable_theme.h"
24
25 namespace OHOS::Ace::NG {
26 constexpr float CHECK_BOX_MARK_SIZE_INVALID_VALUE = -1.0f;
27 const uint32_t ERROR_UINT_CODE = -1;
28 const float ERROR_FLOAT_CODE = -1.0f;
29 const int32_t ERROR_INT_CODE = -1;
30 static std::string nameValue;
31
SetSelect(ArkUINodeHandle node,ArkUI_Bool isSelected)32 void SetSelect(ArkUINodeHandle node, ArkUI_Bool isSelected)
33 {
34 auto *frameNode = reinterpret_cast<FrameNode *>(node);
35 CHECK_NULL_VOID(frameNode);
36 CheckBoxModelNG::SetSelect(frameNode, static_cast<bool>(isSelected));
37 }
38
SetSelectedColor(ArkUINodeHandle node,ArkUI_Uint32 color)39 void SetSelectedColor(ArkUINodeHandle node, ArkUI_Uint32 color)
40 {
41 auto *frameNode = reinterpret_cast<FrameNode *>(node);
42 CHECK_NULL_VOID(frameNode);
43 CheckBoxModelNG::SetSelectedColor(frameNode, Color(color));
44 }
45
SetUnSelectedColor(ArkUINodeHandle node,ArkUI_Uint32 color)46 void SetUnSelectedColor(ArkUINodeHandle node, ArkUI_Uint32 color)
47 {
48 auto *frameNode = reinterpret_cast<FrameNode *>(node);
49 CHECK_NULL_VOID(frameNode);
50 CheckBoxModelNG::SetUnSelectedColor(frameNode, Color(color));
51 }
52
SetCheckboxWidth(ArkUINodeHandle node,float value,int unit)53 void SetCheckboxWidth(ArkUINodeHandle node, float value, int unit)
54 {
55 auto *frameNode = reinterpret_cast<FrameNode *>(node);
56 CHECK_NULL_VOID(frameNode);
57 Dimension width =
58 Dimension(static_cast<double>(value), static_cast<OHOS::Ace::DimensionUnit>(unit));
59 CheckBoxModelNG::SetWidth(frameNode, width);
60 }
61
SetCheckboxHeight(ArkUINodeHandle node,float value,int unit)62 void SetCheckboxHeight(ArkUINodeHandle node, float value, int unit)
63 {
64 auto *frameNode = reinterpret_cast<FrameNode *>(node);
65 CHECK_NULL_VOID(frameNode);
66 Dimension height =
67 Dimension(static_cast<double>(value), static_cast<OHOS::Ace::DimensionUnit>(unit));
68 CheckBoxModelNG::SetHeight(frameNode, height);
69 }
70
SetMark(ArkUINodeHandle node,uint32_t color,float sizeValue,int sizeUnit,float widthValue,int widthUnit)71 void SetMark(ArkUINodeHandle node, uint32_t color, float sizeValue, int sizeUnit, float widthValue, int widthUnit)
72 {
73 auto *frameNode = reinterpret_cast<FrameNode *>(node);
74 CHECK_NULL_VOID(frameNode);
75 CheckBoxModelNG::SetCheckMarkColor(frameNode, Color(color));
76
77 Dimension size = Dimension(static_cast<double>(sizeValue), static_cast<OHOS::Ace::DimensionUnit>(sizeUnit));
78 CheckBoxModelNG::SetCheckMarkSize(frameNode, size);
79
80 Dimension width = Dimension(static_cast<double>(widthValue), static_cast<OHOS::Ace::DimensionUnit>(widthUnit));
81 CheckBoxModelNG::SetCheckMarkWidth(frameNode, width);
82 }
83
SetCheckboxPadding(ArkUINodeHandle node,const float * values,const int * units,uint32_t length)84 void SetCheckboxPadding(ArkUINodeHandle node, const float* values, const int* units, uint32_t length)
85 {
86 auto* frameNode = reinterpret_cast<FrameNode*>(node);
87 CHECK_NULL_VOID(frameNode);
88 if (length != 4) { // 4 : data length
89 return;
90 }
91 CalcLength topDimen;
92 CalcLength rightDimen;
93 CalcLength bottomDimen;
94 CalcLength leftDimen;
95 topDimen = CalcLength(values[0], static_cast<DimensionUnit>(units[0])); // 0: top Dimension
96 rightDimen = CalcLength(values[1], static_cast<DimensionUnit>(units[1])); // 1: right Dimension
97 bottomDimen = CalcLength(values[2], static_cast<DimensionUnit>(units[2])); // 2: bottom Dimension
98 leftDimen = CalcLength(values[3], static_cast<DimensionUnit>(units[3])); // 3: left Dimension
99 NG::PaddingProperty padding;
100 padding.top = std::optional<CalcLength>(topDimen);
101 padding.bottom = std::optional<CalcLength>(bottomDimen);
102 padding.left = std::optional<CalcLength>(leftDimen);
103 padding.right = std::optional<CalcLength>(rightDimen);
104
105 CheckBoxModelNG::SetPadding(frameNode, padding);
106 }
107
SetCheckboxResponseRegion(ArkUINodeHandle node,const float * values,const int32_t * units,uint32_t length)108 void SetCheckboxResponseRegion(ArkUINodeHandle node, const float* values, const int32_t* units, uint32_t length)
109 {
110 auto* frameNode = reinterpret_cast<FrameNode*>(node);
111 CHECK_NULL_VOID(frameNode);
112 std::vector<DimensionRect> region;
113 uint32_t ARRAY_LENGTH = 4; // 4: dimension length
114 for (uint32_t i = 0; i < length / 4; i++) {
115 CalcDimension xDimen =
116 CalcDimension(values[i * ARRAY_LENGTH], static_cast<DimensionUnit>(units[i * ARRAY_LENGTH]));
117 CalcDimension yDimen =
118 CalcDimension(values[i * ARRAY_LENGTH + 1], static_cast<DimensionUnit>(units[i * ARRAY_LENGTH + 1]));
119 CalcDimension widthDimen = CalcDimension(
120 values[i * ARRAY_LENGTH + 2], static_cast<DimensionUnit>(units[i * ARRAY_LENGTH + 2])); // 2: width value
121 CalcDimension heightDimen = CalcDimension(
122 values[i * ARRAY_LENGTH + 3], static_cast<DimensionUnit>(units[i * ARRAY_LENGTH + 3])); // 3: height value
123 DimensionOffset offsetDimen(xDimen, yDimen);
124 DimensionRect dimenRect(widthDimen, heightDimen, offsetDimen);
125 region.emplace_back(dimenRect);
126 }
127 CheckBoxModelNG::SetResponseRegion(frameNode, region);
128 }
129
ResetCheckboxPadding(ArkUINodeHandle node)130 void ResetCheckboxPadding(ArkUINodeHandle node)
131 {
132 auto* frameNode = reinterpret_cast<FrameNode*>(node);
133 CHECK_NULL_VOID(frameNode);
134 NG::PaddingProperty padding;
135 padding.top = std::optional<CalcLength>(CalcLength(0.0, DimensionUnit::VP));
136 padding.bottom = std::optional<CalcLength>(CalcLength(0.0, DimensionUnit::VP));
137 padding.left = std::optional<CalcLength>(CalcLength(0.0, DimensionUnit::VP));
138 padding.right = std::optional<CalcLength>(CalcLength(0.0, DimensionUnit::VP));
139
140 CheckBoxModelNG::SetPadding(frameNode, padding);
141 }
142
ResetCheckboxResponseRegion(ArkUINodeHandle node)143 void ResetCheckboxResponseRegion(ArkUINodeHandle node) {}
144
ResetSelect(ArkUINodeHandle node)145 void ResetSelect(ArkUINodeHandle node)
146 {
147 auto *frameNode = reinterpret_cast<FrameNode *>(node);
148 CHECK_NULL_VOID(frameNode);
149 CheckBoxModelNG::SetSelect(frameNode, false);
150 }
151
ResetSelectedColor(ArkUINodeHandle node)152 void ResetSelectedColor(ArkUINodeHandle node)
153 {
154 auto *frameNode = reinterpret_cast<FrameNode *>(node);
155 CHECK_NULL_VOID(frameNode);
156
157 auto pipelineContext = frameNode->GetContext();
158 CHECK_NULL_VOID(pipelineContext);
159 auto checkBoxTheme = pipelineContext->GetTheme<CheckboxTheme>();
160 CheckBoxModelNG::SetSelectedColor(frameNode, checkBoxTheme->GetActiveColor());
161 }
162
ResetUnSelectedColor(ArkUINodeHandle node)163 void ResetUnSelectedColor(ArkUINodeHandle node)
164 {
165 auto *frameNode = reinterpret_cast<FrameNode *>(node);
166 CHECK_NULL_VOID(frameNode);
167 auto pipelineContext = frameNode->GetContext();
168 CHECK_NULL_VOID(pipelineContext);
169 auto checkBoxTheme = pipelineContext->GetTheme<CheckboxTheme>();
170 CheckBoxModelNG::SetUnSelectedColor(frameNode, checkBoxTheme->GetInactiveColor());
171 }
172
ResetCheckboxWidth(ArkUINodeHandle node)173 void ResetCheckboxWidth(ArkUINodeHandle node)
174 {
175 auto *frameNode = reinterpret_cast<FrameNode *>(node);
176 CHECK_NULL_VOID(frameNode);
177 auto pipelineContext = frameNode->GetContext();
178 CHECK_NULL_VOID(pipelineContext);
179 auto checkBoxTheme = pipelineContext->GetTheme<CheckboxTheme>();
180 CHECK_NULL_VOID(checkBoxTheme);
181 auto defaultWidth = checkBoxTheme->GetDefaultWidth();
182 auto horizontalPadding = checkBoxTheme->GetHotZoneHorizontalPadding();
183 auto width = defaultWidth - horizontalPadding * 2;
184 CheckBoxModelNG::SetWidth(frameNode, width);
185 }
186
ResetCheckboxHeight(ArkUINodeHandle node)187 void ResetCheckboxHeight(ArkUINodeHandle node)
188 {
189 auto *frameNode = reinterpret_cast<FrameNode *>(node);
190 CHECK_NULL_VOID(frameNode);
191 auto pipelineContext = frameNode->GetContext();
192 CHECK_NULL_VOID(pipelineContext);
193 auto checkBoxTheme = pipelineContext->GetTheme<CheckboxTheme>();
194 CHECK_NULL_VOID(checkBoxTheme);
195 auto defaultHeight = checkBoxTheme->GetDefaultHeight();
196 auto verticalPadding = checkBoxTheme->GetHotZoneVerticalPadding();
197 auto height = defaultHeight - verticalPadding * 2;
198
199 CheckBoxModelNG::SetHeight(frameNode, height);
200 }
201
ResetMark(ArkUINodeHandle node)202 void ResetMark(ArkUINodeHandle node)
203 {
204 auto *frameNode = reinterpret_cast<FrameNode *>(node);
205 CHECK_NULL_VOID(frameNode);
206 auto pipelineContext = frameNode->GetContext();
207 CHECK_NULL_VOID(pipelineContext);
208 auto checkBoxTheme = pipelineContext->GetTheme<CheckboxTheme>();
209
210 CheckBoxModelNG::SetCheckMarkColor(frameNode, checkBoxTheme->GetPointColor());
211 CheckBoxModelNG::SetCheckMarkSize(
212 frameNode, Dimension(CHECK_BOX_MARK_SIZE_INVALID_VALUE));
213 CheckBoxModelNG::SetCheckMarkWidth(frameNode, checkBoxTheme->GetCheckStroke());
214 }
215
SetCheckboxShape(ArkUINodeHandle node,ArkUI_Int32 value)216 void SetCheckboxShape(ArkUINodeHandle node, ArkUI_Int32 value)
217 {
218 auto *frameNode = reinterpret_cast<FrameNode *>(node);
219 CHECK_NULL_VOID(frameNode);
220 CheckBoxModelNG::SetCheckboxStyle(frameNode, static_cast<CheckBoxStyle>(value));
221 }
222
ResetCheckboxShape(ArkUINodeHandle node)223 void ResetCheckboxShape(ArkUINodeHandle node)
224 {
225 auto *frameNode = reinterpret_cast<FrameNode *>(node);
226 CHECK_NULL_VOID(frameNode);
227 CheckBoxModelNG::SetCheckboxStyle(frameNode, CheckBoxStyle::CIRCULAR_STYLE);
228 }
229
GetSelect(ArkUINodeHandle node)230 ArkUI_Bool GetSelect(ArkUINodeHandle node)
231 {
232 auto *frameNode = reinterpret_cast<FrameNode *>(node);
233 CHECK_NULL_RETURN(frameNode, ERROR_INT_CODE);
234 return static_cast<ArkUI_Bool>(CheckBoxModelNG::GetSelect(frameNode));
235 }
236
GetSelectedColor(ArkUINodeHandle node)237 ArkUI_Uint32 GetSelectedColor(ArkUINodeHandle node)
238 {
239 auto *frameNode = reinterpret_cast<FrameNode *>(node);
240 CHECK_NULL_RETURN(frameNode, ERROR_UINT_CODE);
241 return CheckBoxModelNG::GetSelectedColor(frameNode).GetValue();
242 }
243
GetUnSelectedColor(ArkUINodeHandle node)244 ArkUI_Uint32 GetUnSelectedColor(ArkUINodeHandle node)
245 {
246 auto *frameNode = reinterpret_cast<FrameNode *>(node);
247 CHECK_NULL_RETURN(frameNode, ERROR_UINT_CODE);
248 return CheckBoxModelNG::GetUnSelectedColor(frameNode).GetValue();
249 }
250
GetCheckMarkColor(ArkUINodeHandle node)251 ArkUI_Uint32 GetCheckMarkColor(ArkUINodeHandle node)
252 {
253 auto *frameNode = reinterpret_cast<FrameNode *>(node);
254 CHECK_NULL_RETURN(frameNode, ERROR_UINT_CODE);
255 return CheckBoxModelNG::GetCheckMarkColor(frameNode).GetValue();
256 }
257
GetCheckMarkSize(ArkUINodeHandle node)258 ArkUI_Float64 GetCheckMarkSize(ArkUINodeHandle node)
259 {
260 auto *frameNode = reinterpret_cast<FrameNode *>(node);
261 CHECK_NULL_RETURN(frameNode, ERROR_FLOAT_CODE);
262 return CheckBoxModelNG::GetCheckMarkSize(frameNode).Value();
263 }
264
GetCheckMarkWidth(ArkUINodeHandle node)265 ArkUI_Float64 GetCheckMarkWidth(ArkUINodeHandle node)
266 {
267 auto *frameNode = reinterpret_cast<FrameNode *>(node);
268 CHECK_NULL_RETURN(frameNode, ERROR_FLOAT_CODE);
269 return CheckBoxModelNG::GetCheckMarkWidth(frameNode).Value();
270 }
271
GetCheckboxShape(ArkUINodeHandle node)272 ArkUI_Int32 GetCheckboxShape(ArkUINodeHandle node)
273 {
274 auto *frameNode = reinterpret_cast<FrameNode *>(node);
275 CHECK_NULL_RETURN(frameNode, ERROR_INT_CODE);
276 return static_cast<ArkUI_Int32>(CheckBoxModelNG::GetCheckboxStyle(frameNode));
277 }
278
SetCheckboxName(ArkUINodeHandle node,ArkUI_CharPtr name)279 void SetCheckboxName(ArkUINodeHandle node, ArkUI_CharPtr name)
280 {
281 CHECK_NULL_VOID(name);
282 auto* frameNode = reinterpret_cast<FrameNode*>(node);
283 CHECK_NULL_VOID(frameNode);
284 CheckBoxModelNG::SetCheckboxName(frameNode, std::string(name));
285 }
286
SetCheckboxGroup(ArkUINodeHandle node,ArkUI_CharPtr group)287 void SetCheckboxGroup(ArkUINodeHandle node, ArkUI_CharPtr group)
288 {
289 CHECK_NULL_VOID(group);
290 auto* frameNode = reinterpret_cast<FrameNode*>(node);
291 CHECK_NULL_VOID(frameNode);
292 CheckBoxModelNG::SetCheckboxGroup(frameNode, std::string(group));
293 }
294
GetCheckboxName(ArkUINodeHandle node)295 ArkUI_CharPtr GetCheckboxName(ArkUINodeHandle node)
296 {
297 auto *frameNode = reinterpret_cast<FrameNode *>(node);
298 CHECK_NULL_RETURN(frameNode, "");
299 nameValue = CheckBoxModelNG::GetCheckboxName(frameNode);
300 return nameValue.c_str();
301 }
302
GetCheckboxGroup(ArkUINodeHandle node)303 ArkUI_CharPtr GetCheckboxGroup(ArkUINodeHandle node)
304 {
305 auto *frameNode = reinterpret_cast<FrameNode *>(node);
306 CHECK_NULL_RETURN(frameNode, "");
307 nameValue = CheckBoxModelNG::GetCheckboxGroup(frameNode);
308 return nameValue.c_str();
309 }
310
311 namespace NodeModifier {
GetCheckboxModifier()312 const ArkUICheckboxModifier *GetCheckboxModifier()
313 {
314 static const ArkUICheckboxModifier modifier = { SetSelect, SetSelectedColor,
315 SetUnSelectedColor, SetCheckboxWidth, SetCheckboxHeight, SetMark, SetCheckboxPadding,
316 SetCheckboxResponseRegion, ResetSelect, ResetSelectedColor,
317 ResetUnSelectedColor, ResetCheckboxWidth, ResetCheckboxHeight, ResetMark, SetCheckboxShape,
318 ResetCheckboxShape, ResetCheckboxPadding, ResetCheckboxResponseRegion,
319 GetSelect, GetSelectedColor, GetUnSelectedColor, GetCheckMarkColor, GetCheckMarkSize, GetCheckMarkWidth,
320 GetCheckboxShape, SetCheckboxName, SetCheckboxGroup, GetCheckboxName, GetCheckboxGroup };
321 return &modifier;
322 }
323
GetCJUICheckboxModifier()324 const CJUICheckboxModifier* GetCJUICheckboxModifier()
325 {
326 static const CJUICheckboxModifier modifier = { SetSelect, SetSelectedColor, SetUnSelectedColor, SetCheckboxWidth,
327 SetCheckboxHeight, SetMark, SetCheckboxPadding, SetCheckboxResponseRegion, ResetSelect, ResetSelectedColor,
328 ResetUnSelectedColor, ResetCheckboxWidth, ResetCheckboxHeight, ResetMark, SetCheckboxShape, ResetCheckboxShape,
329 ResetCheckboxPadding, ResetCheckboxResponseRegion, GetSelect, GetSelectedColor, GetUnSelectedColor,
330 GetCheckMarkColor, GetCheckMarkSize, GetCheckMarkWidth, GetCheckboxShape, SetCheckboxName, SetCheckboxGroup,
331 GetCheckboxName, GetCheckboxGroup };
332 return &modifier;
333 }
334
SetCheckboxChange(ArkUINodeHandle node,void * extraParam)335 void SetCheckboxChange(ArkUINodeHandle node, void* extraParam)
336 {
337 auto* frameNode = reinterpret_cast<FrameNode*>(node);
338 CHECK_NULL_VOID(frameNode);
339 auto onEvent = [node, extraParam](const bool value) {
340 ArkUINodeEvent event;
341 event.kind = COMPONENT_ASYNC_EVENT;
342 event.extraParam = reinterpret_cast<intptr_t>(extraParam);
343 event.componentAsyncEvent.subKind = ON_CHECKBOX_CHANGE;
344 event.componentAsyncEvent.data[0].i32 = static_cast<int>(value);
345 SendArkUIAsyncEvent(&event);
346 };
347 CheckBoxModelNG::SetOnChange(frameNode, std::move(onEvent));
348 }
349
350 } // namespace NodeModifier
351 } // namespace OHOS::Ace::NG
352