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_BASE_PROPERTIES_BORDER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_BORDER_H
18 
19 #include "base/geometry/offset.h"
20 #include "base/geometry/size.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/common/properties/border_edge.h"
23 #include "core/components/common/properties/border_image_edge.h"
24 #include "core/components/common/properties/radius.h"
25 
26 namespace OHOS::Ace {
27 
28 // Border of a box, contains four borderEdges: left, top, right, bottom.
29 // And four radius: topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius.
30 // Each borderEdge is a BorderEdge object and each radius is a Radius object.
31 class ACE_EXPORT Border final {
32 public:
33     Border() = default;
Border(const BorderEdge & edge)34     explicit Border(const BorderEdge& edge) : Border(edge, edge, edge, edge) {}
35 
Border(const BorderImageEdge & edge)36     explicit Border(const BorderImageEdge& edge) : Border(edge, edge, edge, edge) {}
37 
Border(const BorderEdge & edge,const Radius & radius)38     Border(const BorderEdge& edge, const Radius& radius) : Border(edge, edge, edge, edge)
39     {
40         SetBorderRadius(radius);
41     }
42     Border(const BorderEdge& left, const BorderEdge& top, const BorderEdge& right, const BorderEdge& bottom);
43 
44     Border(const BorderImageEdge& leftImage, const BorderImageEdge& topImage,
45         const BorderImageEdge& rightImage, const BorderImageEdge& bottomImage);
46 
47     ~Border() = default;
48 
49     bool IsAllEqual() const;
50     bool HasValue() const;
51     bool HasRadius() const;
52     Offset GetOffset(double dipScale) const;
53     double HorizontalWidth(double dipScale) const;
54     double VerticalWidth(double dipScale) const;
55     Size GetLayoutSize(double dipScale) const;
56     BorderEdge GetValidEdge() const;
57 
IsValid()58     bool IsValid() const
59     {
60         return left_.IsValid() && right_.IsValid() && bottom_.IsValid() && top_.IsValid() && topLeftRadius_.IsValid() &&
61                bottomLeftRadius_.IsValid() && topRightRadius_.IsValid() && bottomRightRadius_.IsValid();
62     }
BorderImageIsValid()63     bool BorderImageIsValid() const
64     {
65         return borderImageLeft_.IsValid() && borderImageRight_.IsValid() &&
66                borderImageBottom_.IsValid() && borderImageTop_.IsValid();
67     }
68 
IsAllSolidStyle()69     bool IsAllSolidStyle() const
70     {
71         return left_.GetBorderStyle() == BorderStyle::SOLID && bottom_.GetBorderStyle() == BorderStyle::SOLID &&
72                right_.GetBorderStyle() == BorderStyle::SOLID && top_.GetBorderStyle() == BorderStyle::SOLID;
73     }
74 
SetBorderRadius(const Radius & radius)75     void SetBorderRadius(const Radius& radius)
76     {
77         topLeftRadius_ = radius;
78         topRightRadius_ = radius;
79         bottomLeftRadius_ = radius;
80         bottomRightRadius_ = radius;
81     }
82 
SetBorderEdge(const BorderEdge & borderEdge)83     void SetBorderEdge(const BorderEdge& borderEdge)
84     {
85         top_ = borderEdge;
86         left_ = borderEdge;
87         right_ = borderEdge;
88         bottom_ = borderEdge;
89     }
90 
SetBorderImageEdge(const BorderImageEdge & borderImageEdge)91     void SetBorderImageEdge(const BorderImageEdge& borderImageEdge)
92     {
93         borderImageLeft_ = borderImageEdge;
94         borderImageTop_ = borderImageEdge;
95         borderImageRight_ = borderImageEdge;
96         borderImageBottom_ = borderImageEdge;
97     }
98 
SetTopLeftRadius(const Radius & topLeftRadius)99     void SetTopLeftRadius(const Radius& topLeftRadius)
100     {
101         topLeftRadius_ = topLeftRadius;
102     }
103 
SetTopRightRadius(const Radius & topRightRadius)104     void SetTopRightRadius(const Radius& topRightRadius)
105     {
106         topRightRadius_ = topRightRadius;
107     }
108 
SetBottomLeftRadius(const Radius & bottomLeftRadius)109     void SetBottomLeftRadius(const Radius& bottomLeftRadius)
110     {
111         bottomLeftRadius_ = bottomLeftRadius;
112     }
113 
SetBottomRightRadius(const Radius & bottomRightRadius)114     void SetBottomRightRadius(const Radius& bottomRightRadius)
115     {
116         bottomRightRadius_ = bottomRightRadius;
117     }
118 
TopLeftRadius()119     const Radius& TopLeftRadius() const
120     {
121         return topLeftRadius_;
122     }
123 
TopRightRadius()124     const Radius& TopRightRadius() const
125     {
126         return topRightRadius_;
127     }
128 
BottomLeftRadius()129     const Radius& BottomLeftRadius() const
130     {
131         return bottomLeftRadius_;
132     }
133 
BottomRightRadius()134     const Radius& BottomRightRadius() const
135     {
136         return bottomRightRadius_;
137     }
138 
Left()139     const BorderEdge& Left() const
140     {
141         return left_;
142     }
143 
Top()144     const BorderEdge& Top() const
145     {
146         return top_;
147     }
148 
Right()149     const BorderEdge& Right() const
150     {
151         return right_;
152     }
153 
Bottom()154     const BorderEdge& Bottom() const
155     {
156         return bottom_;
157     }
158 
159     bool operator==(const Border& border) const
160     {
161         return (border.Left() == left_) && (border.Top() == top_) && (border.Right() == right_) &&
162                (border.Bottom() == bottom_);
163     }
164 
SetLeftEdge(const BorderEdge & edge)165     void SetLeftEdge(const BorderEdge& edge)
166     {
167         left_ = edge;
168     }
169 
SetTopEdge(const BorderEdge & edge)170     void SetTopEdge(const BorderEdge& edge)
171     {
172         top_ = edge;
173     }
174 
SetRightEdge(const BorderEdge & edge)175     void SetRightEdge(const BorderEdge& edge)
176     {
177         right_ = edge;
178     }
179 
SetBottomEdge(const BorderEdge & edge)180     void SetBottomEdge(const BorderEdge& edge)
181     {
182         bottom_ = edge;
183     }
184 
185     void SetWidth(const Dimension& width, const AnimationOption& option = AnimationOption())
186     {
187         SetLeftWidth(width, option);
188         SetTopWidth(width, option);
189         SetRightWidth(width, option);
190         SetBottomWidth(width, option);
191     }
192 
193     void SetLeftWidth(const Dimension& width, const AnimationOption& option = AnimationOption())
194     {
195         left_.SetWidth(width, option);
196     }
197 
198     void SetTopWidth(const Dimension& width, const AnimationOption& option = AnimationOption())
199     {
200         top_.SetWidth(width, option);
201     }
202 
203     void SetRightWidth(const Dimension& width, const AnimationOption& option = AnimationOption())
204     {
205         right_.SetWidth(width, option);
206     }
207 
208     void SetBottomWidth(const Dimension& width, const AnimationOption& option = AnimationOption())
209     {
210         bottom_.SetWidth(width, option);
211     }
212 
SetStyle(BorderStyle style)213     void SetStyle(BorderStyle style)
214     {
215         SetLeftStyle(style);
216         SetTopStyle(style);
217         SetRightStyle(style);
218         SetBottomStyle(style);
219     }
220 
SetLeftStyle(BorderStyle style)221     void SetLeftStyle(BorderStyle style)
222     {
223         left_.SetStyle(style);
224     }
225 
SetTopStyle(BorderStyle style)226     void SetTopStyle(BorderStyle style)
227     {
228         top_.SetStyle(style);
229     }
230 
SetRightStyle(BorderStyle style)231     void SetRightStyle(BorderStyle style)
232     {
233         right_.SetStyle(style);
234     }
235 
SetBottomStyle(BorderStyle style)236     void SetBottomStyle(BorderStyle style)
237     {
238         bottom_.SetStyle(style);
239     }
240 
241     void SetColor(const Color& color, const AnimationOption& option = AnimationOption())
242     {
243         SetLeftColor(color, option);
244         SetTopColor(color, option);
245         SetRightColor(color, option);
246         SetBottomColor(color, option);
247     }
248 
249     void SetLeftColor(const Color& color, const AnimationOption& option = AnimationOption())
250     {
251         left_.SetColor(color, option);
252     }
253 
254     void SetTopColor(const Color& color, const AnimationOption& option = AnimationOption())
255     {
256         top_.SetColor(color, option);
257     }
258 
259     void SetRightColor(const Color& color, const AnimationOption& option = AnimationOption())
260     {
261         right_.SetColor(color, option);
262     }
263 
264     void SetBottomColor(const Color& color, const AnimationOption& option = AnimationOption())
265     {
266         bottom_.SetColor(color, option);
267     }
268 
SetContextAndCallback(const WeakPtr<PipelineContext> & context,const RenderNodeAnimationCallback & callback)269     void SetContextAndCallback(const WeakPtr<PipelineContext>& context, const RenderNodeAnimationCallback& callback)
270     {
271         left_.SetContextAndCallback(context, callback);
272         top_.SetContextAndCallback(context, callback);
273         right_.SetContextAndCallback(context, callback);
274         bottom_.SetContextAndCallback(context, callback);
275 
276         topLeftRadius_.SetContextAndCallback(context, callback);
277         topRightRadius_.SetContextAndCallback(context, callback);
278         bottomLeftRadius_.SetContextAndCallback(context, callback);
279         bottomRightRadius_.SetContextAndCallback(context, callback);
280     }
281 
SetBorderImageWidth(const Dimension & width,BorderImageDirection direction)282     void SetBorderImageWidth(const Dimension& width, BorderImageDirection direction)
283     {
284         if (direction == BorderImageDirection::TOP) {
285             borderImageTop_.SetBorderImageWidth(width);
286         } else if (direction == BorderImageDirection::LEFT) {
287             borderImageLeft_.SetBorderImageWidth(width);
288         } else if (direction == BorderImageDirection::RIGHT) {
289             borderImageRight_.SetBorderImageWidth(width);
290         } else if (direction == BorderImageDirection::BOTTOM) {
291             borderImageBottom_.SetBorderImageWidth(width);
292         }
293     }
SetBorderImageSlice(const Dimension & slice,BorderImageDirection direction)294     void SetBorderImageSlice(const Dimension& slice, BorderImageDirection direction)
295     {
296         if (direction == BorderImageDirection::TOP) {
297             borderImageTop_.SetBorderImageSlice(slice);
298         } else if (direction == BorderImageDirection::LEFT) {
299             borderImageLeft_.SetBorderImageSlice(slice);
300         } else if (direction == BorderImageDirection::RIGHT) {
301             borderImageRight_.SetBorderImageSlice(slice);
302         } else if (direction == BorderImageDirection::BOTTOM) {
303             borderImageBottom_.SetBorderImageSlice(slice);
304         }
305     }
SetBorderImageOutset(const Dimension & outset,BorderImageDirection direction)306     void SetBorderImageOutset(const Dimension& outset, BorderImageDirection direction)
307     {
308         if (direction == BorderImageDirection::TOP) {
309             borderImageTop_.SetBorderImageOutset(outset);
310         } else if (direction == BorderImageDirection::LEFT) {
311             borderImageLeft_.SetBorderImageOutset(outset);
312         } else if (direction == BorderImageDirection::RIGHT) {
313             borderImageRight_.SetBorderImageOutset(outset);
314         } else if (direction == BorderImageDirection::BOTTOM) {
315             borderImageBottom_.SetBorderImageOutset(outset);
316         }
317     }
318 
SetBorderImageRepeat(BorderImageRepeat repeat)319     void SetBorderImageRepeat(BorderImageRepeat repeat)
320     {
321         borderImageTop_.SetBorderImageRepeat(repeat);
322         borderImageLeft_.SetBorderImageRepeat(repeat);
323         borderImageRight_.SetBorderImageRepeat(repeat);
324         borderImageBottom_.SetBorderImageRepeat(repeat);
325     }
ImageLeftEdge()326     const BorderImageEdge& ImageLeftEdge() const
327     {
328         return borderImageLeft_;
329     }
330 
ImageTopEdge()331     const BorderImageEdge& ImageTopEdge() const
332     {
333         return borderImageTop_;
334     }
335 
ImageRightEdge()336     const BorderImageEdge& ImageRightEdge() const
337     {
338         return borderImageRight_;
339     }
340 
ImageBottomEdge()341     const BorderImageEdge& ImageBottomEdge() const
342     {
343         return borderImageBottom_;
344     }
345 
346 private:
347     BorderEdge left_;
348     BorderEdge top_;
349     BorderEdge right_;
350     BorderEdge bottom_;
351     BorderImageEdge borderImageLeft_;
352     BorderImageEdge borderImageTop_;
353     BorderImageEdge borderImageRight_;
354     BorderImageEdge borderImageBottom_;
355     Radius topLeftRadius_;
356     Radius topRightRadius_;
357     Radius bottomLeftRadius_;
358     Radius bottomRightRadius_;
359 };
360 
361 } // namespace OHOS::Ace
362 
363 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_BORDER_H
364