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