1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import router from '@ohos.router';
17import { ColumnSize, ScreenManager } from '../../model/common/ScreenManager';
18import { Constants } from '../../model/common/Constants';
19
20@Observed
21export class EditExitDialogCallback {
22  public discardCallback: Function = (): void => {};
23}
24
25@CustomDialog
26export struct EditExitDialog {
27  @Consume editExitDialogCallback: EditExitDialogCallback;
28  @StorageLink('isHorizontal') isHorizontal: boolean = ScreenManager.getInstance().isHorizontal();
29  @StorageLink('isSidebar') isSidebar: boolean = ScreenManager.getInstance().isSidebar();
30  @StorageLink('leftBlank') leftBlank: number[] =
31    [0, ScreenManager.getInstance().getStatusBarHeight(), 0, ScreenManager.getInstance().getNaviBarHeight()];
32  controller?: CustomDialogController;
33  private isPcDevice: boolean = AppStorage.get<string>('deviceType') === Constants.PC_DEVICE_TYPE;
34
35  build() {
36    Column() {
37      Column() {
38        Text($r('app.string.edit_exit_dialog_context_text'))
39          .fontSize($r('sys.float.ohos_id_text_size_body1'))
40          .fontWeight(FontWeight.Regular)
41          .fontColor($r('sys.color.ohos_id_color_text_primary'))
42      }
43      .width('100%')
44      .margin({
45        top: $r('app.float.dialog_content_margin'),
46        bottom: $r('sys.float.ohos_id_elements_margin_vertical_l')
47      })
48
49      Column() {
50        Row() {
51          Column() {
52            Button() {
53              Text($r('app.string.save_dialog_cancel_text'))
54                .fontSize($r('sys.float.ohos_id_text_size_button1'))
55                .fontColor($r('app.color.color_control_highlight'))
56                .fontWeight(FontWeight.Medium)
57                .width('100%')
58                .textAlign(TextAlign.Center)
59            }
60            .key('Cancel')
61            .margin({
62              right: $r('app.float.dialog_double_buttons_margin_right')
63            })
64            .backgroundColor(this.isPcDevice ? $r('sys.color.ohos_id_color_button_normal') : $r('app.color.transparent'))
65            .borderRadius($r('sys.float.ohos_id_corner_radius_button'))
66            .height($r('app.float.details_dialog_button_height'))
67            .onClick(() => {
68              this.controller?.close();
69            })
70          }.width('50%')
71
72          if (!this.isPcDevice) {
73            Divider()
74              .vertical(true)
75              .color(Constants.DEFAULT_DIVIDER_COLOR)
76              .height(Constants.DEFAULT_DIVIDER_HEIGHT)
77          }
78
79          Column() {
80            Button() {
81              Text($r('app.string.edit_exit_dialog_discard_text'))
82                .fontSize($r('sys.float.ohos_id_text_size_button1'))
83                .fontColor($r('app.color.color_control_highlight'))
84                .fontWeight(FontWeight.Medium)
85                .width('100%')
86                .textAlign(TextAlign.Center)
87            }
88            .key('Discard')
89            .margin({
90              left: $r('app.float.dialog_double_buttons_margin_left')
91            })
92            .backgroundColor(this.isPcDevice ? $r('sys.color.ohos_id_color_button_normal') : $r('app.color.transparent'))
93            .borderRadius($r('sys.float.ohos_id_corner_radius_button'))
94            .height($r('app.float.details_dialog_button_height'))
95            .onClick(() => {
96              this.controller?.close();
97              this.editExitDialogCallback && this.editExitDialogCallback.discardCallback()
98              router.back()
99            })
100          }.width('50%')
101        }
102      }
103      .width('100%')
104      .height($r('app.float.details_dialog_button_area_height'))
105    }
106    .width(this.isPcDevice ? $r('app.float.pc_dialog_width') : ScreenManager.getInstance()
107                                                                 .getColumnsWidth(ColumnSize.COLUMN_FOUR))
108    .backgroundColor($r('app.color.white'))
109    .borderRadius($r('sys.float.ohos_id_corner_radius_default_l'))
110    .margin({
111      right: $r('app.float.dialog_content_margin'),
112      left: $r('app.float.dialog_content_margin'),
113      bottom: this.isHorizontal || this.isSidebar ? 0 : Constants.DIALOG_BOTTOM_OFFSET + this.leftBlank[3]
114    })
115    .padding({
116      left: $r('app.float.dialog_double_buttons_padding'),
117      right: $r('app.float.dialog_double_buttons_padding')
118    })
119    .alignItems(HorizontalAlign.Start)
120    .shadow({
121      radius: $r('app.float.dialog_defult_shadow_m_radio'),
122      color: $r('app.color.dialog_defult_shadow_m_color'),
123      offsetX: $r('app.float.dialog_defult_shadow_m_offsetX'),
124      offsetY: $r('app.float.dialog_defult_shadow_m_offsetY')
125    })
126  }
127}