WindowHelper.ets 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. try {
  63. await WindowHelper.cacheWindow.destroyWindow();
  64. } catch (error) {
  65. } finally {
  66. WindowHelper.cacheWindow = null
  67. }
  68. }
  69. }
  70. private static _windowClass: window.Window | undefined = undefined;
  71. private static _isFullScreen: boolean = false;
  72. private static _topRectHeight: number = 0;
  73. private static _bottomRectHeight: number = 0;
  74. /**
  75. * 设置窗口管理器
  76. * @param value 在AppAbility中设置
  77. */
  78. public static set windowClass(value: window.Window | undefined) {
  79. WindowHelper._windowClass = value;
  80. if (value) {
  81. try {
  82. const uc = value.getUIContext()
  83. GlobalContext.setUiContext(uc)
  84. try {
  85. let avoidArea = value.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
  86. WindowHelper._bottomRectHeight = uc.px2vp(avoidArea.bottomRect.height)
  87. let avoidArea2 = value.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT);
  88. WindowHelper._topRectHeight = uc.px2vp(avoidArea2.topRect.height)
  89. let avoidArea3 = value.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
  90. const a = uc.px2vp(avoidArea3.topRect.height)
  91. WindowHelper._topRectHeight = a > WindowHelper._topRectHeight ? a : WindowHelper._topRectHeight
  92. } catch (error) {
  93. }
  94. } catch (error) {
  95. }
  96. }
  97. }
  98. /**
  99. * 获取当前窗口管理器
  100. * @returns
  101. */
  102. public static get windowClass(): window.Window | undefined {
  103. return WindowHelper._windowClass;
  104. }
  105. /**
  106. * 获取底部安全区高度
  107. * @returns
  108. */
  109. public static get bottomRectHeight(): number {
  110. return WindowHelper._bottomRectHeight
  111. }
  112. /**
  113. * 获取顶部安全区高度
  114. * @returns
  115. */
  116. public static get topRectHeight(): number {
  117. return WindowHelper._isFullScreen ? WindowHelper._topRectHeight : 0
  118. }
  119. public static get windowsWidth() {
  120. try {
  121. return GlobalContext.getUiContext()?.px2vp(display.getDefaultDisplaySync().width)??0
  122. } catch (error) {
  123. return 0
  124. }
  125. }
  126. public static get windowsHeight() {
  127. try {
  128. return GlobalContext.getUiContext()?.px2vp(display.getDefaultDisplaySync().height)??0
  129. } catch (error) {
  130. return 0
  131. }
  132. }
  133. /**
  134. * 设置是否全屏
  135. * @param isLayoutFullScreen
  136. */
  137. static setWindowLayoutFullScreen(isLayoutFullScreen: boolean) {
  138. if (WindowHelper._windowClass) {
  139. WindowHelper._isFullScreen = isLayoutFullScreen
  140. WindowHelper._windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).catch(() => {
  141. });
  142. }
  143. }
  144. /**
  145. * 隐藏状态栏
  146. */
  147. static hideStatusBar() {
  148. if (WindowHelper._windowClass) {
  149. WindowHelper._windowClass.setSpecificSystemBarEnabled('status', false).catch(() => {
  150. });
  151. WindowHelper._windowClass.setSpecificSystemBarEnabled('navigationIndicator', false).catch(() => {
  152. });
  153. }
  154. }
  155. /**
  156. * 显示状态栏
  157. */
  158. static showStatusBar() {
  159. if (WindowHelper._windowClass) {
  160. WindowHelper._windowClass.setSpecificSystemBarEnabled('status', true).catch(() => {
  161. });
  162. WindowHelper._windowClass.setSpecificSystemBarEnabled('navigationIndicator', true).catch(() => {
  163. });
  164. }
  165. }
  166. /**
  167. * 设置状态栏内容颜色
  168. * #ffffff
  169. * 不可以用white这种
  170. */
  171. static setStatusBar(color: string) {
  172. if (WindowHelper._windowClass) {
  173. WindowHelper._windowClass.setWindowSystemBarProperties({
  174. statusBarContentColor: color,
  175. }).catch(() => {
  176. })
  177. }
  178. }
  179. }
  180. /**
  181. * 窗口入参对象
  182. */
  183. class WinOptions {
  184. /**
  185. * 窗口名称 默认window
  186. */
  187. name?: string;
  188. /**
  189. * 窗口类型 默认TYPE_DIALOG
  190. */
  191. windowType?: window.WindowType;
  192. /**
  193. *窗口要显示的路由 如:pages/Welcome需要在main_pages.json中声明
  194. */
  195. router: string = '';
  196. /**
  197. * 窗口背景颜色,默认#33606266
  198. */
  199. bgColor?: string;
  200. /**
  201. * 窗口创建回调函数
  202. */
  203. callBack?: (code: number, msg: string) => void;
  204. }