From b49e0d28dd84e491ee153e2860def513ff9b0908 Mon Sep 17 00:00:00 2001 From: ywcoder <1104410818@qq.com> Date: Thu, 13 Mar 2025 21:13:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E3=80=81DTS2025031127074=E5=8D=95?= =?UTF-8?q?=E9=97=AE=E9=A2=981=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GlobalDialogDecoupledFromThePage.ets | 76 ++++++++++++++----- .../src/main/ets/uitls/DialogComponent.ets | 63 --------------- .../src/main/ets/uitls/PromptActionClass.ets | 69 +++++++++++++++++ 3 files changed, 128 insertions(+), 80 deletions(-) delete mode 100644 CustomDialogPractice/entry/src/main/ets/uitls/DialogComponent.ets create mode 100644 CustomDialogPractice/entry/src/main/ets/uitls/PromptActionClass.ets diff --git a/CustomDialogPractice/entry/src/main/ets/pages/GlobalDialogDecoupledFromThePage.ets b/CustomDialogPractice/entry/src/main/ets/pages/GlobalDialogDecoupledFromThePage.ets index f1697725..74bd700b 100644 --- a/CustomDialogPractice/entry/src/main/ets/pages/GlobalDialogDecoupledFromThePage.ets +++ b/CustomDialogPractice/entry/src/main/ets/pages/GlobalDialogDecoupledFromThePage.ets @@ -13,18 +13,68 @@ * limitations under the License. */ -import { buildText } from '../uitls/DialogComponent'; -import { PromptAction } from '@kit.ArkUI'; +import { PromptActionClass } from '../uitls/PromptActionClass'; +import { ComponentContent } from '@kit.ArkUI'; + +class Params { + text: string = "" + + constructor(text: string) { + this.text = text; + } +} + + +@Builder +function buildText(params: Params) { + Column() { + Row() { + Text('Title') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .width('100%') + .height(56) + .justifyContent(FlexAlign.Center) + + Text(params.text) + .fontSize(14) + + Button('CONFIRM') + .fontSize(16) + .fontColor('#0A59F7') + .backgroundColor(Color.White) + .onClick(() => { + PromptActionClass.closeDialog(); + }) + .width('100%') + .margin({ + top: 8, + bottom: 16 + }) + } + .padding({ + left: 24, + right: 24 + }) + .justifyContent(FlexAlign.Center) + .alignItems(HorizontalAlign.Center) + .backgroundColor(Color.White) + .borderRadius(32) + .margin({ left: 16, right: 16 }) +} @Component export struct GlobalDialogDecoupledFromThePage { - promptAction: PromptAction = this.getUIContext().getPromptAction(); - @State message: string = 'This is a dialog content.'; - @Consume('NavPathStack') pageStack: NavPathStack; + @State message: string = "This is a dialog content."; + private ctx: UIContext = this.getUIContext(); + private contentNode: ComponentContent = + new ComponentContent(this.ctx, wrapBuilder(buildText), new Params(this.message)); - @Builder - customDialogComponent() { - buildText({ message: this.message }) + aboutToAppear(): void { + PromptActionClass.setContext(this.ctx); + PromptActionClass.setContentNode(this.contentNode); + PromptActionClass.setOptions({ alignment: DialogAlignment.Center, maskColor: 'rgba(0, 0, 0, 0.2)' }); } build() { @@ -38,15 +88,7 @@ export struct GlobalDialogDecoupledFromThePage { .margin({ bottom: 16 }) .backgroundColor('#0A59F7') .onClick(() => { - this.promptAction.openCustomDialog({ - builder: () => { - this.customDialogComponent() - }, - alignment: DialogAlignment.Center, - maskColor: 'rgba(0, 0, 0, 0.2)', - }).then((dialogId: number) => { - AppStorage.setOrCreate('dialogId', dialogId); - }) + PromptActionClass.openDialog(); }) } .width('100%') diff --git a/CustomDialogPractice/entry/src/main/ets/uitls/DialogComponent.ets b/CustomDialogPractice/entry/src/main/ets/uitls/DialogComponent.ets deleted file mode 100644 index 0ec61415..00000000 --- a/CustomDialogPractice/entry/src/main/ets/uitls/DialogComponent.ets +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { promptAction } from "@kit.ArkUI"; - -// DocsCode 1 -@Component -export struct buildText { - @Prop message: string = ''; - - build() { - Column() { - Row() { - Text('Title') - .fontSize(20) - .fontWeight(FontWeight.Bold) - } - .width('100%') - .height(56) - .justifyContent(FlexAlign.Center) - - Text(this.message) - .fontSize(14) - - Button('CONFIRM') - .fontSize(16) - .fontColor('#0A59F7') - .backgroundColor(Color.White) - .onClick(() => { - let dialogId = AppStorage.get('dialogId'); - promptAction.closeCustomDialog(dialogId) - }) - .width('100%') - .margin({ - top: 8, - bottom: 16 - }) - } - .padding({ - left: 24, - right: 24 - }) - .justifyContent(FlexAlign.Center) - .alignItems(HorizontalAlign.Center) - .backgroundColor(Color.White) - .borderRadius(32) - .margin({ left: 16, right: 16 }) - } -} - -// DocsCode 1 \ No newline at end of file diff --git a/CustomDialogPractice/entry/src/main/ets/uitls/PromptActionClass.ets b/CustomDialogPractice/entry/src/main/ets/uitls/PromptActionClass.ets new file mode 100644 index 00000000..f83cea56 --- /dev/null +++ b/CustomDialogPractice/entry/src/main/ets/uitls/PromptActionClass.ets @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BusinessError } from '@kit.BasicServicesKit'; +import { ComponentContent, promptAction } from '@kit.ArkUI'; +import { UIContext } from '@ohos.arkui.UIContext'; + +export class PromptActionClass { + static ctx: UIContext; + static contentNode: ComponentContent; + static options: promptAction.BaseDialogOptions; + + static setContext(context: UIContext) { + PromptActionClass.ctx = context; + } + + static setContentNode(node: ComponentContent) { + PromptActionClass.contentNode = node; + } + + static setOptions(options: promptAction.BaseDialogOptions) { + PromptActionClass.options = options; + } + + static openDialog() { + if (PromptActionClass.contentNode !== null) { + PromptActionClass.ctx.getPromptAction() + .openCustomDialog(PromptActionClass.contentNode, PromptActionClass.options) + .then(() => { + console.info('OpenCustomDialog complete.') + }) + .catch((error: BusinessError) => { + let message = (error as BusinessError).message; + let code = (error as BusinessError).code; + console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`); + }) + } + } + + static closeDialog() { + if (PromptActionClass.contentNode !== null) { + PromptActionClass.ctx.getPromptAction() + .closeCustomDialog(PromptActionClass.contentNode) + .then(() => { + console.info('CloseCustomDialog complete.') + if (PromptActionClass.contentNode !== null) { + PromptActionClass.contentNode.dispose(); // 释放contentNode + } + }) + .catch((error: BusinessError) => { + let message = (error as BusinessError).message; + let code = (error as BusinessError).code; + console.error(`CloseCustomDialog args error code is ${code}, message is ${message}`); + }) + } + } +} \ No newline at end of file -- Gitee From c5e8edc567ba44bbd87c10e92a3ac73e05bb8850 Mon Sep 17 00:00:00 2001 From: ywcoder <1104410818@qq.com> Date: Thu, 20 Mar 2025 11:13:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1=E3=80=81DTS2025031127074=E5=8D=95?= =?UTF-8?q?=E9=97=AE=E9=A2=982=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/pages/CustomDialogDisplayAndExitAnimations.ets | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/CustomDialogPractice/entry/src/main/ets/pages/CustomDialogDisplayAndExitAnimations.ets b/CustomDialogPractice/entry/src/main/ets/pages/CustomDialogDisplayAndExitAnimations.ets index c25234e7..1cf78948 100644 --- a/CustomDialogPractice/entry/src/main/ets/pages/CustomDialogDisplayAndExitAnimations.ets +++ b/CustomDialogPractice/entry/src/main/ets/pages/CustomDialogDisplayAndExitAnimations.ets @@ -82,17 +82,9 @@ export struct CustomDialogDisplayAndExitAnimations { transition: TransitionEffect.asymmetric( TransitionEffect.OPACITY .animation({ duration: 1000 }) - .combine( - TransitionEffect - .translate({ y: 1000 }) - .animation({ duration: 1000 })) , TransitionEffect.OPACITY - .animation({ delay: 1000, duration: 1000 }) - .combine( - TransitionEffect - .translate({ y: 1000 }) - .animation({ duration: 1000 })) + .animation({ delay: 500, duration: 1000 }) ) }).then((dialogId: number) => { this.customDialogComponentId = dialogId; -- Gitee