1/* 2 * Copyright (c) 2022 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 CommonEvent from '@ohos.commonEvent' 18import Window from '@ohos.window' 19import prompt from '@ohos.prompt' 20import { EventConstants,Logger } from '@ohos/base' 21 22const TAG: string = 'WindowManager' 23 24export const WINDOW_NAMES = { 25 HOME: 'Home', 26 RECENTS: 'RecentsPage', 27} 28 29export const WINDOW_PAGES = { 30 HOME: 'pages/Home', 31 RECENTS: 'pages/RecentsPage', 32} 33 34const SUBSCRIBER_INFO = { 35 events: [ 36 EventConstants.EVENT_ENTER_HOME, 37 EventConstants.EVENT_ENTER_RECENTS, 38 EventConstants.EVENT_CLEAR_RECENTS 39 ] 40} 41 42export default class WindowManager { 43 public mWindowSize: number[] = [720, 1080] 44 private subscriber = undefined 45 private context: any = undefined 46 47 constructor(context) { 48 this.context = context 49 } 50 51 subscribeCallBack = async (err, data) => { 52 if (err.code) { 53 Logger.error(TAG, `subscribe, failed: ${JSON.stringify(err)}`) 54 return 55 } 56 Logger.info(TAG, `subscribe, ${JSON.stringify(data)}`) 57 switch (data.event) { 58 case EventConstants.EVENT_CLEAR_RECENTS: 59 let message = this.context.resourceManager.getStringSync($r('app.string.clear_all_missions_message').id) 60 prompt.showToast({ 61 message: message 62 }) 63 case EventConstants.EVENT_ENTER_HOME: 64 this.hideWindow(WINDOW_NAMES.RECENTS) 65 this.showOrCreateWindow(WINDOW_NAMES.HOME, WINDOW_PAGES.HOME, false) 66 break 67 case EventConstants.EVENT_ENTER_RECENTS: 68 this.showOrCreateWindow(WINDOW_NAMES.RECENTS, WINDOW_PAGES.RECENTS, true) 69 break 70 default: 71 break 72 } 73 } 74 75 async registerWindowEvent() { 76 this.subscriber = await CommonEvent.createSubscriber(SUBSCRIBER_INFO) 77 CommonEvent.subscribe(this.subscriber, this.subscribeCallBack) 78 } 79 80 async getWindowSize() { 81 let displayData = await display.getDefaultDisplay() 82 this.mWindowSize = [displayData.width, displayData.height] 83 return this.mWindowSize 84 } 85 86 /** 87 * 设置窗口大小 88 * 89 * @param width 窗口宽度 90 * @param width 窗口高度 91 */ 92 async setWindowSize(width: number, height: number): Promise<void> { 93 const abilityWindow = await Window.getTopWindow() 94 void abilityWindow.resetSize(width, height) 95 } 96 97 async createWindow(windowName: string, page: string, isFullScreen) { 98 Logger.info(TAG, `createWindow, name: ${windowName} page: ${page}`) 99 try { 100 let displayData = await display.getDefaultDisplay() 101 let window = await Window.create(this.context, windowName, Window.WindowType.TYPE_DESKTOP) 102 await window.resetSize(displayData.height, displayData.width) 103 await window.loadContent(page) 104 if (isFullScreen) { 105 await window.setFullScreen(true) 106 } 107 await window.show() 108 } catch (error) { 109 Logger.error(TAG, `createWindow, create error: ${JSON.stringify(error)}`) 110 } 111 } 112 113 async showOrCreateWindow(windowName: string, page: string, isFullScreen) { 114 Logger.info(TAG, `showOrCreateWindow, name ${windowName}`) 115 try { 116 let window = await Window.find(windowName) 117 await window.show() 118 } catch (error) { 119 Logger.error(TAG, `showWindow, show error: ${JSON.stringify(error)}`) 120 await this.createWindow(windowName, page, isFullScreen) 121 } 122 } 123 124 async hideWindow(windowName: string) { 125 Logger.info(TAG, 'hideWindow') 126 try { 127 let window = await Window.find(windowName) 128 await window.hide() 129 } catch (error) { 130 Logger.error(TAG, `showWindow, show error: ${JSON.stringify(error)}`) 131 } 132 } 133 134 async destoryWindow(windowName: string) { 135 Logger.info(TAG, 'destoryWindow') 136 try { 137 let window = await Window.find(windowName) 138 await window.destroy() 139 } catch (error) { 140 Logger.error(TAG, `showWindow, show error: ${JSON.stringify(error)}`) 141 } 142 } 143}