• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ng/base/view_abstract.h"
18 #include "core/components_ng/pattern/checkbox/checkbox_model_ng.h"
19 #include "core/pipeline_ng/pipeline_context.h"
20 #include "frameworks/core/components/checkable/checkable_theme.h"
21 
22 namespace OHOS::Ace::NG {
23 constexpr float CHECK_BOX_MARK_SIZE_INVALID_VALUE = -1.0f;
24 const uint32_t ERROR_UINT_CODE = -1;
25 const float ERROR_FLOAT_CODE = -1.0f;
26 const int32_t ERROR_INT_CODE = -1;
27 static std::string nameValue;
28 
SetSelect(ArkUINodeHandle node,ArkUI_Bool isSelected)29 void SetSelect(ArkUINodeHandle node, ArkUI_Bool isSelected)
30 {
31     auto *frameNode = reinterpret_cast<FrameNode *>(node);
32     CHECK_NULL_VOID(frameNode);
33     CheckBoxModelNG::SetSelect(frameNode, static_cast<bool>(isSelected));
34 }
35 
SetSelectedColor(ArkUINodeHandle node,ArkUI_Uint32 color)36 void SetSelectedColor(ArkUINodeHandle node, ArkUI_Uint32 color)
37 {
38     auto *frameNode = reinterpret_cast<FrameNode *>(node);
39     CHECK_NULL_VOID(frameNode);
40     CheckBoxModelNG::SetSelectedColor(frameNode, Color(color));
41 }
42 
SetUnSelectedColor(ArkUINodeHandle node,ArkUI_Uint32 color)43 void SetUnSelectedColor(ArkUINodeHandle node, ArkUI_Uint32 color)
44 {
45     auto *frameNode = reinterpret_cast<FrameNode *>(node);
46     CHECK_NULL_VOID(frameNode);
47     CheckBoxModelNG::SetUnSelectedColor(frameNode, Color(color));
48 }
49 
SetCheckboxWidth(ArkUINodeHandle node,float value,int unit)50 void SetCheckboxWidth(ArkUINodeHandle node, float value, int unit)
51 {
52     auto *frameNode = reinterpret_cast<FrameNode *>(node);
53     CHECK_NULL_VOID(frameNode);
54     Dimension width =
55         Dimension(static_cast<double>(value), static_cast<OHOS::Ace::DimensionUnit>(unit));
56     CheckBoxModelNG::SetWidth(frameNode, width);
57 }
58 
SetCheckboxHeight(ArkUINodeHandle node,float value,int unit)59 void SetCheckboxHeight(ArkUINodeHandle node, float value, int unit)
60 {
61     auto *frameNode = reinterpret_cast<FrameNode *>(node);
62     CHECK_NULL_VOID(frameNode);
63     Dimension height =
64         Dimension(static_cast<double>(value), static_cast<OHOS::Ace::DimensionUnit>(unit));
65     CheckBoxModelNG::SetHeight(frameNode, height);
66 }
67 
SetMark(ArkUINodeHandle node,uint32_t color,float sizeValue,int sizeUnit,float widthValue,int widthUnit)68 void SetMark(ArkUINodeHandle node, uint32_t color, float sizeValue, int sizeUnit, float widthValue, int widthUnit)
69 {
70     auto *frameNode = reinterpret_cast<FrameNode *>(node);
71     CHECK_NULL_VOID(frameNode);
72     CheckBoxModelNG::SetCheckMarkColor(frameNode, Color(color));
73 
74     Dimension size = Dimension(static_cast<double>(sizeValue), static_cast<OHOS::Ace::DimensionUnit>(sizeUnit));
75     CheckBoxModelNG::SetCheckMarkSize(frameNode, size);
76 
77     Dimension width = Dimension(static_cast<double>(widthValue), static_cast<OHOS::Ace::DimensionUnit>(widthUnit));
78     CheckBoxModelNG::SetCheckMarkWidth(frameNode, width);
79 }
80 
SetCheckboxPadding(ArkUINodeHandle node,const ArkUI_Float32 * values,const int * units,uint32_t length)81 void SetCheckboxPadding(ArkUINodeHandle node, const ArkUI_Float32* values, const int* units, uint32_t length)
82 {
83     auto* frameNode = reinterpret_cast<FrameNode*>(node);
84     CHECK_NULL_VOID(frameNode);
85     if (length != 4) { // 4 : data length
86         return;
87     }
88     CalcLength topDim;
89     CalcLength rightDim;
90     CalcLength bottomDim;
91     CalcLength leftDim;
92     topDim = CalcLength(values[0], static_cast<DimensionUnit>(units[0])); // 0: top Dimension
93     rightDim = CalcLength(values[1], static_cast<DimensionUnit>(units[1])); // 1: right Dimension
94     bottomDim = CalcLength(values[2], static_cast<DimensionUnit>(units[2])); // 2: bottom Dimension
95     leftDim = CalcLength(values[3], static_cast<DimensionUnit>(units[3])); // 3: left Dimension
96     NG::PaddingProperty padding;
97     padding.top = std::optional<CalcLength>(topDim);
98     padding.bottom = std::optional<CalcLength>(bottomDim);
99     padding.left = std::optional<CalcLength>(leftDim);
100     padding.right = std::optional<CalcLength>(rightDim);
101 
102     CheckBoxModelNG::SetPadding(frameNode, padding);
103 }
104 
SetCheckboxResponseRegion(ArkUINodeHandle node,const ArkUI_Float32 * values,const int32_t * units,uint32_t length)105 void SetCheckboxResponseRegion(ArkUINodeHandle node, const ArkUI_Float32* values, const int32_t* units, uint32_t length)
106 {
107     auto* frameNode = reinterpret_cast<FrameNode*>(node);
108     CHECK_NULL_VOID(frameNode);
109     std::vector<DimensionRect> region;
110     uint32_t ARRAY_LENGTH = 4; // 4: dimension length
111     for (uint32_t i = 0; i < length / 4; i++) {
112         CalcDimension xDimen =
113             CalcDimension(values[i * ARRAY_LENGTH], static_cast<DimensionUnit>(units[i * ARRAY_LENGTH]));
114         CalcDimension yDimen =
115             CalcDimension(values[i * ARRAY_LENGTH + 1], static_cast<DimensionUnit>(units[i * ARRAY_LENGTH + 1]));
116         CalcDimension widthDimen = CalcDimension(
117             values[i * ARRAY_LENGTH + 2], static_cast<DimensionUnit>(units[i * ARRAY_LENGTH + 2])); // 2: width value
118         CalcDimension heightDimen = CalcDimension(
119             values[i * ARRAY_LENGTH + 3], static_cast<DimensionUnit>(units[i * ARRAY_LENGTH + 3])); // 3: height value
120         DimensionOffset offsetDimen(xDimen, yDimen);
121         DimensionRect dimenRect(widthDimen, heightDimen, offsetDimen);
122         region.emplace_back(dimenRect);
123     }
124     CheckBoxModelNG::SetResponseRegion(frameNode, region);
125 }
126 
SetCheckboxOnChange(ArkUINodeHandle node,void * callback)127 void SetCheckboxOnChange(ArkUINodeHandle node, void* callback)
128 {
129     auto* frameNode = reinterpret_cast<FrameNode*>(node);
130     CHECK_NULL_VOID(frameNode);
131     if (callback) {
132         auto onChange = reinterpret_cast<std::function<void(bool)>*>(callback);
133         CheckBoxModelNG::SetOnChange(frameNode, std::move(*onChange));
134     } else {
135         CheckBoxModelNG::SetOnChange(frameNode, nullptr);
136     }
137 }
138 
ResetCheckboxPadding(ArkUINodeHandle node)139 void ResetCheckboxPadding(ArkUINodeHandle node)
140 {
141     auto* frameNode = reinterpret_cast<FrameNode*>(node);
142     CHECK_NULL_VOID(frameNode);
143     NG::PaddingProperty padding;
144     padding.top = std::optional<CalcLength>(CalcLength(0.0, DimensionUnit::VP));
145     padding.bottom = std::optional<CalcLength>(CalcLength(0.0, DimensionUnit::VP));
146     padding.left = std::optional<CalcLength>(CalcLength(0.0, DimensionUnit::VP));
147     padding.right = std::optional<CalcLength>(CalcLength(0.0, DimensionUnit::VP));
148 
149     CheckBoxModelNG::SetPadding(frameNode, padding);
150 }
151 
ResetCheckboxResponseRegion(ArkUINodeHandle node)152 void ResetCheckboxResponseRegion(ArkUINodeHandle node) {}
153 
ResetCheckboxOnChange(ArkUINodeHandle node)154 void ResetCheckboxOnChange(ArkUINodeHandle node)
155 {
156     auto *frameNode = reinterpret_cast<FrameNode *>(node);
157     CHECK_NULL_VOID(frameNode);
158     CheckBoxModelNG::SetOnChange(frameNode, nullptr);
159 }
160 
ResetSelect(ArkUINodeHandle node)161 void ResetSelect(ArkUINodeHandle node)
162 {
163     auto *frameNode = reinterpret_cast<FrameNode *>(node);
164     CHECK_NULL_VOID(frameNode);
165     CheckBoxModelNG::SetSelect(frameNode, false);
166 }
167 
ResetSelectedColor(ArkUINodeHandle node)168 void ResetSelectedColor(ArkUINodeHandle node)
169 {
170     auto *frameNode = reinterpret_cast<FrameNode *>(node);
171     CHECK_NULL_VOID(frameNode);
172     CheckBoxModelNG::ResetSelectedColor(frameNode);
173 }
174 
ResetUnSelectedColor(ArkUINodeHandle node)175 void ResetUnSelectedColor(ArkUINodeHandle node)
176 {
177     auto *frameNode = reinterpret_cast<FrameNode *>(node);
178     CHECK_NULL_VOID(frameNode);
179     CheckBoxModelNG::ResetUnSelectedColor(frameNode);
180 }
181 
ResetCheckboxWidth(ArkUINodeHandle node)182 void ResetCheckboxWidth(ArkUINodeHandle node)
183 {
184     auto *frameNode = reinterpret_cast<FrameNode *>(node);
185     CHECK_NULL_VOID(frameNode);
186     auto pipelineContext = frameNode->GetContext();
187     CHECK_NULL_VOID(pipelineContext);
188     auto checkBoxTheme = pipelineContext->GetTheme<CheckboxTheme>();
189     CHECK_NULL_VOID(checkBoxTheme);
190     auto defaultWidth = checkBoxTheme->GetDefaultWidth();
191     auto horizontalPadding = checkBoxTheme->GetHotZoneHorizontalPadding();
192     auto width = defaultWidth - horizontalPadding * 2;
193     CheckBoxModelNG::SetWidth(frameNode, width);
194 }
195 
ResetCheckboxHeight(ArkUINodeHandle node)196 void ResetCheckboxHeight(ArkUINodeHandle node)
197 {
198     auto *frameNode = reinterpret_cast<FrameNode *>(node);
199     CHECK_NULL_VOID(frameNode);
200     auto pipelineContext = frameNode->GetContext();
201     CHECK_NULL_VOID(pipelineContext);
202     auto checkBoxTheme = pipelineContext->GetTheme<CheckboxTheme>();
203     CHECK_NULL_VOID(checkBoxTheme);
204     auto defaultHeight = checkBoxTheme->GetDefaultHeight();
205     auto verticalPadding = checkBoxTheme->GetHotZoneVerticalPadding();
206     auto height = defaultHeight - verticalPadding * 2;
207 
208     CheckBoxModelNG::SetHeight(frameNode, height);
209 }
210 
ResetMark(ArkUINodeHandle node)211 void ResetMark(ArkUINodeHandle node)
212 {
213     auto *frameNode = reinterpret_cast<FrameNode *>(node);
214     CHECK_NULL_VOID(frameNode);
215     auto pipelineContext = frameNode->GetContext();
216     CHECK_NULL_VOID(pipelineContext);
217     auto checkBoxTheme = pipelineContext->GetTheme<CheckboxTheme>();
218 
219     CheckBoxModelNG::ResetCheckMarkColor(frameNode);
220     CheckBoxModelNG::SetCheckMarkSize(
221         frameNode, Dimension(CHECK_BOX_MARK_SIZE_INVALID_VALUE));
222     CheckBoxModelNG::SetCheckMarkWidth(frameNode, checkBoxTheme->GetCheckStroke());
223 }
224 
SetCheckboxShape(ArkUINodeHandle node,ArkUI_Int32 value)225 void SetCheckboxShape(ArkUINodeHandle node, ArkUI_Int32 value)
226 {
227     auto *frameNode = reinterpret_cast<FrameNode *>(node);
228     CHECK_NULL_VOID(frameNode);
229     CheckBoxModelNG::SetCheckboxStyle(frameNode, static_cast<CheckBoxStyle>(value));
230 }
231 
ResetCheckboxShape(ArkUINodeHandle node)232 void ResetCheckboxShape(ArkUINodeHandle node)
233 {
234     auto *frameNode = reinterpret_cast<FrameNode *>(node);
235     CHECK_NULL_VOID(frameNode);
236     CheckBoxModelNG::SetCheckboxStyle(frameNode, CheckBoxStyle::CIRCULAR_STYLE);
237 }
238 
GetSelect(ArkUINodeHandle node)239 ArkUI_Bool GetSelect(ArkUINodeHandle node)
240 {
241     auto *frameNode = reinterpret_cast<FrameNode *>(node);
242     CHECK_NULL_RETURN(frameNode, ERROR_INT_CODE);
243     return static_cast<ArkUI_Bool>(CheckBoxModelNG::GetSelect(frameNode));
244 }
245 
GetSelectedColor(ArkUINodeHandle node)246 ArkUI_Uint32 GetSelectedColor(ArkUINodeHandle node)
247 {
248     auto *frameNode = reinterpret_cast<FrameNode *>(node);
249     CHECK_NULL_RETURN(frameNode, ERROR_UINT_CODE);
250     return CheckBoxModelNG::GetSelectedColor(frameNode).GetValue();
251 }
252 
GetUnSelectedColor(ArkUINodeHandle node)253 ArkUI_Uint32 GetUnSelectedColor(ArkUINodeHandle node)
254 {
255     auto *frameNode = reinterpret_cast<FrameNode *>(node);
256     CHECK_NULL_RETURN(frameNode, ERROR_UINT_CODE);
257     return CheckBoxModelNG::GetUnSelectedColor(frameNode).GetValue();
258 }
259 
GetCheckMarkColor(ArkUINodeHandle node)260 ArkUI_Uint32 GetCheckMarkColor(ArkUINodeHandle node)
261 {
262     auto *frameNode = reinterpret_cast<FrameNode *>(node);
263     CHECK_NULL_RETURN(frameNode, ERROR_UINT_CODE);
264     return CheckBoxModelNG::GetCheckMarkColor(frameNode).GetValue();
265 }
266 
GetCheckMarkSize(ArkUINodeHandle node)267 ArkUI_Float64 GetCheckMarkSize(ArkUINodeHandle node)
268 {
269     auto *frameNode = reinterpret_cast<FrameNode *>(node);
270     CHECK_NULL_RETURN(frameNode, ERROR_FLOAT_CODE);
271     return CheckBoxModelNG::GetCheckMarkSize(frameNode).Value();
272 }
273 
GetCheckMarkWidth(ArkUINodeHandle node)274 ArkUI_Float64 GetCheckMarkWidth(ArkUINodeHandle node)
275 {
276     auto *frameNode = reinterpret_cast<FrameNode *>(node);
277     CHECK_NULL_RETURN(frameNode, ERROR_FLOAT_CODE);
278     return CheckBoxModelNG::GetCheckMarkWidth(frameNode).Value();
279 }
280 
GetCheckboxShape(ArkUINodeHandle node)281 ArkUI_Int32 GetCheckboxShape(ArkUINodeHandle node)
282 {
283     auto *frameNode = reinterpret_cast<FrameNode *>(node);
284     CHECK_NULL_RETURN(frameNode, ERROR_INT_CODE);
285     return static_cast<ArkUI_Int32>(CheckBoxModelNG::GetCheckboxStyle(frameNode));
286 }
287 
SetCheckboxName(ArkUINodeHandle node,ArkUI_CharPtr name)288 void SetCheckboxName(ArkUINodeHandle node, ArkUI_CharPtr name)
289 {
290     CHECK_NULL_VOID(name);
291     auto* frameNode = reinterpret_cast<FrameNode*>(node);
292     CHECK_NULL_VOID(frameNode);
293     CheckBoxModelNG::SetCheckboxName(frameNode, std::string(name));
294 }
295 
SetCheckboxGroup(ArkUINodeHandle node,ArkUI_CharPtr group)296 void SetCheckboxGroup(ArkUINodeHandle node, ArkUI_CharPtr group)
297 {
298     CHECK_NULL_VOID(group);
299     auto* frameNode = reinterpret_cast<FrameNode*>(node);
300     CHECK_NULL_VOID(frameNode);
301     CheckBoxModelNG::SetCheckboxGroup(frameNode, std::string(group));
302 }
303 
GetCheckboxName(ArkUINodeHandle node)304 ArkUI_CharPtr GetCheckboxName(ArkUINodeHandle node)
305 {
306     auto *frameNode = reinterpret_cast<FrameNode *>(node);
307     CHECK_NULL_RETURN(frameNode, "");
308     nameValue = CheckBoxModelNG::GetCheckboxName(frameNode);
309     return nameValue.c_str();
310 }
311 
GetCheckboxGroup(ArkUINodeHandle node)312 ArkUI_CharPtr GetCheckboxGroup(ArkUINodeHandle node)
313 {
314     auto *frameNode = reinterpret_cast<FrameNode *>(node);
315     CHECK_NULL_RETURN(frameNode, "");
316     nameValue = CheckBoxModelNG::GetCheckboxGroup(frameNode);
317     return nameValue.c_str();
318 }
319 
320 namespace NodeModifier {
GetCheckboxModifier()321 const ArkUICheckboxModifier *GetCheckboxModifier()
322 {
323     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
324     static const ArkUICheckboxModifier modifier = {
325         .setSelect = SetSelect,
326         .setSelectedColor = SetSelectedColor,
327         .setUnSelectedColor = SetUnSelectedColor,
328         .setCheckboxWidth = SetCheckboxWidth,
329         .setCheckboxHeight = SetCheckboxHeight,
330         .setMark = SetMark,
331         .setCheckboxPadding = SetCheckboxPadding,
332         .setCheckboxResponseRegion = SetCheckboxResponseRegion,
333         .setCheckboxOnChange = SetCheckboxOnChange,
334         .resetSelect = ResetSelect,
335         .resetSelectedColor = ResetSelectedColor,
336         .resetUnSelectedColor = ResetUnSelectedColor,
337         .resetCheckboxWidth = ResetCheckboxWidth,
338         .resetCheckboxHeight = ResetCheckboxHeight,
339         .resetMark = ResetMark,
340         .setCheckboxShape = SetCheckboxShape,
341         .resetCheckboxShape = ResetCheckboxShape,
342         .resetCheckboxPadding = ResetCheckboxPadding,
343         .resetCheckboxResponseRegion = ResetCheckboxResponseRegion,
344         .resetCheckboxOnChange = ResetCheckboxOnChange,
345         .getSelect = GetSelect,
346         .getSelectedColor = GetSelectedColor,
347         .getUnSelectedColor = GetUnSelectedColor,
348         .getCheckMarkColor = GetCheckMarkColor,
349         .getCheckMarkSize = GetCheckMarkSize,
350         .getCheckMarkWidth = GetCheckMarkWidth,
351         .getCheckboxShape = GetCheckboxShape,
352         .setCheckboxName = SetCheckboxName,
353         .setCheckboxGroup = SetCheckboxGroup,
354         .getCheckboxName = GetCheckboxName,
355         .getCheckboxGroup = GetCheckboxGroup,
356     };
357     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
358     return &modifier;
359 }
360 
GetCJUICheckboxModifier()361 const CJUICheckboxModifier* GetCJUICheckboxModifier()
362 {
363     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
364     static const CJUICheckboxModifier modifier = {
365         .setSelect = SetSelect,
366         .setSelectedColor = SetSelectedColor,
367         .setUnSelectedColor = SetUnSelectedColor,
368         .setCheckboxWidth = SetCheckboxWidth,
369         .setCheckboxHeight = SetCheckboxHeight,
370         .setMark = SetMark,
371         .setCheckboxPadding = SetCheckboxPadding,
372         .setCheckboxResponseRegion = SetCheckboxResponseRegion,
373         .resetSelect = ResetSelect,
374         .resetSelectedColor = ResetSelectedColor,
375         .resetUnSelectedColor = ResetUnSelectedColor,
376         .resetCheckboxWidth = ResetCheckboxWidth,
377         .resetCheckboxHeight = ResetCheckboxHeight,
378         .resetMark = ResetMark,
379         .setCheckboxShape = SetCheckboxShape,
380         .resetCheckboxShape = ResetCheckboxShape,
381         .resetCheckboxPadding = ResetCheckboxPadding,
382         .resetCheckboxResponseRegion = ResetCheckboxResponseRegion,
383         .getSelect = GetSelect,
384         .getSelectedColor = GetSelectedColor,
385         .getUnSelectedColor = GetUnSelectedColor,
386         .getCheckMarkColor = GetCheckMarkColor,
387         .getCheckMarkSize = GetCheckMarkSize,
388         .getCheckMarkWidth = GetCheckMarkWidth,
389         .getCheckboxShape = GetCheckboxShape,
390         .setCheckboxName = SetCheckboxName,
391         .setCheckboxGroup = SetCheckboxGroup,
392         .getCheckboxName = GetCheckboxName,
393         .getCheckboxGroup = GetCheckboxGroup,
394     };
395     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
396     return &modifier;
397 }
398 
SetCheckboxChange(ArkUINodeHandle node,void * extraParam)399 void SetCheckboxChange(ArkUINodeHandle node, void* extraParam)
400 {
401     auto* frameNode = reinterpret_cast<FrameNode*>(node);
402     CHECK_NULL_VOID(frameNode);
403     auto onEvent = [node, extraParam](const bool value) {
404         ArkUINodeEvent event;
405         event.kind = COMPONENT_ASYNC_EVENT;
406         event.extraParam = reinterpret_cast<intptr_t>(extraParam);
407         event.componentAsyncEvent.subKind = ON_CHECKBOX_CHANGE;
408         event.componentAsyncEvent.data[0].i32 = static_cast<int>(value);
409         SendArkUISyncEvent(&event);
410     };
411     CheckBoxModelNG::SetOnChange(frameNode, std::move(onEvent));
412 }
413 
414 } // namespace NodeModifier
415 } // namespace OHOS::Ace::NG
416