• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "window_scene.h"
17 
18 #include <configuration.h>
19 #include "window_impl.h"
20 #include "window_manager_hilog.h"
21 #include "window_model.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 const std::string WindowScene::MAIN_WINDOW_ID = "main window";
26 namespace {
27     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowScene"};
28 }
~WindowScene()29 WindowScene::~WindowScene()
30 {
31 }
32 
Init(DisplayId displayId,const std::shared_ptr<AbilityRuntime::Context> & context,sptr<IWindowLifeCycle> & listener,sptr<WindowOption> option)33 WMError WindowScene::Init(DisplayId displayId, const std::shared_ptr<AbilityRuntime::Context>& context,
34     sptr<IWindowLifeCycle>& listener, sptr<WindowOption> option)
35 {
36     WLOGFD("WindowScene with window session!");
37     displayId_ = displayId;
38     if (option == nullptr) {
39         option = new(std::nothrow) WindowOption();
40         if (option == nullptr) {
41             WLOGFE("alloc WindowOption failed");
42             return WMError::WM_ERROR_NULLPTR;
43         }
44     }
45     option->SetDisplayId(displayId);
46     option->SetWindowTag(WindowTag::MAIN_WINDOW);
47 
48     mainWindow_ = Window::Create(GenerateMainWindowName(context), option, context);
49     if (mainWindow_ == nullptr) {
50         WLOGFE("mainWindow_ is NULL");
51         return WMError::WM_ERROR_NULLPTR;
52     }
53 
54     Previewer::PreviewerWindowModel& windowModel =  Previewer::PreviewerWindow::GetInstance().GetWindowParams();
55     Ace::ViewportConfig config;
56     config.SetSize(windowModel.originWidth, windowModel.originHeight);
57     config.SetPosition(0, 0);
58     config.SetOrientation(static_cast<int32_t>(Previewer::PreviewerWindow::TransOrientation(windowModel.orientation)));
59     config.SetDensity(windowModel.density);
60     mainWindow_->SetViewportConfig(config);
61 
62     Previewer::PreviewerWindow::GetInstance().SetWindowObject(mainWindow_.GetRefPtr());
63     mainWindow_->RegisterLifeCycleListener(listener);
64     return WMError::WM_OK;
65 }
66 
GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context> & context) const67 std::string WindowScene::GenerateMainWindowName(const std::shared_ptr<AbilityRuntime::Context>& context) const
68 {
69     if (context == nullptr) {
70         return MAIN_WINDOW_ID + std::to_string(count++);
71     } else {
72         auto options = context->GetOptions();
73         std::string windowName = options.bundleName + std::to_string(count++);
74         std::size_t pos = windowName.find_last_of('.');
75         return (pos == std::string::npos) ? windowName : windowName.substr(pos + 1); // skip '.'
76     }
77 }
78 
CreateWindow(const std::string & windowName,sptr<WindowOption> & option) const79 sptr<Window> WindowScene::CreateWindow(const std::string& windowName, sptr<WindowOption>& option) const
80 {
81     return nullptr;
82 }
83 
GetMainWindow() const84 const sptr<Window>& WindowScene::GetMainWindow() const
85 {
86     return mainWindow_;
87 }
88 
GetSubWindow()89 std::vector<sptr<Window>> WindowScene::GetSubWindow()
90 {
91     return std::vector<sptr<Window>>();
92 }
93 
GoDestroy()94 WMError WindowScene::GoDestroy()
95 {
96     if (mainWindow_ == nullptr) {
97         return WMError::WM_ERROR_NULLPTR;
98     }
99 
100     WMError ret = mainWindow_->Destroy();
101     if (ret != WMError::WM_OK) {
102         WLOGFE("WindowScene go destroy failed name: %{public}s", mainWindow_->GetWindowName().c_str());
103         return ret;
104     }
105     mainWindow_ = nullptr;
106     return WMError::WM_OK;
107 }
108 
UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)109 void WindowScene::UpdateConfiguration(const std::shared_ptr<AppExecFwk::Configuration>& configuration)
110 {
111     if (mainWindow_ == nullptr) {
112         WLOGFE("Update configuration failed, because main window is null");
113         return;
114     }
115     WLOGFI("notify mainWindow winId:%{public}u", mainWindow_->GetWindowId());
116     mainWindow_->UpdateConfiguration(configuration);
117 }
118 } // namespace Rosen
119 } // namespace OHOS
120