WindowHelper.ets 5.0 KB

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