• 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/data_panel_modifier.h"
16 
17 #include "core/components_ng/pattern/data_panel/data_panel_model_ng.h"
18 #include "core/common/container.h"
19 #include "core/common/resource/resource_parse_utils.h"
20 #include "core/components_ng/pattern/data_panel/data_panel_paint_property.h"
21 
22 namespace OHOS::Ace::NG {
23 namespace {
24 constexpr bool IS_CLOSE_EFFECT = true;
25 constexpr double DEFAULT_STROKE_WIDTH = 24.0;
26 constexpr double DEFAULT_RADIUS = 20.0;
27 constexpr double DEFAULT_OFFSET_X = 5.0;
28 constexpr double DEFAULT_OFFSET_Y = 5.0;
29 const std::string DEFAULT_TRACK_BACKGROUND = "#08182431";
30 }
SetCloseEffect(ArkUINodeHandle node,ArkUI_Bool value)31 void SetCloseEffect(ArkUINodeHandle node, ArkUI_Bool value)
32 {
33     auto* frameNode = reinterpret_cast<FrameNode*>(node);
34     CHECK_NULL_VOID(frameNode);
35     DataPanelModelNG::SetCloseEffect(frameNode, value);
36 }
37 
ResetCloseEffect(ArkUINodeHandle node)38 void ResetCloseEffect(ArkUINodeHandle node)
39 {
40     auto* frameNode = reinterpret_cast<FrameNode*>(node);
41     CHECK_NULL_VOID(frameNode);
42     DataPanelModelNG::SetCloseEffect(frameNode, IS_CLOSE_EFFECT);
43 }
44 
SetDataPanelTrackBackgroundColor(ArkUINodeHandle node,uint32_t value)45 void SetDataPanelTrackBackgroundColor(ArkUINodeHandle node, uint32_t value)
46 {
47     auto* frameNode = reinterpret_cast<FrameNode*>(node);
48     CHECK_NULL_VOID(frameNode);
49     DataPanelModelNG::SetTrackBackground(frameNode, Color(value));
50 }
51 
SetDataPanelTrackBackgroundColorPtr(ArkUINodeHandle node,uint32_t value,void * colorRawPtr)52 void SetDataPanelTrackBackgroundColorPtr(ArkUINodeHandle node, uint32_t value, void* colorRawPtr)
53 {
54     auto* frameNode = reinterpret_cast<FrameNode*>(node);
55     CHECK_NULL_VOID(frameNode);
56     DataPanelModelNG::SetTrackBackground(frameNode, Color(value));
57 
58     if (SystemProperties::ConfigChangePerform()) {
59         auto* color = reinterpret_cast<ResourceObject*>(colorRawPtr);
60         auto colorResObj = AceType::Claim(color);
61         DataPanelModelNG::CreateWithResourceObj(
62             frameNode, DataPanelResourceType::TRACK_BACKGROUND_COLOR, colorResObj);
63     }
64 }
65 
ResetDataPanelTrackBackgroundColor(ArkUINodeHandle node)66 void ResetDataPanelTrackBackgroundColor(ArkUINodeHandle node)
67 {
68     auto* frameNode = reinterpret_cast<FrameNode*>(node);
69     CHECK_NULL_VOID(frameNode);
70     DataPanelModelNG::SetTrackBackground(frameNode, Color::FromString(DEFAULT_TRACK_BACKGROUND));
71     DataPanelModelNG::CreateWithResourceObj(frameNode, DataPanelResourceType::TRACK_BACKGROUND_COLOR, nullptr);
72 }
73 
SetDataPanelStrokeWidth(ArkUINodeHandle node,ArkUI_Float32 value,int32_t unit)74 void SetDataPanelStrokeWidth(ArkUINodeHandle node, ArkUI_Float32 value, int32_t unit)
75 {
76     auto* frameNode = reinterpret_cast<FrameNode*>(node);
77     CHECK_NULL_VOID(frameNode);
78     auto unitEnum = static_cast<OHOS::Ace::DimensionUnit>(unit);
79     DataPanelModelNG::SetStrokeWidth(frameNode, Dimension(value, unitEnum));
80 }
81 
SetDataPanelStrokeWidthPtr(ArkUINodeHandle node,ArkUI_Float32 value,int32_t unit,void * strokeWidthRawPtr)82 void SetDataPanelStrokeWidthPtr(ArkUINodeHandle node, ArkUI_Float32 value, int32_t unit, void* strokeWidthRawPtr)
83 {
84     auto* frameNode = reinterpret_cast<FrameNode*>(node);
85     CHECK_NULL_VOID(frameNode);
86     auto unitEnum = static_cast<OHOS::Ace::DimensionUnit>(unit);
87     DataPanelModelNG::SetStrokeWidth(frameNode, Dimension(value, unitEnum));
88 
89     if (SystemProperties::ConfigChangePerform()) {
90         auto* strokeWidth = reinterpret_cast<ResourceObject*>(strokeWidthRawPtr);
91         auto strokeWidthResObj = AceType::Claim(strokeWidth);
92         DataPanelModelNG::CreateWithResourceObj(frameNode, DataPanelResourceType::STROKE_WIDTH, strokeWidthResObj);
93     }
94 }
95 
ResetDataPanelStrokeWidth(ArkUINodeHandle node)96 void ResetDataPanelStrokeWidth(ArkUINodeHandle node)
97 {
98     auto* frameNode = reinterpret_cast<FrameNode*>(node);
99     CHECK_NULL_VOID(frameNode);
100     DataPanelModelNG::SetStrokeWidth(frameNode, Dimension(DEFAULT_STROKE_WIDTH, DimensionUnit::VP));
101     if (SystemProperties::ConfigChangePerform()) {
102         DataPanelModelNG::CreateWithResourceObj(frameNode, DataPanelResourceType::STROKE_WIDTH, nullptr);
103     }
104 }
105 
ConvertThemeColor(std::vector<OHOS::Ace::NG::Gradient> & colors)106 void ConvertThemeColor(std::vector<OHOS::Ace::NG::Gradient>& colors)
107 {
108     auto container = Container::Current();
109     CHECK_NULL_VOID(container);
110     auto pipelineContext = container->GetPipelineContext();
111     CHECK_NULL_VOID(pipelineContext);
112     auto themeManager = pipelineContext->GetThemeManager();
113     CHECK_NULL_VOID(themeManager);
114     RefPtr<DataPanelTheme> theme = themeManager->GetTheme<DataPanelTheme>();
115     auto themeColors = theme->GetColorsArray();
116     for (const auto& item : themeColors) {
117         OHOS::Ace::NG::Gradient gradient;
118         OHOS::Ace::NG::GradientColor gradientColorStart;
119         gradientColorStart.SetLinearColor(LinearColor(item.first));
120         gradientColorStart.SetDimension(Dimension(0.0));
121         gradient.AddColor(gradientColorStart);
122         OHOS::Ace::NG::GradientColor gradientColorEnd;
123         gradientColorEnd.SetLinearColor(LinearColor(item.second));
124         gradientColorEnd.SetDimension(Dimension(1.0));
125         gradient.AddColor(gradientColorEnd);
126         colors.emplace_back(gradient);
127     }
128 }
129 
SetValueColors(ArkUINodeHandle node,const struct ArkUIGradientType * gradient)130 void SetValueColors(ArkUINodeHandle node, const struct ArkUIGradientType* gradient)
131 {
132     auto* frameNode = reinterpret_cast<FrameNode*>(node);
133     CHECK_NULL_VOID(frameNode);
134     std::vector<OHOS::Ace::NG::Gradient> shadowColors(gradient->length);
135     uint32_t pos = 0;
136     for (uint32_t i = 0; i < gradient->length; i++) {
137         if (gradient->gradientLength[i] == 0) {
138             shadowColors.clear();
139             ConvertThemeColor(shadowColors);
140             break;
141         }
142         OHOS::Ace::NG::Gradient tempGradient;
143         for (uint32_t j = 0; j < gradient->gradientLength[i]; j++, pos++) {
144             OHOS::Ace::NG::GradientColor gradientColor;
145             gradientColor.SetLinearColor(LinearColor(Color(gradient->color[pos])));
146             gradientColor.SetDimension(
147                 Dimension(gradient->offset[pos].number, static_cast<DimensionUnit>(gradient->offset[pos].unit)));
148             tempGradient.AddColor(gradientColor);
149         }
150         shadowColors.at(i) = tempGradient;
151     }
152     DataPanelModelNG::SetValueColors(frameNode, shadowColors);
153 }
154 
SetValueColorsPtr(ArkUINodeHandle node,const struct ArkUIGradientType * gradient,void * colorRawPtr)155 void SetValueColorsPtr(ArkUINodeHandle node, const struct ArkUIGradientType* gradient, void* colorRawPtr)
156 {
157     auto* frameNode = reinterpret_cast<FrameNode*>(node);
158     CHECK_NULL_VOID(frameNode);
159     std::vector<OHOS::Ace::NG::Gradient> shadowColors(gradient->length);
160     std::vector<RefPtr<ResourceObject>> colorVectorObj;
161     if (colorRawPtr) {
162         colorVectorObj = *(static_cast<std::vector<RefPtr<ResourceObject>>*>(colorRawPtr));
163     }
164     uint32_t pos = 0;
165     for (uint32_t i = 0; i < gradient->length; i++) {
166         if (gradient->gradientLength[i] == 0) {
167             shadowColors.clear();
168             ConvertThemeColor(shadowColors);
169             break;
170         }
171         OHOS::Ace::NG::Gradient tempGradient;
172         for (uint32_t j = 0; j < gradient->gradientLength[i]; j++, pos++) {
173             OHOS::Ace::NG::GradientColor gradientColor;
174             gradientColor.SetLinearColor(LinearColor(Color(gradient->color[pos])));
175             gradientColor.SetDimension(
176                 Dimension(gradient->offset[pos].number, static_cast<DimensionUnit>(gradient->offset[pos].unit)));
177             tempGradient.AddColor(gradientColor);
178         }
179 
180         if (i < colorVectorObj.size() && colorVectorObj[i] && SystemProperties::ConfigChangePerform()) {
181             std::string key = "shadow.color." + std::to_string(i);
182             tempGradient.AddResource(
183                 key, colorVectorObj[i], [i](const RefPtr<ResourceObject>& resObj, NG::Gradient& gradient) {
184                     Color color;
185                     ResourceParseUtils::ParseResColor(resObj, color);
186                     gradient.ClearColors();
187                     NG::GradientColor startColor;
188                     startColor.SetLinearColor(LinearColor(color));
189                     startColor.SetDimension(Dimension(0.0));
190                     NG::GradientColor endColor;
191                     endColor.SetLinearColor(LinearColor(color));
192                     endColor.SetDimension(Dimension(1.0));
193                     gradient.AddColor(startColor);
194                     gradient.AddColor(endColor);
195                 });
196         }
197 
198         shadowColors.at(i) = tempGradient;
199     }
200     DataPanelModelNG::SetValueColors(frameNode, shadowColors);
201 }
202 
ResetValueColors(ArkUINodeHandle node)203 void ResetValueColors(ArkUINodeHandle node)
204 {
205     auto* frameNode = reinterpret_cast<FrameNode*>(node);
206     CHECK_NULL_VOID(frameNode);
207     std::vector<OHOS::Ace::NG::Gradient> colors;
208     ConvertThemeColor(colors);
209     DataPanelModelNG::SetValueColors(frameNode, colors);
210 }
211 
SetTrackShadow(ArkUINodeHandle node,const struct ArkUIGradientType * gradient,ArkUI_Float32 radius,ArkUI_Float32 offsetX,ArkUI_Float32 offsetY)212 void SetTrackShadow(ArkUINodeHandle node, const struct ArkUIGradientType* gradient, ArkUI_Float32 radius,
213     ArkUI_Float32 offsetX, ArkUI_Float32 offsetY)
214 {
215     auto* frameNode = reinterpret_cast<FrameNode*>(node);
216     CHECK_NULL_VOID(frameNode);
217     OHOS::Ace::NG::DataPanelShadow shadow;
218 
219     std::vector<OHOS::Ace::NG::Gradient> shadowColors(gradient->length);
220     uint32_t pos = 0;
221     for (uint32_t i = 0; i < gradient->length; i++) {
222         if (gradient->gradientLength[i] == 0) {
223             shadowColors.clear();
224             ConvertThemeColor(shadowColors);
225             break;
226         }
227         OHOS::Ace::NG::Gradient tempGradient;
228         for (uint32_t j = 0; j < gradient->gradientLength[i]; j++, pos++) {
229             OHOS::Ace::NG::GradientColor gradientColor;
230             gradientColor.SetLinearColor(LinearColor(Color(gradient->color[pos])));
231             gradientColor.SetDimension(
232                 Dimension(gradient->offset[pos].number, static_cast<DimensionUnit>(gradient->offset[pos].unit)));
233             tempGradient.AddColor(gradientColor);
234         }
235         shadowColors.at(i) = tempGradient;
236     }
237 
238     shadow.radius = radius;
239     shadow.offsetX = offsetX;
240     shadow.offsetY = offsetY;
241     shadow.colors = shadowColors;
242     DataPanelModelNG::SetShadowOption(frameNode, shadow);
243 }
244 
FillShadowColorsWithResource(const ArkUIGradientType * gradient,const std::vector<RefPtr<ResourceObject>> & colorVectorObj,std::vector<OHOS::Ace::NG::Gradient> & shadowColors)245 void FillShadowColorsWithResource(const ArkUIGradientType* gradient,
246     const std::vector<RefPtr<ResourceObject>>& colorVectorObj, std::vector<OHOS::Ace::NG::Gradient>& shadowColors)
247 {
248     uint32_t pos = 0;
249     for (uint32_t i = 0; i < gradient->length; i++) {
250         if (gradient->gradientLength[i] == 0) {
251             shadowColors.clear();
252             ConvertThemeColor(shadowColors);
253             break;
254         }
255         OHOS::Ace::NG::Gradient tempGradient;
256         for (uint32_t j = 0; j < gradient->gradientLength[i]; j++, pos++) {
257             OHOS::Ace::NG::GradientColor gradientColor;
258             gradientColor.SetLinearColor(LinearColor(Color(gradient->color[pos])));
259             gradientColor.SetDimension(
260                 Dimension(gradient->offset[pos].number, static_cast<DimensionUnit>(gradient->offset[pos].unit)));
261             tempGradient.AddColor(gradientColor);
262         }
263         if (i < colorVectorObj.size() && colorVectorObj[i] && SystemProperties::ConfigChangePerform()) {
264             std::string key = "trackShadow.color." + std::to_string(i);
265             tempGradient.AddResource(
266                 key, colorVectorObj[i], [i](const RefPtr<ResourceObject>& resObj, NG::Gradient& gradient) {
267                     Color color;
268                     ResourceParseUtils::ParseResColor(resObj, color);
269                     gradient.ClearColors();
270                     NG::GradientColor startColor;
271                     startColor.SetLinearColor(LinearColor(color));
272                     startColor.SetDimension(Dimension(0.0));
273                     NG::GradientColor endColor;
274                     endColor.SetLinearColor(LinearColor(color));
275                     endColor.SetDimension(Dimension(1.0));
276                     gradient.AddColor(startColor);
277                     gradient.AddColor(endColor);
278                 });
279         }
280         shadowColors.at(i) = tempGradient;
281     }
282 }
283 
284 
AddShadowResource(const struct ArkUIShadowOptionsResource * shadowRes,NG::DataPanelShadow & shadow)285 void AddShadowResource(const struct ArkUIShadowOptionsResource* shadowRes, NG::DataPanelShadow& shadow)
286 {
287     if (!SystemProperties::ConfigChangePerform()) {
288         return;
289     }
290 
291     auto* radiusObj = reinterpret_cast<ResourceObject*>(shadowRes->radiusRawPtr);
292     auto resObjValue = AceType::Claim(radiusObj);
293     shadow.AddResource("shadow.radius", resObjValue,
294         [](const RefPtr<ResourceObject>& resRadius, NG::DataPanelShadow& shadow) {
295             RefPtr<DataPanelTheme> theme = GetTheme<DataPanelTheme>();
296             double radius = theme->GetTrackShadowRadius().ConvertToVp();
297             ResourceParseUtils::ParseResDouble(resRadius, radius);
298             if (NonPositive(radius)) {
299                 radius = theme->GetTrackShadowRadius().ConvertToVp();
300             }
301             shadow.SetRadius(radius);
302         });
303     if (radiusObj != nullptr) {
304         radiusObj->DecRefCount();
305     }
306 
307     auto* offsetXObj = reinterpret_cast<ResourceObject*>(shadowRes->offsetXRawPtr);
308     auto offsetXObjValue = AceType::Claim(offsetXObj);
309     shadow.AddResource("shadow.offsetX", offsetXObjValue,
310         [](const RefPtr<ResourceObject>& resOffsetX, NG::DataPanelShadow& shadow) {
311             RefPtr<DataPanelTheme> theme = GetTheme<DataPanelTheme>();
312             double val = theme->GetTrackShadowOffsetX().ConvertToVp();
313             ResourceParseUtils::ParseResDouble(resOffsetX, val);
314             if (val < 0.0) {
315                 val = theme->GetTrackShadowOffsetX().ConvertToVp();
316             }
317             shadow.SetOffsetX(val);
318         });
319     if (offsetXObj != nullptr) {
320         offsetXObj->DecRefCount();
321     }
322 
323     auto* offsetYObj = reinterpret_cast<ResourceObject*>(shadowRes->offsetYRawPtr);
324     auto offsetYObjValue = AceType::Claim(offsetYObj);
325     shadow.AddResource("shadow.offsetY", offsetYObjValue,
326         [](const RefPtr<ResourceObject>& resOffsetY, NG::DataPanelShadow& shadow) {
327             RefPtr<DataPanelTheme> theme = GetTheme<DataPanelTheme>();
328             double val = theme->GetTrackShadowOffsetY().ConvertToVp();
329             ResourceParseUtils::ParseResDouble(resOffsetY, val);
330             shadow.SetOffsetY(val);
331         });
332     if (offsetYObj != nullptr) {
333         offsetYObj->DecRefCount();
334     }
335 }
336 
SetTrackShadowPtr(ArkUINodeHandle node,const struct ArkUIGradientType * gradient,const struct ArkUIDatePanelTrackShadow * trackShadow,const struct ArkUIShadowOptionsResource * shadowRes)337 void SetTrackShadowPtr(ArkUINodeHandle node, const struct ArkUIGradientType* gradient,
338     const struct ArkUIDatePanelTrackShadow* trackShadow, const struct ArkUIShadowOptionsResource* shadowRes)
339 {
340     auto* frameNode = reinterpret_cast<FrameNode*>(node);
341     CHECK_NULL_VOID(frameNode);
342     NG::DataPanelShadow shadow;
343 
344     std::vector<NG::Gradient> shadowColors(gradient->length);
345     std::vector<RefPtr<ResourceObject>> colorVectorObj;
346     if (trackShadow->colorRawPtr) {
347         colorVectorObj = *(static_cast<std::vector<RefPtr<ResourceObject>>*>(trackShadow->colorRawPtr));
348     }
349     FillShadowColorsWithResource(gradient, colorVectorObj, shadowColors);
350 
351     AddShadowResource(shadowRes, shadow);
352 
353     shadow.radius = trackShadow->radius;
354     shadow.offsetX = trackShadow->offsetX;
355     shadow.offsetY = trackShadow->offsetY;
356     shadow.colors = shadowColors;
357     DataPanelModelNG::SetShadowOption(frameNode, shadow);
358 }
359 
SetNullTrackShadow(ArkUINodeHandle node)360 void SetNullTrackShadow(ArkUINodeHandle node)
361 {
362     auto* frameNode = reinterpret_cast<FrameNode*>(node);
363     CHECK_NULL_VOID(frameNode);
364     DataPanelShadow shadow;
365     shadow.isShadowVisible = false;
366     DataPanelModelNG::SetShadowOption(frameNode, shadow);
367 }
368 
ResetTrackShadow(ArkUINodeHandle node)369 void ResetTrackShadow(ArkUINodeHandle node)
370 {
371     auto* frameNode = reinterpret_cast<FrameNode*>(node);
372     CHECK_NULL_VOID(frameNode);
373     DataPanelShadow shadow;
374     shadow.isShadowVisible = true;
375     shadow.radius = DEFAULT_RADIUS;
376     shadow.offsetX = DEFAULT_OFFSET_X;
377     shadow.offsetY = DEFAULT_OFFSET_Y;
378     std::vector<OHOS::Ace::NG::Gradient> colors;
379     ConvertThemeColor(colors);
380     shadow.colors = colors;
381     DataPanelModelNG::SetShadowOption(frameNode, shadow);
382 }
383 
384 namespace NodeModifier {
GetDataPanelModifier()385 const ArkUIDataPanelModifier* GetDataPanelModifier()
386 {
387     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
388     static const ArkUIDataPanelModifier modifier = {
389         .setCloseEffect = SetCloseEffect,
390         .resetCloseEffect = ResetCloseEffect,
391         .setDataPanelTrackBackgroundColor = SetDataPanelTrackBackgroundColor,
392         .setDataPanelTrackBackgroundColorPtr = SetDataPanelTrackBackgroundColorPtr,
393         .resetDataPanelTrackBackgroundColor = ResetDataPanelTrackBackgroundColor,
394         .setDataPanelStrokeWidth = SetDataPanelStrokeWidth,
395         .setDataPanelStrokeWidthPtr = SetDataPanelStrokeWidthPtr,
396         .resetDataPanelStrokeWidth = ResetDataPanelStrokeWidth,
397         .setValueColors = SetValueColors,
398         .setValueColorsPtr = SetValueColorsPtr,
399         .resetValueColors = ResetValueColors,
400         .setTrackShadow = SetTrackShadow,
401         .setTrackShadowPtr = SetTrackShadowPtr,
402         .setNullTrackShadow = SetNullTrackShadow,
403         .resetTrackShadow = ResetTrackShadow,
404     };
405     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
406 
407     return &modifier;
408 }
409 
GetCJUIDataPanelModifier()410 const CJUIDataPanelModifier* GetCJUIDataPanelModifier()
411 {
412     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
413     static const CJUIDataPanelModifier modifier = {
414         .setCloseEffect = SetCloseEffect,
415         .resetCloseEffect = ResetCloseEffect,
416         .setDataPanelTrackBackgroundColor = SetDataPanelTrackBackgroundColor,
417         .setDataPanelTrackBackgroundColorPtr = SetDataPanelTrackBackgroundColorPtr,
418         .resetDataPanelTrackBackgroundColor = ResetDataPanelTrackBackgroundColor,
419         .setDataPanelStrokeWidth = SetDataPanelStrokeWidth,
420         .setDataPanelStrokeWidthPtr = SetDataPanelStrokeWidthPtr,
421         .resetDataPanelStrokeWidth = ResetDataPanelStrokeWidth,
422         .setValueColors = SetValueColors,
423         .setValueColorsPtr = SetValueColorsPtr,
424         .resetValueColors = ResetValueColors,
425         .setTrackShadow = SetTrackShadow,
426         .setTrackShadowPtr = SetTrackShadowPtr,
427         .setNullTrackShadow = SetNullTrackShadow,
428         .resetTrackShadow = ResetTrackShadow,
429     };
430     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
431 
432     return &modifier;
433 }
434 }
435 } // namespace OHOS::Ace::NG
436