1 /*
2  * Copyright (c) 2021 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_EDGE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_BORDER_EDGE_H
18 
19 #include "base/geometry/animatable_dimension.h"
20 #include "base/geometry/dimension.h"
21 #include "base/utils/utils.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/components/common/properties/animatable_color.h"
24 #include "core/components/common/properties/color.h"
25 
26 namespace OHOS::Ace {
27 
28 // A edge of a Border.
29 class ACE_EXPORT BorderEdge final {
30 public:
31     BorderEdge() = default;
32     ~BorderEdge() = default;
33     BorderEdge(const Color& color, const Dimension& width, BorderStyle style);
34 
35     bool HasValue() const;
36 
GetColor()37     const AnimatableColor& GetColor() const
38     {
39         return color_;
40     }
41 
GetBorderStyle()42     BorderStyle GetBorderStyle() const
43     {
44         return style_;
45     }
46 
GetWidth()47     const AnimatableDimension& GetWidth() const
48     {
49         return width_;
50     }
51 
GetWidthInPx(double dipScale)52     double GetWidthInPx(double dipScale) const
53     {
54         return width_.ConvertToPx(dipScale);
55     }
56 
57     bool operator==(const BorderEdge& borderEdge) const
58     {
59         return (borderEdge.GetColor() == color_) && NearEqual(borderEdge.GetWidth().Value(), width_.Value()) &&
60                (borderEdge.GetWidth().Unit() == width_.Unit()) && (borderEdge.GetBorderStyle() == style_);
61     }
62 
63     void SetWidth(const Dimension& width, const AnimationOption& option = AnimationOption())
64     {
65         if (width.Value() < 0.0) {
66             width_.SetValue(0.0);
67             return;
68         }
69         width_ = AnimatableDimension(width, option);
70     }
71 
SetWidth(const AnimatableDimension & width)72     void SetWidth(const AnimatableDimension& width)
73     {
74         if (width.Value() < 0.0) {
75             width_.SetValue(0.0);
76             return;
77         }
78         width_ = width;
79     }
80 
81     void SetColor(const Color& color, const AnimationOption& option = AnimationOption())
82     {
83         color_ = AnimatableColor(color, option);
84     }
85 
SetColor(const AnimatableColor & color)86     void SetColor(const AnimatableColor& color)
87     {
88         color_ = color;
89     }
90 
SetStyle(BorderStyle style)91     void SetStyle(BorderStyle style)
92     {
93         style_ = style;
94     }
95 
IsValid()96     bool IsValid() const
97     {
98         return width_.IsValid();
99     }
100 
ToString()101     std::string ToString() const
102     {
103         return std::string("BorderEdge(")
104             .append("width:")
105             .append(std::to_string(width_.Value()))
106             .append(" color:")
107             .append(std::to_string(color_.GetValue()))
108             .append(" style:")
109             .append(std::to_string((int)style_))
110             .append(")");
111     }
112 
SetContextAndCallback(const WeakPtr<PipelineContext> & context,const RenderNodeAnimationCallback & callback)113     void SetContextAndCallback(const WeakPtr<PipelineContext>& context, const RenderNodeAnimationCallback& callback)
114     {
115         width_.SetContextAndCallback(context, callback);
116         color_.SetContextAndCallback(context, callback);
117     }
118 
119 private:
120     AnimatableColor color_;
121     AnimatableDimension width_;
122     BorderStyle style_ { BorderStyle::NONE };
123 };
124 
125 } // namespace OHOS::Ace
126 
127 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_BORDER_EDGE_H
128