• 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/rect_modifier.h"
16 
17 #include "core/components_ng/base/frame_node.h"
18 #include "core/components_ng/pattern/pattern.h"
19 #include "core/components_ng/pattern/shape/rect_model_ng.h"
20 
21 namespace OHOS::Ace::NG {
22 namespace {
23 constexpr uint32_t VALID_RADIUS_PAIR_FLAG = 1;
24 const std::vector<std::string> RADIUS_TYPES = { "TopLeft", "TopRight", "BottomRight", "BottomLeft" };
25 } // namespace
SetRectRadiusWidth(ArkUINodeHandle node,ArkUI_Float32 radiusWidthValue,ArkUI_Int32 radiusWidthUnit,void * resObjPtr)26 void SetRectRadiusWidth(
27     ArkUINodeHandle node, ArkUI_Float32 radiusWidthValue, ArkUI_Int32 radiusWidthUnit, void* resObjPtr)
28 {
29     auto* frameNode = reinterpret_cast<FrameNode*>(node);
30     CHECK_NULL_VOID(frameNode);
31     auto radiusWidth = CalcDimension(radiusWidthValue, (DimensionUnit)radiusWidthUnit);
32     RectModelNG::SetRadiusWidth(frameNode, radiusWidth);
33     auto pattern = frameNode->GetPattern();
34     CHECK_NULL_VOID(pattern);
35     pattern->UnRegisterResource("RectRadiusWidth");
36     if (SystemProperties::ConfigChangePerform() && resObjPtr) {
37         auto resObj = AceType::Claim(reinterpret_cast<ResourceObject*>(resObjPtr));
38         pattern->RegisterResource<CalcDimension>("RectRadiusWidth", resObj, radiusWidth);
39     }
40 }
41 
ResetRectRadiusWidth(ArkUINodeHandle node)42 void ResetRectRadiusWidth(ArkUINodeHandle node)
43 {
44     auto* frameNode = reinterpret_cast<FrameNode*>(node);
45     CHECK_NULL_VOID(frameNode);
46     CalcDimension defaultDimension;
47     defaultDimension.Reset();
48     RectModelNG::SetRadiusWidth(frameNode, defaultDimension);
49     auto pattern = frameNode->GetPattern();
50     CHECK_NULL_VOID(pattern);
51     pattern->UnRegisterResource("RectRadiusWidth");
52     return;
53 }
54 
SetRectRadiusHeight(ArkUINodeHandle node,ArkUI_Float32 radiusHeightValue,ArkUI_Int32 radiusHeightUnit,void * resObjPtr)55 void SetRectRadiusHeight(
56     ArkUINodeHandle node, ArkUI_Float32 radiusHeightValue, ArkUI_Int32 radiusHeightUnit, void* resObjPtr)
57 {
58     auto* frameNode = reinterpret_cast<FrameNode*>(node);
59     CHECK_NULL_VOID(frameNode);
60     auto radiusHeight = CalcDimension(radiusHeightValue, (DimensionUnit)radiusHeightUnit);
61     RectModelNG::SetRadiusHeight(frameNode, radiusHeight);
62     auto pattern = frameNode->GetPattern();
63     CHECK_NULL_VOID(pattern);
64     pattern->UnRegisterResource("RectRadiusHeight");
65     if (SystemProperties::ConfigChangePerform() && resObjPtr) {
66         auto resObj = AceType::Claim(reinterpret_cast<ResourceObject*>(resObjPtr));
67         pattern->RegisterResource<CalcDimension>("RectRadiusHeight", resObj, radiusHeight);
68     }
69 }
70 
ResetRectRadiusHeight(ArkUINodeHandle node)71 void ResetRectRadiusHeight(ArkUINodeHandle node)
72 {
73     auto* frameNode = reinterpret_cast<FrameNode*>(node);
74     CHECK_NULL_VOID(frameNode);
75     CalcDimension defaultDimension;
76     defaultDimension.Reset();
77     RectModelNG::SetRadiusHeight(frameNode, defaultDimension);
78     auto pattern = frameNode->GetPattern();
79     CHECK_NULL_VOID(pattern);
80     pattern->UnRegisterResource("RectRadiusHeight");
81 }
82 
SetRectRadiusWithArray(ArkUINodeHandle node,ArkUI_Float32 * radiusValues,ArkUI_Int32 * radiusUnits,ArkUI_Uint32 * radiusValidPairs,ArkUI_Uint32 radiusValidPairsSize,void * radiusXResObjArray,void * radiusYResObjArray)83 void SetRectRadiusWithArray(ArkUINodeHandle node, ArkUI_Float32* radiusValues, ArkUI_Int32* radiusUnits,
84     ArkUI_Uint32* radiusValidPairs, ArkUI_Uint32 radiusValidPairsSize, void* radiusXResObjArray,
85     void* radiusYResObjArray)
86 {
87     NG::ResetRectRadiusHeight(node);
88     NG::ResetRectRadiusWidth(node);
89     auto* frameNode = reinterpret_cast<FrameNode*>(node);
90     CHECK_NULL_VOID(frameNode);
91     CHECK_NULL_VOID(radiusValues);
92     CHECK_NULL_VOID(radiusUnits);
93     CHECK_NULL_VOID(radiusValidPairs);
94     auto pattern = frameNode->GetPattern();
95     CHECK_NULL_VOID(pattern);
96     RefPtr<ResourceObject>* radiusXResObjPtr = static_cast<RefPtr<ResourceObject>*>(radiusXResObjArray);
97     RefPtr<ResourceObject>* radiusYResObjPtr = static_cast<RefPtr<ResourceObject>*>(radiusYResObjArray);
98     for (size_t index = 0; index < radiusValidPairsSize; index++) {
99         if (radiusValidPairs[index] == VALID_RADIUS_PAIR_FLAG) {
100             std::string radiusType = RADIUS_TYPES[index];
101             std::string key = std::string("RectRadius") + radiusType;
102             pattern->UnRegisterResource(key);
103             uint32_t xIndex = index * 2;
104             uint32_t yIndex = xIndex + 1;
105             auto radiusX = CalcDimension(radiusValues[xIndex], (DimensionUnit)radiusUnits[xIndex]);
106             auto radiusY = CalcDimension(radiusValues[yIndex], (DimensionUnit)radiusUnits[yIndex]);
107             if (SystemProperties::ConfigChangePerform() && (radiusXResObjPtr[index] || radiusYResObjPtr[index])) {
108                 RectModelNG::SetRadiusValue(
109                     frameNode, radiusX, radiusY, radiusXResObjPtr[index], radiusYResObjPtr[index], index);
110             }
111             RectModelNG::SetRadiusValue(frameNode, radiusX, radiusY, index);
112         }
113     }
114 }
115 
SetRectRadiusWithValue(ArkUINodeHandle node,ArkUI_Float32 radiusValue,ArkUI_Int32 radiusUnit,void * resObjPtr)116 void SetRectRadiusWithValue(ArkUINodeHandle node, ArkUI_Float32 radiusValue, ArkUI_Int32 radiusUnit, void* resObjPtr)
117 {
118     auto* frameNode = reinterpret_cast<FrameNode*>(node);
119     CHECK_NULL_VOID(frameNode);
120     NG::ResetRectRadiusWidth(node);
121     NG::ResetRectRadiusHeight(node);
122     auto radius = CalcDimension(radiusValue, (DimensionUnit)radiusUnit);
123     RectModelNG::SetRadiusWidth(frameNode, radius);
124     RectModelNG::SetRadiusHeight(frameNode, radius);
125     auto pattern = frameNode->GetPattern();
126     CHECK_NULL_VOID(pattern);
127     pattern->UnRegisterResource("RectRadius");
128     if (SystemProperties::ConfigChangePerform() && resObjPtr) {
129         auto resObj = AceType::Claim(reinterpret_cast<ResourceObject*>(resObjPtr));
130         pattern->RegisterResource<CalcDimension>("RectRadius", resObj, radius);
131     }
132 }
133 
ResetRectRadius(ArkUINodeHandle node)134 void ResetRectRadius(ArkUINodeHandle node)
135 {
136     NG::ResetRectRadiusHeight(node);
137     NG::ResetRectRadiusWidth(node);
138     auto* frameNode = reinterpret_cast<FrameNode*>(node);
139     CHECK_NULL_VOID(frameNode);
140     auto pattern = frameNode->GetPattern();
141     CHECK_NULL_VOID(pattern);
142     pattern->UnRegisterResource("RectRadius");
143 }
144 
145 namespace NodeModifier {
GetRectModifier()146 const ArkUIRectModifier* GetRectModifier()
147 {
148     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
149     static const ArkUIRectModifier modifier = {
150         .setRectRadiusWidth = SetRectRadiusWidth,
151         .resetRectRadiusWidth = ResetRectRadiusWidth,
152         .setRectRadiusHeight = SetRectRadiusHeight,
153         .resetRectRadiusHeight = ResetRectRadiusHeight,
154         .setRectRadiusWithArray = SetRectRadiusWithArray,
155         .setRectRadiusWithValue = SetRectRadiusWithValue,
156         .resetRectRadius = ResetRectRadius,
157     };
158     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
159 
160     return &modifier;
161 }
162 
GetCJUIRectModifier()163 const CJUIRectModifier* GetCJUIRectModifier()
164 {
165     CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
166     static const CJUIRectModifier modifier = {
167         .setRectRadiusWidth = SetRectRadiusWidth,
168         .resetRectRadiusWidth = ResetRectRadiusWidth,
169         .setRectRadiusHeight = SetRectRadiusHeight,
170         .resetRectRadiusHeight = ResetRectRadiusHeight,
171         .setRectRadiusWithArray = SetRectRadiusWithArray,
172         .setRectRadiusWithValue = SetRectRadiusWithValue,
173         .resetRectRadius = ResetRectRadius,
174     };
175     CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
176 
177     return &modifier;
178 }
179 }
180 } // namespace OHOS::Ace::NG
181