HarmonyOSBaseLibs/src/main/ets/utils/WindowHelper.ets
徐勤民 348174c4c8 fix
2024-10-15 18:47:21 +08:00

172 行
4.7 KiB
Plaintext

import { display, window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
export class WindowHelper {
/**
* 缓存窗体,关闭时需要
* 同时只能出现一个窗口,所以只做一个缓存就可以
*/
private static cacheWindow: window.Window | null = null;
/**
* 根据参数创建窗口
* @param options
* @returns
*/
static async open(options: WinOptions): Promise<void> {
if (WindowHelper.cacheWindow) {
options.callBack && options.callBack(-1, '窗口已存在')
return
}
if (!options) {
options = new WinOptions();
}
if (!options.name) {
options.name = 'window';
}
if (options.windowType == undefined) {
options.windowType = window.WindowType.TYPE_DIALOG;
}
if (!options.bgColor) {
options.bgColor = '#33606266';
}
try {
//创建窗口
let windowClass = await window.createWindow({
name: options.name,
windowType: options.windowType,
ctx: getContext() as common.UIAbilityContext
});
//将窗口缓存
WindowHelper.cacheWindow = windowClass;
await windowClass.setUIContent(options.router);
//获取屏幕四大角
let d = display.getDefaultDisplaySync();
//设置窗口大小
await windowClass.resize(d.width, d.height);
// 设置窗口背景颜色
windowClass.setWindowBackgroundColor(options.bgColor);
//显示窗口
await windowClass.showWindow();
} catch (exception) {
options.callBack && options.callBack(-1, '创建窗口失败,原因为:' + JSON.stringify(exception))
}
}
/**
* 关闭窗口
* @returns
*/
static async close(): Promise<void> {
if (WindowHelper.cacheWindow) {
await WindowHelper.cacheWindow.destroyWindow();
WindowHelper.cacheWindow = null
}
}
private static _windowClass: window.Window | undefined = undefined;
private static _isFullScreen: boolean = false;
private static _topRectHeight: number = 0;
private static _bottomRectHeight: number = 0;
/**
* 设置窗口管理器
* @param value 在AppAbility中设置
*/
public static set windowClass(value: window.Window | undefined) {
WindowHelper._windowClass = value;
if (WindowHelper._windowClass) {
let avoidArea = WindowHelper._windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
WindowHelper._bottomRectHeight = px2vp(avoidArea.bottomRect.height)
let avoidArea2 = WindowHelper._windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT);
WindowHelper._topRectHeight = px2vp(avoidArea2.topRect.height)
let avoidArea3 = WindowHelper._windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
const a = px2vp(avoidArea3.topRect.height)
WindowHelper._topRectHeight = a > WindowHelper._topRectHeight ? a : WindowHelper._topRectHeight
// console.log('=====>', WindowHelper._topRectHeight)
}
}
/**
* 获取当前窗口管理器
* @returns
*/
public static get windowClass(): window.Window | undefined {
return WindowHelper._windowClass;
}
/**
* 获取底部安全区高度
* @returns
*/
public static get bottomRectHeight(): number {
return WindowHelper._bottomRectHeight
}
/**
* 获取顶部安全区高度
* @returns
*/
public static get topRectHeight(): number {
return WindowHelper._isFullScreen ? WindowHelper._topRectHeight : 0
}
/**
* 设置是否全屏
* @param isLayoutFullScreen
*/
static setWindowLayoutFullScreen(isLayoutFullScreen: boolean) {
if (WindowHelper._windowClass) {
WindowHelper._isFullScreen = isLayoutFullScreen
WindowHelper._windowClass.setWindowLayoutFullScreen(isLayoutFullScreen);
}
}
/**
* 隐藏状态栏
*/
static hideStatusBar() {
if (WindowHelper._windowClass) {
WindowHelper._windowClass.setSpecificSystemBarEnabled('status', false);
WindowHelper._windowClass.setSpecificSystemBarEnabled('navigationIndicator', false);
}
}
/**
* 显示状态栏
*/
static showStatusBar() {
if (WindowHelper._windowClass) {
WindowHelper._windowClass.setSpecificSystemBarEnabled('status', true);
WindowHelper._windowClass.setSpecificSystemBarEnabled('navigationIndicator', true);
}
}
}
/**
* 窗口入参对象
*/
class WinOptions {
/**
* 窗口名称 默认window
*/
name?: string;
/**
* 窗口类型 默认TYPE_DIALOG
*/
windowType?: window.WindowType;
/**
*窗口要显示的路由 如:pages/Welcome需要在main_pages.json中声明
*/
router: string = '';
/**
* 窗口背景颜色,默认#33606266
*/
bgColor?: string;
/**
* 窗口创建回调函数
*/
callBack?: (code: number, msg: string) => void;
}