WindowHelper.ets 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { display, window } from '@kit.ArkUI';
  2. import { common } from '@kit.AbilityKit';
  3. import { GlobalContext } from '../ContextConfig';
  4. export class WindowHelper {
  5. private constructor() {
  6. }
  7. /**
  8. * 缓存窗体,关闭时需要
  9. * 同时只能出现一个窗口,所以只做一个缓存就可以
  10. */
  11. private static cacheWindow: window.Window | null = null;
  12. /**
  13. * 根据参数创建窗口
  14. * @param options
  15. * @returns
  16. */
  17. static async open(options: WinOptions): Promise<void> {
  18. if (WindowHelper.cacheWindow) {
  19. options.callBack && options.callBack(-1, '窗口已存在')
  20. return
  21. }
  22. if (!options) {
  23. options = new WinOptions();
  24. }
  25. if (!options.name) {
  26. options.name = 'window';
  27. }
  28. if (options.windowType == undefined) {
  29. options.windowType = window.WindowType.TYPE_DIALOG;
  30. }
  31. if (!options.bgColor) {
  32. options.bgColor = '#33606266';
  33. }
  34. try {
  35. //创建窗口
  36. let windowClass = await window.createWindow({
  37. name: options.name,
  38. windowType: options.windowType,
  39. ctx: getContext() as common.UIAbilityContext
  40. });
  41. //将窗口缓存
  42. WindowHelper.cacheWindow = windowClass;
  43. await windowClass.setUIContent(options.router);
  44. //获取屏幕四大角
  45. let d = display.getDefaultDisplaySync();
  46. //设置窗口大小
  47. await windowClass.resize(d.width, d.height);
  48. // 设置窗口背景颜色
  49. windowClass.setWindowBackgroundColor(options.bgColor);
  50. //显示窗口
  51. await windowClass.showWindow();
  52. } catch (exception) {
  53. options.callBack && options.callBack(-1, '创建窗口失败,原因为:' + JSON.stringify(exception))
  54. }
  55. }
  56. /**
  57. * 关闭窗口
  58. * @returns
  59. */
  60. static async close(): Promise<void> {
  61. if (WindowHelper.cacheWindow) {
  62. await WindowHelper.cacheWindow.destroyWindow();
  63. WindowHelper.cacheWindow = null
  64. }
  65. }
  66. private static _windowClass: window.Window | undefined = undefined;
  67. private static _isFullScreen: boolean = false;
  68. private static _topRectHeight: number = 0;
  69. private static _bottomRectHeight: number = 0;
  70. /**
  71. * 设置窗口管理器
  72. * @param value 在AppAbility中设置
  73. */
  74. public static set windowClass(value: window.Window | undefined) {
  75. WindowHelper._windowClass = value;
  76. if (value) {
  77. GlobalContext.setUiContext(value.getUIContext())
  78. }
  79. if (WindowHelper._windowClass) {
  80. let avoidArea = WindowHelper._windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
  81. WindowHelper._bottomRectHeight = px2vp(avoidArea.bottomRect.height)
  82. let avoidArea2 = WindowHelper._windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT);
  83. WindowHelper._topRectHeight = px2vp(avoidArea2.topRect.height)
  84. let avoidArea3 = WindowHelper._windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
  85. const a = px2vp(avoidArea3.topRect.height)
  86. WindowHelper._topRectHeight = a > WindowHelper._topRectHeight ? a : WindowHelper._topRectHeight
  87. }
  88. }
  89. /**
  90. * 获取当前窗口管理器
  91. * @returns
  92. */
  93. public static get windowClass(): window.Window | undefined {
  94. return WindowHelper._windowClass;
  95. }
  96. /**
  97. * 获取底部安全区高度
  98. * @returns
  99. */
  100. public static get bottomRectHeight(): number {
  101. return WindowHelper._bottomRectHeight
  102. }
  103. /**
  104. * 获取顶部安全区高度
  105. * @returns
  106. */
  107. public static get topRectHeight(): number {
  108. return WindowHelper._isFullScreen ? WindowHelper._topRectHeight : 0
  109. }
  110. public static get windowsWidth() {
  111. return px2vp(display.getDefaultDisplaySync().width)
  112. }
  113. public static get windowsHeight() {
  114. return px2vp(display.getDefaultDisplaySync().height)
  115. }
  116. /**
  117. * 设置是否全屏
  118. * @param isLayoutFullScreen
  119. */
  120. static setWindowLayoutFullScreen(isLayoutFullScreen: boolean) {
  121. if (WindowHelper._windowClass) {
  122. WindowHelper._isFullScreen = isLayoutFullScreen
  123. WindowHelper._windowClass.setWindowLayoutFullScreen(isLayoutFullScreen);
  124. }
  125. }
  126. /**
  127. * 隐藏状态栏
  128. */
  129. static hideStatusBar() {
  130. if (WindowHelper._windowClass) {
  131. WindowHelper._windowClass.setSpecificSystemBarEnabled('status', false);
  132. WindowHelper._windowClass.setSpecificSystemBarEnabled('navigationIndicator', false);
  133. }
  134. }
  135. /**
  136. * 显示状态栏
  137. */
  138. static showStatusBar() {
  139. if (WindowHelper._windowClass) {
  140. WindowHelper._windowClass.setSpecificSystemBarEnabled('status', true);
  141. WindowHelper._windowClass.setSpecificSystemBarEnabled('navigationIndicator', true);
  142. }
  143. }
  144. /**
  145. * 设置状态栏内容颜色
  146. * #ffffff
  147. * 不可以用white这种
  148. */
  149. static setStatusBar(color: string) {
  150. if (WindowHelper._windowClass) {
  151. WindowHelper._windowClass.setWindowSystemBarProperties({
  152. statusBarContentColor: color,
  153. })
  154. }
  155. }
  156. }
  157. /**
  158. * 窗口入参对象
  159. */
  160. class WinOptions {
  161. /**
  162. * 窗口名称 默认window
  163. */
  164. name?: string;
  165. /**
  166. * 窗口类型 默认TYPE_DIALOG
  167. */
  168. windowType?: window.WindowType;
  169. /**
  170. *窗口要显示的路由 如:pages/Welcome需要在main_pages.json中声明
  171. */
  172. router: string = '';
  173. /**
  174. * 窗口背景颜色,默认#33606266
  175. */
  176. bgColor?: string;
  177. /**
  178. * 窗口创建回调函数
  179. */
  180. callBack?: (code: number, msg: string) => void;
  181. }