From 5d05e39e18f757651970058d702f28e81b2b94b6 Mon Sep 17 00:00:00 2001 From: EasyGuohf <163991322+EasyGuohf@users.noreply.github.com> Date: Thu, 18 Dec 2025 21:07:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=89=E5=85=A8=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/VerificationCodeController.ets | 4 ++ .../src/main/ets/common/utils/WindowUtil.ets | 49 ++++++++------ .../main/ets/view/BackgroundColorChange.ets | 64 +++++++++++-------- .../main/ets/entryability/EntryAbility.ets | 3 +- 4 files changed, 71 insertions(+), 49 deletions(-) diff --git a/verificationcodescenariolibrary/src/main/ets/VerificationCodeController.ets b/verificationcodescenariolibrary/src/main/ets/VerificationCodeController.ets index 3633cac..6f5805e 100644 --- a/verificationcodescenariolibrary/src/main/ets/VerificationCodeController.ets +++ b/verificationcodescenariolibrary/src/main/ets/VerificationCodeController.ets @@ -19,4 +19,8 @@ export class VerificationCodeController { public static initWindowConfig(windowStage: window.WindowStage): void { WindowUtil.initialize(windowStage); } + + public static releaseWindowConfig(): void { + WindowUtil.unregisterBreakPoint(); + } } \ No newline at end of file diff --git a/verificationcodescenariolibrary/src/main/ets/common/utils/WindowUtil.ets b/verificationcodescenariolibrary/src/main/ets/common/utils/WindowUtil.ets index ee820f1..1b1af40 100644 --- a/verificationcodescenariolibrary/src/main/ets/common/utils/WindowUtil.ets +++ b/verificationcodescenariolibrary/src/main/ets/common/utils/WindowUtil.ets @@ -23,6 +23,12 @@ const TAG: string = '[WindowUtil]'; export class WindowUtil { private static windowClass: window.Window; + private static windowSizeChangeCallBack: () => void = () => { + BreakpointSystem.getInstance().onWindowSizeChange(WindowUtil.windowClass); + }; + private static avoidAreaChangeCallBack: (avoidAreaOption: window.AvoidAreaOptions) => void = (avoidAreaOption) => { + WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); + }; public static initialize(windowStage: window.WindowStage) { try { @@ -39,20 +45,10 @@ export class WindowUtil { } public static requestFullScreen(data: window.Window): void { - // Realize the immersive effect. - try { - const promise: Promise = data.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 (err) { - const error = err as BusinessError; + data.setWindowLayoutFullScreen(true).catch((err: BusinessError) => { Logger.error(TAG, - `Failed to set the window layout to full-screen mode.code: ${error.code}, message: ${error.message} `); - } + `Failed to set the window layout to full-screen mode. Cause: ${err.code}, ${err.message}`); + }); } public static registerBreakPoint(data: window.Window) { @@ -63,10 +59,8 @@ export class WindowUtil { data.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); WindowUtil.setAvoidArea(window.AvoidAreaType.TYPE_SYSTEM, systemAvoidArea); WindowUtil.setAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR, navIndicatorAvoidArea); - data.on('windowSizeChange', () => BreakpointSystem.getInstance().onWindowSizeChange(data)); - data.on('avoidAreaChange', (avoidAreaOption) => { - WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); - }); + data.on('windowSizeChange', WindowUtil.windowSizeChangeCallBack); + data.on('avoidAreaChange', WindowUtil.avoidAreaChangeCallBack); } catch (err) { const error = err as BusinessError; Logger.error(TAG, @@ -74,13 +68,26 @@ export class WindowUtil { } } + 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}`); + } + } + // Get status bar height and indicator height. private static setAvoidArea(type: window.AvoidAreaType, area: window.AvoidArea) { const context: UIContext | undefined = AppStorage.get(Constants.KEY_UI_CONTEXT); - if (type === window.AvoidAreaType.TYPE_SYSTEM) { - AppStorage.setOrCreate(Constants.KEY_STATUS_BAR_HEIGHT, context?.px2vp(area.topRect.height)); - } else if (type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { - AppStorage.setOrCreate(Constants.KEY_NAV_INDICATOR_HEIGHT, context?.px2vp(area.bottomRect.height)); + if (context) { + if (type === window.AvoidAreaType.TYPE_SYSTEM) { + AppStorage.setOrCreate(Constants.KEY_STATUS_BAR_HEIGHT, context.px2vp(area.topRect.height)); + } else if (type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { + AppStorage.setOrCreate(Constants.KEY_NAV_INDICATOR_HEIGHT, context.px2vp(area.bottomRect.height)); + } } } } \ No newline at end of file diff --git a/verificationcodescenariolibrary/src/main/ets/view/BackgroundColorChange.ets b/verificationcodescenariolibrary/src/main/ets/view/BackgroundColorChange.ets index 6b1ceb1..2a92fe7 100644 --- a/verificationcodescenariolibrary/src/main/ets/view/BackgroundColorChange.ets +++ b/verificationcodescenariolibrary/src/main/ets/view/BackgroundColorChange.ets @@ -56,6 +56,27 @@ struct BackgroundColorChange { private windowClass: window.Window = AppStorage.get(Constants.KEY_WINDOWS) as window.Window; private isFirstOpenPage: boolean = true; private registerCount: number = 1; + private windowEventCallBack: (windowState: window.WindowEventType) => void = async (windowState) => { + if (windowState === window.WindowEventType.WINDOW_ACTIVE && this.registerCount === 1) { + this.registerCount++; + try { + await this.inputController.attach(true, this.textConfig); + this.listen(); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}`); + } + } + }; + private insertTextCallBack: (text: string) => void = (text) => { + if (this.codeText.length >= this.verifyCodeLength || isNaN(Number(text)) || text === ' ') { + return; + } + this.codeText += text; + }; + private deleteLeftCallBack: () => void = () => { + this.codeText = this.codeText.substring(0, this.codeText.length - 1); + }; aboutToDisappear(): void { this.detach(); @@ -68,18 +89,7 @@ struct BackgroundColorChange { this.listen(); return; } - this.windowClass.on('windowEvent', async (windowState) => { - if (windowState === window.WindowEventType.WINDOW_ACTIVE && this.registerCount === 1) { - this.registerCount++; - try { - await this.inputController.attach(true, this.textConfig); - this.listen(); - } catch (error) { - const err: BusinessError = error as BusinessError; - Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}`); - } - } - }); + this.windowClass.on('windowEvent', this.windowEventCallBack); } catch (error) { const err: BusinessError = error as BusinessError; Logger.error(TAG, `Attach catch error, Cause: the code is ${err.code}, the message is ${err.message}`); @@ -88,15 +98,8 @@ struct BackgroundColorChange { listen() { try { - this.inputController.on('insertText', (text: string) => { - if (this.codeText.length >= this.verifyCodeLength || isNaN(Number(text)) || text === ' ') { - return; - } - this.codeText += text; - }); - this.inputController.on('deleteLeft', () => { - this.codeText = this.codeText.substring(0, this.codeText.length - 1); - }); + this.inputController.on('insertText', this.insertTextCallBack); + this.inputController.on('deleteLeft', this.deleteLeftCallBack); } catch (error) { const err: BusinessError = error as BusinessError; Logger.error(TAG, `Register callback failed: ${err.code}, ${err.message}`); @@ -107,14 +110,17 @@ struct BackgroundColorChange { try { this.isFirstOpenPage = false; this.registerCount = 1; - this.windowClass.off('windowEvent'); - this.inputController.off('insertText'); - this.inputController.off('deleteLeft'); - this.inputController.detach((_err: BusinessError) => { + this.windowClass.off('windowEvent', this.windowEventCallBack); + this.inputController.off('insertText', this.insertTextCallBack); + this.inputController.off('deleteLeft', this.deleteLeftCallBack); + this.inputController.detach((err: BusinessError) => { + if (err) { + Logger.error(TAG, `Detach catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); + } }); } catch (error) { const err: BusinessError = error as BusinessError; - Logger.error(TAG, `Detach catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); + Logger.error(TAG, `Detach or off catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); } } @@ -149,7 +155,11 @@ struct BackgroundColorChange { .height($r('app.float.text_height')) .defaultFocus(true) .onClick(() => { - this.inputController.attach(true, this.textConfig); + this.inputController.attach(true, this.textConfig).catch((err: BusinessError) => { + if (err) { + Logger.error(TAG, `Detach catch error, Cause: the code is ${err.code}, the message is ${err.message}.`); + } + }); }) .onKeyPreIme((key: KeyEvent) => { if (key.type === KeyType.Up && key.unicode && (key.unicode >= 48 && key.unicode <= 57)) { diff --git a/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets b/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets index 198f964..1032dac 100644 --- a/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets +++ b/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets @@ -47,8 +47,9 @@ export default class EntryAbility extends UIAbility { } onWindowStageDestroy(): void { - // Main window is destroyed, release UI related resources Logger.info(TAG, 'Ability onWindowStageDestroy'); + // Main window is destroyed, release UI related resources + VerificationCodeController.releaseWindowConfig(); } onForeground(): void { -- Gitee From 0bd557c3e045398e0c0c2d1315883e3c5d940f0f Mon Sep 17 00:00:00 2001 From: EasyGuohf <163991322+EasyGuohf@users.noreply.github.com> Date: Fri, 19 Dec 2025 12:20:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=89=E5=85=A8=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/entryability/EntryAbility.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets b/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets index 1032dac..6f8f691 100644 --- a/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets +++ b/verificationcodescenariosample/src/main/ets/entryability/EntryAbility.ets @@ -37,7 +37,7 @@ export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { windowStage.loadContent('pages/Index', (err, _data) => { - if (err.code) { + if (err?.code) { Logger.error(TAG, `Failed to load the content. Cause: the code is ${err.code}, the message is ${err.message}.`); return; } -- Gitee