From e09abbf7131bb6f4aeafa1a5eddbaf709fe497c8 Mon Sep 17 00:00:00 2001 From: TickKiwi <> Date: Sat, 21 Dec 2024 18:09:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=BC=B9=E7=AA=97=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E5=90=8E=E7=AC=AC=E4=BA=8C=E6=AC=A1=E6=89=93=E5=BC=80=EF=BC=8C?= =?UTF-8?q?requestFocusWhenShow=E4=B8=8D=E7=94=9F=E6=95=88=EF=BC=8C?= =?UTF-8?q?=E6=80=BB=E4=BC=9A=E8=8E=B7=E7=84=A6=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-profile.json5 | 13 +++ .../main/ets/entryability/EntryAbility.ets | 2 +- entry/src/main/ets/pages/CustomTest.ets | 104 ++++++++++++++++++ .../resources/base/profile/main_pages.json | 3 +- .../ets/model/dialog/base/CustomDialog.ets | 3 + 5 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 entry/src/main/ets/pages/CustomTest.ets diff --git a/build-profile.json5 b/build-profile.json5 index f044322..c6b2c99 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -23,6 +23,19 @@ } ], "signingConfigs": [ + { + "name": "default", + "type": "HarmonyOS", + "material": { + "certpath": "/Users/qiyinoqiyiguo/.ohos/config/default_dialoghub_MWsAVMPMKmMH77VEftagorGm3KI4KaglVf5KFS0dy0c=.cer", + "storePassword": "0000001B4FCC6869DF486EEDC668A74F1ED94B763CC91BAD0C605407A241C2F2A44B2BC6B59D2E01D40707", + "keyAlias": "debugKey", + "keyPassword": "0000001B2A42D43D3673C82F2956B2AFF115C8A8F37EAC720418CD48BC2A47D8A6A8831FD0BEEA073E003B", + "profile": "/Users/qiyinoqiyiguo/.ohos/config/default_dialoghub_MWsAVMPMKmMH77VEftagorGm3KI4KaglVf5KFS0dy0c=.p7b", + "signAlg": "SHA256withECDSA", + "storeFile": "/Users/qiyinoqiyiguo/.ohos/config/default_dialoghub_MWsAVMPMKmMH77VEftagorGm3KI4KaglVf5KFS0dy0c=.p12" + } + } ] }, "modules": [ diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 5445a81..479b29f 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -16,7 +16,7 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - windowStage.loadContent('pages/Index', (err) => { + windowStage.loadContent('pages/CustomTest', (err) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; diff --git a/entry/src/main/ets/pages/CustomTest.ets b/entry/src/main/ets/pages/CustomTest.ets new file mode 100644 index 0000000..158622a --- /dev/null +++ b/entry/src/main/ets/pages/CustomTest.ets @@ -0,0 +1,104 @@ +import { + DialogHub, + AnimationType, + DialogAction, + InfCustomDialog, + DialogDismissReason, + InfSheet, + InfPopup, + DialogConfig, + CustomKeyboardAvoidMode +} from "hadss_dialog" +import { CustomDialog } from "hadss_dialog/src/main/ets/model/dialog/base/CustomDialog" + +export class CustomDialogParams { + text: string = "" + num: number = 0 + onClose?: () => void + onCancel?: (() => void) + + constructor(text: string, onCancel?: (() => void)) { + this.text = text + if (onCancel) { + this.onCancel = onCancel + } + } +} + +@Builder +export function CustomTestBuilder() { + CustomTest() +} + +@Builder +export function CustomDialogBuilder(params: CustomDialogParams) { + Column() { + Text(params.num.toString()).fontSize(20) + Text(params.text) + .fontSize(20) + .fontColor(Color.Black) + .margin({ bottom: 15 }) + TextInput({ placeholder: '请输入' }) + + Row() { + Button("确认").onClick(() => { + }) + Blank().width(50) + Button("取消").onClick(() => { + if (params.onCancel) { + params.onCancel() + } + console.log('cancel clicked'); + console.log('params is null: ', params.onClose); + if (params.onClose) { + params.onClose(); + } + }) + } + } + .borderRadius(10) + .padding(20) + .height(280) + .backgroundColor(Color.White) +} + +@Entry +@Component +struct CustomTest { + customDialog: InfCustomDialog | undefined = undefined; + + aboutToAppear(): void { + this.initDialog(); + } + + initDialog() { + DialogHub.init(this.getUIContext()); + let config: DialogConfig = { + dialogBehavior: { + isModal: true, + passThroughGesture: false, + autoDismiss: true, + keyboardAvoidMode: CustomKeyboardAvoidMode.CONTENT_AVOID, + keyboardAvoidSpace: 0, + requestFocusWhenShow: true + } + }; + this.customDialog = DialogHub.getCustomDialog().setContent(wrapBuilder(CustomDialogBuilder), new CustomDialogParams('I am common dialog !')) + .setConfig(config).build(); + } + build() { + Column() { + TextInput({ placeholder: '请输入内容...' }) + TextInput({ placeholder: "第二个弹窗" }) + Button('显示弹窗') + .onClick(() => { + console.log('tagsf button clicked') + this.customDialog?.show(); + }) + } + .width('100%') + .height('100%') + .justifyContent(FlexAlign.Center) + .backgroundColor(Color.Gray) + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index 73d2a8e..ae2ba98 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -7,6 +7,7 @@ "pages/TemplateTest", "pages/NavigationExample", "pages/CustomPopupTest", - "pages/APITestPage" + "pages/APITestPage", + "pages/CustomTest" ] } \ No newline at end of file diff --git a/hadss_dialog/src/main/ets/model/dialog/base/CustomDialog.ets b/hadss_dialog/src/main/ets/model/dialog/base/CustomDialog.ets index f9dae52..429ee8b 100644 --- a/hadss_dialog/src/main/ets/model/dialog/base/CustomDialog.ets +++ b/hadss_dialog/src/main/ets/model/dialog/base/CustomDialog.ets @@ -145,6 +145,9 @@ export class CustomDialog extends CommonDialog implements InfCustomDialog { } public dismiss(dismissReason?: DialogDismissReason | undefined): boolean { + + this.customOptions.focusable = false; + this.dialogExecutor?.update(['focusable']); if (this.needRestoreAlwaysTop) { (this.builderOptions as CustomOption)!.layerPolicy!.alwaysTop = true; this.needRestoreAlwaysTop = false; -- Gitee From ac41e408d7bdf4d7f97b3a24c28d583973a468fe Mon Sep 17 00:00:00 2001 From: TickKiwi <> Date: Tue, 24 Dec 2024 10:27:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E6=A1=86=E6=BF=80=E6=B4=BB=E9=94=AE=E7=9B=98=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E8=A7=84=E9=81=BF=E5=90=8E=EF=BC=8C=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=90=AB=E6=9C=89=E8=BE=93=E5=85=A5=E6=A1=86=E7=9A=84?= =?UTF-8?q?overlay=E5=BC=B9=E7=AA=97=EF=BC=8C=E6=AD=A4=E6=97=B6=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E5=BA=94=E8=AF=A5=E8=A7=84=E9=81=BF=E9=94=AE=E7=9B=98?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hadss_dialog/src/main/ets/model/DialogHub.ets | 13 +++ .../ets/model/dialog/base/CustomDialog.ets | 93 +++++++++++++------ 2 files changed, 76 insertions(+), 30 deletions(-) diff --git a/hadss_dialog/src/main/ets/model/DialogHub.ets b/hadss_dialog/src/main/ets/model/DialogHub.ets index 7ca43ac..8f02f31 100644 --- a/hadss_dialog/src/main/ets/model/DialogHub.ets +++ b/hadss_dialog/src/main/ets/model/DialogHub.ets @@ -46,6 +46,9 @@ export class DialogHub { private static globalEventListener: GlobalEventHelper = new GlobalEventHelper() private static isInit = false + //当前窗口中软键盘高度 + private static keyboardHeight: number = 0; + private constructor() { } @@ -261,4 +264,14 @@ export class DialogHub { static removeEventListener(listener: GlobalEvent) { DialogHub.globalEventListener.removeListener(listener) } + + //保存当前弹窗所在窗口中软键盘高度 + static setKeyboardHeight(height: number) { + DialogHub.keyboardHeight = height; + } + + //获取当前弹窗所在窗口中软键盘高度 + static getKeyboardHeight() { + return DialogHub.keyboardHeight; + } } \ No newline at end of file diff --git a/hadss_dialog/src/main/ets/model/dialog/base/CustomDialog.ets b/hadss_dialog/src/main/ets/model/dialog/base/CustomDialog.ets index 429ee8b..9ab567a 100644 --- a/hadss_dialog/src/main/ets/model/dialog/base/CustomDialog.ets +++ b/hadss_dialog/src/main/ets/model/dialog/base/CustomDialog.ets @@ -25,6 +25,7 @@ import { KEYBOARD_ANIMATION_TIME, KEYBOARD_ANIMATION_INTERVAL } from '../../../c import { OverlayExecutor } from '../executor/OverlayExecutor'; import { CustomKeyboardAvoidMode } from '../../../common/been/config/DialogConfig'; import { DialogStyle } from '../../../common/been/style/DialogStyle'; +import { DialogHub } from '../../DialogHub'; /** * 基于系统BindSheet封装的弹窗实例 @@ -34,7 +35,7 @@ export class CustomDialog extends CommonDialog implements InfCustomDialog { private appLifeCycleHelper: AppLifeCycleHelper; private needRestoreAlwaysTop: boolean = false; private customOptions: CustomOption; - private originalPosition: Length = 0; + private originalPosition: Length | undefined = undefined; private spaceNotEnough: boolean = false; private static DEFAULT_SPACE: number = 16; //拉起键盘时默认与弹窗保持16vp距离 private ANIMATION_TIMES: number = 20; //键盘避让,弹窗执行动画的次数 @@ -57,7 +58,6 @@ export class CustomDialog extends CommonDialog implements InfCustomDialog { let changeSpace = space / animationSplitTimes; let pos = startPos - changeSpace; let interval = setInterval(() => { - console.log('interval count: ', count); if (count >= animationSplitTimes) { clearInterval(interval); } @@ -73,39 +73,65 @@ export class CustomDialog extends CommonDialog implements InfCustomDialog { //处理键盘避让 private processKeyboardAvoid() { this.appLifeCycleHelper.register('keyboardHeightChange', this, (height) => { - if (this.customOptions.keyboardAvoidMode != CustomKeyboardAvoidMode.CONTENT_AVOID) { + console.log('keyboardHeight: ', height); + DialogHub.setKeyboardHeight(height); + //弹窗未显示,或避让模式不是整个弹窗避让键盘时,不进行后续避让处理 + if (!this.isShow() || this.customOptions.keyboardAvoidMode != CustomKeyboardAvoidMode.CONTENT_AVOID) { return; } // let space: number = 0; - if (height > 0) { //拉起键盘时进行判断 - let rectInfo = componentUtils.getRectangleById(this.dialogId); - let rectHeight = rectInfo.size.height, rectOffsetY = rectInfo.screenOffset.y; - let displayClass = display.getDefaultDisplaySync(); - //处理间距 - let avoidSpace = this.customOptions.keyboardAvoidSpace; - let avoidSpaceNum = 0; - if (avoidSpace == null) { //默认避让16vp - avoidSpaceNum = CustomDialog.DEFAULT_SPACE; - } else { - if (typeof avoidSpace == 'string') { - if (avoidSpace.endsWith('vp')) { - avoidSpaceNum = parseInt(avoidSpace.substring(0, avoidSpace.length - 2)); - } else if (avoidSpace.endsWith('px')) { - avoidSpaceNum = px2vp(parseInt(avoidSpace.substring(0, avoidSpace.length - 2))); - } + this.avoidKeyboard(height, true); + }) + } + + /** + * + * @param height 键盘高度 + * @param processAnimation 是否执行弹窗避让键盘动画 + */ + private avoidKeyboard(height: number, processAnimation?: boolean) { + if (height > 0) { //拉起键盘时进行判断 + console.log('start judge') + let rectInfo = componentUtils.getRectangleById(this.dialogId); + let rectHeight = rectInfo.size.height, rectOffsetY = rectInfo.screenOffset.y; + let displayClass = display.getDefaultDisplaySync(); + //处理间距 + let avoidSpace = this.customOptions.keyboardAvoidSpace; + let avoidSpaceNum = 0; + if (avoidSpace == null) { //默认避让16vp + avoidSpaceNum = CustomDialog.DEFAULT_SPACE; + } else { + if (typeof avoidSpace == 'string') { + if (avoidSpace.endsWith('vp')) { + avoidSpaceNum = parseInt(avoidSpace.substring(0, avoidSpace.length - 2)); + } else if (avoidSpace.endsWith('px')) { + avoidSpaceNum = px2vp(parseInt(avoidSpace.substring(0, avoidSpace.length - 2))); } } - this.animationSpace = px2vp(rectOffsetY + rectHeight + height - displayClass.height) + avoidSpaceNum; - if (this.animationSpace > 0) { //键盘遮挡弹窗时进行处理 - this.spaceNotEnough = true; + } + console.log('height: ', rectOffsetY, rectHeight, displayClass.height, avoidSpaceNum); + this.animationSpace = px2vp(rectOffsetY + rectHeight + height - displayClass.height) + avoidSpaceNum; + if (this.animationSpace > 0) { //键盘遮挡弹窗时进行处理 + this.spaceNotEnough = true; + console.log('offset.y: ', this.customOptions.offset.y); + if (this.originalPosition == undefined) { this.originalPosition = (this.customOptions.offset && this.customOptions.offset.y) ? this.customOptions.offset.y : 0; + } + console.log("originalPosition: ", this.originalPosition); + console.log('animationSpace: ', this.animationSpace); + if (processAnimation) { this.startKeyboardAnimation(this.originalPosition as number, this.animationSpace); + } else { + this.customOptions.offset = { + x: 0, y: ((this.originalPosition as number) - this.animationSpace) + } + this.update(['offset']); } - } else if (this.spaceNotEnough) { - this.startKeyboardAnimation((this.originalPosition as number) - this.animationSpace, -this.animationSpace); } - }) + } else if (this.spaceNotEnough) { + this.startKeyboardAnimation((this.originalPosition as number) - this.animationSpace, -this.animationSpace); + } } public resetIndex(index: number); @@ -145,9 +171,6 @@ export class CustomDialog extends CommonDialog implements InfCustomDialog { } public dismiss(dismissReason?: DialogDismissReason | undefined): boolean { - - this.customOptions.focusable = false; - this.dialogExecutor?.update(['focusable']); if (this.needRestoreAlwaysTop) { (this.builderOptions as CustomOption)!.layerPolicy!.alwaysTop = true; this.needRestoreAlwaysTop = false; @@ -158,15 +181,25 @@ export class CustomDialog extends CommonDialog implements InfCustomDialog { public show(isInsideCall?: boolean | undefined): boolean { let res = super.show(isInsideCall); // precess defaultFocus + console.log('requestFocusWhenShow: ', this.customOptions.requestFocusWhenShow); if (this.customOptions.requestFocusWhenShow) { this.customOptions.focusable = true; - this.update(['focusable']) + + //如果弹窗打开就获焦,则打开时需要进行键盘避让处理 + if (res && DialogHub.getKeyboardHeight() > 0) { + setTimeout(() => { + this.avoidKeyboard(DialogHub.getKeyboardHeight()); + }, 10) + } else { + this.update(['focusable']) + } } else { setTimeout(()=>{ this.customOptions.focusable = true; this.update(['focusable']) - },10) + },100) } + return res; } } -- Gitee From 75ac3e801eba3506e1f2b405098c7c40beb38816 Mon Sep 17 00:00:00 2001 From: TickKiwi <> Date: Tue, 24 Dec 2024 10:41:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=BF=98=E5=8E=9F=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=92=8C=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-profile.json5 | 13 ------------- entry/src/main/ets/entryability/EntryAbility.ets | 2 +- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/build-profile.json5 b/build-profile.json5 index c6b2c99..f044322 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -23,19 +23,6 @@ } ], "signingConfigs": [ - { - "name": "default", - "type": "HarmonyOS", - "material": { - "certpath": "/Users/qiyinoqiyiguo/.ohos/config/default_dialoghub_MWsAVMPMKmMH77VEftagorGm3KI4KaglVf5KFS0dy0c=.cer", - "storePassword": "0000001B4FCC6869DF486EEDC668A74F1ED94B763CC91BAD0C605407A241C2F2A44B2BC6B59D2E01D40707", - "keyAlias": "debugKey", - "keyPassword": "0000001B2A42D43D3673C82F2956B2AFF115C8A8F37EAC720418CD48BC2A47D8A6A8831FD0BEEA073E003B", - "profile": "/Users/qiyinoqiyiguo/.ohos/config/default_dialoghub_MWsAVMPMKmMH77VEftagorGm3KI4KaglVf5KFS0dy0c=.p7b", - "signAlg": "SHA256withECDSA", - "storeFile": "/Users/qiyinoqiyiguo/.ohos/config/default_dialoghub_MWsAVMPMKmMH77VEftagorGm3KI4KaglVf5KFS0dy0c=.p12" - } - } ] }, "modules": [ diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 479b29f..5445a81 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -16,7 +16,7 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - windowStage.loadContent('pages/CustomTest', (err) => { + windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; -- Gitee