1 /*
2 * Copyright (c) 2025 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 "adapter/ohos/osal/page_viewport_config_ohos.h"
17
18 #include "adapter/ohos/entrance/utils.h"
19 #include "base/geometry/ng/size_t.h"
20
21 namespace OHOS::Ace {
22
Clone() const23 RefPtr<PageViewportConfig> PageViewportConfigOhos::Clone() const
24 {
25 auto ret = MakeRefPtr<PageViewportConfigOhos>();
26 ret->config_ = config_;
27 ret->avoidAreas_ = avoidAreas_;
28 ret->pipeline_ = pipeline_;
29 return ret;
30 }
31
CreateRootLayoutConstraint() const32 NG::LayoutConstraintF PageViewportConfigOhos::CreateRootLayoutConstraint() const
33 {
34 NG::LayoutConstraintF layoutConstraint;
35 layoutConstraint.scaleProperty = NG::ScaleProperty::CreateScaleProperty();
36 auto rootWidth = config_.Width();
37 auto rootHeight = config_.Height();
38 layoutConstraint.percentReference.SetWidth(rootWidth);
39 layoutConstraint.percentReference.SetHeight(rootHeight);
40 layoutConstraint.maxSize.SetWidth(rootWidth);
41 layoutConstraint.maxSize.SetHeight(rootHeight);
42 return layoutConstraint;
43 }
44
GetSafeArea() const45 NG::SafeAreaInsets PageViewportConfigOhos::GetSafeArea() const
46 {
47 NG::SafeAreaInsets insets;
48 auto pipeline = pipeline_.Upgrade();
49 CHECK_NULL_RETURN(pipeline, insets);
50 auto mgr = pipeline->GetSafeAreaManager();
51 CHECK_NULL_RETURN(mgr, insets);
52 for (auto& avoidArea : avoidAreas_) {
53 auto type = avoidArea.first;
54 if (type == OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM ||
55 type == OHOS::Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR ||
56 (type == OHOS::Rosen::AvoidAreaType::TYPE_CUTOUT && mgr->GetUseCutout())) {
57 auto newInsets = ConvertAvoidArea(avoidArea.second);
58 insets = insets.Combine(newInsets);
59 }
60 }
61 return insets;
62 }
63
ApplySafeArea()64 void PageViewportConfigOhos::ApplySafeArea()
65 {
66 auto pipeline = pipeline_.Upgrade();
67 CHECK_NULL_VOID(pipeline);
68 auto mgr = pipeline->GetSafeAreaManager();
69 CHECK_NULL_VOID(mgr);
70 BackupInfo backupInfo;
71 backupInfo.rootWidth = static_cast<uint32_t>(pipeline->GetRootWidth());
72 backupInfo.rootHeight = static_cast<uint32_t>(pipeline->GetRootHeight());
73 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "backup rootSize, width:%{public}u, height:%{public}u",
74 backupInfo.rootWidth, backupInfo.rootHeight);
75 for (auto& avoidArea : avoidAreas_) {
76 if (avoidArea.first == OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM) {
77 auto insets = mgr->GetSystemSafeArea();
78 backupInfo.safeAreas[OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM] = insets;
79 auto newInsets = ConvertAvoidArea(avoidArea.second);
80 mgr->UpdateSystemSafeArea(newInsets);
81 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "ApplyConfig system from %{public}s to %{public}s",
82 insets.ToString().c_str(), newInsets.ToString().c_str());
83 } else if (avoidArea.first == OHOS::Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR) {
84 auto insets = mgr->GetNavSafeArea();
85 backupInfo.safeAreas[OHOS::Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR] = insets;
86 auto newInsets = ConvertAvoidArea(avoidArea.second);
87 mgr->UpdateNavSafeArea(newInsets);
88 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "ApplyConfig navigationIndicator from %{public}s to %{public}s",
89 insets.ToString().c_str(), newInsets.ToString().c_str());
90 } else if (avoidArea.first == OHOS::Rosen::AvoidAreaType::TYPE_CUTOUT && mgr->GetUseCutout()) {
91 auto insets = mgr->GetCutoutSafeAreaWithoutProcess();
92 backupInfo.safeAreas[OHOS::Rosen::AvoidAreaType::TYPE_CUTOUT] = insets;
93 auto newInsets = ConvertAvoidArea(avoidArea.second);
94 mgr->UpdateCutoutSafeArea(newInsets, NG::OptionalSize<uint32_t>(config_.Width(), config_.Height()));
95 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "ApplyConfig cutout from %{public}s to %{public}s",
96 insets.ToString().c_str(), newInsets.ToString().c_str());
97 }
98 }
99 backupInfos_.emplace(std::move(backupInfo));
100 }
101
RestoreSafeArea()102 void PageViewportConfigOhos::RestoreSafeArea()
103 {
104 auto pipeline = pipeline_.Upgrade();
105 CHECK_NULL_VOID(pipeline);
106 auto mgr = pipeline->GetSafeAreaManager();
107 CHECK_NULL_VOID(mgr);
108 if (backupInfos_.empty()) {
109 return;
110 }
111 auto backupInfo = std::move(backupInfos_.top());
112 backupInfos_.pop();
113 for (auto& avoidArea : backupInfo.safeAreas) {
114 if (avoidArea.first == OHOS::Rosen::AvoidAreaType::TYPE_SYSTEM) {
115 mgr->UpdateSystemSafeArea(avoidArea.second);
116 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "RestoreConfig system: %{public}s",
117 avoidArea.second.ToString().c_str());
118 } else if (avoidArea.first == OHOS::Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR) {
119 mgr->UpdateNavSafeArea(avoidArea.second);
120 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "RestoreConfig navigationIndicator: %{public}s",
121 avoidArea.second.ToString().c_str());
122 } else if (avoidArea.first == OHOS::Rosen::AvoidAreaType::TYPE_CUTOUT && mgr->GetUseCutout()) {
123 mgr->UpdateCutoutSafeArea(
124 avoidArea.second, NG::OptionalSize<uint32_t>(backupInfo.rootWidth, backupInfo.rootHeight));
125 TAG_LOGI(AceLogTag::ACE_NAVIGATION, "RestoreConfig cutout width:%{public}u, height:%{public}u, "
126 "insets:%{public}s", backupInfo.rootWidth, backupInfo.rootHeight, avoidArea.second.ToString().c_str());
127 }
128 }
129 }
130 } // namespace OHOS::Ace