• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, WindowType, WindowEventId } from "../util/WindowConst"
20
21const windowPoint = {
22  x: 50, // 窗口移动的起始坐标X
23  y: 250, // 窗口移动的起始坐标Y
24}
25
26class WindowManger {
27  private TAG: string = 'WindowManger'
28
29  initMainWindow(windowStage: window.WindowStage) {
30    windowStage.getMainWindow((err, data) => {
31      if (err.code) {
32        Logger.error(this.TAG, 'Failed to obtain the main window. Cause: ' + JSON.stringify(err))
33        return
34      }
35      let mainWindow = data
36      // 窗口规避区域
37      mainWindow.on('avoidAreaChange', ({type, area}) => {
38        if (type === window.AvoidAreaType.TYPE_SYSTEM) {
39          AppStorage.SetOrCreate<number>("topHeight", area.topRect.height)
40          AppStorage.SetOrCreate<number>("topWidth", area.topRect.width)
41        }
42      })
43      mainWindow.getAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
44      // 设置主窗口沉浸式
45      mainWindow.setLayoutFullScreen(true)
46      // 设置主窗口导航栏、状态栏、文字颜色等属性
47      let sysBarProps = {
48        statusBarColor: WindowColor.statusBarColor,
49        navigationBarColor: WindowColor.navigationBarColor,
50        statusBarContentColor: WindowColor.statusBarContentColor,
51        navigationBarContentColor: WindowColor.navigationBarContentColor
52      }
53      // 加载状态变量
54      mainWindow.setSystemBarProperties(sysBarProps)
55    })
56  }
57
58  async initSubWindow(windowStage: window.WindowStage, windowAttribute: WindowType) {
59    // 创建应用子窗口
60    let subWindow = await windowStage.createSubWindow("mySubWindow")
61    subWindow.on('avoidAreaChange', ({type, area}) => {
62      if (type === window.AvoidAreaType.TYPE_SYSTEM) {
63        AppStorage.SetOrCreate<number>("topHeight", area.topRect.height)
64        AppStorage.SetOrCreate<number>("bottomHeight", area.bottomRect.height)
65      }
66    })
67    Logger.info('show')
68    subWindow.resize(320, 240) // 长320vp,宽240vp
69    subWindow.moveWindowTo(10, 500) // 移动至坐标x为10,y为500的位置
70    subWindow.setUIContent("pages/SubWindowPage")
71    subWindow.setWindowTouchable(true)
72    subWindow.showWindow()
73
74    // onTouch的坐标绑定
75    let innerEvent = {
76      eventId: WindowEventId.SUB_WINDOW_INNER_EVENT_ID
77    }
78    let callback = (eventData) => {
79      Logger.info(this.TAG, 'onTouchEventData' + eventData.data.x)
80      subWindow.moveWindowTo(windowPoint.x + eventData.data.x, windowPoint.y + eventData.data.y)
81    }
82    emitter.on(innerEvent, callback)
83  }
84
85  async setSubWindowAttribute(windowStage: window.WindowStage, windowAttribute: WindowType) {
86    let subWindow: window.Window = await windowStage.getMainWindow()
87    await subWindow.moveWindowTo(windowAttribute.moveToWidth, windowAttribute.moveToHeight)
88    // 设置子窗口为可触状态
89    await subWindow.setWindowTouchable(windowAttribute.setTouchable)
90    // 设置子窗口的大小
91    await subWindow.resize(windowAttribute.resetSizeWidth, windowAttribute.resetSizeHeight)
92    // 设置子窗口亮度
93    await subWindow.setWindowBrightness(windowAttribute.setBrightness)
94    // 设置子窗口为隐私模式
95    await subWindow.setWindowPrivacyMode(windowAttribute.setPrivacyMode)
96  }
97
98  changeWindowDirection(windowStage: window.WindowStage, orientation: window.Orientation) {
99    windowStage.getMainWindow((err, data) => {
100      if (err.code) {
101        Logger.error(this.TAG, 'Failed to change the window: ' + JSON.stringify(err))
102        return
103      }
104      data.setPreferredOrientation(orientation)
105    })
106  }
107}
108
109export default new WindowManger()