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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SAFE_AREA_SAFE_AREA_MANAGER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SAFE_AREA_SAFE_AREA_MANAGER_H 18 19 #include "base/memory/ace_type.h" 20 #include "base/utils/noncopyable.h" 21 #include "core/components_ng/base/frame_node.h" 22 #include "core/components_ng/property/safe_area_insets.h" 23 #include "core/components_ng/property/transition_property.h" 24 25 namespace OHOS::Ace::NG { 26 // SafeAreaManager stores layout information to apply SafeArea correctly. 27 class SafeAreaManager : public virtual AceType { 28 DECLARE_ACE_TYPE(SafeAreaManager, AceType); 29 30 public: 31 SafeAreaManager() = default; 32 ~SafeAreaManager() override = default; 33 34 /** 35 * @brief Updates the system safe area. 36 * 37 * @param safeArea The new system safe area. 38 * @return True if the system safe area was modified, false otherwise. 39 */ 40 bool UpdateSystemSafeArea(const SafeAreaInsets& safeArea); 41 42 /** 43 * @brief Updates the navigation indictor safe area. 44 * 45 * @param safeArea The new navigation indictor safe area. 46 * @return True if the system safe area was modified, false otherwise. 47 */ 48 bool UpdateNavArea(const SafeAreaInsets& safeArea); 49 50 /** 51 * @brief Retrieves the system safe area insets. 52 * 53 * This function returns the safe area insets of the system, which represents the portion of the screen that is 54 * covered by system UI elements such as the status bar or navigation bar. 55 * 56 * @return The system safe area insets. 57 */ 58 SafeAreaInsets GetSystemSafeArea() const; 59 60 /** 61 * @brief Updates the cutout safe area. 62 * 63 * This function is responsible for updating the cutout safe area based on the provided SafeAreaInsets. 64 * 65 * @param safeArea The SafeAreaInsets representing the new cutout safe area. 66 * @return true if the cutout safe area was successfully updated, false otherwise. 67 */ 68 bool UpdateCutoutSafeArea(const SafeAreaInsets& safeArea); 69 70 /** 71 * @brief Retrieves the safe area insets that account for any cutout areas on the screen. 72 * 73 * @return The safe area insets that account for any cutout areas on the screen. 74 */ 75 SafeAreaInsets GetCutoutSafeArea() const; 76 77 /** 78 * @brief Retrieves the safe area insets combining System and Cutout. 79 * 80 * @return The System & Cutout safe area insets. 81 */ 82 SafeAreaInsets GetSafeArea() const; 83 84 /** 85 * @brief Updates the safe area to accommodate the keyboard. 86 * 87 * This function is called to update the safe area when the keyboard is shown or hidden. 88 * 89 * @param keyboardHeight The height of the keyboard in pixels. 90 * @return true if the safe area was modified, false otherwise. 91 */ 92 bool UpdateKeyboardSafeArea(float keyboardHeight); 93 94 /** 95 * @brief Retrieves the inset of the safe area caused by the keyboard. 96 * 97 * @return The inset of the safe area caused by the keyboard. 98 */ GetKeyboardInset()99 SafeAreaInsets::Inset GetKeyboardInset() const 100 { 101 return keyboardInset_; 102 } 103 UpdateKeyboardOffset(float offset)104 void UpdateKeyboardOffset(float offset) 105 { 106 keyboardOffset_ = offset; 107 } 108 float GetKeyboardOffset() const; 109 KeyboardSafeAreaEnabled()110 bool KeyboardSafeAreaEnabled() const 111 { 112 return keyboardSafeAreaEnabled_; 113 } 114 115 SafeAreaInsets GetCombinedSafeArea(const SafeAreaExpandOpts& opts) const; 116 GetGeoRestoreNodes()117 const std::set<WeakPtr<FrameNode>>& GetGeoRestoreNodes() const 118 { 119 return geoRestoreNodes_; 120 } 121 AddGeoRestoreNode(const WeakPtr<FrameNode> & node)122 void AddGeoRestoreNode(const WeakPtr<FrameNode>& node) 123 { 124 geoRestoreNodes_.insert(node); 125 } 126 AddNeedExpandNode(const WeakPtr<FrameNode> & node)127 void AddNeedExpandNode(const WeakPtr<FrameNode>& node) 128 { 129 needExpandNodes_.insert(node); 130 } 131 ClearNeedExpandNode()132 void ClearNeedExpandNode() 133 { 134 needExpandNodes_.clear(); 135 } 136 RemoveRestoreNode(const WeakPtr<FrameNode> & node)137 void RemoveRestoreNode(const WeakPtr<FrameNode>& node) 138 { 139 geoRestoreNodes_.erase(node); 140 } 141 GetSafeAreaCurve()142 RefPtr<InterpolatingSpring> GetSafeAreaCurve() const 143 { 144 return safeAreaCurve_; 145 } 146 void ExpandSafeArea(); 147 148 OffsetF GetWindowWrapperOffset(); 149 150 bool SetIsFullScreen(bool value); 151 bool SetIsNeedAvoidWindow(bool value); 152 bool SetIgnoreSafeArea(bool value); 153 bool SetKeyBoardAvoidMode(bool value); 154 bool SetIsAtomicService(bool value); 155 bool IsAtomicService() const; 156 157 private: 158 bool isAtomicService_ = false; 159 160 // app window is full screen 161 // todo: remove and only use isNeedAvoidWindow_ 162 bool isFullScreen_ = false; 163 164 /** 165 * @brief Indicates whether the UI within the current window type needs to avoid SafeAreaInsets. 166 */ 167 bool isNeedAvoidWindow_ = false; 168 169 /** 170 * @brief Indicates whether to ignore the SafeAreaInsets, specified by the developer from frontend. 171 */ 172 bool ignoreSafeArea_ = false; 173 174 /** 175 * @brief Indicates whether the keyboard safe area is enabled. When enabled, UI avoids the keyboard inset and the 176 * Page is compressed when the keyboard is up. When disabled, the size of Page doesn't change, but Page would 177 * offset vertically according to [keyboardOffset_]. 178 */ 179 bool keyboardSafeAreaEnabled_ = false; 180 181 SafeAreaInsets systemSafeArea_; 182 SafeAreaInsets cutoutSafeArea_; 183 SafeAreaInsets navSafeArea_; 184 // keyboard is bottom direction only 185 SafeAreaInsets::Inset keyboardInset_; 186 187 /** 188 * @brief A set of weak pointers to FrameNode objects whose geometry info are saved before their SafeArea 189 * expansion. The geometry info of these nodes need to be restored when their next Layout task begins. 190 */ 191 std::set<WeakPtr<FrameNode>> geoRestoreNodes_; 192 193 struct DepthCompare { operatorDepthCompare194 bool operator()(const WeakPtr<FrameNode>& a, const WeakPtr<FrameNode>& b) const 195 { 196 auto ptrA = a.Upgrade(); 197 auto ptrB = b.Upgrade(); 198 if (!ptrA || !ptrB) { 199 return false; 200 } 201 if (ptrA->GetDepth() < ptrB->GetDepth()) { 202 return true; 203 } 204 if (ptrA->GetDepth() == ptrB->GetDepth()) { 205 return ptrA < ptrB; 206 } 207 return false; 208 } 209 }; 210 std::set<WeakPtr<FrameNode>, DepthCompare> needExpandNodes_; 211 // amount of offset to apply to Page when keyboard is up 212 float keyboardOffset_ = 0.0f; 213 214 static constexpr float SAFE_AREA_VELOCITY = 0.0f; 215 static constexpr float SAFE_AREA_MASS = 1.0f; 216 static constexpr float SAFE_AREA_STIFFNESS = 228.0f; 217 static constexpr float SAFE_AREA_DAMPING = 30.0f; 218 RefPtr<InterpolatingSpring> safeAreaCurve_ = AceType::MakeRefPtr<InterpolatingSpring>( 219 SAFE_AREA_VELOCITY, SAFE_AREA_MASS, SAFE_AREA_STIFFNESS, SAFE_AREA_DAMPING); 220 221 ACE_DISALLOW_COPY_AND_MOVE(SafeAreaManager); 222 }; 223 } // namespace OHOS::Ace::NG 224 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SAFE_AREA_SAFE_AREA_MANAGER_H 225