diff --git a/src/main/ets/ContextConfig.ets b/src/main/ets/ContextConfig.ets index 0ee97d6..76346f2 100644 --- a/src/main/ets/ContextConfig.ets +++ b/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; + } } diff --git a/src/main/ets/utils/WindowHelper.ets b/src/main/ets/utils/WindowHelper.ets index c0bddad..1ccc1fb 100644 --- a/src/main/ets/utils/WindowHelper.ets +++ b/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) }