1/* 2 * Copyright (c) 2021-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 context from '@ohos.app.ability.common'; 17import { renderSize } from '../../../base/utils/Tools'; 18import AbilityCommonUtil, { ResultCodePicker } from '../../../base//utils/AbilityCommonUtil'; 19import { StartModeOptions } from '../../../base/model/StartModeOptions'; 20import { FilePickerUtil } from '../../../base/utils/FilePickerUtil'; 21import { FilesData } from '../../../databases/model/FileData'; 22 23@Styles 24function pressedStyles() { 25 .borderRadius($r('app.float.common_borderRadius8')) 26 .backgroundColor($r('app.color.hicloud_hmos_bg')) 27} 28 29@Styles 30function normalStyles() { 31 .borderRadius($r('app.float.common_borderRadius8')) 32 .backgroundColor($r('app.color.transparent_color')) 33} 34 35@Extend(Text) 36function subtitleStyles(fontSize: Resource) { 37 .fontSize(fontSize) 38 .alignSelf(ItemAlign.Start) 39} 40 41@Component 42export struct TopBar { 43 private startModeOptions: StartModeOptions = FilePickerUtil.getStartOptionsFromStorage(); 44 private title: string = ''; 45 private subtitle?: string = ''; 46 private fileSize?: number = 0; 47 @Prop selectAll: boolean; 48 @Prop isMulti: boolean; 49 @Link checkedNum: number; 50 @Link checkedList: ESObject[]; 51 @State active: boolean = false; 52 @State rActive: boolean = false; 53 public backCallback: () => void = () => {}; // 点击返回键的事件回调 54 public menuCallback: () => void = () => {}; // 点击全选的事件回调 55 filterCallBack: Function = () => {}; 56 57 aboutToAppear() { 58 59 } 60 61 initTitle(): string | Resource { 62 if (this.isMulti) { 63 return this.checkedNum === 0 ? $r('app.string.selected_none') : this.checkedNum === 1 ? 64 $r('app.string.selected_items_singular', this.checkedNum) : 65 $r('app.string.selected_items_plural', this.checkedNum); 66 } else { 67 return this.title; 68 } 69 } 70 71 filePickerTitle() { 72 if (this.isMulti) { 73 return this.checkedNum === 0 ? $r('app.string.selected_none') : 74 $r('app.string.selected', this.checkedNum, globalThis.filePickNum); 75 } else { 76 return this.title; 77 } 78 } 79 80 checkSelectedFileList(): boolean { 81 if (!this.checkedNum) { 82 return false; 83 } 84 if (this.checkedList.some((item: FilesData) => item.isFolder)) { 85 return false; 86 } 87 return true; 88 } 89 90 terminate(): void { 91 if (!this.checkSelectedFileList()) { 92 return; 93 } 94 const uriList: string[] = this.checkedList.map((item: FilesData): string => item.uri); 95 const fileNameList: string[] = this.checkedList.map((item: FilesData): string => item.fileName); 96 AbilityCommonUtil.terminateFilePicker(uriList, ResultCodePicker.SUCCESS, this.startModeOptions); 97 } 98 99 build() { 100 Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start }) { 101 Column() { 102 Image(this.isMulti ? $r('app.media.hidisk_cancel_normal') : $r('app.media.hidisk_ic_return')) 103 .objectFit(ImageFit.Contain) 104 .width($r('app.float.common_size24')) 105 .height($r('app.float.common_size24')) 106 .interpolation(ImageInterpolation.Medium) 107 }.padding({ 108 left: $r('app.float.common_padding12'), 109 right: $r('app.float.common_padding12'), 110 top: $r('app.float.common_padding10'), 111 bottom: $r('app.float.common_padding10') 112 }) 113 .stateStyles({ 114 pressed: pressedStyles, 115 normal: normalStyles 116 }) 117 .onClick(() => { 118 this.backCallback(); 119 }) 120 121 Column() { 122 Text(this.filePickerTitle()) 123 .fontColor($r('app.color.black')) 124 .fontSize($r('app.float.common_font_size20')) 125 .fontWeight(FontWeight.Medium) 126 .maxLines(1) 127 .textOverflow({ overflow: TextOverflow.Ellipsis }) 128 if (this.fileSize && this.isMulti) { 129 Text($r('app.string.choose_size', renderSize(this.fileSize.toString()))) 130 .fontColor($r('app.color.black')) 131 .subtitleStyles($r('app.float.common_font_size14')) 132 } 133 if (this.subtitle !== '' && !this.isMulti) { 134 Text(this.subtitle) 135 .fontColor($r('app.color.black')) 136 .subtitleStyles($r('app.float.common_font_size14')) 137 } 138 } 139 .padding({ left: $r('app.float.common_margin6') }) 140 .layoutWeight(1) 141 .alignItems(HorizontalAlign.Start) 142 143 if (!this.isMulti) { 144 Column() { 145 Image($r('app.media.hidisk_cancel_normal')) 146 .objectFit(ImageFit.Contain) 147 .width($r('app.float.common_size24')) 148 .height($r('app.float.common_size24')) 149 .interpolation(ImageInterpolation.Medium) 150 }.padding({ 151 left: $r('app.float.common_padding12'), 152 right: $r('app.float.common_padding12'), 153 top: $r('app.float.common_padding10'), 154 bottom: $r('app.float.common_padding10') 155 }) 156 .stateStyles({ 157 pressed: pressedStyles, 158 normal: normalStyles 159 }) 160 .onClick(() => { 161 AbilityCommonUtil.terminateFilePicker([], ResultCodePicker.CANCEL, this.startModeOptions); 162 }) 163 } else { 164 Column() { 165 Image($r('app.media.ic_ok')) 166 .objectFit(ImageFit.Contain) 167 .width($r('app.float.common_size24')) 168 .height($r('app.float.common_size24')) 169 }.padding({ 170 left: $r('app.float.common_padding12'), 171 right: $r('app.float.common_padding12'), 172 top: $r('app.float.common_padding10'), 173 bottom: $r('app.float.common_padding10') 174 }) 175 .stateStyles({ 176 pressed: pressedStyles, 177 normal: normalStyles 178 }) 179 .onClick(() => { 180 this.terminate(); 181 }) 182 .opacity(this.checkSelectedFileList() ? $r('app.float.common_opacity10') : $r('app.float.common_opacity4')) 183 } 184 } 185 .height($r('app.float.common_mark_y50')) 186 .padding({ 187 left: $r('app.float.common_padding12'), 188 right: $r('app.float.common_padding4') 189 }) 190 .backgroundColor($r('app.color.white')) 191 } 192}