diff --git a/multibusinessofficelibrary/src/main/ets/BusinessOfficeController.ets b/multibusinessofficelibrary/src/main/ets/BusinessOfficeController.ets index cfdfd0b8edf0c72821bd76685263b2629ab28fe0..f0938183c7c49948eb69399bca4196311e52c159 100644 --- a/multibusinessofficelibrary/src/main/ets/BusinessOfficeController.ets +++ b/multibusinessofficelibrary/src/main/ets/BusinessOfficeController.ets @@ -19,4 +19,8 @@ export class BusinessOfficeController { public static initMainWindowConfig(windowStage: window.WindowStage): void { WindowUtil.initialize(windowStage); } + + public static releaseWindowConfig(): void { + WindowUtil.unregisterBreakPoint(); + } } \ No newline at end of file diff --git a/multibusinessofficelibrary/src/main/ets/utils/BreakpointSystem.ets b/multibusinessofficelibrary/src/main/ets/utils/BreakpointSystem.ets index ca9ad1f92be9001c2eabf500933f9d44ab137ee8..34a8717ea63d3d32306e6dfe2f81f340a7ab0b9a 100644 --- a/multibusinessofficelibrary/src/main/ets/utils/BreakpointSystem.ets +++ b/multibusinessofficelibrary/src/main/ets/utils/BreakpointSystem.ets @@ -15,6 +15,7 @@ import { window } from '@kit.ArkUI'; import type { BusinessError } from '@kit.BasicServicesKit'; +import { CommonConstants } from '../constants/CommonConstants'; import Logger from './Logger'; const TAG: string = '[BreakpointSystem]'; @@ -88,16 +89,16 @@ export class BreakpointSystem { return BreakpointSystem.instance; } - public updateCurrentBreakpoint(preKey: string, breakpoint: BreakpointTypeEnum): void { + public updateCurrentBreakpoint(breakpoint: BreakpointTypeEnum): void { this.currentBreakpoint = breakpoint; - AppStorage.setOrCreate(`${preKey}BreakPoint`, this.currentBreakpoint); + AppStorage.setOrCreate(CommonConstants.KEY_MAIN_BREAKPOINT, this.currentBreakpoint); } - public onWindowSizeChange(preKey: string, window: window.Window): void { - this.updateWidthBp(preKey, window); + public onWindowSizeChange(window: window.Window): void { + this.updateWidthBp(window); } - public updateWidthBp(preKey: string, window: window.Window): void { + public updateWidthBp(window: window.Window): void { try { const mainWindow: window.WindowProperties = window.getWindowProperties(); const windowWidth: number = mainWindow.windowRect.width; @@ -114,7 +115,7 @@ export class BreakpointSystem { } else { widthBp = BreakpointTypeEnum.XL; } - this.updateCurrentBreakpoint(preKey, widthBp); + this.updateCurrentBreakpoint(widthBp); } catch (error) { const err: BusinessError = error as BusinessError; Logger.error(TAG, `UpdateBreakpoint fail, error code: ${err.code}, message: ${err.message}`); diff --git a/multibusinessofficelibrary/src/main/ets/utils/ResourceUtils.ets b/multibusinessofficelibrary/src/main/ets/utils/ResourceUtils.ets index ac9e19dc128710fe2beabf472275e708777a30e0..48554da24f5411e8680d78a7dd174e6730b4546c 100644 --- a/multibusinessofficelibrary/src/main/ets/utils/ResourceUtils.ets +++ b/multibusinessofficelibrary/src/main/ets/utils/ResourceUtils.ets @@ -13,6 +13,7 @@ * limitations under the License. */ +import { BusinessError } from '@kit.BasicServicesKit'; import { CommonConstants } from '../constants/CommonConstants'; import Logger from './Logger'; @@ -24,8 +25,9 @@ export class ResourceUtils { const context: UIContext = AppStorage.get(CommonConstants.KEY_MAIN_UI_CONTEXT)!; const resManager = context.getHostContext()?.resourceManager; return resManager!.getStringArrayValueSync(resId); - } catch (err) { - Logger.error(TAG, `getStringArrayValueSync catch err: ${err.code}`); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, `GetStringArrayValueSync catch err: ${err.code}, ${err.message}.`); } return []; } @@ -35,8 +37,9 @@ export class ResourceUtils { const context: UIContext = AppStorage.get(CommonConstants.KEY_MAIN_UI_CONTEXT)!; const resManager = context.getHostContext()?.resourceManager; return resManager!.getStringSync(resId); - } catch (err) { - Logger.error(TAG, `getStringSync catch err: ${err.code}`); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, `GetStringSync catch err: ${err.code}, ${err.message}.`); } return ''; } diff --git a/multibusinessofficelibrary/src/main/ets/utils/WindowUtil.ets b/multibusinessofficelibrary/src/main/ets/utils/WindowUtil.ets index 04b89b693d1ff0f4d22aa53fcd308bfc05ca43e0..8ab6b857deec28ac75d493dc1142cf254cdcdd32 100644 --- a/multibusinessofficelibrary/src/main/ets/utils/WindowUtil.ets +++ b/multibusinessofficelibrary/src/main/ets/utils/WindowUtil.ets @@ -14,7 +14,7 @@ */ import { display, window } from '@kit.ArkUI'; -import type { BusinessError } from '@kit.BasicServicesKit'; +import { BusinessError } from '@kit.BasicServicesKit'; import { CommonConstants } from '../constants/CommonConstants'; import { BreakpointSystem } from './BreakpointSystem'; import Logger from './Logger'; @@ -22,127 +22,123 @@ import Logger from './Logger'; const TAG: string = '[WindowUtil]'; export class WindowUtil { + private static windowClass: window.Window; + public static initialize(windowStage: window.WindowStage) { try { - const uiContext: UIContext = windowStage.getMainWindowSync().getUIContext(); - AppStorage.setOrCreate(`${CommonConstants.KEY_PREFIX_MAIN}UIContext`, uiContext); - WindowUtil.requestFullScreen(windowStage); - WindowUtil.registerBreakPoint(CommonConstants.KEY_PREFIX_MAIN, windowStage); - WindowUtil.setWindowDecor(windowStage, false, CommonConstants.WINDOW_DECOR_HEIGHT); - WindowUtil.setWindowLimit(windowStage, { minWidth: CommonConstants.WINDOW_MIN_WIDTH }); + WindowUtil.windowClass = windowStage.getMainWindowSync(); + const uiContext: UIContext = WindowUtil.windowClass.getUIContext(); + AppStorage.setOrCreate(CommonConstants.KEY_MAIN_UI_CONTEXT, uiContext); + WindowUtil.requestFullScreen(WindowUtil.windowClass); + WindowUtil.registerBreakPoint(WindowUtil.windowClass); + WindowUtil.setWindowDecor(WindowUtil.windowClass, false, CommonConstants.WINDOW_DECOR_HEIGHT); + WindowUtil.setWindowLimit(WindowUtil.windowClass, { minWidth: CommonConstants.WINDOW_MIN_WIDTH }); } catch (err) { const error = err as BusinessError; Logger.error(TAG, `Initialize failed. Cause code: ${error.code}, message: ${error.message}`); } } - public static requestFullScreen(windowStage: window.WindowStage): void { - windowStage.getMainWindow((err: BusinessError, data: window.Window) => { - if (err.code) { - return; - } - const windowClass: window.Window = data; - // Realize the immersive effect. - try { - const promise: Promise = windowClass.setWindowLayoutFullScreen(true); - promise.then(() => { - Logger.info(TAG, 'Succeeded in setting the window layout to full-screen mode.'); - }).catch((err: BusinessError) => { - Logger.error(TAG, - `Failed to set the window layout to full-screen mode. Cause: ${err.code}, ${err.message}`); - }); - } catch { - Logger.error(TAG, 'Failed to set the window layout to full-screen mode. '); - } + public static requestFullScreen(windowClass: window.Window): void { + windowClass.setWindowLayoutFullScreen(true).catch((err: BusinessError) => { + Logger.error(TAG, + `Failed to set the window layout to full-screen mode. Cause: ${err.code}, ${err.message}.`); }); } - public static registerBreakPoint(preKey: string, windowStage: window.WindowStage) { - windowStage.getMainWindow((err: BusinessError, data: window.Window) => { - if (err.code) { - Logger.error(TAG, `Failed to get main window: ${err.message}`); - return; - } - try { - BreakpointSystem.getInstance().updateWidthBp(preKey, data); - WindowUtil.updateWindowSize(preKey, data.getWindowProperties().windowRect.width, - data.getWindowProperties().windowRect.height); - const systemAvoidArea: window.AvoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); - const navIndicatorAvoidArea: window.AvoidArea = - data.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); - WindowUtil.setAvoidArea(window.AvoidAreaType.TYPE_SYSTEM, systemAvoidArea, preKey); - WindowUtil.setAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR, navIndicatorAvoidArea, preKey); - data.on('windowSizeChange', (windowSize: window.Size) => { - BreakpointSystem.getInstance().onWindowSizeChange(preKey, data); - WindowUtil.updateWindowSize(preKey, windowSize.width, windowSize.height); - }); - data.on('avoidAreaChange', (avoidAreaOption) => { - WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area, preKey); - }); - } catch (e) { - const error = e as BusinessError; - Logger.error(TAG, - `Register avoidAreaChange or windowSizeChange failed. code: ${error.code}, message: ${error.message}`); - } - }) + public static registerBreakPoint(windowClass: window.Window) { + try { + BreakpointSystem.getInstance().updateWidthBp(windowClass); + WindowUtil.updateWindowSize(windowClass.getWindowProperties().windowRect.width, + windowClass.getWindowProperties().windowRect.height); + const systemAvoidArea: window.AvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); + const navIndicatorAvoidArea: window.AvoidArea = + windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); + WindowUtil.setAvoidArea(window.AvoidAreaType.TYPE_SYSTEM, systemAvoidArea); + WindowUtil.setAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR, navIndicatorAvoidArea); + windowClass.on('windowSizeChange', WindowUtil.windowSizeChangeCallBack); + windowClass.on('avoidAreaChange', WindowUtil.avoidAreaChangeCallBack); + } catch (e) { + const error = e as BusinessError; + Logger.error(TAG, + `Register avoidAreaChange or windowSizeChange failed. code: ${error.code}, message: ${error.message}`); + } } - // Get status bar height and indicator height. - private static setAvoidArea(type: window.AvoidAreaType, area: window.AvoidArea, preKey: string) { - const context: UIContext | undefined = AppStorage.get(CommonConstants.KEY_MAIN_UI_CONTEXT); - if (type === window.AvoidAreaType.TYPE_SYSTEM) { - AppStorage.setOrCreate(`${preKey}StatusBarHeight`, context?.px2vp(area.topRect.height)); - } else if (type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { - AppStorage.setOrCreate(`${preKey}NavIndicatorHeight`, context?.px2vp(area.bottomRect.height)); + public static unregisterBreakPoint() { + try { + WindowUtil.windowClass.off('windowSizeChange', WindowUtil.windowSizeChangeCallBack); + WindowUtil.windowClass.off('avoidAreaChange', WindowUtil.avoidAreaChangeCallBack); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, + `Failed to unregister the windowSizeChange or the WindowAvoidArea. Cause: ${err.code}, ${err.message}`); } } - public static setWindowDecor(windowStage: window.WindowStage, visible: boolean, height?: number) { + public static setWindowDecor(windowClass: window.Window, visible: boolean, height?: number) { // Hide 2in1 title bar. - windowStage.getMainWindow().then((data: window.Window) => { - // Hide 2in1 title bar. - try { - if (canIUse('SystemCapability.Window.SessionManager')) { - data.setWindowDecorVisible(visible); - if (height) { - data.setWindowDecorHeight(height); - } + try { + if (canIUse('SystemCapability.Window.SessionManager')) { + windowClass.setWindowDecorVisible(visible); + if (height) { + windowClass.setWindowDecorHeight(height); } - } catch (exception) { - Logger.error(TAG, `Failed to set the window decor. Cause: ${exception.code}`); } - }).catch((err: BusinessError) => { - Logger.error(TAG, `Get main window failed. Cause: ${err.message}`); - }); + } catch (err) { + const error = err as BusinessError; + Logger.error(TAG, `Failed to set the window decor. Cause: ${error.code}, ${error.message}.`); + } + } - public static setWindowLimit(windowStage: window.WindowStage, windowLimits: window.WindowLimits) { - // Hide 2in1 title bar. - windowStage.getMainWindow().then((data: window.Window) => { - try { - // Set mini window. - if (canIUse('SystemCapability.Window.SessionManager')) { - let promise = data.setWindowLimits(windowLimits); - promise.then(() => { - Logger.error(TAG, 'Succeeded in changing the window limits.'); - }).catch((err: BusinessError) => { - Logger.error(TAG, `Failed to change the window limits. Cause: ${err.code}`); - }) - } - } catch (exception) { - Logger.error(TAG, `Failed to change the window limits. Cause: ${exception.code}`); + public static setWindowLimit(windowClass: window.Window, windowLimits: window.WindowLimits) { + try { + // Set mini window. + if (canIUse('SystemCapability.Window.SessionManager')) { + windowClass.setWindowLimits(windowLimits).then(() => { + Logger.info(TAG, 'Succeeded in changing the window limits.'); + }).catch((err: BusinessError) => { + Logger.error(TAG, `Failed to change the window limits. Cause: ${err.code}, ${err.message}.`); + }) } - }).catch((err: BusinessError) => { - Logger.error(TAG, `Get main window failed. Cause: ${err.message}`); - }); + } catch (err) { + const error = err as BusinessError; + Logger.error(TAG, `Failed to change the window limits. Cause: ${error.code}, ${error.message}.`); + } + } - private static updateWindowSize(preKey: string, windowWidth: number, windowHeight: number): void { + private static windowSizeChangeCallBack: (windowSize: window.Size) => void = (windowSize) => { + BreakpointSystem.getInstance().onWindowSizeChange(WindowUtil.windowClass); + WindowUtil.updateWindowSize(windowSize.width, windowSize.height); + }; + + private static avoidAreaChangeCallBack: (avoidAreaOption: window.AvoidAreaOptions) => void = (avoidAreaOption) => { + WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); + }; + + // Get status bar height and indicator height. + private static setAvoidArea(type: window.AvoidAreaType, area: window.AvoidArea) { + const context: UIContext | undefined = AppStorage.get(CommonConstants.KEY_MAIN_UI_CONTEXT); + if (context) { + if (type === window.AvoidAreaType.TYPE_SYSTEM) { + AppStorage.setOrCreate(CommonConstants.KEY_MAIN_STATUS_BAR_HEIGHT, context.px2vp(area.topRect.height)); + } else if (type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { + AppStorage.setOrCreate(CommonConstants.KEY_MAIN_NAV_INDICATOR_HEIGHT, context.px2vp(area.bottomRect.height)); + } + } + } + + private static updateWindowSize(windowWidth: number, windowHeight: number): void { try { - AppStorage.setOrCreate(`${preKey}WindowHeight`, windowHeight / display.getDefaultDisplaySync().densityPixels); - AppStorage.setOrCreate(`${preKey}WindowWidth`, windowWidth / display.getDefaultDisplaySync().densityPixels); - } catch (error) { - Logger.error(TAG, `display catch err: ${error.code}`); + AppStorage.setOrCreate(CommonConstants.KEY_MAIN_WINDOW_HEIGHT, + windowHeight / display.getDefaultDisplaySync().densityPixels); + AppStorage.setOrCreate(CommonConstants.KEY_MAIN_WINDOW_WIDTH, + windowWidth / display.getDefaultDisplaySync().densityPixels); + } catch (err) { + const error = err as BusinessError; + Logger.error(TAG, `Display catch err: ${error.code}, ${error.message}.`); } } } \ No newline at end of file diff --git a/multibusinesssample/src/main/ets/entryability/EntryAbility.ets b/multibusinesssample/src/main/ets/entryability/EntryAbility.ets index ed78dd1b51a39b3624a7f527e3b9e15f34fa583a..82f360cc93f2e4ed9fc33a4acfdc6bcd8b6e13c2 100644 --- a/multibusinesssample/src/main/ets/entryability/EntryAbility.ets +++ b/multibusinesssample/src/main/ets/entryability/EntryAbility.ets @@ -39,7 +39,7 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability Logger.info(TAG, 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err) => { - if (err.code) { + if (err?.code) { Logger.error(TAG, `Failed to load the content. Cause: ${err.code}`); return; } @@ -50,6 +50,7 @@ export default class EntryAbility extends UIAbility { onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources + BusinessOfficeController.releaseWindowConfig(); Logger.info(TAG, 'Ability onWindowStageDestroy'); }