From 17c5364500aafb346e57c9c9fa31defe3fb0c4b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=93=E5=BD=930?= Date: Wed, 9 Apr 2025 11:10:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=AA=8C=E8=AF=81=E7=A0=81=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E5=90=88=E9=9B=86sample=E6=8E=92=E6=9F=A5=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/Constants.ets | 40 ------ .../main/ets/entryability/EntryAbility.ets | 131 ++++++++++-------- .../main/ets/pages/BackgroundColorChange.ets | 44 ++++-- .../src/main/ets/pages/TextBoxShowCursor.ets | 96 ++++++++----- 4 files changed, 167 insertions(+), 144 deletions(-) diff --git a/entry/src/main/ets/common/Constants.ets b/entry/src/main/ets/common/Constants.ets index 1df7518..d05832f 100644 --- a/entry/src/main/ets/common/Constants.ets +++ b/entry/src/main/ets/common/Constants.ets @@ -20,18 +20,6 @@ export default class Constants { * One hundred percent. */ public static readonly ONE_HUNDRED_PERCENT: string = '100%'; - /** - * Ninety percent. - */ - public static readonly NINETY_PERCENT: string = '90%'; - /** - * Eighty percent. - */ - public static readonly EIGHTY_PERCENT: string = '80%'; - /** - * Eighty-five percent. - */ - public static readonly EIGHTY_FIVE_PERCENT: string = '85%'; /** * Rotate angle. */ @@ -68,14 +56,6 @@ export default class Constants { * Negative two. */ public static readonly NEGATIVE_TWO: number = -2; - /** - * Negative one hundred ninety-seven. - */ - public static readonly ONE_HUNDRED_NINETY_SEVEN: number = -197; - /** - * Negative one hundred and fourteen. - */ - public static readonly ONE_HUNDRED_FOURTEEN: number = -114; /** * Font weight 700. */ @@ -108,10 +88,6 @@ export default class Constants { * One hundred and twenty. */ public static readonly ONE_HUNDRED_AND_TWENTY: number = 120; - /** - * Three hundred and forty. - */ - public static readonly THREE_HUNDRED_AND_FORTY: number = 340; /** * Ninety. */ @@ -132,18 +108,10 @@ export default class Constants { * Four. */ public static readonly FOUR: number = 4; - /** - * Line move. - */ - public static readonly LINE_MOVE: number = 14; /** * Line height. */ public static readonly LINE_HEIGHT: number = 1; - /** - * Line width. - */ - public static readonly LINE_WIDTH: number = 48; /** * Item divider. */ @@ -276,14 +244,6 @@ export default class Constants { * Breakpoint sm. */ public static readonly BREAK_POINT_SM: string = 'sm'; - /** - * Breakpoint md. - */ - public static readonly BREAK_POINT_MD: string = 'md'; - /** - * Breakpoint lg. - */ - public static readonly BREAK_POINT_LG: string = 'lg'; /** * Destination. */ diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 3a1f3c2..e365757 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -13,102 +13,119 @@ * limitations under the License. */ -import { AbilityConstant, Configuration, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { display, window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { BusinessError } from '@kit.BasicServicesKit'; +const TAG: string = '[EntryAbility]'; + export default class EntryAbility extends UIAbility { private windowObj?: window.Window; private curBp: string = ''; - private updateBreakpoint(windowWidth: number): void { - try { - let windowWidthVp = windowWidth / display.getDefaultDisplaySync().densityPixels; - let newBp: string = ''; - if (windowWidthVp < 600) { - newBp = 'sm'; - } else if (windowWidthVp < 840) { - newBp = 'md'; - } else { - newBp = 'lg'; - } - if (this.curBp !== newBp) { - this.curBp = newBp; - AppStorage.setOrCreate('currentBreakpoint', this.curBp); - } - } catch (err) { - hilog.error(0x0000, 'EntryAbility', `An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); - } - } - onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); - AppStorage.setOrCreate('systemColorMode', this.context.config.colorMode); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onCreate'); this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } onDestroy(): void { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { windowStage.getMainWindow().then((windowObj: window.Window) => { - let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; - let avoidArea = windowObj.getWindowAvoidArea(type); - let bottomRectHeight = avoidArea.bottomRect.height; - AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); - type = window.AvoidAreaType.TYPE_SYSTEM; - avoidArea = windowObj.getWindowAvoidArea(type); - let topRectHeight = avoidArea.topRect.height; - AppStorage.setOrCreate('topRectHeight', topRectHeight); - this.windowObj = windowObj; - this.updateBreakpoint(windowObj.getWindowProperties().windowRect.width); - windowObj.on('windowSizeChange', (windowSize) => { - this.updateBreakpoint(windowSize.width); - }) + try { + let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; + let avoidArea = windowObj.getWindowAvoidArea(type); + let bottomRectHeight = avoidArea.bottomRect.height; + AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); + type = window.AvoidAreaType.TYPE_SYSTEM; + avoidArea = windowObj.getWindowAvoidArea(type); + let topRectHeight = avoidArea.topRect.height; + AppStorage.setOrCreate('topRectHeight', topRectHeight); + this.windowObj = windowObj; + this.updateBreakpoint(windowObj.getWindowProperties().windowRect.width); + windowObj.on('windowSizeChange', (windowSize) => { + this.updateBreakpoint(windowSize.width); + }) + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'get window info catch err: Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + } }); - windowStage.loadContent('pages/Index', (err, data) => { + windowStage.loadContent('pages/Index', (err, _data) => { if (err.code) { - hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', - JSON.stringify(err.message) ?? ''); + hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); return; } this.setWindowClass(windowStage); - hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); + hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in loading the content.'); let windowClass: window.Window = windowStage.getMainWindowSync(); let isLayoutFullScreen = true; windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => { - console.info('Succeeded in setting the window layout to full-screen mode.'); + hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in setting the window layout to full-screen mode.'); }).catch((err: BusinessError) => { - console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err.message)); + hilog.error(0x0000, TAG, 'Failed to set the window layout to full-screen mode. Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); }); }); } - private setWindowClass(windowStage: window.WindowStage): void { - windowStage.getMainWindow((err: BusinessError, data) => { - const errCode: number = err.code; - if (errCode) { - hilog.info(0x0000, 'testTag', 'Failed to get main window'); - return; - } - AppStorage.setOrCreate('windowClass', data); - }) - } - onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground'); + } + + private updateBreakpoint(windowWidth: number): void { + try { + let windowWidthVp = windowWidth / display.getDefaultDisplaySync().densityPixels; + let newBp: string = ''; + if (windowWidthVp < 600) { + newBp = 'sm'; + } else if (windowWidthVp < 840) { + newBp = 'md'; + } else { + newBp = 'lg'; + } + if (this.curBp !== newBp) { + this.curBp = newBp; + AppStorage.setOrCreate('currentBreakpoint', this.curBp); + } + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, '%{public}s', + `An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + } + } + + private setWindowClass(windowStage: window.WindowStage): void { + try { + windowStage.getMainWindow((err: BusinessError, data) => { + const errCode: number = err.code; + if (errCode) { + hilog.error(0x0000, TAG, 'Failed to get main window. Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + return; + } + AppStorage.setOrCreate('windowClass', data); + }) + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, '%{public}s', + `An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + } } } diff --git a/entry/src/main/ets/pages/BackgroundColorChange.ets b/entry/src/main/ets/pages/BackgroundColorChange.ets index b5eafda..dd844c2 100644 --- a/entry/src/main/ets/pages/BackgroundColorChange.ets +++ b/entry/src/main/ets/pages/BackgroundColorChange.ets @@ -62,21 +62,29 @@ struct BackgroundColorChange { } async attach() { - if (this.isFirstOpenPage) { - await this.inputController.attach(true, this.textConfig); - this.listen(); - return; - } try { + if (this.isFirstOpenPage) { + await this.inputController.attach(true, this.textConfig); + this.listen(); + return; + } this.windowClass.on('windowEvent', async (windowState) => { if (windowState === window.WindowEventType.WINDOW_ACTIVE && this.registerCount === 1) { this.registerCount++; - await this.inputController.attach(true, this.textConfig); - this.listen(); + try { + await this.inputController.attach(true, this.textConfig); + this.listen(); + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + } } }); } catch (error) { - hilog.error(0x0000, TAG, `failed to getWindowState callback, error code : ${error.code}`); + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); } } @@ -93,13 +101,19 @@ struct BackgroundColorChange { } detach(): void { - this.isFirstOpenPage = false; - this.registerCount = 1; - this.windowClass.off('windowEvent'); - this.inputController.off('insertText'); - this.inputController.off('deleteLeft'); - this.inputController.detach((_err: BusinessError) => { - }); + try { + this.isFirstOpenPage = false; + this.registerCount = 1; + this.windowClass.off('windowEvent'); + this.inputController.off('insertText'); + this.inputController.off('deleteLeft'); + this.inputController.detach((_err: BusinessError) => { + }); + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'detach catch err: Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + } } @Builder diff --git a/entry/src/main/ets/pages/TextBoxShowCursor.ets b/entry/src/main/ets/pages/TextBoxShowCursor.ets index e55709e..f1cc4be 100644 --- a/entry/src/main/ets/pages/TextBoxShowCursor.ets +++ b/entry/src/main/ets/pages/TextBoxShowCursor.ets @@ -67,21 +67,29 @@ struct TextBoxShowCursor { * Binding an Input Method and Subscribing to Input Method-related Events. */ async attach() { - if (this.isFirstOpenPage) { - await this.inputController.attach(true, this.textConfig); - this.listen(); - return; - } try { + if (this.isFirstOpenPage) { + await this.inputController.attach(true, this.textConfig); + this.listen(); + return; + } this.windowClass.on('windowEvent', async (windowState) => { if (windowState === window.WindowEventType.WINDOW_ACTIVE && this.registerCount === 1) { this.registerCount++; - await this.inputController.attach(true, this.textConfig); - this.listen(); + try { + await this.inputController.attach(true, this.textConfig); + this.listen(); + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + } } }); } catch (error) { - hilog.error(0x0000, TAG, `failed to getWindowState callback, error code : ${error.code}`); + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); } } @@ -90,34 +98,46 @@ struct TextBoxShowCursor { * the keyboard. */ listen() { - this.inputController.on('insertText', (text: string) => { - if (this.codeText.length >= this.verifyCodeLength || isNaN(Number(text)) || text === ' ') { - return; - } - this.codeText += text; - if (this.codeText.length === this.verifyCodeLength) { - this.flag = false; - } - }) - this.inputController.on('deleteLeft', () => { - this.codeText = this.codeText.substring(0, this.codeText.length - 1); - if (this.codeText.length !== this.verifyCodeLength) { - this.flag = true; - } - }) + try { + this.inputController.on('insertText', (text: string) => { + if (this.codeText.length >= this.verifyCodeLength || isNaN(Number(text)) || text === ' ') { + return; + } + this.codeText += text; + if (this.codeText.length === this.verifyCodeLength) { + this.flag = false; + } + }) + this.inputController.on('deleteLeft', () => { + this.codeText = this.codeText.substring(0, this.codeText.length - 1); + if (this.codeText.length !== this.verifyCodeLength) { + this.flag = true; + } + }) + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'input controller on listen catch error, Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + } } /** * Unbinding. */ detach() { - this.isFirstOpenPage = false; - this.registerCount = 1; - this.windowClass.off('windowEvent'); - this.inputController.off('insertText'); - this.inputController.off('deleteLeft'); - this.inputController.detach((_err: BusinessError) => { - }) + try { + this.isFirstOpenPage = false; + this.registerCount = 1; + this.windowClass.off('windowEvent'); + this.inputController.off('insertText'); + this.inputController.off('deleteLeft'); + this.inputController.detach((_err: BusinessError) => { + }) + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'detach catch err: Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + } } /** @@ -157,7 +177,13 @@ struct TextBoxShowCursor { .onClick(() => { // When you click this component, the input method is displayed. Because the Text component is used here, // you need to re-attach it instead of directly using showSoftKeyboard. - this.inputController.attach(true, this.textConfig); + try { + this.inputController.attach(true, this.textConfig); + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + } }) .width(this.curBp === 'sm' ? Constants.FULL_WIDTH : $r('app.float.context_area')) @@ -190,7 +216,13 @@ struct TextBoxShowCursor { }) .hitTestBehavior(HitTestMode.Transparent) .onClick(() => { - this.inputController.attach(true, this.textConfig); + try { + this.inputController.attach(true, this.textConfig); + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, 'attach catch error, Cause: %{public}s', + `the code is ${err.code}, the message is ${err.message}`); + } }) .justifyContent(FlexAlign.Center) .height(Constants.ONE_HUNDRED_PERCENT) -- Gitee