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/container_modal/container_modal_constants.h"
19 #include "core/components_ng/property/safe_area_insets.h"
20 #include "core/pipeline_ng/pipeline_context.h"
21
22 namespace OHOS::Ace::NG {
UpdateCutoutSafeArea(const SafeAreaInsets & safeArea)23 bool SafeAreaManager::UpdateCutoutSafeArea(const SafeAreaInsets& safeArea)
24 {
25 // cutout regions currently not adjacent to edges, so ignore it.
26 return false;
27 }
28
UpdateSystemSafeArea(const SafeAreaInsets & safeArea)29 bool SafeAreaManager::UpdateSystemSafeArea(const SafeAreaInsets& safeArea)
30 {
31 if (systemSafeArea_ == safeArea) {
32 return false;
33 }
34 systemSafeArea_ = safeArea;
35 return true;
36 }
37
UpdateNavArea(const SafeAreaInsets & safeArea)38 bool SafeAreaManager::UpdateNavArea(const SafeAreaInsets& safeArea)
39 {
40 if (navSafeArea_ == safeArea) {
41 return false;
42 }
43 navSafeArea_ = safeArea;
44 return true;
45 }
46
UpdateKeyboardSafeArea(float keyboardHeight)47 bool SafeAreaManager::UpdateKeyboardSafeArea(float keyboardHeight)
48 {
49 uint32_t bottom;
50 if (systemSafeArea_.bottom_.IsValid()) {
51 bottom = systemSafeArea_.bottom_.start;
52 } else {
53 bottom = PipelineContext::GetCurrentRootHeight();
54 }
55 SafeAreaInsets::Inset inset = { .start = bottom - keyboardHeight, .end = bottom };
56 if (inset == keyboardInset_) {
57 return false;
58 }
59 keyboardInset_ = inset;
60 return true;
61 }
62
GetCombinedSafeArea(const SafeAreaExpandOpts & opts) const63 SafeAreaInsets SafeAreaManager::GetCombinedSafeArea(const SafeAreaExpandOpts& opts) const
64 {
65 SafeAreaInsets res;
66 if (ignoreSafeArea_ || (!isFullScreen_ && !isNeedAvoidWindow_)) {
67 return res;
68 }
69 if (opts.type & SAFE_AREA_TYPE_CUTOUT) {
70 res = res.Combine(cutoutSafeArea_);
71 }
72 if (opts.type & SAFE_AREA_TYPE_SYSTEM) {
73 res = res.Combine(systemSafeArea_).Combine(navSafeArea_);
74 }
75 if (keyboardSafeAreaEnabled_ && (opts.type & SAFE_AREA_TYPE_KEYBOARD)) {
76 res.bottom_ = res.bottom_.Combine(keyboardInset_);
77 }
78 return res;
79 }
80
SetIsFullScreen(bool value)81 bool SafeAreaManager::SetIsFullScreen(bool value)
82 {
83 if (isFullScreen_ == value) {
84 return false;
85 }
86 isFullScreen_ = value;
87 return true;
88 }
89
SetIsNeedAvoidWindow(bool value)90 bool SafeAreaManager::SetIsNeedAvoidWindow(bool value)
91 {
92 if (isNeedAvoidWindow_ == value) {
93 return false;
94 }
95 isNeedAvoidWindow_ = value;
96 return true;
97 }
98
SetIgnoreSafeArea(bool value)99 bool SafeAreaManager::SetIgnoreSafeArea(bool value)
100 {
101 if (ignoreSafeArea_ == value) {
102 return false;
103 }
104 ignoreSafeArea_ = value;
105 return true;
106 }
107
SetKeyBoardAvoidMode(bool value)108 bool SafeAreaManager::SetKeyBoardAvoidMode(bool value)
109 {
110 if (keyboardSafeAreaEnabled_ == value) {
111 return false;
112 }
113 keyboardSafeAreaEnabled_ = value;
114 return true;
115 }
116
SetIsAtomicService(bool value)117 bool SafeAreaManager::SetIsAtomicService(bool value)
118 {
119 if (isAtomicService_ == value) {
120 return false;
121 }
122 isAtomicService_ = value;
123 return true;
124 }
125
IsAtomicService() const126 bool SafeAreaManager::IsAtomicService() const
127 {
128 return isAtomicService_;
129 }
130
GetSystemSafeArea() const131 SafeAreaInsets SafeAreaManager::GetSystemSafeArea() const
132 {
133 return systemSafeArea_;
134 }
135
GetCutoutSafeArea() const136 SafeAreaInsets SafeAreaManager::GetCutoutSafeArea() const
137 {
138 if (ignoreSafeArea_ || !isFullScreen_) {
139 return {};
140 }
141 return cutoutSafeArea_;
142 }
143
GetSafeArea() const144 SafeAreaInsets SafeAreaManager::GetSafeArea() const
145 {
146 if (ignoreSafeArea_ || (!isFullScreen_ && !isNeedAvoidWindow_)) {
147 return {};
148 }
149 return systemSafeArea_.Combine(cutoutSafeArea_).Combine(navSafeArea_);
150 }
151
GetKeyboardOffset() const152 float SafeAreaManager::GetKeyboardOffset() const
153 {
154 if (keyboardSafeAreaEnabled_) {
155 return 0.0f;
156 }
157 return keyboardOffset_;
158 }
159
GetWindowWrapperOffset()160 OffsetF SafeAreaManager::GetWindowWrapperOffset()
161 {
162 auto pipelineContext = PipelineContext::GetCurrentContext();
163 CHECK_NULL_RETURN(pipelineContext, OffsetF());
164 auto windowManager = pipelineContext->GetWindowManager();
165 auto isContainerModal = pipelineContext->GetWindowModal() == WindowModal::CONTAINER_MODAL && windowManager &&
166 windowManager->GetWindowMode() == WindowMode::WINDOW_MODE_FLOATING;
167 if (isContainerModal) {
168 auto wrapperOffset = OffsetF(static_cast<float>((CONTAINER_BORDER_WIDTH + CONTENT_PADDING).ConvertToPx()),
169 static_cast<float>((CONTAINER_TITLE_HEIGHT + CONTAINER_BORDER_WIDTH).ConvertToPx()));
170 return wrapperOffset;
171 }
172 return OffsetF();
173 }
174
ExpandSafeArea()175 void SafeAreaManager::ExpandSafeArea()
176 {
177 ACE_LAYOUT_SCOPED_TRACE("ExpandSafeArea node count %zu", needExpandNodes_.size());
178 auto pipeline = PipelineContext::GetCurrentContext();
179 CHECK_NULL_VOID(pipeline);
180 bool isFocusOnPage = pipeline->CheckPageFocus();
181 auto iter = needExpandNodes_.begin();
182 while (iter != needExpandNodes_.end()) {
183 auto frameNode = (*iter).Upgrade();
184 if (frameNode) {
185 frameNode->SaveGeoState();
186 frameNode->ExpandSafeArea(isFocusOnPage);
187 }
188 ++iter;
189 }
190 ClearNeedExpandNode();
191 }
192 } // namespace OHOS::Ace::NG