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 
16 #include "safe_area_insets.h"
17 
18 #include <string>
19 
20 namespace OHOS::Ace::NG {
Combine(const SafeAreaInsets & other) const21 SafeAreaInsets SafeAreaInsets::Combine(const SafeAreaInsets& other) const
22 {
23     SafeAreaInsets res;
24     res.left_ = left_.Combine(other.left_);
25     res.top_ = top_.Combine(other.top_);
26     res.right_ = right_.Combine(other.right_);
27     res.bottom_ = bottom_.Combine(other.bottom_);
28     return res;
29 }
30 
31 using Inset = SafeAreaInsets::Inset;
Combine(const Inset & other) const32 Inset SafeAreaInsets::Inset::Combine(const Inset& other) const
33 {
34     Inset res;
35     if (other.IsValid() && this->IsValid()) {
36         res = { .start = std::min(start, other.start), .end = std::max(end, other.end) };
37     } else if (other.IsValid()) {
38         res = other;
39     } else {
40         res = *this;
41     }
42     return res;
43 }
44 
IsValid() const45 bool SafeAreaInsets::IsValid() const
46 {
47     return top_.IsValid() || left_.IsValid() || right_.IsValid() || bottom_.IsValid();
48 }
49 
ToString() const50 std::string Inset::ToString() const
51 {
52     using std::to_string;
53     return "[start: " + to_string(start) + ", end: " + to_string(end) + "]";
54 }
55 
ToString() const56 std::string SafeAreaInsets::ToString() const
57 {
58     return "SafeAreaInsets left_: " + left_.ToString() + ", top_: " + top_.ToString() +
59            ", right_: " + right_.ToString() + ", bottom_: " + bottom_.ToString();
60 }
61 
IsOverlapped(float pos) const62 bool SafeAreaInsets::Inset::IsOverlapped(float pos) const
63 {
64     return IsValid() && LessOrNearEqual(static_cast<float>(start), pos) &&
65         LessOrNearEqual(pos, static_cast<float>(end));
66 }
67 } // namespace OHOS::Ace::NG
68