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