1/* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import display from '@ohos.display' 17import promptAction from '@ohos.promptAction'; 18import CommonEvent from '@ohos.commonEvent' 19import Window from '@ohos.window' 20import { EventConstants,Logger } from '@ohos/base' 21 22const TAG: string = 'WindowManager' 23 24export const WINDOW_NAMES = { 25 home: 'Home', 26 recents: 'RecentsPage', 27 form: 'FormPage', 28} 29 30export const WINDOW_PAGES = { 31 home: 'pages/Home', 32 recents: 'pages/RecentsPage', 33 form: 'pages/FormPage', 34} 35 36const SUBSCRIBER_INFO = { 37 events: [ 38 EventConstants.EVENT_ENTER_HOME, 39 EventConstants.EVENT_ENTER_RECENTS, 40 EventConstants.EVENT_CLEAR_RECENTS, 41 EventConstants.EVENT_ENTER_FORM_MANAGER 42 ] 43} 44 45export default class WindowManager { 46 public mWindowSize: number[] = [720, 1080] 47 private subscriber = undefined 48 private context: any = undefined 49 50 constructor(context) { 51 this.context = context 52 } 53 54 subscribeCallBack = async (err, data) => { 55 if (err.code) { 56 Logger.error(TAG, `subscribe, failed: ${JSON.stringify(err)}`) 57 return 58 } 59 Logger.info(TAG, `subscribe, ${JSON.stringify(data)}`) 60 switch (data.event) { 61 case EventConstants.EVENT_CLEAR_RECENTS: 62 let uninstallMessage: string = this.context.resourceManager.getStringSync($r('app.string.clear_all_missions_message').id); 63 promptAction.showToast({ 64 message: uninstallMessage 65 }); 66 this.hideWindow(WINDOW_NAMES.recents); 67 break; 68 case EventConstants.EVENT_ENTER_HOME: 69 this.hideWindow(WINDOW_NAMES.recents); 70 this.hideWindow(WINDOW_NAMES.form); 71 break 72 case EventConstants.EVENT_ENTER_RECENTS: 73 this.showOrCreateWindow(WINDOW_NAMES.recents, WINDOW_PAGES.recents, true); 74 break 75 case EventConstants.EVENT_ENTER_FORM_MANAGER: 76 this.showOrCreateWindow(WINDOW_NAMES.form, WINDOW_PAGES.form, true); 77 break 78 default: 79 break 80 } 81 } 82 83 async registerWindowEvent() { 84 this.subscriber = await CommonEvent.createSubscriber(SUBSCRIBER_INFO) 85 CommonEvent.subscribe(this.subscriber, this.subscribeCallBack) 86 } 87 88 async getWindowSize() { 89 let displayData = await display.getDefaultDisplay() 90 this.mWindowSize = [displayData.width, displayData.height] 91 return this.mWindowSize 92 } 93 94 /** 95 * 设置窗口大小 96 * 97 * @param width 窗口宽度 98 * @param width 窗口高度 99 */ 100 async setWindowSize(width: number, height: number): Promise<void> { 101 const abilityWindow = await Window.getLastWindow(this.context); 102 void abilityWindow.resetSize(width, height); 103 } 104 105 async createWindow(windowName: string, page: string, isFullScreen) { 106 Logger.info(TAG, `createWindow, name: ${windowName} page: ${page}`) 107 try { 108 let displayData = await display.getDefaultDisplay() 109 let window = await Window.create(this.context, windowName, Window.WindowType.TYPE_DESKTOP) 110 await window.resetSize(displayData.height, displayData.width) 111 await window.loadContent(page) 112 if (isFullScreen) { 113 await window.setFullScreen(true) 114 } 115 await window.show() 116 } catch (error) { 117 Logger.error(TAG, `createWindow, create error: ${JSON.stringify(error)}`) 118 } 119 } 120 121 async showOrCreateWindow(windowName: string, page: string, isFullScreen) { 122 Logger.info(TAG, `showOrCreateWindow, name ${windowName}`) 123 try { 124 let window = await Window.find(windowName) 125 await window.show() 126 } catch (error) { 127 Logger.error(TAG, `showWindow, show error: ${JSON.stringify(error)}`) 128 await this.createWindow(windowName, page, isFullScreen) 129 } 130 } 131 132 async hideWindow(windowName: string) { 133 Logger.info(TAG, 'hideWindow'); 134 try { 135 let window = await Window.find(windowName); 136 await window.destroy(); 137 } catch (error) { 138 Logger.error(TAG, `hideWindow, show error: ${JSON.stringify(error)}`); 139 } 140 } 141 142 async destoryWindow(windowName: string) { 143 Logger.info(TAG, 'destoryWindow') 144 try { 145 let window = await Window.find(windowName); 146 await window.destroy(); 147 } catch (error) { 148 Logger.error(TAG, `destoryWindow, show error: ${JSON.stringify(error)}`); 149 } 150 } 151}