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 {sEventManager} from "./event/EventManager";
20import {obtainLocalEvent} from "./event/EventUtil";
21import {Rect} from "./Constants";
22
23export type WindowInfo = {
24  visibility: boolean;
25  rect: Rect;
26};
27export enum WindowType {
28  STATUS_BAR = "SystemUi_StatusBar",
29  NAVIGATION_BAR = "SystemUi_NavigationBar",
30  DROPDOWN_PANEL = "SystemUi_DropdownPanel",
31  NOTIFICATION_PANEL = "SystemUi_NotificationPanel",
32  CONTROL_PANEL = "SystemUi_ControlPanel",
33  VOLUME_PANEL = "SystemUi_VolumePanel",
34  BANNER_NOTICE = 'SystemUi_BannerNotice'
35}
36
37export const WINDOW_SHOW_HIDE_EVENT = "WindowShowHideEvent";
38export const WINDOW_RESIZE_EVENT = "WindowResizeEvent";
39
40type WindowHandle = typeof Window.Window;
41const TAG = "WindowManagerSc";
42const SYSTEM_WINDOW_TYPE_MAP: { [key in WindowType]: number } = {
43  SystemUi_StatusBar: 2108,
44  SystemUi_NavigationBar: 2112,
45  SystemUi_DropdownPanel: 2109,
46  SystemUi_NotificationPanel: 2111,
47  SystemUi_ControlPanel: 2111,
48  SystemUi_VolumePanel: 2111,
49  SystemUi_BannerNotice: 2111,
50};
51const DEFAULT_WINDOW_INFO: WindowInfo = {
52  visibility: false,
53  rect: { left: 0, top: 0, width: 0, height: 0 },
54};
55
56/**
57 * Manage window size changes.
58 */
59class WindowManager {
60  mWindowInfos: Map<WindowType, WindowInfo> = new Map();
61
62  static getInstance(): WindowManager {
63    if (globalThis.WindowManager == null) {
64      globalThis.WindowManager = new WindowManager();
65    }
66    return globalThis.WindowManager;
67  }
68
69  async createWindow(context: any, name: WindowType, rect: Rect, loadContent: string): Promise<WindowHandle> {
70    Log.showInfo(TAG, `createWindow name: ${name}, rect: ${JSON.stringify(rect)}, url: ${loadContent}`);
71    let winHandle = null;
72    try{
73      winHandle = await Window.create(context, name, SYSTEM_WINDOW_TYPE_MAP[name]);
74      await winHandle.moveTo(rect.left, rect.top);
75      await winHandle.resetSize(rect.width, rect.height);
76      await winHandle.loadContent(loadContent);
77      this.mWindowInfos.set(name, { visibility: false, rect });
78      Log.showInfo(TAG, `create window[${name}] success.`);
79    } catch (err) {
80      Log.showError(TAG, `create window[${name}] failed. error:${JSON.stringify(err)}`);
81    }
82    return winHandle;
83  }
84
85  async resetSizeWindow(name: WindowType, rect: Rect): Promise<void> {
86    Log.showInfo(TAG, `resetSizeWindow name: ${name}, rect: ${JSON.stringify(rect)}`);
87    let window = null;
88    try {
89      window = await Window.find(name);
90      await window.moveTo(rect.left, rect.top);
91      await window.resetSize(rect.width, rect.height);
92    } catch(err) {
93      Log.showError(TAG, `resetSizeWindow failed. error:${JSON.stringify(err)}`);
94    }
95    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), rect });
96    sEventManager.publish(
97      obtainLocalEvent(WINDOW_RESIZE_EVENT, {
98        windowName: name,
99        rect,
100      })
101    );
102    Log.showInfo(TAG, `resize window[${name}] success.`);
103  }
104
105  async showWindow(name: WindowType): Promise<void> {
106    Log.showInfo(TAG, `showWindow name: ${name}`);
107    let window = null;
108    try {
109      window = await Window.find(name);
110      await window.show();
111    } catch (err) {
112      Log.showError(TAG, `showWindow failed. error:${JSON.stringify(err)}`);
113    }
114    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), visibility: true });
115    sEventManager.publish(
116      obtainLocalEvent(WINDOW_SHOW_HIDE_EVENT, {
117        windowName: name,
118        isShow: true,
119      })
120    );
121    Log.showInfo(TAG, `show window[${name}] success.`);
122  }
123
124  async hideWindow(name: WindowType): Promise<void> {
125    Log.showInfo(TAG, `hideWindow name: ${name}`);
126    let window = null;
127    try {
128      window = await Window.find(name);
129      await window.hide();
130    } catch (err) {
131      Log.showError(TAG, `hideWindow failed. error:${JSON.stringify(err)}`);
132    }
133    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), visibility: false });
134    sEventManager.publish(
135      obtainLocalEvent(WINDOW_SHOW_HIDE_EVENT, {
136        windowName: name,
137        isShow: false,
138      })
139    );
140    Log.showInfo(TAG, `hide window[${name}] success.`);
141  }
142
143  getWindowInfo(name: WindowType): WindowInfo | undefined {
144    return this.mWindowInfos.get(name);
145  }
146
147  // function need remove
148  setWindowInfo(configInfo) {
149    Log.showDebug(TAG, `setWindowInfo, configInfo ${JSON.stringify(configInfo)}`);
150    let maxWidth = AppStorage.SetAndLink("maxWidth", configInfo.maxWidth);
151    let maxHeight = AppStorage.SetAndLink("maxHeight", configInfo.maxHeight);
152    let minHeight = AppStorage.SetAndLink("minHeight", configInfo.minHeight);
153    maxWidth.set(configInfo.maxWidth);
154    maxHeight.set(configInfo.maxHeight);
155    minHeight.set(configInfo.minHeight);
156  }
157}
158
159export let sWindowManager = WindowManager.getInstance();
160
161