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

-调整 AppAbility 中的窗口创建和上下文设置逻辑
-优化 WindowHelper 类,增加对 UIContext 的处理
- 重构 ContextConfig 类,简化上下文设置方法
这个提交包含在:
徐勤民 2025-08-26 15:29:45 +08:00
父节点 20c20e5d5a
当前提交 dbca937b2d
共有 2 个文件被更改,包括 11 次插入14 次删除

查看文件

@ -3,8 +3,6 @@
*/ */
import type common from '@ohos.app.ability.common'; import type common from '@ohos.app.ability.common';
import { WindowHelper } from './utils/WindowHelper';
import { BusinessError } from '@kit.BasicServicesKit';
declare namespace globalThis { declare namespace globalThis {
let _brushEngineContext: common.UIAbilityContext; let _brushEngineContext: common.UIAbilityContext;
@ -18,16 +16,6 @@ export class GlobalContext {
static setContext(context: common.UIAbilityContext): void { static setContext(context: common.UIAbilityContext): void {
globalThis._brushEngineContext = context; 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 { static getUiContext(): UIContext | undefined {
@ -36,5 +24,9 @@ export class GlobalContext {
} }
return globalThis._uiUIContext; return globalThis._uiUIContext;
} }
static setUiContext(u: UIContext) {
globalThis._uiUIContext = u;
}
} }

查看文件

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