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 #ifndef FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_SAFE_AREA_INSETS_H
16 #define FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_SAFE_AREA_INSETS_H
17 
18 #include <cstdint>
19 #include <string>
20 
21 namespace OHOS::Ace::NG {
22 /**
23  * @brief Represents the safe area insets in all four directions of the screen.
24  *
25  * The SafeAreaInsets class represents the insets for the safe area of a device's screen.
26  * It provides information about the areas of the screen that are not covered by app's UI elements,
27  * such as the status bar, navigation bar, or notch.
28  */
29 struct SafeAreaInsets {
30     /**
31      * @brief Represents an Inset in one direction of the screen with start/end values in pixel index. For example, a
32      * status bar of height 120px would be Inset(start = 0, end = 119) in Top direction.
33      */
34     struct Inset {
35         uint32_t start = 0;
36         uint32_t end = 0;
37 
LengthSafeAreaInsets::Inset38         uint32_t Length() const
39         {
40             return end - start;
41         }
IsValidSafeAreaInsets::Inset42         bool IsValid() const
43         {
44             return start < end;
45         }
LessOrNearEqualSafeAreaInsets::Inset46         inline bool LessOrNearEqual(float left, float right) const
47         {
48             constexpr float epsilon = 0.01f;
49             return (left - right) < epsilon;
50         }
51 
52         /**
53          * @brief Checks if the given position is overlapped by the safe area insets.
54          *
55          * @param pos The position to check.
56          * @return True if the position is overlapped by the safe area insets, false otherwise.
57          */
58         bool IsOverlapped(float pos) const;
59 
60         /**
61          * @brief Combines the current inset with another inset.
62          *
63          * This function combines the current inset with another inset and returns the result.
64          *
65          * @param other The other inset to combine with.
66          * @return The combined inset.
67          */
68         Inset Combine(const Inset& other) const;
69 
70         bool operator==(const Inset& other) const
71         {
72             return start == other.start && end == other.end;
73         }
74         std::string ToString() const;
75     };
76 
77     Inset left_;
78     Inset top_;
79     Inset right_;
80     Inset bottom_;
81 
82     SafeAreaInsets() = default;
83 
SafeAreaInsetsSafeAreaInsets84     SafeAreaInsets(Inset left, Inset top, Inset right, Inset bottom)
85         : left_(left), top_(top), right_(right), bottom_(bottom)
86     {}
87 
88     bool IsValid() const;
89 
90     std::string ToString() const;
91 
92     bool operator==(const SafeAreaInsets& other) const
93     {
94         return left_ == other.left_ && top_ == other.top_ && right_ == other.right_ && bottom_ == other.bottom_;
95     }
96 
97     bool operator!=(const SafeAreaInsets& other) const
98     {
99         return !(*this == other);
100     }
101 
102     /**
103      * @brief Combines the current SafeAreaInsets with another SafeAreaInsets.
104      *
105      * This function takes the union of the current SafeAreaInsets with another SafeAreaInsets.
106      *
107      * @param other The SafeAreaInsets to combine with.
108      * @return The combined SafeAreaInsets.
109      */
110     SafeAreaInsets Combine(const SafeAreaInsets& other) const;
111 };
112 
113 inline constexpr uint32_t SAFE_AREA_TYPE_NONE = 0;
114 inline constexpr uint32_t SAFE_AREA_TYPE_SYSTEM = 1;
115 inline constexpr uint32_t SAFE_AREA_TYPE_CUTOUT = 1 << 1;
116 inline constexpr uint32_t SAFE_AREA_TYPE_KEYBOARD = 1 << 2;
117 inline constexpr uint32_t SAFE_AREA_TYPE_ALL = 0b111;
118 
119 inline constexpr uint32_t SAFE_AREA_EDGE_NONE = 0;
120 inline constexpr uint32_t SAFE_AREA_EDGE_TOP = 1;
121 inline constexpr uint32_t SAFE_AREA_EDGE_BOTTOM = 1 << 1;
122 inline constexpr uint32_t SAFE_AREA_EDGE_START = 1 << 2;
123 inline constexpr uint32_t SAFE_AREA_EDGE_END = 1 << 3;
124 inline constexpr uint32_t SAFE_AREA_EDGE_ALL = 0b1111;
125 
126 struct SafeAreaExpandOpts {
127     uint32_t type = SAFE_AREA_TYPE_NONE;
128     uint32_t edges = SAFE_AREA_EDGE_NONE;
129     bool switchToNone = false;
130 
131     bool operator==(const SafeAreaExpandOpts& other) const
132     {
133         return type == other.type && edges == other.edges;
134     }
135 
ExpansiveToMarkSafeAreaExpandOpts136     bool ExpansiveToMark() const
137     {
138         return (edges & SAFE_AREA_EDGE_START) || (edges & SAFE_AREA_EDGE_TOP) || (edges & SAFE_AREA_EDGE_BOTTOM);
139     }
140 
141     bool operator!=(const SafeAreaExpandOpts& other) const
142     {
143         return !(*this == other);
144     }
145 
ExpansiveSafeAreaExpandOpts146     bool Expansive() const
147     {
148         return type != SAFE_AREA_TYPE_NONE && edges != SAFE_AREA_EDGE_NONE;
149     }
150 
ExpansiveToKeyboardSafeAreaExpandOpts151     bool ExpansiveToKeyboard() const
152     {
153         return (edges & SAFE_AREA_EDGE_BOTTOM) && (type & SAFE_AREA_TYPE_KEYBOARD);
154     }
155 
ToStringSafeAreaExpandOpts156     std::string ToString()
157     {
158         return "SafeAreaExpandOpts: type:" + TypeToString() + ", edges: " + EdgeToString();
159     }
160 
TypeToStringSafeAreaExpandOpts161     const std::string TypeToString()
162     {
163         switch (type) {
164             case SAFE_AREA_TYPE_NONE:
165                 return "SAFE_AREA_TYPE_NONE";
166             case SAFE_AREA_TYPE_SYSTEM:
167                 return "SAFE_AREA_TYPE_SYSTEM";
168             case SAFE_AREA_TYPE_CUTOUT:
169                 return "SAFE_AREA_TYPE_CUTOUT";
170             case SAFE_AREA_TYPE_KEYBOARD:
171                 return "SAFE_AREA_TYPE_KEYBOARD";
172             case SAFE_AREA_TYPE_ALL:
173                 return "SAFE_AREA_TYPE_ALL";
174             default:
175                 return "SAFE_AREA_TYPE_OTHERS_" + std::to_string(type);
176         }
177     }
178 
EdgeToStringSafeAreaExpandOpts179     const std::string EdgeToString()
180     {
181         switch (edges) {
182             case SAFE_AREA_EDGE_NONE:
183                 return "SAFE_AREA_EDGE_NONE";
184             case SAFE_AREA_EDGE_TOP:
185                 return "SAFE_AREA_EDGE_TOP";
186             case SAFE_AREA_EDGE_BOTTOM:
187                 return "SAFE_AREA_EDGE_BOTTOM";
188             case SAFE_AREA_EDGE_START:
189                 return "SAFE_AREA_EDGE_START";
190             case SAFE_AREA_EDGE_END:
191                 return "SAFE_AREA_EDGE_END";
192             case SAFE_AREA_EDGE_ALL:
193                 return "SAFE_AREA_EDGE_ALL";
194             default:
195                 return "SAFE_AREA_EDGE_OTHERS_" + std::to_string(edges);;
196         }
197     }
198 };
199 } // namespace OHOS::Ace::NG
200 #endif // FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_SAFE_AREA_INSETS_H
201