1/* 2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 */ 15import Logger from '../utils/Logger'; 16import window from '@ohos.window'; 17import common from '@ohos.app.ability.common'; 18import display from '@ohos.display'; 19 20const TAG = 'FloatWindowController'; 21const FLOAT_SIZE = vp2px(120); 22 23/** 24 * 悬浮窗控制类 25 */ 26export default class FloatWindowController { 27 28 // 创建单例模式 29 private static mInstance: FloatWindowController | null = null; 30 31 public static getInstance(): FloatWindowController { 32 if (FloatWindowController.mInstance == null) { 33 FloatWindowController.mInstance = new FloatWindowController(); 34 } 35 return FloatWindowController.mInstance; 36 } 37 38 private constructor() { 39 // 私有化构造函数 40 } 41 42 private floatWindow: window.Window | null = null; 43 private windowStage: window.WindowStage | null = null; 44 45 //ability调用,监听windowStage状态变化 46 async initWindowStage(context: common.UIAbilityContext, windowStage: window.WindowStage) { 47 this.windowStage = windowStage; 48 let mainWin = await windowStage.getMainWindow(); 49 50 await mainWin.setWindowSystemBarProperties({ 51 statusBarColor: '#182431', 52 navigationBarColor: '#182431' 53 }); 54 55 windowStage.on('windowStageEvent', (event: window.WindowStageEventType) => { 56 if (event === window.WindowStageEventType.SHOWN) { 57 this.destroyFloatWindow(); 58 } else if (event === window.WindowStageEventType.HIDDEN) { 59 this.createAndShowFloatWindow(context); 60 } 61 }); 62 } 63 64 //此处有一个bug,当前ability调用windowStage.getMainWindow().hide()方法,会与上一个ability一起hide 65 async hideMain() { 66 if(this.windowStage){ 67 let mainWin: window.Window = await this.windowStage.getMainWindow(); 68 mainWin.hide(); 69 } 70 } 71 72 //创建悬浮窗 73 private async createAndShowFloatWindow(context: common.UIAbilityContext) { 74 if (context.isTerminating()) { 75 return; 76 } 77 78 Logger.info(TAG,` createAndShowWindow`); 79 try { 80 let w = display.getDefaultDisplaySync().width; 81 // 创建应用子窗口 82 this.floatWindow = await window.createWindow({ 83 name: 'DemoFloatWindow', 84 windowType: window.WindowType.TYPE_FLOAT, 85 ctx: context 86 }); 87 await this.floatWindow.moveWindowTo(w - FLOAT_SIZE, FLOAT_SIZE * 2); 88 await this.floatWindow.resize(FLOAT_SIZE, FLOAT_SIZE); 89 await this.floatWindow.setUIContent('pages/Float'); 90 this.floatWindow.setWindowBackgroundColor('#00000000'); 91 await this.floatWindow.showWindow(); 92 } catch (err) { 93 Logger.error(TAG,`createSubWindow failed, code is ${err.code}, message is ${err.message}`); 94 } 95 } 96 97 // 销毁悬浮窗 98 public async destroyFloatWindow() { 99 Logger.info(TAG,` destroyWindow`); 100 if (this.floatWindow) { 101 await this.floatWindow.destroyWindow(); 102 } 103 } 104} 105