1 2/* 3 * Copyright (c) 2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16import { Log } from '@ohos/base/src/main/ets/utils/Log'; 17import type { DialogCallback } from '../../model/common/DialogUtil'; 18import screenManager from '@ohos/base/src/main/ets/manager/ScreenManager'; 19import { Constants } from '../../model/common/Constants'; 20import { showToast } from '@ohos/base/src/main/ets/utils/UiUtil'; 21import { getResourceString } from '@ohos/base/src/main/ets/utils/ResourceUtils'; 22 23@CustomDialog 24export struct RenameDialog { 25 private TAG: string = 'RenameDialog' 26 @StorageLink('isHorizontal') isHorizontal: boolean = screenManager.isHorizontal(); 27 @StorageLink('isSidebar') isSidebar: boolean = screenManager.isSidebar(); 28 @StorageLink('leftBlank') leftBlank: [number, number, number, number] = [0, 0, 0, 0]; 29 controller: CustomDialogController; 30 @Consume renameFileName: string; 31 @Consume dialogCallback: DialogCallback; 32 @State isNull: boolean = false; 33 @State dividerColor: Resource = $r('app.color.dialog_divider_h_color_182431'); 34 @State dividerWidth: string = '1vp'; 35 private inputName: string = ''; 36 37 aboutToAppear(): void { 38 Log.info(this.TAG, 'aboutToAppear'); 39 this.inputName = this.renameFileName; 40 this.isNull = this.inputName === ''; 41 if (this.inputName.length === Constants.RENAME_MAX_LENGTH) { 42 getResourceString($r('app.string.Maximum_length_reached')).then((message: string) => { 43 showToast(message) 44 }) 45 } 46 } 47 48 build() { 49 Column() { 50 Row() { 51 Text($r('app.string.rename')) 52 .fontSize($r('sys.float.ohos_id_text_size_headline7')) 53 .fontWeight(FontWeight.Medium) 54 .fontColor($r('sys.color.ohos_id_color_text_primary')) 55 }.alignItems(VerticalAlign.Center) 56 .height($r('app.float.dialog_title_height')) 57 .margin({ 58 left: $r('app.float.dialog_content_margin'), right: $r('app.float.dialog_content_margin') 59 }) 60 61 Row() { 62 Column() { 63 TextInput({ placeholder: '', text: this.inputName }) 64 .fontSize($r('sys.float.ohos_id_text_size_body1')) 65 .fontFamily($r('app.string.id_text_font_family_regular')) 66 .fontColor($r('app.color.text_input_font_color_182431')) 67 .maxLength(Constants.RENAME_MAX_LENGTH) 68 .enterKeyType(EnterKeyType.Done) 69 .backgroundColor($r('app.color.transparent')) 70 .onChange((value: string) => { 71 Log.info(this.TAG, `TextInput onChange : ${value}`) 72 this.inputName = value 73 if (value.length === Constants.RENAME_MAX_LENGTH) { 74 getResourceString($r('app.string.Maximum_length_reached')).then((message: string) => { 75 showToast(message) 76 }) 77 } 78 this.isNull = this.inputName === ''; 79 }) 80 .margin({ 81 left: $r('app.float.input_text_margin'), right: $r('app.float.input_text_margin') 82 }) 83 84 Divider().vertical(false).strokeWidth(this.dividerWidth) 85 .color(this.dividerColor) 86 .margin({ 87 left: $r('app.float.dialog_content_margin'), right: $r('app.float.dialog_content_margin') 88 }) 89 } 90 }.margin({ 91 bottom: $r('sys.float.ohos_id_text_paragraph_margin_s') }) 92 93 Stack({ alignContent: Alignment.Top }) { 94 Row() { 95 Button() { 96 Text($r('app.string.no')) 97 .fontSize($r('sys.float.ohos_id_text_size_button1')) 98 .fontColor($r('app.color.color_control_highlight')) 99 .fontWeight(FontWeight.Medium) 100 .width('50%') 101 .textAlign(TextAlign.Center) 102 } 103 .margin({ right: $r('app.float.details_dialog_button_margin_right') }) 104 .backgroundColor($r('app.color.transparent')) 105 .height($r('app.float.details_dialog_button_height')) 106 .onClick(() => { 107 this.dialogCallback && this.dialogCallback.cancelCallback(); 108 this.controller.close() 109 }) 110 111 Row() { 112 Divider() 113 .vertical(true) 114 .height($r('app.float.dialog_divider_height')) 115 .color($r('app.color.divider_vertical_color')) 116 }.height($r('app.float.details_dialog_button_height')) 117 .alignItems(VerticalAlign.Center) 118 119 Button() { 120 Text($r('app.string.yes')) 121 .fontSize($r('sys.float.ohos_id_text_size_button1')) 122 .fontColor($r('app.color.color_control_highlight')) 123 .fontWeight(FontWeight.Medium) 124 .width('50%') 125 .textAlign(TextAlign.Center) 126 } 127 .enabled(!this.isNull) 128 .opacity(this.isNull ? $r('app.float.disable_button_opacity') : 1) 129 .margin({ right: $r('app.float.details_dialog_button_margin_left') }) 130 .backgroundColor($r('app.color.transparent')) 131 .height($r('app.float.details_dialog_button_height')) 132 .onClick(() => { 133 let check = /[\\.\\\\/:*?<>\"|\[\]{}\s]/; 134 let passCheck = check.test(this.inputName); 135 if (passCheck) { 136 getResourceString($r('app.string.specific_characters_not_supported')).then((message: string) => { 137 showToast(message) 138 }) 139 return; 140 } 141 if (this.inputName == this.renameFileName) { 142 this.controller.close(); 143 return; 144 } 145 this.dialogCallback && this.dialogCallback.confirmCallback(this.inputName); 146 this.controller.close(); 147 }) 148 } 149 } 150 .height($r('app.float.details_dialog_button_area_height')) 151 .margin({ 152 left: $r('app.float.dialog_content_margin'), right: $r('app.float.dialog_content_margin') 153 }) 154 } 155 .alignItems(HorizontalAlign.Start) 156 .borderRadius($r('app.float.dialog_border_radius')) 157 .width(screenManager.getColumnsWidth(4)) 158 .backgroundColor($r('app.color.white')) 159 .margin({ 160 right: $r('app.float.dialog_window_margin'), 161 left: $r('app.float.dialog_window_margin'), 162 bottom: this.isHorizontal || this.isSidebar ? 0 : Constants.DIALOG_BOTTOM_OFFSET + px2vp(this.leftBlank[3]) 163 }) 164 } 165} 166