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