123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- import { display, window } from '@kit.ArkUI';
- import { common } from '@kit.AbilityKit';
- export class WindowHelper {
- private constructor() {
- }
- /**
- * 缓存窗体,关闭时需要
- * 同时只能出现一个窗口,所以只做一个缓存就可以
- */
- 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);
- }
- }
- /**
- * 设置状态栏内容颜色
- * #ffffff
- * 不可以用white这种
- */
- static setStatusBar(color: string) {
- if (WindowHelper._windowClass) {
- WindowHelper._windowClass.setWindowSystemBarProperties({
- statusBarContentColor: color,
- })
- }
- }
- }
- /**
- * 窗口入参对象
- */
- 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;
- }
|