• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2/*
3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import Window from "@ohos.window";
18import Log from "./Log";
19import EventManager from "./event/EventManager";
20import { obtainLocalEvent } from "./event/EventUtil";
21import { Rect } from "./Constants";
22import createOrGet from "./SingleInstanceHelper";
23
24export type WindowInfo = {
25  visibility: boolean;
26  rect: Rect;
27};
28export enum WindowType {
29  STATUS_BAR = "SystemUi_StatusBar",
30  NAVIGATION_BAR = "SystemUi_NavigationBar",
31  DROPDOWN_PANEL = "SystemUi_DropdownPanel",
32  NOTIFICATION_PANEL = "SystemUi_NotificationPanel",
33  CONTROL_PANEL = "SystemUi_ControlPanel",
34  VOLUME_PANEL = "SystemUi_VolumePanel",
35  BANNER_NOTICE = 'SystemUi_BannerNotice'
36}
37
38export const WINDOW_SHOW_HIDE_EVENT = "WindowShowHideEvent";
39export const WINDOW_RESIZE_EVENT = "WindowResizeEvent";
40
41type WindowHandle = typeof Window.Window;
42const TAG = "WindowManagerSc";
43const SYSTEM_WINDOW_TYPE_MAP: { [key in WindowType]: number } = {
44  SystemUi_StatusBar: 2108,
45  SystemUi_NavigationBar: 2112,
46  SystemUi_DropdownPanel: 2109,
47  SystemUi_NotificationPanel: 2111,
48  SystemUi_ControlPanel: 2111,
49  SystemUi_VolumePanel: 2111,
50  SystemUi_BannerNotice: 2111,
51};
52const DEFAULT_WINDOW_INFO: WindowInfo = {
53  visibility: false,
54  rect: { left: 0, top: 0, width: 0, height: 0 },
55};
56
57/**
58 * Manage window size changes.
59 */
60class WindowManager {
61  mWindowInfos: Map<WindowType, WindowInfo> = new Map();
62
63  async createWindow(context: any, name: WindowType, rect: Rect, loadContent: string): Promise<WindowHandle> {
64    Log.showInfo(TAG, `createWindow name: ${name}, rect: ${JSON.stringify(rect)}, url: ${loadContent}`);
65    let winHandle = null;
66    try{
67      winHandle = await Window.create(context, name, SYSTEM_WINDOW_TYPE_MAP[name]);
68      await winHandle.moveTo(rect.left, rect.top);
69      await winHandle.resetSize(rect.width, rect.height);
70      await winHandle.loadContent(loadContent);
71      this.mWindowInfos.set(name, { visibility: false, rect });
72      Log.showInfo(TAG, `create window[${name}] success.`);
73    } catch (err) {
74      Log.showError(TAG, `create window[${name}] failed. error:${JSON.stringify(err)}`);
75    }
76    return winHandle;
77  }
78
79  async resetSizeWindow(name: WindowType, rect: Rect): Promise<void> {
80    Log.showInfo(TAG, `resetSizeWindow name: ${name}, rect: ${JSON.stringify(rect)}`);
81    let window = null;
82    try {
83      window = await Window.find(name);
84      await window.moveTo(rect.left, rect.top);
85      await window.resetSize(rect.width, rect.height);
86    } catch(err) {
87      Log.showError(TAG, `resetSizeWindow failed. error:${JSON.stringify(err)}`);
88    }
89    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), rect });
90    EventManager.publish(
91      obtainLocalEvent(WINDOW_RESIZE_EVENT, {
92        windowName: name,
93        rect,
94      })
95    );
96    Log.showInfo(TAG, `resize window[${name}] success.`);
97  }
98
99  async showWindow(name: WindowType): Promise<void> {
100    Log.showInfo(TAG, `showWindow name: ${name}`);
101    let window = null;
102    try {
103      window = await Window.find(name);
104      await window.show();
105    } catch (err) {
106      Log.showError(TAG, `showWindow failed. error:${JSON.stringify(err)}`);
107    }
108    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), visibility: true });
109    EventManager.publish(
110      obtainLocalEvent(WINDOW_SHOW_HIDE_EVENT, {
111        windowName: name,
112        isShow: true,
113      })
114    );
115    Log.showInfo(TAG, `show window[${name}] success.`);
116  }
117
118  async hideWindow(name: WindowType): Promise<void> {
119    Log.showInfo(TAG, `hideWindow name: ${name}`);
120    let window = null;
121    try {
122      window = await Window.find(name);
123      await window.hide();
124    } catch (err) {
125      Log.showError(TAG, `hideWindow failed. error:${JSON.stringify(err)}`);
126    }
127    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), visibility: false });
128    EventManager.publish(
129      obtainLocalEvent(WINDOW_SHOW_HIDE_EVENT, {
130        windowName: name,
131        isShow: false,
132      })
133    );
134    Log.showInfo(TAG, `hide window[${name}] success.`);
135  }
136
137  getWindowInfo(name: WindowType): WindowInfo | undefined {
138    return this.mWindowInfos.get(name);
139  }
140
141  // function need remove
142  setWindowInfo(configInfo) {
143    Log.showDebug(TAG, `setWindowInfo, configInfo ${JSON.stringify(configInfo)}`);
144    let maxWidth = AppStorage.SetAndLink("maxWidth", configInfo.maxWidth);
145    let maxHeight = AppStorage.SetAndLink("maxHeight", configInfo.maxHeight);
146    let minHeight = AppStorage.SetAndLink("minHeight", configInfo.minHeight);
147    maxWidth.set(configInfo.maxWidth);
148    maxHeight.set(configInfo.maxHeight);
149    minHeight.set(configInfo.minHeight);
150  }
151}
152
153let sWindowManager = createOrGet(WindowManager, TAG);
154export default sWindowManager as WindowManager;
155