diff --git a/ArkUI/entry/src/main/ets/pages/PopUpAgainInCustomPopUps.ets b/ArkUI/entry/src/main/ets/pages/PopUpAgainInCustomPopUps.ets index 35fbdb3728ea2eae31595a521d1265a96db4d253..8f8a80d6d65c089ef2f0d51e30ac4b4a497c8b96 100644 --- a/ArkUI/entry/src/main/ets/pages/PopUpAgainInCustomPopUps.ets +++ b/ArkUI/entry/src/main/ets/pages/PopUpAgainInCustomPopUps.ets @@ -18,139 +18,171 @@ */ // [Start pop_up_again_in_custom_pop_ups] -@CustomDialog -struct CustomDialogExampleTwo { - controllerTwo?: CustomDialogController; +import { ComponentContent, promptAction, UIContext } from '@kit.ArkUI'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BusinessError } from '@kit.BasicServicesKit'; - build() { +let dialogA_ContentNode: ComponentContent; +let dialogB_ContentNode: ComponentContent; + +@Builder +function dialogA_Component() { + Column() { Column() { - Text('I am the second pop-up window') - .fontSize(30) - .height(100) - Button('Click on me to close the second pop-up window') + Text('dialog A') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .justifyContent(FlexAlign.Center) + .height(120) + + Row() { + Text('close') + .fontColor('#0A59F7') .onClick(() => { - if (this.controllerTwo != undefined) { - this.controllerTwo.close(); - } + // close dialog A. + PromptActionClass.closeDialog(dialogA_ContentNode); }) - .margin(20) + .width('50%') + .height('100%') + .textAlign(TextAlign.Center) + + Text('open dialog B') + .fontColor('#0A59F7') + .onClick(() => { + // Open dialog B. + PromptActionClass.setContentNode(dialogB_ContentNode); + PromptActionClass.openDialog(); + }) + .width('50%') + .height('100%') + .textAlign(TextAlign.Center) } + .height(50) } + .width(360) + .borderRadius(32) + .backgroundColor(Color.White) } -@CustomDialog -struct CustomDialogExample { - @Link textValue: string; - @Link inputValue: string; - dialogControllerTwo: CustomDialogController | null = new CustomDialogController({ - builder: CustomDialogExampleTwo(), - alignment: DialogAlignment.Bottom, - offset: { dx: 0, dy: -25 } - }) - // If you need to pass in controllers for multiple other pop ups in a custom pop-up, the controller definition for the current custom pop-up should be placed after the other incoming controllers - controller?: CustomDialogController; - cancel: () => void = () => { +@Builder +function dialogB_Component() { + Column() { + Column() { + Text('dialog B') + .fontSize(20) + .fontWeight(FontWeight.Bold) + } + .justifyContent(FlexAlign.Center) + .height(120) + + Row() { + Text('close') + .fontColor('#0A59F7') + .onClick(() => { + // close dialog B. + PromptActionClass.closeDialog(dialogB_ContentNode); + }) + .width('50%') + .height('100%') + .textAlign(TextAlign.Center) + } + .height(50) } - confirm: () => void = () => { + .width(360) + .borderRadius(32) + .backgroundColor(Color.White) +} + +@Entry +@Component +struct PopUpAgainInCustomPopUps { + aboutToAppear(): void { + let uiContext = this.getUIContext(); + dialogA_ContentNode = new ComponentContent(uiContext, wrapBuilder(dialogA_Component)); + dialogB_ContentNode = new ComponentContent(uiContext, wrapBuilder(dialogB_Component)); } build() { Column() { - Text('Change text') - .fontSize(20) - .margin({ top: 10, bottom: 10 }) - TextInput({ placeholder: '', text: this.textValue }) - .height(60) - .width('90%') - .onChange((value: string) => { - this.textValue = value; - }) - Text('Whether to change a text?') - .fontSize(16) - .margin({ bottom: 10 }) - Flex({ justifyContent: FlexAlign.SpaceAround }) { - Button('cancel') - .onClick(() => { - if (this.controller != undefined) { - this.controller.close(); - this.cancel(); - } - }).backgroundColor(0xffffff).fontColor(Color.Black) - Button('confirm') - .onClick(() => { - if (this.controller != undefined) { - this.inputValue = this.textValue; - this.controller.close(); - this.confirm(); - } - }) - .backgroundColor(0xffffff) - .fontColor(Color.Red) - } - .margin({ bottom: 10 }) - - Button('Click on me to open the second pop-up window') + Button('open Dialog A') .onClick(() => { - if (this.dialogControllerTwo != null) { - this.dialogControllerTwo.open(); - } + // Open dialog A. + let uiContext = this.getUIContext(); + PromptActionClass.setContext(uiContext); + PromptActionClass.setContentNode(dialogA_ContentNode); + PromptActionClass.openDialog(); }) - .margin(20) } - .borderRadius(10) + .width('100%') + .height('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Center) } } -@Component -export struct CustomDialogUser { - @State textValue: string = ''; - @State inputValue: string = 'click me'; - dialogController: CustomDialogController | null = new CustomDialogController({ - builder: CustomDialogExample({ - cancel: this.onCancel, - confirm: this.onAccept, - textValue: $textValue, - inputValue: $inputValue - }), - cancel: this.exitApp, - autoCancel: true, - alignment: DialogAlignment.Bottom, - offset: { dx: 0, dy: -20 }, - gridCount: 4, - customStyle: false, - backgroundColor: 0xd9ffffff, - cornerRadius: 10, - }) - - // When the custom component is about to be destructed, leave dialogController empty - aboutToDisappear() { - this.dialogController = null; // Set dialogController to empty +// Dialog class for functions such as opening, closing, and updating packages. +export class PromptActionClass { + static ctx: UIContext; + static contentNode: ComponentContent; + static options: promptAction.BaseDialogOptions; + + static setContext(context: UIContext) { + PromptActionClass.ctx = context; } - onCancel() { - console.info('Callback when the first button is clicked'); + static setContentNode(node: ComponentContent) { + PromptActionClass.contentNode = node; } - onAccept() { - console.info('Callback when the second button is clicked'); + static setOptions(options: promptAction.BaseDialogOptions) { + PromptActionClass.options = options; } - exitApp() { - console.info('Click the callback in the blank area'); + static openDialog() { + if (PromptActionClass.contentNode !== null) { + PromptActionClass.ctx.getPromptAction() + .openCustomDialog(PromptActionClass.contentNode, PromptActionClass.options) + .then(() => { + hilog.info(0xFF00, 'TAG', 'OpenCustomDialog complete'); + }) + .catch((error: BusinessError) => { + let message = (error as BusinessError).message; + let code = (error as BusinessError).code; + hilog.info(0xFF00, 'TAG', `OpenCustomDialog args error code is ${code}, message is ${message}`); + }) + } } - build() { - Column() { - Button(this.inputValue) - .onClick(() => { - if (this.dialogController != null) { - this.dialogController.open(); - } + static closeDialog(contentNode: ComponentContent) { + if (PromptActionClass.contentNode !== null) { + PromptActionClass.ctx.getPromptAction() + .closeCustomDialog(contentNode) + .then(() => { + hilog.info(0xFF00, 'TAG', 'CloseCustomDialog complete'); + }) + .catch((error: BusinessError) => { + let message = (error as BusinessError).message; + let code = (error as BusinessError).code; + hilog.info(0xFF00, 'TAG', `CloseCustomDialog args error code is ${code}, message is ${message}`); + }) + } + } + + static updateDialog(options: promptAction.BaseDialogOptions) { + if (PromptActionClass.contentNode !== null) { + PromptActionClass.ctx.getPromptAction() + .updateCustomDialog(PromptActionClass.contentNode, options) + .then(() => { + hilog.info(0xFF00, 'TAG', 'UpdateCustomDialog complete'); + }) + .catch((error: BusinessError) => { + let message = (error as BusinessError).message; + let code = (error as BusinessError).code; + hilog.info(0xFF00, 'TAG', `UpdateCustomDialog args error code is ${code}, message is ${message}`); }) - .backgroundColor(0x317aff) } - .width('100%') - .margin({ top: 5 }) } } + // [End pop_up_again_in_custom_pop_ups] \ No newline at end of file