Browse Source

refactor(app): 重构应用初始化和窗口处理逻辑

-调整 AppAbility 中的窗口创建和上下文设置逻辑
-优化 WindowHelper 类,增加对 UIContext 的处理
- 重构 ContextConfig 类,简化上下文设置方法
徐勤民 1 week ago
parent
commit
dbca937b2d
2 changed files with 11 additions and 14 deletions
  1. 4 12
      src/main/ets/ContextConfig.ets
  2. 7 2
      src/main/ets/utils/WindowHelper.ets

+ 4 - 12
src/main/ets/ContextConfig.ets

@@ -3,8 +3,6 @@
  */
 
 import type common from '@ohos.app.ability.common';
-import { WindowHelper } from './utils/WindowHelper';
-import { BusinessError } from '@kit.BasicServicesKit';
 
 declare namespace globalThis {
   let _brushEngineContext: common.UIAbilityContext;
@@ -18,16 +16,6 @@ export class GlobalContext {
 
   static setContext(context: common.UIAbilityContext): void {
     globalThis._brushEngineContext = context;
-
-    context.windowStage.getMainWindow((err: BusinessError, data) => {
-      let errCode: number = err.code;
-      if (errCode !== 0) {
-        console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
-        return;
-      }
-      WindowHelper.windowClass = data;
-      globalThis._uiUIContext = WindowHelper.windowClass.getUIContext();
-    });
   }
 
   static getUiContext(): UIContext | undefined {
@@ -36,5 +24,9 @@ export class GlobalContext {
     }
     return globalThis._uiUIContext;
   }
+
+  static setUiContext(u: UIContext) {
+    globalThis._uiUIContext = u;
+  }
 }
 

+ 7 - 2
src/main/ets/utils/WindowHelper.ets

@@ -1,10 +1,12 @@
 import { display, window } from '@kit.ArkUI';
 import { common } from '@kit.AbilityKit';
+import { GlobalContext } from '../ContextConfig';
 
 
 export class WindowHelper {
   private constructor() {
   }
+
   /**
    * 缓存窗体,关闭时需要
    * 同时只能出现一个窗口,所以只做一个缓存就可以
@@ -78,6 +80,9 @@ export class WindowHelper {
    */
   public static set windowClass(value: window.Window | undefined) {
     WindowHelper._windowClass = value;
+    if (value) {
+      GlobalContext.setUiContext(value.getUIContext())
+    }
     if (WindowHelper._windowClass) {
       let avoidArea = WindowHelper._windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
       WindowHelper._bottomRectHeight = px2vp(avoidArea.bottomRect.height)
@@ -114,11 +119,11 @@ export class WindowHelper {
     return WindowHelper._isFullScreen ? WindowHelper._topRectHeight : 0
   }
 
-  public static get windowsWidth(){
+  public static get windowsWidth() {
     return px2vp(display.getDefaultDisplaySync().width)
   }
 
-  public static get windowsHeight(){
+  public static get windowsHeight() {
     return px2vp(display.getDefaultDisplaySync().height)
   }