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 window from '@ohos.window'; 17import emitter from '@ohos.events.emitter'; 18import Logger from '../util/Logger'; 19import { WindowColor, WindowEventId } from '../util/WindowConst'; 20 21const windowPoint = { 22 x: 50, // 窗口移动的起始坐标X 23 y: 250, // 窗口移动的起始坐标Y 24}; 25const WIDTH = 320; 26const HEIGHT = 240; 27const MOVE_X = 10; 28const MOVE_Y = 500; 29 30class WindowType { 31 moveToWidth: number; 32 moveToHeight: number; 33 setTouchable: boolean; 34 resetSizeWidth: number; 35 resetSizeHeight: number; 36 setPrivacyMode: boolean; 37 setBrightness: number; 38} 39 40class WindowManger { 41 private tag: string = 'WindowManger'; 42 43 initMainWindow(windowStage: window.WindowStage) { 44 windowStage.getMainWindow((err, data) => { 45 if (err.code) { 46 Logger.error(this.tag, 'Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 47 return; 48 } 49 let mainWindow = data; 50 // 窗口规避区域 51 mainWindow.on('avoidAreaChange', ({type, area}) => { 52 if (type === window.AvoidAreaType.TYPE_SYSTEM) { 53 AppStorage.SetOrCreate<number>('topHeight', area.topRect.height); 54 AppStorage.SetOrCreate<number>('topWidth', area.topRect.width); 55 } 56 }); 57 mainWindow.getAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); 58 // 设置主窗口沉浸式 59 mainWindow.setLayoutFullScreen(true); 60 // 设置主窗口导航栏、状态栏、文字颜色等属性 61 let sysBarProps = { 62 statusBarColor: WindowColor.statusBarColor, 63 navigationBarColor: WindowColor.navigationBarColor, 64 statusBarContentColor: WindowColor.statusBarContentColor, 65 navigationBarContentColor: WindowColor.navigationBarContentColor 66 }; 67 // 加载状态变量 68 mainWindow.setSystemBarProperties(sysBarProps); 69 }); 70 } 71 72 async initSubWindow(windowStage: window.WindowStage, windowAttribute: WindowType) { 73 // 创建应用子窗口 74 let subWindow = await windowStage.createSubWindow('mySubWindow'); 75 subWindow.on('avoidAreaChange', ({type, area}) => { 76 if (type === window.AvoidAreaType.TYPE_SYSTEM) { 77 AppStorage.SetOrCreate<number>('topHeight', area.topRect.height); 78 AppStorage.SetOrCreate<number>('bottomHeight', area.bottomRect.height); 79 } 80 }); 81 try { 82 subWindow.setWindowFocusable(true, (err) => { 83 if (err.code) { 84 console.error('Failed to set the window to be focusable. Cause:' + JSON.stringify(err)); 85 return; 86 } 87 console.info('Succeeded in setting the window to be focusable.'); 88 }); 89 subWindow.on('windowEvent', (data) => { 90 console.info('Sub Window event happened. Event:' + JSON.stringify(data)); 91 let message = null; 92 switch (JSON.stringify(data)) { 93 case '1': 94 message = $r('app.string.foreground'); 95 break; 96 case '2': 97 message = $r('app.string.get_focus'); 98 break; 99 case '3': 100 message = $r('app.string.lose_focus'); 101 break; 102 case '4': 103 message = $r('app.string.background'); 104 break; 105 default: 106 message = $r('app.string.unknown'); 107 break; 108 } 109 AppStorage.SetOrCreate('focusText', message); 110 }); 111 } catch (exception) { 112 console.error('Failed to register callback. Cause: ' + JSON.stringify(exception)); 113 } 114 115 try { 116 windowStage.on('windowStageEvent', (data) => { 117 console.info('Succeeded in enabling the listener for window stage event changes. Data: ' + 118 JSON.stringify(data)); 119 }); 120 } catch (exception) { 121 console.error('Failed to enable the listener for window stage event changes. Cause:' + 122 JSON.stringify(exception)); 123 }; 124 125 Logger.info('show'); 126 subWindow.resize(WIDTH, HEIGHT); 127 subWindow.moveWindowTo(MOVE_X, MOVE_Y); // 移动至坐标x为10,y为500的位置 128 subWindow.setUIContent('pages/SubWindowPage'); 129 subWindow.setWindowTouchable(true); 130 subWindow.showWindow(); 131 132 // onTouch的坐标绑定 133 let innerEvent = { 134 eventId: WindowEventId.SUB_WINDOW_INNER_EVENT_ID 135 } 136 let callback = (eventData) => { 137 Logger.info(this.tag, 'onTouchEventData' + eventData.data.x); 138 subWindow.moveWindowTo(windowPoint.x + eventData.data.x, windowPoint.y + eventData.data.y) 139 } 140 emitter.on(innerEvent, callback) 141 } 142 143 async setSubWindowAttribute(windowStage: window.WindowStage, windowAttribute: WindowType) { 144 let subWindow: window.Window = await windowStage.getMainWindow() 145 await subWindow.moveWindowTo(windowAttribute.moveToWidth, windowAttribute.moveToHeight) 146 // 设置子窗口为可触状态 147 await subWindow.setWindowTouchable(windowAttribute.setTouchable) 148 // 设置子窗口的大小 149 await subWindow.resize(windowAttribute.resetSizeWidth, windowAttribute.resetSizeHeight) 150 // 设置子窗口亮度 151 await subWindow.setWindowBrightness(windowAttribute.setBrightness) 152 // 设置子窗口为隐私模式 153 await subWindow.setWindowPrivacyMode(windowAttribute.setPrivacyMode) 154 } 155 156 changeWindowDirection(windowStage: window.WindowStage, orientation: window.Orientation) { 157 windowStage.getMainWindow((err, data) => { 158 if (err.code) { 159 Logger.error(this.tag, 'Failed to change the window: ' + JSON.stringify(err)); 160 return 161 } 162 data.setPreferredOrientation(orientation) 163 }); 164 } 165} 166 167export default new WindowManger()