From 28a96ca56e1fcbfb6c4f144085db2623e12a58a1 Mon Sep 17 00:00:00 2001 From: d30044417 Date: Mon, 22 Dec 2025 11:05:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20WindowUtil=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/component/MultipleImageComponent.ets | 2 + .../src/main/ets/utils/WindowUtil.ets | 65 ++++++++++--------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/multipleimagelibrary/src/main/ets/component/MultipleImageComponent.ets b/multipleimagelibrary/src/main/ets/component/MultipleImageComponent.ets index f33e804..96e613a 100644 --- a/multipleimagelibrary/src/main/ets/component/MultipleImageComponent.ets +++ b/multipleimagelibrary/src/main/ets/component/MultipleImageComponent.ets @@ -18,6 +18,7 @@ import { display } from '@kit.ArkUI'; import { CommonConstants } from '../constants/CommonConstants'; import { BreakpointType, BreakpointTypeEnum } from '../utils/BreakpointSystem'; import Logger from '../utils/Logger'; +import { WindowUtil } from '../utils/WindowUtil'; import { DataSource, PhotoData } from '../viewmodel/DataSource'; const TAG: string = '[Index]' @@ -66,6 +67,7 @@ export struct MultipleImageComponent { aboutToDisappear(): void { try { display?.off('change'); + WindowUtil.unRegisterBreakPoint(); } catch (error) { Logger.error(TAG, `Failed to unregister the callback for display changes. Cause: ${error.message}`); } diff --git a/multipleimagelibrary/src/main/ets/utils/WindowUtil.ets b/multipleimagelibrary/src/main/ets/utils/WindowUtil.ets index 1110666..109eff4 100644 --- a/multipleimagelibrary/src/main/ets/utils/WindowUtil.ets +++ b/multipleimagelibrary/src/main/ets/utils/WindowUtil.ets @@ -31,42 +31,33 @@ export class WindowUtil { const uiContext: UIContext = WindowUtil.windowClass.getUIContext(); WindowUtil.uiContext = uiContext; AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_SYSTEM_UICONTEXT, uiContext); - WindowUtil.registerBreakPoint(windowStage); - WindowUtil.requestFullScreen(windowStage); + WindowUtil.registerBreakPoint(WindowUtil.windowClass); + WindowUtil.requestFullScreen(WindowUtil.windowClass); } catch (err) { Logger.error(TAG, `WindowUtil initialize failed. Cause: ${err.code} ${err.message}`); } } - public static requestFullScreen(windowStage: window.WindowStage): void { - windowStage.getMainWindow((err: BusinessError, data: window.Window) => { - if (err.code) { - return; - } - let sysBarProps: window.SystemBarProperties = { statusBarContentColor: '#FFFFFF' }; - data.setWindowSystemBarProperties(sysBarProps); - 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(data: window.Window): void { + let sysBarProps: window.SystemBarProperties = { statusBarContentColor: '#FFFFFF' }; + data.setWindowSystemBarProperties(sysBarProps); + 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 registerBreakPoint(windowStage: window.WindowStage) { - windowStage.getMainWindow((err: BusinessError, data: window.Window) => { - if (err.code) { - Logger.error(TAG, `Failed to get main window: ${err.message}`); - return; - } + public static registerBreakPoint(data: window.Window) { + try { BreakpointSystem.getInstance().updateWidthBp(data); let avoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_NAVIGATOR_BAR_HEIGHT, @@ -82,7 +73,21 @@ export class WindowUtil { WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); } }); - }) + } catch (e) { + const error = e as BusinessError; + Logger.error(TAG, + `Register avoidAreaChange or windowSizeChange failed. code: ${error.code}, message: ${error.message}`); + } + } + + public static unRegisterBreakPoint() { + try { + WindowUtil.windowClass?.off('windowSizeChange'); + WindowUtil.windowClass?.off('avoidAreaChange'); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, `windowClass off failed. code: ${err.code}, message: ${err.message}`); + } } // Get status bar height and indicator height. -- Gitee From 267b653d01fcea1b3d38062fadf45c14de7dc6a6 Mon Sep 17 00:00:00 2001 From: d30044417 Date: Sun, 4 Jan 2026 10:41:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20MultiImage=E4=BA=8C=E8=BD=AE?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E8=A7=84=E8=8C=83=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/component/MultipleImageComponent.ets | 5 ++++- .../src/main/ets/utils/BreakpointSystem.ets | 8 ++++++-- .../src/main/ets/utils/WindowUtil.ets | 18 ++++++++++-------- .../src/main/ets/viewmodel/DataSource.ets | 5 ++++- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/multipleimagelibrary/src/main/ets/component/MultipleImageComponent.ets b/multipleimagelibrary/src/main/ets/component/MultipleImageComponent.ets index 96e613a..d01acb3 100644 --- a/multipleimagelibrary/src/main/ets/component/MultipleImageComponent.ets +++ b/multipleimagelibrary/src/main/ets/component/MultipleImageComponent.ets @@ -15,6 +15,7 @@ import { common } from '@kit.AbilityKit'; import { display } from '@kit.ArkUI'; +import type { BusinessError } from '@kit.BasicServicesKit'; import { CommonConstants } from '../constants/CommonConstants'; import { BreakpointType, BreakpointTypeEnum } from '../utils/BreakpointSystem'; import Logger from '../utils/Logger'; @@ -156,7 +157,9 @@ export struct MultipleImageComponent { .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => { if (extraInfo.currentOffset > 0 && index === 0 && !this.slide) { let context = this.getUIContext().getHostContext() as common.UIAbilityContext; - context.terminateSelf(); + context.terminateSelf().catch((error: BusinessError)=>{ + Logger.error(TAG, `Context terminateSelf fail, error code: ${error.code}, message: ${error.message}`); + }); } this.slide = true; if (extraInfo.currentOffset > 0 && this.currentIndex > 0) { diff --git a/multipleimagelibrary/src/main/ets/utils/BreakpointSystem.ets b/multipleimagelibrary/src/main/ets/utils/BreakpointSystem.ets index a02ee47..b95d00e 100644 --- a/multipleimagelibrary/src/main/ets/utils/BreakpointSystem.ets +++ b/multipleimagelibrary/src/main/ets/utils/BreakpointSystem.ets @@ -60,12 +60,13 @@ export class BreakpointType { } return this.lg; } -}; +} +; export class BreakpointSystem { private static instance: BreakpointSystem; private currentBreakpoint: BreakpointTypeEnum = BreakpointTypeEnum.MD; - private uiContext: UIContext = AppStorage.get(CommonConstants.KEY_PREFIX_SYSTEM_UICONTEXT) as UIContext; + private uiContext: UIContext | undefined = AppStorage.get(CommonConstants.KEY_PREFIX_SYSTEM_UICONTEXT); private constructor() { } @@ -87,6 +88,9 @@ export class BreakpointSystem { } public updateWidthBp(window: window.Window): void { + if (!this.uiContext) { + return; + } try { const mainWindow: window.WindowProperties = window.getWindowProperties(); const windowWidth: number = mainWindow.windowRect.width; diff --git a/multipleimagelibrary/src/main/ets/utils/WindowUtil.ets b/multipleimagelibrary/src/main/ets/utils/WindowUtil.ets index 109eff4..250c861 100644 --- a/multipleimagelibrary/src/main/ets/utils/WindowUtil.ets +++ b/multipleimagelibrary/src/main/ets/utils/WindowUtil.ets @@ -40,17 +40,19 @@ export class WindowUtil { public static requestFullScreen(data: window.Window): void { let sysBarProps: window.SystemBarProperties = { statusBarContentColor: '#FFFFFF' }; - data.setWindowSystemBarProperties(sysBarProps); + data.setWindowSystemBarProperties(sysBarProps).catch((error: BusinessError) => { + Logger.error(TAG, `SetWindowSystemBarProperties failed. Cause: ${error.code} ${error.message}`); + }); 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}`); - }); + windowClass.setWindowLayoutFullScreen(true) + .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.'); } diff --git a/multipleimagelibrary/src/main/ets/viewmodel/DataSource.ets b/multipleimagelibrary/src/main/ets/viewmodel/DataSource.ets index 49e9a39..459981e 100644 --- a/multipleimagelibrary/src/main/ets/viewmodel/DataSource.ets +++ b/multipleimagelibrary/src/main/ets/viewmodel/DataSource.ets @@ -24,7 +24,10 @@ class DataSource implements IDataSource { return this.list.length; } - getData(index: number): PhotoData { + getData(index: number): PhotoData | null { + if (index < 0 || index > this.list.length) { + return null; + } return this.list[index]; } -- Gitee