19b256929Sopenharmony_ci// @ts-nocheck
29b256929Sopenharmony_ci/*
39b256929Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
49b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
59b256929Sopenharmony_ci * you may not use this file except in compliance with the License.
69b256929Sopenharmony_ci * You may obtain a copy of the License at
79b256929Sopenharmony_ci *
89b256929Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
99b256929Sopenharmony_ci *
109b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
119b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
129b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139b256929Sopenharmony_ci * See the License for the specific language governing permissions and
149b256929Sopenharmony_ci * limitations under the License.
159b256929Sopenharmony_ci */
169b256929Sopenharmony_ci
179b256929Sopenharmony_ciimport Window from "@ohos.window";
189b256929Sopenharmony_ciimport {Log} from "./Log";
199b256929Sopenharmony_ciimport {sEventManager} from "./event/EventManager";
209b256929Sopenharmony_ciimport {obtainLocalEvent} from "./event/EventUtil";
219b256929Sopenharmony_ciimport {Rect} from "./Constants";
229b256929Sopenharmony_ci
239b256929Sopenharmony_ciexport type WindowInfo = {
249b256929Sopenharmony_ci  visibility: boolean;
259b256929Sopenharmony_ci  rect: Rect;
269b256929Sopenharmony_ci};
279b256929Sopenharmony_ciexport enum WindowType {
289b256929Sopenharmony_ci  STATUS_BAR = "SystemUi_StatusBar",
299b256929Sopenharmony_ci  NAVIGATION_BAR = "SystemUi_NavigationBar",
309b256929Sopenharmony_ci  DROPDOWN_PANEL = "SystemUi_DropdownPanel",
319b256929Sopenharmony_ci  NOTIFICATION_PANEL = "SystemUi_NotificationPanel",
329b256929Sopenharmony_ci  CONTROL_PANEL = "SystemUi_ControlPanel",
339b256929Sopenharmony_ci  VOLUME_PANEL = "SystemUi_VolumePanel",
349b256929Sopenharmony_ci  BANNER_NOTICE = 'SystemUi_BannerNotice'
359b256929Sopenharmony_ci}
369b256929Sopenharmony_ci
379b256929Sopenharmony_ciexport const WINDOW_SHOW_HIDE_EVENT = "WindowShowHideEvent";
389b256929Sopenharmony_ciexport const WINDOW_RESIZE_EVENT = "WindowResizeEvent";
399b256929Sopenharmony_ci
409b256929Sopenharmony_citype WindowHandle = typeof Window.Window;
419b256929Sopenharmony_ciconst TAG = "WindowManagerSc";
429b256929Sopenharmony_ciconst SYSTEM_WINDOW_TYPE_MAP: { [key in WindowType]: number } = {
439b256929Sopenharmony_ci  SystemUi_StatusBar: 2108,
449b256929Sopenharmony_ci  SystemUi_NavigationBar: 2112,
459b256929Sopenharmony_ci  SystemUi_DropdownPanel: 2109,
469b256929Sopenharmony_ci  SystemUi_NotificationPanel: 2111,
479b256929Sopenharmony_ci  SystemUi_ControlPanel: 2111,
489b256929Sopenharmony_ci  SystemUi_VolumePanel: 2111,
499b256929Sopenharmony_ci  SystemUi_BannerNotice: 2111,
509b256929Sopenharmony_ci};
519b256929Sopenharmony_ciconst DEFAULT_WINDOW_INFO: WindowInfo = {
529b256929Sopenharmony_ci  visibility: false,
539b256929Sopenharmony_ci  rect: { left: 0, top: 0, width: 0, height: 0 },
549b256929Sopenharmony_ci};
559b256929Sopenharmony_ci
569b256929Sopenharmony_ci/**
579b256929Sopenharmony_ci * Manage window size changes.
589b256929Sopenharmony_ci */
599b256929Sopenharmony_ciclass WindowManager {
609b256929Sopenharmony_ci  mWindowInfos: Map<WindowType, WindowInfo> = new Map();
619b256929Sopenharmony_ci
629b256929Sopenharmony_ci  static getInstance(): WindowManager {
639b256929Sopenharmony_ci    if (globalThis.WindowManager == null) {
649b256929Sopenharmony_ci      globalThis.WindowManager = new WindowManager();
659b256929Sopenharmony_ci    }
669b256929Sopenharmony_ci    return globalThis.WindowManager;
679b256929Sopenharmony_ci  }
689b256929Sopenharmony_ci
699b256929Sopenharmony_ci  async createWindow(context: any, name: WindowType, rect: Rect, loadContent: string): Promise<WindowHandle> {
709b256929Sopenharmony_ci    Log.showInfo(TAG, `createWindow name: ${name}, rect: ${JSON.stringify(rect)}, url: ${loadContent}`);
719b256929Sopenharmony_ci    let winHandle = null;
729b256929Sopenharmony_ci    try{
739b256929Sopenharmony_ci      winHandle = await Window.create(context, name, SYSTEM_WINDOW_TYPE_MAP[name]);
749b256929Sopenharmony_ci      await winHandle.moveTo(rect.left, rect.top);
759b256929Sopenharmony_ci      await winHandle.resetSize(rect.width, rect.height);
769b256929Sopenharmony_ci      await winHandle.loadContent(loadContent);
779b256929Sopenharmony_ci      this.mWindowInfos.set(name, { visibility: false, rect });
789b256929Sopenharmony_ci      Log.showInfo(TAG, `create window[${name}] success.`);
799b256929Sopenharmony_ci    } catch (err) {
809b256929Sopenharmony_ci      Log.showError(TAG, `create window[${name}] failed. error:${JSON.stringify(err)}`);
819b256929Sopenharmony_ci    }
829b256929Sopenharmony_ci    return winHandle;
839b256929Sopenharmony_ci  }
849b256929Sopenharmony_ci
859b256929Sopenharmony_ci  async resetSizeWindow(name: WindowType, rect: Rect): Promise<void> {
869b256929Sopenharmony_ci    Log.showInfo(TAG, `resetSizeWindow name: ${name}, rect: ${JSON.stringify(rect)}`);
879b256929Sopenharmony_ci    let window = null;
889b256929Sopenharmony_ci    try {
899b256929Sopenharmony_ci      window = await Window.find(name);
909b256929Sopenharmony_ci      await window.moveTo(rect.left, rect.top);
919b256929Sopenharmony_ci      await window.resetSize(rect.width, rect.height);
929b256929Sopenharmony_ci    } catch(err) {
939b256929Sopenharmony_ci      Log.showError(TAG, `resetSizeWindow failed. error:${JSON.stringify(err)}`);
949b256929Sopenharmony_ci    }
959b256929Sopenharmony_ci    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), rect });
969b256929Sopenharmony_ci    sEventManager.publish(
979b256929Sopenharmony_ci      obtainLocalEvent(WINDOW_RESIZE_EVENT, {
989b256929Sopenharmony_ci        windowName: name,
999b256929Sopenharmony_ci        rect,
1009b256929Sopenharmony_ci      })
1019b256929Sopenharmony_ci    );
1029b256929Sopenharmony_ci    Log.showInfo(TAG, `resize window[${name}] success.`);
1039b256929Sopenharmony_ci  }
1049b256929Sopenharmony_ci
1059b256929Sopenharmony_ci  async showWindow(name: WindowType): Promise<void> {
1069b256929Sopenharmony_ci    Log.showInfo(TAG, `showWindow name: ${name}`);
1079b256929Sopenharmony_ci    let window = null;
1089b256929Sopenharmony_ci    try {
1099b256929Sopenharmony_ci      window = await Window.find(name);
1109b256929Sopenharmony_ci      await window.show();
1119b256929Sopenharmony_ci    } catch (err) {
1129b256929Sopenharmony_ci      Log.showError(TAG, `showWindow failed. error:${JSON.stringify(err)}`);
1139b256929Sopenharmony_ci    }
1149b256929Sopenharmony_ci    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), visibility: true });
1159b256929Sopenharmony_ci    sEventManager.publish(
1169b256929Sopenharmony_ci      obtainLocalEvent(WINDOW_SHOW_HIDE_EVENT, {
1179b256929Sopenharmony_ci        windowName: name,
1189b256929Sopenharmony_ci        isShow: true,
1199b256929Sopenharmony_ci      })
1209b256929Sopenharmony_ci    );
1219b256929Sopenharmony_ci    Log.showInfo(TAG, `show window[${name}] success.`);
1229b256929Sopenharmony_ci  }
1239b256929Sopenharmony_ci
1249b256929Sopenharmony_ci  async hideWindow(name: WindowType): Promise<void> {
1259b256929Sopenharmony_ci    Log.showInfo(TAG, `hideWindow name: ${name}`);
1269b256929Sopenharmony_ci    let window = null;
1279b256929Sopenharmony_ci    try {
1289b256929Sopenharmony_ci      window = await Window.find(name);
1299b256929Sopenharmony_ci      await window.hide();
1309b256929Sopenharmony_ci    } catch (err) {
1319b256929Sopenharmony_ci      Log.showError(TAG, `hideWindow failed. error:${JSON.stringify(err)}`);
1329b256929Sopenharmony_ci    }
1339b256929Sopenharmony_ci    this.mWindowInfos.set(name, { ...(this.mWindowInfos.get(name) ?? DEFAULT_WINDOW_INFO), visibility: false });
1349b256929Sopenharmony_ci    sEventManager.publish(
1359b256929Sopenharmony_ci      obtainLocalEvent(WINDOW_SHOW_HIDE_EVENT, {
1369b256929Sopenharmony_ci        windowName: name,
1379b256929Sopenharmony_ci        isShow: false,
1389b256929Sopenharmony_ci      })
1399b256929Sopenharmony_ci    );
1409b256929Sopenharmony_ci    Log.showInfo(TAG, `hide window[${name}] success.`);
1419b256929Sopenharmony_ci  }
1429b256929Sopenharmony_ci
1439b256929Sopenharmony_ci  getWindowInfo(name: WindowType): WindowInfo | undefined {
1449b256929Sopenharmony_ci    return this.mWindowInfos.get(name);
1459b256929Sopenharmony_ci  }
1469b256929Sopenharmony_ci
1479b256929Sopenharmony_ci  // function need remove
1489b256929Sopenharmony_ci  setWindowInfo(configInfo) {
1499b256929Sopenharmony_ci    Log.showDebug(TAG, `setWindowInfo, configInfo ${JSON.stringify(configInfo)}`);
1509b256929Sopenharmony_ci    let maxWidth = AppStorage.SetAndLink("maxWidth", configInfo.maxWidth);
1519b256929Sopenharmony_ci    let maxHeight = AppStorage.SetAndLink("maxHeight", configInfo.maxHeight);
1529b256929Sopenharmony_ci    let minHeight = AppStorage.SetAndLink("minHeight", configInfo.minHeight);
1539b256929Sopenharmony_ci    maxWidth.set(configInfo.maxWidth);
1549b256929Sopenharmony_ci    maxHeight.set(configInfo.maxHeight);
1559b256929Sopenharmony_ci    minHeight.set(configInfo.minHeight);
1569b256929Sopenharmony_ci  }
1579b256929Sopenharmony_ci}
1589b256929Sopenharmony_ci
1599b256929Sopenharmony_ciexport let sWindowManager = WindowManager.getInstance();
1609b256929Sopenharmony_ci
161