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 #include "safe_area_manager.h" 16 17 #include "base/utils/utils.h" 18 #include "core/components_ng/property/safe_area_insets.h" 19 #include "core/pipeline_ng/pipeline_context.h" 20 namespace OHOS::Ace::NG { UpdateCutoutSafeArea(const SafeAreaInsets & safeArea)21bool SafeAreaManager::UpdateCutoutSafeArea(const SafeAreaInsets& safeArea) 22 { 23 if (cutoutSafeArea_ == safeArea) { 24 return false; 25 } 26 cutoutSafeArea_ = safeArea; 27 return true; 28 } 29 UpdateSystemSafeArea(const SafeAreaInsets & safeArea)30bool SafeAreaManager::UpdateSystemSafeArea(const SafeAreaInsets& safeArea) 31 { 32 if (systemSafeArea_ == safeArea) { 33 return false; 34 } 35 systemSafeArea_ = safeArea; 36 return true; 37 } 38 UpdateKeyboardSafeArea(float keyboardHeight)39bool SafeAreaManager::UpdateKeyboardSafeArea(float keyboardHeight) 40 { 41 uint32_t bottom; 42 if (systemSafeArea_.bottom_.IsValid()) { 43 bottom = systemSafeArea_.bottom_.start; 44 } else { 45 bottom = PipelineContext::GetCurrentRootHeight(); 46 } 47 SafeAreaInsets::Inset inset = { .start = bottom - keyboardHeight, .end = bottom }; 48 if (inset == keyboardInset_) { 49 return false; 50 } 51 keyboardInset_ = inset; 52 return true; 53 } 54 GetCombinedSafeArea(const SafeAreaExpandOpts & opts) const55SafeAreaInsets SafeAreaManager::GetCombinedSafeArea(const SafeAreaExpandOpts& opts) const 56 { 57 SafeAreaInsets res; 58 if (opts.type & SAFE_AREA_TYPE_CUTOUT) { 59 res = res.Combine(cutoutSafeArea_); 60 } 61 if (opts.type & SAFE_AREA_TYPE_SYSTEM) { 62 res = res.Combine(systemSafeArea_); 63 } 64 return res; 65 } 66 SetIsFullScreen(bool value)67bool SafeAreaManager::SetIsFullScreen(bool value) 68 { 69 if (isFullScreen_ == value) { 70 return false; 71 } 72 isFullScreen_ = value; 73 return true; 74 } 75 SetIgnoreSafeArea(bool value)76bool SafeAreaManager::SetIgnoreSafeArea(bool value) 77 { 78 if (ignoreSafeArea_ == value) { 79 return false; 80 } 81 ignoreSafeArea_ = value; 82 return true; 83 } 84 GetSystemSafeArea() const85SafeAreaInsets SafeAreaManager::GetSystemSafeArea() const 86 { 87 if (ignoreSafeArea_ || !isFullScreen_) { 88 return {}; 89 } 90 return systemSafeArea_; 91 } 92 GetCutoutSafeArea() const93SafeAreaInsets SafeAreaManager::GetCutoutSafeArea() const 94 { 95 if (ignoreSafeArea_ || !isFullScreen_) { 96 return {}; 97 } 98 return cutoutSafeArea_; 99 } 100 GetSafeArea() const101SafeAreaInsets SafeAreaManager::GetSafeArea() const 102 { 103 if (ignoreSafeArea_ || !isFullScreen_) { 104 return {}; 105 } 106 return systemSafeArea_.Combine(cutoutSafeArea_); 107 } 108 } // namespace OHOS::Ace::NG