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