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