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