WindowModel.ets 4.78 KB
import window from '@ohos.window';
import { AsyncCallback, BusinessError } from '@ohos.base';
import deviceInfo from '@ohos.deviceInfo'
import display from '@ohos.display';


interface SystemBarProperties {
  statusBarColor?: string;
  isStatusBarLightIcon?: boolean;
  statusBarContentColor?: string;
  navigationBarColor?: string;
  isNavigationBarLightIcon?: boolean;
  navigationBarContentColor?: string;
}

export class Size {
  width: number = 0
  height: number = 0

  constructor(width: number, height: number) {
    this.width = width;
    this.height = height;
  }
}

export class WindowModel {
  private windowStage?: window.WindowStage;
  private windowClass?: window.Window;
  private isFullScreen: boolean = false
  static shared: WindowModel = new WindowModel()
  static TAG = "WindowModel";

  setWindowStage(windowStage: window.WindowStage) {
    this.windowStage = windowStage;
    this.windowClass = windowStage.getMainWindowSync();
  }

  getWindowStage(): window.WindowStage {
    return this.windowStage as window.WindowStage
  }

  getWindowClass(): window.Window {
    return this.windowClass as window.Window
  }

  /**
   * @deprecated 应用整体全屏,不需要再设置全屏与否了。
   */
  setMainWindowFullScreen(fullScreen: boolean) {
    if (deviceInfo.deviceType != "phone") {
      return
    }
    if (this.windowStage === undefined) {
      return;
    }
    this.windowStage.getMainWindow((err, windowClass: window.Window) => {
      if (err.code) {
        return;
      }
      windowClass.setWindowLayoutFullScreen(fullScreen, (err) => {
        if (err.code) {
          return;
        }
      });
      windowClass.setWindowSystemBarEnable(fullScreen ? [] : ['status', 'navigation'], (err) => {
        if (err.code) {
          return;
        }
      });
    });
  }

  getWindowSize(): Promise<Size> {
    return new Promise((resolve, reject) => {
      if (!this.windowStage) {
        let dis = display.getDefaultDisplaySync();
        reject(new Size(dis.width, dis.height))
        return;
      }
      let rect = this.windowStage.getMainWindowSync().getWindowProperties().windowRect;
      resolve(new Size(rect.width, rect.height));
    });
  }

  setWindowKeepScreenOn(isKeepScreenOn: boolean) {
    this.windowStage?.getMainWindow((err, windowClass: window.Window) => {
      windowClass?.setWindowKeepScreenOn(isKeepScreenOn, (err: BusinessError) => {
        const errCode: number = err.code;
        if (errCode) {
          console.error(WindowModel.TAG + '设置屏幕常亮:' + isKeepScreenOn + ',失败: ' + JSON.stringify(err));
          return;
        }
        console.info(WindowModel.TAG + '设置屏幕常亮:' + isKeepScreenOn + ",成功");
      })
    })
  }

  /**
   * 设置窗口的显示方向属性
   * @param orientation
   */
  setPreferredOrientation(orientation: window.Orientation): Promise<void> {
    return new Promise((resolve, reject) => {
      if (!this.windowStage) {
        console.error('Failed, the main window is empty');
        reject("Failed, the main window is empty")
        return;
      }
      this.windowStage.getMainWindow((err: BusinessError, windowClass: window.Window) => {
        if (err.code) {
          console.error('Failed to obtain the main window. Cause: ' + err.message);
          reject(err)
          return;
        }
        // 2.设置窗口的显示方向属性,使用Promise异步回调。
        windowClass.setPreferredOrientation(orientation).then(() => {
          console.info('Succeeded in setting the window orientation.');
          resolve()
        }).catch((err: BusinessError) => {
          console.error('Failed to set the window orientation. Cause: ' + err.message);
          reject(err)
        });
      });
    })
  }

  /**
   * 设置窗口全屏模式时窗口内导航栏、状态栏的属性,使用callback异步回调。
   * @param systemBarProperties
   * @param callback
   */
  setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback?: AsyncCallback<void>): void {
    this.windowClass?.setWindowSystemBarProperties(systemBarProperties, (err: BusinessError) => {
      callback && callback(err)
    })
  }

  /**
   * 状态栏显示设置
   */
  setSpecificSystemBarEnabled(visible: boolean) {
    this.windowClass?.setSpecificSystemBarEnabled('status', visible)
  }

  setWindowSystemBarEnable(names: Array<'status' | 'navigation'>) {
    this.windowClass?.setWindowSystemBarEnable(names)
  }

  getWindowProperties() {
    return this.windowClass?.getWindowProperties()
  }

  /**
   * @deprecated 应用整体全屏,不需要再设置全屏与否了。
   */
  setWindowLayoutFullScreen(isFullScreen: boolean) {
    this.isFullScreen = isFullScreen
    this.windowClass?.setWindowLayoutFullScreen(isFullScreen)
  }

  getIsFullScreen(): boolean {
    return this.isFullScreen
  }
}