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