• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "interfaces/inner_api/ace_kit/src/view/ui_context_impl.h"
17 
18 #include "iremote_object.h"
19 
20 #include "ui/base/utils/utils.h"
21 #include "interfaces/inner_api/ace_kit/src/view/frame_node_impl.h"
22 #include "interfaces/inner_api/ace_kit/src/view/overlay/overlay_manager_impl.h"
23 
24 #include "base/geometry/ng/rect_t.h"
25 #include "base/thread/task_executor.h"
26 #include "bridge/common/utils/engine_helper.h"
27 #include "core/common/ace_application_info.h"
28 #include "core/common/container_scope.h"
29 #include "core/common/display_info.h"
30 #include "core/pipeline_ng/pipeline_context.h"
31 
32 namespace OHOS::Ace::Kit {
33 constexpr int32_t API_VERSION_LIMIT = 1000;
34 
Current()35 RefPtr<UIContext> UIContext::Current()
36 {
37     const auto& pipeline = NG::PipelineContext::GetCurrentContextSafelyWithCheck();
38     CHECK_NULL_RETURN(pipeline, nullptr);
39     return pipeline->GetUIContext();
40 }
41 
~UIContextImpl()42 UIContextImpl::~UIContextImpl()
43 {
44     Reset();
45 }
46 
Reset()47 void UIContextImpl::Reset()
48 {
49     context_ = nullptr;
50 }
51 
RunScopeUITaskSync(Task && task,const std::string & name)52 void UIContextImpl::RunScopeUITaskSync(Task&& task, const std::string& name)
53 {
54     CHECK_NULL_VOID(context_);
55     const auto& taskExecutor = context_->GetTaskExecutor();
56     CHECK_NULL_VOID(taskExecutor);
57     ContainerScope scope(context_->GetInstanceId());
58     taskExecutor->PostSyncTask(task, TaskExecutor::TaskType::UI, name);
59 }
60 
RunScopeUITask(Task && task,const std::string & name)61 void UIContextImpl::RunScopeUITask(Task&& task, const std::string& name)
62 {
63     CHECK_NULL_VOID(context_);
64     const auto& taskExecutor = context_->GetTaskExecutor();
65     CHECK_NULL_VOID(taskExecutor);
66     ContainerScope scope(context_->GetInstanceId());
67     taskExecutor->PostTask(task, TaskExecutor::TaskType::UI, name);
68 }
69 
RunScopeUIDelayedTask(Task && task,const std::string & name,uint32_t delayTime)70 void UIContextImpl::RunScopeUIDelayedTask(Task&& task, const std::string& name, uint32_t delayTime)
71 {
72     CHECK_NULL_VOID(context_);
73     const auto& taskExecutor = context_->GetTaskExecutor();
74     CHECK_NULL_VOID(taskExecutor);
75     ContainerScope scope(context_->GetInstanceId());
76     taskExecutor->PostDelayedTask(task, TaskExecutor::TaskType::UI, delayTime, name);
77 }
78 
OnBackPressed()79 void UIContextImpl::OnBackPressed()
80 {
81     CHECK_NULL_VOID(context_);
82     bool result = context_->OnBackPressed();
83     if (!result) {
84         auto delegate = OHOS::Ace::EngineHelper::GetCurrentDelegate();
85         CHECK_NULL_VOID(delegate);
86         delegate->Back("");
87     }
88 }
89 
GetLocalColorMode()90 ColorMode UIContextImpl::GetLocalColorMode()
91 {
92     CHECK_NULL_RETURN(context_, ColorMode::COLOR_MODE_UNDEFINED);
93     return static_cast<ColorMode>(context_->GetLocalColorMode());
94 }
95 
GetColorMode()96 ColorMode UIContextImpl::GetColorMode()
97 {
98     CHECK_NULL_RETURN(context_, ColorMode::COLOR_MODE_UNDEFINED);
99     return static_cast<ColorMode>(context_->GetColorMode());
100 }
101 
GetFontScale()102 float UIContextImpl::GetFontScale()
103 {
104     CHECK_NULL_RETURN(context_, 1.0f);
105     return context_->GetFontScale();
106 }
107 
GetOverlayManager()108 RefPtr<OverlayManager> UIContextImpl::GetOverlayManager()
109 {
110     if (overlayManager_) {
111         return overlayManager_;
112     }
113     CHECK_NULL_RETURN(context_, nullptr);
114     overlayManager_ = AceType::MakeRefPtr<OverlayManagerImpl>(context_);
115     return overlayManager_;
116 }
117 
AddAfterLayoutTask(Task && task,bool isFlushInImplicitAnimationTask)118 void UIContextImpl::AddAfterLayoutTask(Task&& task, bool isFlushInImplicitAnimationTask)
119 {
120     CHECK_NULL_VOID(context_);
121     context_->AddAfterLayoutTask(std::move(task), isFlushInImplicitAnimationTask);
122 }
123 
RequestFrame()124 void UIContextImpl::RequestFrame()
125 {
126     CHECK_NULL_VOID(context_);
127     context_->RequestFrame();
128 }
129 
GetApiTargetVersion()130 int32_t UIContextImpl::GetApiTargetVersion()
131 {
132     return AceApplicationInfo::GetInstance().GetApiTargetVersion() % API_VERSION_LIMIT;
133 }
134 
GreatOrEqualTargetAPIVersion(int32_t version)135 bool UIContextImpl::GreatOrEqualTargetAPIVersion(int32_t version)
136 {
137     return AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(static_cast<PlatformVersion>(version));
138 }
139 
GetContainerModalTitleHeight()140 int32_t UIContextImpl::GetContainerModalTitleHeight()
141 {
142     CHECK_NULL_RETURN(context_, 0);
143     return context_->GetContainerModalTitleHeight();
144 }
145 
GetContainerModalButtonsWidth()146 int32_t UIContextImpl::GetContainerModalButtonsWidth()
147 {
148     CHECK_NULL_RETURN(context_, 0);
149     Ace::NG::RectF containerModal;
150     Ace::NG::RectF buttonsRect;
151     context_->GetContainerModalButtonsRect(containerModal, buttonsRect);
152     return static_cast<int32_t>(buttonsRect.Width());
153 }
154 
RegisterArkUIObjectLifecycleCallback(ArkUIObjectLifecycleCallback && callback)155 void UIContextImpl::RegisterArkUIObjectLifecycleCallback(ArkUIObjectLifecycleCallback&& callback)
156 {
157     CHECK_NULL_VOID(context_);
158     context_->RegisterArkUIObjectLifecycleCallback(std::move(callback));
159 }
160 
UnregisterArkUIObjectLifecycleCallback()161 void UIContextImpl::UnregisterArkUIObjectLifecycleCallback()
162 {
163     CHECK_NULL_VOID(context_);
164     context_->UnregisterArkUIObjectLifecycleCallback();
165 }
166 
GetContainerModalButtonsOffset()167 NG::OffsetF UIContextImpl::GetContainerModalButtonsOffset()
168 {
169     Ace::NG::RectF buttonsRect;
170     CHECK_NULL_RETURN(context_, buttonsRect.GetOffset());
171     Ace::NG::RectF containerModal;
172     context_->GetContainerModalButtonsRect(containerModal, buttonsRect);
173     return buttonsRect.GetOffset();
174 }
175 
GetToken()176 sptr<IRemoteObject> UIContextImpl::GetToken()
177 {
178     CHECK_NULL_RETURN(context_, nullptr);
179     ContainerScope scope(context_->GetInstanceId());
180     auto container = Container::Current();
181     CHECK_NULL_RETURN(container, nullptr);
182     return container->GetToken();
183 }
184 
GetDisplayInfo()185 RefPtr<DisplayInfo> UIContextImpl::GetDisplayInfo()
186 {
187     CHECK_NULL_RETURN(context_, nullptr);
188     ContainerScope scope(context_->GetInstanceId());
189     auto container = Container::Current();
190     CHECK_NULL_RETURN(container, nullptr);
191     return container->GetDisplayInfo();
192 }
193 
GetWindowMode()194 WindowMode UIContextImpl::GetWindowMode()
195 {
196     CHECK_NULL_RETURN(context_, WindowMode::WINDOW_MODE_UNDEFINED);
197     auto windowManager = context_->GetWindowManager();
198     CHECK_NULL_RETURN(windowManager, WindowMode::WINDOW_MODE_UNDEFINED);
199     return windowManager->GetWindowMode();
200 }
GetIsMidScene()201 bool UIContextImpl::GetIsMidScene()
202 {
203     CHECK_NULL_RETURN(context_, false);
204     auto windowManager = context_->GetWindowManager();
205     CHECK_NULL_RETURN(windowManager, false);
206     bool isMidScene = false;
207     windowManager->GetIsMidScene(isMidScene);
208     return isMidScene;
209 }
IsAccessibilityEnabled()210 bool UIContextImpl::IsAccessibilityEnabled()
211 {
212     return AceApplicationInfo::GetInstance().IsAccessibilityEnabled();
213 }
214 
RegisterSurfaceChangedCallback(std::function<void (int32_t,int32_t,int32_t,int32_t,WindowSizeChangeReason)> && callback)215 int32_t UIContextImpl::RegisterSurfaceChangedCallback(
216     std::function<void(int32_t, int32_t, int32_t, int32_t, WindowSizeChangeReason)>&& callback)
217 {
218     CHECK_NULL_RETURN(context_, 0);
219     return context_->RegisterSurfaceChangedCallback(std::move(callback));
220 }
221 
UnregisterSurfaceChangedCallback(int32_t callbackId)222 void UIContextImpl::UnregisterSurfaceChangedCallback(int32_t callbackId)
223 {
224     CHECK_NULL_VOID(context_);
225     context_->UnregisterSurfaceChangedCallback(callbackId);
226 }
227 
RegisterFoldStatusChangedCallback(std::function<void (FoldStatus)> && callback)228 int32_t UIContextImpl::RegisterFoldStatusChangedCallback(std::function<void(FoldStatus)>&& callback)
229 {
230     CHECK_NULL_RETURN(context_, 0);
231     return context_->RegisterFoldStatusChangedCallback(std::move(callback));
232 }
233 
UnRegisterFoldStatusChangedCallback(int32_t callbackId)234 void UIContextImpl::UnRegisterFoldStatusChangedCallback(int32_t callbackId)
235 {
236     CHECK_NULL_VOID(context_);
237     context_->UnRegisterFoldStatusChangedCallback(callbackId);
238 }
239 
RegisterRotationEndCallback(std::function<void ()> && callback)240 int32_t UIContextImpl::RegisterRotationEndCallback(std::function<void()>&& callback)
241 {
242     CHECK_NULL_RETURN(context_, 0);
243     return context_->RegisterRotationEndCallback(std::move(callback));
244 }
245 
UnregisterRotationEndCallback(int32_t callbackId)246 void UIContextImpl::UnregisterRotationEndCallback(int32_t callbackId)
247 {
248     CHECK_NULL_VOID(context_);
249     context_->UnregisterRotationEndCallback(callbackId);
250 }
251 
AddWindowSizeChangeCallback(int32_t nodeId)252 void UIContextImpl::AddWindowSizeChangeCallback(int32_t nodeId)
253 {
254     CHECK_NULL_VOID(context_);
255     context_->AddWindowSizeChangeCallback(nodeId);
256 }
257 
258 } // namespace OHOS::Ace::Kit
259