1/* 2 * Copyright (c) 2024 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 16export enum DownloadIconStyle { 17 FULL_FILLED = 1, 18 LINES = 2 19} 20 21export enum DownloadDescription { 22 DOWNLOAD = 1, 23 DOWNLOAD_FILE = 2, 24 SAVE = 3, 25 SAVE_IMAGE = 4, 26 SAVE_FILE = 5, 27 DOWNLOAD_AND_SHARE = 6, 28 RECEIVE = 7, 29 CONTINUE_TO_RECEIVE = 8 30} 31 32export enum DownloadLayoutDirection { 33 HORIZONTAL = 0, 34 VERTICAL = 1 35} 36 37const downloadDescriptionResourceMap = new Map([ 38 [DownloadDescription.DOWNLOAD, $r('sys.string.ohos_id_text_save_button_description_download')], 39 [DownloadDescription.DOWNLOAD_FILE, $r('sys.string.ohos_id_text_save_button_description_download_file')], 40 [DownloadDescription.SAVE, $r('sys.string.ohos_id_text_save_button_description_save')], 41 [DownloadDescription.SAVE_IMAGE, $r('sys.string.ohos_id_text_save_button_description_save_image')], 42 [DownloadDescription.SAVE_FILE, $r('sys.string.ohos_id_text_save_button_description_save_file')], 43 [DownloadDescription.DOWNLOAD_AND_SHARE, $r('sys.string.ohos_id_text_save_button_description_download_and_share')], 44 [DownloadDescription.RECEIVE, $r('sys.string.ohos_id_text_save_button_description_receive')], 45 [DownloadDescription.CONTINUE_TO_RECEIVE, $r('sys.string.ohos_id_text_save_button_description_continue_to_receive')] 46]); 47 48export interface DownloadContentOptions { 49 icon?: DownloadIconStyle; 50 text?: DownloadDescription; 51} 52 53export interface DownloadStyleOptions { 54 iconSize?: Dimension; 55 layoutDirection?: DownloadLayoutDirection; 56 fontSize?: Dimension; 57 fontStyle?: FontStyle; 58 fontWeight?: number | FontWeight | string; 59 fontFamily?: string | Resource; 60 fontColor?: ResourceColor; 61 iconColor?: ResourceColor; 62 textIconSpace?: Dimension; 63} 64 65const MARGIN_DEFAULT = '0vp'; 66 67@Component 68export struct DownloadFileButton { 69 @State contentOptions: DownloadContentOptions = { 70 icon: DownloadIconStyle.FULL_FILLED, 71 text: DownloadDescription.DOWNLOAD 72 }; 73 @State styleOptions: DownloadStyleOptions = { 74 iconSize: '16vp', 75 layoutDirection: DownloadLayoutDirection.HORIZONTAL, 76 fontSize: '16fp', 77 fontStyle: FontStyle.Normal, 78 fontWeight: FontWeight.Medium, 79 fontFamily: 'HarmonyOS Sans', 80 fontColor: '#ffffffff', 81 iconColor: '#ffffffff', 82 textIconSpace: '4vp' 83 }; 84 85 private getTextContent(description: DownloadDescription): Resource | undefined { 86 return downloadDescriptionResourceMap.get(description); 87 } 88 89 @Builder 90 downloadImage() { 91 Image( 92 this.contentOptions.icon === DownloadIconStyle.LINES ? 93 $r('sys.media.ohos_save_button_line') : 94 $r('sys.media.ohos_save_button_filled') 95 ) 96 .size({ width: this.styleOptions.iconSize, height: this.styleOptions.iconSize }) 97 .fillColor(this.styleOptions.iconColor) 98 } 99 100 @Builder 101 downloadText() { 102 Text( 103 !this.contentOptions.text || !this.getTextContent(this.contentOptions.text) ? 104 $r('sys.string.ohos_id_text_save_button_description_download') : 105 this.getTextContent(this.contentOptions.text) 106 ) 107 .fontSize(this.styleOptions.fontSize) 108 .fontColor(this.styleOptions.fontColor) 109 .fontStyle(this.styleOptions.fontStyle) 110 .fontWeight(this.styleOptions.fontWeight) 111 .fontFamily(this.styleOptions.fontFamily) 112 .margin({ 113 top: this.styleOptions.layoutDirection === DownloadLayoutDirection.VERTICAL ? 114 this.styleOptions.textIconSpace : 115 MARGIN_DEFAULT, 116 left: this.styleOptions.layoutDirection === DownloadLayoutDirection.HORIZONTAL ? 117 this.styleOptions.textIconSpace : 118 MARGIN_DEFAULT 119 }) 120 } 121 122 build() { 123 if (this.styleOptions.layoutDirection === DownloadLayoutDirection.HORIZONTAL) { 124 Row() { 125 if (this.contentOptions.icon) { 126 this.downloadImage() 127 } 128 if (this.contentOptions.text) { 129 this.downloadText() 130 } 131 } 132 } else { 133 Column() { 134 if (this.contentOptions.icon) { 135 this.downloadImage() 136 } 137 if (this.contentOptions.text) { 138 this.downloadText() 139 } 140 } 141 } 142 } 143} 144