1 /*
2  * Copyright (c) 2021-2022 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 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_BOX_COMPONENT_HELPER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_BOX_COMPONENT_HELPER_H
18 
19 #include "core/components/common/properties/color.h"
20 #include "core/components/common/properties/decoration.h"
21 
22 // Helper class for updating of the attributes for Box Component
23 // Used by RenderBox and by JSViewAbstract
24 
25 namespace OHOS::Ace {
26 class BoxComponentHelper {
27 public:
28     static void SetBorderColor(
29         const RefPtr<Decoration> decoration, const Color& color, const AnimationOption& option = AnimationOption())
30     {
31         if (!decoration) {
32             return;
33         }
34         Border border = decoration->GetBorder();
35         border.SetColor(color);
36         decoration->SetBorder(border);
37     }
38 
39     static void SetBorderColor(const RefPtr<Decoration> decoration, const Color& colorLeft, const Color& colorRight,
40         const Color& colorTop, const Color& colorBottom, const AnimationOption& option = AnimationOption())
41     {
42         if (!decoration) {
43             return;
44         }
45         Border border = decoration->GetBorder();
46         border.SetLeftColor(colorLeft, option);
47         border.SetRightColor(colorRight, option);
48         border.SetTopColor(colorTop, option);
49         border.SetBottomColor(colorBottom, option);
50         decoration->SetBorder(border);
51     }
52 
GetBorderColor(const RefPtr<Decoration> decoration)53     static Color GetBorderColor(const RefPtr<Decoration> decoration)
54     {
55         if (decoration == nullptr) {
56             return Color();
57         }
58         return decoration->GetBorder().Left().GetColor();
59     }
60 
GetBorderColorLeft(const RefPtr<Decoration> decoration)61     static Color GetBorderColorLeft(const RefPtr<Decoration> decoration)
62     {
63         if (decoration == nullptr) {
64             return Color();
65         }
66         return decoration->GetBorder().Left().GetColor();
67     }
68 
GetBorderColorRight(const RefPtr<Decoration> decoration)69     static Color GetBorderColorRight(const RefPtr<Decoration> decoration)
70     {
71         if (decoration == nullptr) {
72             return Color();
73         }
74         return decoration->GetBorder().Right().GetColor();
75     }
76 
GetBorderColorTop(const RefPtr<Decoration> decoration)77     static Color GetBorderColorTop(const RefPtr<Decoration> decoration)
78     {
79         if (decoration == nullptr) {
80             return Color();
81         }
82         return decoration->GetBorder().Top().GetColor();
83     }
84 
GetBorderColorBottom(const RefPtr<Decoration> decoration)85     static Color GetBorderColorBottom(const RefPtr<Decoration> decoration)
86     {
87         if (decoration == nullptr) {
88             return Color();
89         }
90         return decoration->GetBorder().Bottom().GetColor();
91     }
92 
93     static void SetBorderRadius(
94         const RefPtr<Decoration> decoration, const Dimension& radius, const AnimationOption& option = AnimationOption())
95     {
96         if (!decoration) {
97             return;
98         }
99         Border border = decoration->GetBorder();
100         border.SetBorderRadius(Radius(AnimatableDimension(radius, option)));
101         decoration->SetBorder(border);
102     }
103 
104     static void SetBorderRadius(const RefPtr<Decoration> decoration, const Dimension& radiusTopLeft,
105         const Dimension& radiusTopRight, const Dimension& radiusBottomLeft, const Dimension& radiusBottomRight,
106         const AnimationOption& option = AnimationOption())
107     {
108         if (!decoration) {
109             return;
110         }
111         Border border = decoration->GetBorder();
112         border.SetTopLeftRadius(Radius(AnimatableDimension(radiusTopLeft, option)));
113         border.SetTopRightRadius(Radius(AnimatableDimension(radiusTopRight, option)));
114         border.SetBottomLeftRadius(Radius(AnimatableDimension(radiusBottomLeft, option)));
115         border.SetBottomRightRadius(Radius(AnimatableDimension(radiusBottomRight, option)));
116         decoration->SetBorder(border);
117     }
118 
GetBorderRadius(const RefPtr<Decoration> decoration)119     static Radius GetBorderRadius(const RefPtr<Decoration> decoration)
120     {
121         if (decoration == nullptr) {
122             return Radius(0.0);
123         }
124         Border border = decoration->GetBorder();
125         if (!border.HasRadius()) {
126             return Radius(0.0);
127         }
128         return border.TopLeftRadius();
129     }
130 
GetBorderRadiusTopLeft(const RefPtr<Decoration> decoration)131     static Radius GetBorderRadiusTopLeft(const RefPtr<Decoration> decoration)
132     {
133         if (decoration == nullptr) {
134             return Radius(0.0);
135         }
136         Border border = decoration->GetBorder();
137         if (!border.HasRadius()) {
138             return Radius(0.0);
139         }
140         return border.TopLeftRadius();
141     }
142 
GetBorderRadiusTopRight(const RefPtr<Decoration> decoration)143     static Radius GetBorderRadiusTopRight(const RefPtr<Decoration> decoration)
144     {
145         if (decoration == nullptr) {
146             return Radius(0.0);
147         }
148         Border border = decoration->GetBorder();
149         if (!border.HasRadius()) {
150             return Radius(0.0);
151         }
152         return border.TopRightRadius();
153     }
154 
GetBorderRadiusBottomLeft(const RefPtr<Decoration> decoration)155     static Radius GetBorderRadiusBottomLeft(const RefPtr<Decoration> decoration)
156     {
157         if (decoration == nullptr) {
158             return Radius(0.0);
159         }
160         Border border = decoration->GetBorder();
161         if (!border.HasRadius()) {
162             return Radius(0.0);
163         }
164         return border.BottomLeftRadius();
165     }
166 
GetBorderRadiusBottomRight(const RefPtr<Decoration> decoration)167     static Radius GetBorderRadiusBottomRight(const RefPtr<Decoration> decoration)
168     {
169         if (decoration == nullptr) {
170             return Radius(0.0);
171         }
172         Border border = decoration->GetBorder();
173         if (!border.HasRadius()) {
174             return Radius(0.0);
175         }
176         return border.BottomRightRadius();
177     }
178 
SetBorderStyle(const RefPtr<Decoration> decoration,const BorderStyle & style)179     static void SetBorderStyle(const RefPtr<Decoration> decoration, const BorderStyle& style)
180     {
181         if (!decoration) {
182             return;
183         }
184         Border border = decoration->GetBorder();
185         border.SetStyle(style);
186         decoration->SetBorder(border);
187     }
188 
SetBorderStyle(const RefPtr<Decoration> decoration,const BorderStyle & styleLeft,const BorderStyle & styleRight,const BorderStyle & styleTop,const BorderStyle & styleBottom)189     static void SetBorderStyle(const RefPtr<Decoration> decoration, const BorderStyle& styleLeft,
190         const BorderStyle& styleRight, const BorderStyle& styleTop, const BorderStyle& styleBottom)
191     {
192         if (!decoration) {
193             return;
194         }
195         Border border = decoration->GetBorder();
196         border.SetLeftStyle(styleLeft);
197         border.SetRightStyle(styleRight);
198         border.SetTopStyle(styleTop);
199         border.SetBottomStyle(styleBottom);
200         decoration->SetBorder(border);
201     }
202 
GetBorderStyle(const RefPtr<Decoration> decoration)203     static BorderStyle GetBorderStyle(const RefPtr<Decoration> decoration)
204     {
205         if (decoration == nullptr) {
206             return BorderStyle::NONE;
207         }
208         Border border = decoration->GetBorder();
209         return border.Left().GetBorderStyle();
210     }
211 
GetBorderStyleLeft(const RefPtr<Decoration> decoration)212     static BorderStyle GetBorderStyleLeft(const RefPtr<Decoration> decoration)
213     {
214         if (decoration == nullptr) {
215             return BorderStyle::NONE;
216         }
217         Border border = decoration->GetBorder();
218         return border.Left().GetBorderStyle();
219     }
220 
GetBorderStyleRight(const RefPtr<Decoration> decoration)221     static BorderStyle GetBorderStyleRight(const RefPtr<Decoration> decoration)
222     {
223         if (decoration == nullptr) {
224             return BorderStyle::NONE;
225         }
226         Border border = decoration->GetBorder();
227         return border.Right().GetBorderStyle();
228     }
229 
GetBorderStyleTop(const RefPtr<Decoration> decoration)230     static BorderStyle GetBorderStyleTop(const RefPtr<Decoration> decoration)
231     {
232         if (decoration == nullptr) {
233             return BorderStyle::NONE;
234         }
235         Border border = decoration->GetBorder();
236         return border.Top().GetBorderStyle();
237     }
238 
GetBorderStyleBottom(const RefPtr<Decoration> decoration)239     static BorderStyle GetBorderStyleBottom(const RefPtr<Decoration> decoration)
240     {
241         if (decoration == nullptr) {
242             return BorderStyle::NONE;
243         }
244         Border border = decoration->GetBorder();
245         return border.Bottom().GetBorderStyle();
246     }
247 
248     static void SetBorderWidth(
249         const RefPtr<Decoration> decoration, const Dimension& width, const AnimationOption& option = AnimationOption())
250     {
251         if (!decoration) {
252             return;
253         }
254         Border border = decoration->GetBorder();
255         border.SetWidth(width, option);
256         decoration->SetBorder(border);
257     }
258 
259     static void SetBorderWidth(const RefPtr<Decoration> decoration, const Dimension& left, const Dimension& right,
260         const Dimension& top, const Dimension& bottom, const AnimationOption& option = AnimationOption())
261     {
262         if (!decoration) {
263             return;
264         }
265         Border border = decoration->GetBorder();
266         border.SetLeftWidth(left, option);
267         border.SetRightWidth(right, option);
268         border.SetTopWidth(top, option);
269         border.SetBottomWidth(bottom, option);
270         decoration->SetBorder(border);
271     }
272 
GetBorderWidth(const RefPtr<Decoration> decoration)273     static Dimension GetBorderWidth(const RefPtr<Decoration> decoration)
274     {
275         if (decoration == nullptr) {
276             return Dimension(0);
277         }
278         return decoration->GetBorder().Left().GetWidth();
279     }
280 
GetBorderLeftWidth(const RefPtr<Decoration> decoration)281     static Dimension GetBorderLeftWidth(const RefPtr<Decoration> decoration)
282     {
283         if (decoration == nullptr) {
284             return Dimension(0);
285         }
286         return decoration->GetBorder().Left().GetWidth();
287     }
288 
GetBorderRightWidth(const RefPtr<Decoration> decoration)289     static Dimension GetBorderRightWidth(const RefPtr<Decoration> decoration)
290     {
291         if (decoration == nullptr) {
292             return Dimension(0);
293         }
294         return decoration->GetBorder().Right().GetWidth();
295     }
296 
GetBorderTopWidth(const RefPtr<Decoration> decoration)297     static Dimension GetBorderTopWidth(const RefPtr<Decoration> decoration)
298     {
299         if (decoration == nullptr) {
300             return Dimension(0);
301         }
302         return decoration->GetBorder().Top().GetWidth();
303     }
304 
GetBorderBottomWidth(const RefPtr<Decoration> decoration)305     static Dimension GetBorderBottomWidth(const RefPtr<Decoration> decoration)
306     {
307         if (decoration == nullptr) {
308             return Dimension(0);
309         }
310         return decoration->GetBorder().Bottom().GetWidth();
311     }
312 };
313 
314 } // namespace OHOS::Ace
315 
316 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_BOX_COMPONENT_HELPER_H
317