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 "ui/base/utils/utils.h"
19 #include "interfaces/inner_api/ace_kit/src/view/frame_node_impl.h"
20 #include "interfaces/inner_api/ace_kit/src/view/overlay/overlay_manager_impl.h"
21
22 #include "base/thread/task_executor.h"
23 #include "bridge/common/utils/engine_helper.h"
24 #include "core/common/container_scope.h"
25 #include "core/pipeline_ng/pipeline_context.h"
26
27 namespace OHOS::Ace::Kit {
28
Current()29 RefPtr<UIContext> UIContext::Current()
30 {
31 const auto& pipeline = NG::PipelineContext::GetCurrentContextSafelyWithCheck();
32 CHECK_NULL_RETURN(pipeline, nullptr);
33 return pipeline->GetUIContext();
34 }
35
~UIContextImpl()36 UIContextImpl::~UIContextImpl()
37 {
38 Reset();
39 }
40
Reset()41 void UIContextImpl::Reset()
42 {
43 context_ = nullptr;
44 }
45
RunScopeUITaskSync(Task && task,const std::string & name)46 void UIContextImpl::RunScopeUITaskSync(Task&& task, const std::string& name)
47 {
48 CHECK_NULL_VOID(context_);
49 const auto& taskExecutor = context_->GetTaskExecutor();
50 CHECK_NULL_VOID(taskExecutor);
51 ContainerScope scope(context_->GetInstanceId());
52 taskExecutor->PostSyncTask(task, TaskExecutor::TaskType::UI, name);
53 }
54
RunScopeUITask(Task && task,const std::string & name)55 void UIContextImpl::RunScopeUITask(Task&& task, const std::string& name)
56 {
57 CHECK_NULL_VOID(context_);
58 const auto& taskExecutor = context_->GetTaskExecutor();
59 CHECK_NULL_VOID(taskExecutor);
60 ContainerScope scope(context_->GetInstanceId());
61 taskExecutor->PostTask(task, TaskExecutor::TaskType::UI, name);
62 }
63
OnBackPressed()64 void UIContextImpl::OnBackPressed()
65 {
66 CHECK_NULL_VOID(context_);
67 bool result = context_->OnBackPressed();
68 if (!result) {
69 auto delegate = OHOS::Ace::EngineHelper::GetCurrentDelegate();
70 CHECK_NULL_VOID(delegate);
71 delegate->Back("");
72 }
73 }
74
GetLocalColorMode()75 ColorMode UIContextImpl::GetLocalColorMode()
76 {
77 CHECK_NULL_RETURN(context_, ColorMode::COLOR_MODE_UNDEFINED);
78 return static_cast<ColorMode>(context_->GetLocalColorMode());
79 }
80
GetColorMode()81 ColorMode UIContextImpl::GetColorMode()
82 {
83 CHECK_NULL_RETURN(context_, ColorMode::COLOR_MODE_UNDEFINED);
84 return static_cast<ColorMode>(context_->GetColorMode());
85 }
86
GetFontScale()87 float UIContextImpl::GetFontScale()
88 {
89 CHECK_NULL_RETURN(context_, 1.0f);
90 return context_->GetFontScale();
91 }
92
GetOverlayManager()93 RefPtr<OverlayManager> UIContextImpl::GetOverlayManager()
94 {
95 if (overlayManager_) {
96 return overlayManager_;
97 }
98 CHECK_NULL_RETURN(context_, nullptr);
99 overlayManager_ = AceType::MakeRefPtr<OverlayManagerImpl>(context_);
100 return overlayManager_;
101 }
102 } // namespace OHOS::Ace::Kit
103