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 269 void SetContextAndCallback(const WeakPtr<PipelineContext>& context, const RenderNodeAnimationCallback& callback); 270 SetBorderImageWidth(const Dimension & width,BorderImageDirection direction)271 void SetBorderImageWidth(const Dimension& width, BorderImageDirection direction) 272 { 273 if (direction == BorderImageDirection::TOP) { 274 borderImageTop_.SetBorderImageWidth(width); 275 } else if (direction == BorderImageDirection::LEFT) { 276 borderImageLeft_.SetBorderImageWidth(width); 277 } else if (direction == BorderImageDirection::RIGHT) { 278 borderImageRight_.SetBorderImageWidth(width); 279 } else if (direction == BorderImageDirection::BOTTOM) { 280 borderImageBottom_.SetBorderImageWidth(width); 281 } 282 } SetBorderImageSlice(const Dimension & slice,BorderImageDirection direction)283 void SetBorderImageSlice(const Dimension& slice, BorderImageDirection direction) 284 { 285 if (direction == BorderImageDirection::TOP) { 286 borderImageTop_.SetBorderImageSlice(slice); 287 } else if (direction == BorderImageDirection::LEFT) { 288 borderImageLeft_.SetBorderImageSlice(slice); 289 } else if (direction == BorderImageDirection::RIGHT) { 290 borderImageRight_.SetBorderImageSlice(slice); 291 } else if (direction == BorderImageDirection::BOTTOM) { 292 borderImageBottom_.SetBorderImageSlice(slice); 293 } 294 } SetBorderImageOutset(const Dimension & outset,BorderImageDirection direction)295 void SetBorderImageOutset(const Dimension& outset, BorderImageDirection direction) 296 { 297 if (direction == BorderImageDirection::TOP) { 298 borderImageTop_.SetBorderImageOutset(outset); 299 } else if (direction == BorderImageDirection::LEFT) { 300 borderImageLeft_.SetBorderImageOutset(outset); 301 } else if (direction == BorderImageDirection::RIGHT) { 302 borderImageRight_.SetBorderImageOutset(outset); 303 } else if (direction == BorderImageDirection::BOTTOM) { 304 borderImageBottom_.SetBorderImageOutset(outset); 305 } 306 } 307 SetBorderImageRepeat(BorderImageRepeat repeat)308 void SetBorderImageRepeat(BorderImageRepeat repeat) 309 { 310 borderImageTop_.SetBorderImageRepeat(repeat); 311 borderImageLeft_.SetBorderImageRepeat(repeat); 312 borderImageRight_.SetBorderImageRepeat(repeat); 313 borderImageBottom_.SetBorderImageRepeat(repeat); 314 } ImageLeftEdge()315 const BorderImageEdge& ImageLeftEdge() const 316 { 317 return borderImageLeft_; 318 } 319 ImageTopEdge()320 const BorderImageEdge& ImageTopEdge() const 321 { 322 return borderImageTop_; 323 } 324 ImageRightEdge()325 const BorderImageEdge& ImageRightEdge() const 326 { 327 return borderImageRight_; 328 } 329 ImageBottomEdge()330 const BorderImageEdge& ImageBottomEdge() const 331 { 332 return borderImageBottom_; 333 } 334 335 private: 336 BorderEdge left_; 337 BorderEdge top_; 338 BorderEdge right_; 339 BorderEdge bottom_; 340 BorderImageEdge borderImageLeft_; 341 BorderImageEdge borderImageTop_; 342 BorderImageEdge borderImageRight_; 343 BorderImageEdge borderImageBottom_; 344 Radius topLeftRadius_; 345 Radius topRightRadius_; 346 Radius bottomLeftRadius_; 347 Radius bottomRightRadius_; 348 }; 349 350 } // namespace OHOS::Ace 351 352 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_BORDER_H 353