From 0123c30e4d3ce1c773422d51c2b5c8c36d46c825 Mon Sep 17 00:00:00 2001 From: tianlanqing <1692438977@qq.com> Date: Thu, 16 May 2024 17:56:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=8B=E8=BD=BD=E6=8E=A7?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: t30052775 --- bundle.json | 3 +- interfaces/kits/downloadfilebutton/BUILD.gn | 54 ++++ .../downloadfilebutton/downloadfilebutton.js | 238 ++++++++++++++++++ .../native_module_ohos_downloadfilebutton.cpp | 49 ++++ .../src/DownloadFileButton.ets | 125 +++++++++ 5 files changed, 468 insertions(+), 1 deletion(-) create mode 100644 interfaces/kits/downloadfilebutton/BUILD.gn create mode 100644 interfaces/kits/downloadfilebutton/downloadfilebutton.js create mode 100644 interfaces/kits/downloadfilebutton/native_module_ohos_downloadfilebutton.cpp create mode 100644 interfaces/kits/downloadfilebutton/src/DownloadFileButton.ets diff --git a/bundle.json b/bundle.json index 450283ee..d494103e 100644 --- a/bundle.json +++ b/bundle.json @@ -48,7 +48,8 @@ "fwk_group": [ "//foundation/filemanagement/user_file_service/interfaces/kits/picker:picker", "//foundation/filemanagement/user_file_service/interfaces/kits/native/recent:recent", - "//foundation/filemanagement/user_file_service/interfaces/kits/native/trash:trash" + "//foundation/filemanagement/user_file_service/interfaces/kits/native/trash:trash", + "//foundation/filemanagement/user_file_service/interfaces/kits/downloadfilebutton:downloadfilebutton" ], "service_group": [ "//foundation/filemanagement/user_file_service/services:user_file_managers", diff --git a/interfaces/kits/downloadfilebutton/BUILD.gn b/interfaces/kits/downloadfilebutton/BUILD.gn new file mode 100644 index 00000000..d78745fd --- /dev/null +++ b/interfaces/kits/downloadfilebutton/BUILD.gn @@ -0,0 +1,54 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") +import("//build/ohos.gni") +import("//foundation/arkui/ace_engine/ace_config.gni") +import("//foundation/arkui/ace_engine/adapter/preview/build/config.gni") +import("//foundation/arkui/ace_engine/build/ace_gen_obj.gni") + +es2abc_gen_abc("gen_downloadfilebutton_abc") { + src_js = rebase_path("downloadfilebutton.js") + dst_file = rebase_path(target_out_dir + "/downloadfilebutton.abc") + in_puts = [ "downloadfilebutton.js" ] + out_puts = [ target_out_dir + "/downloadfilebutton.abc" ] + extra_args = [ "--module" ] +} + +gen_js_obj("downloadfilebutton_abc") { + input = get_label_info(":gen_downloadfilebutton_abc", "target_out_dir") + "/downloadfilebutton.abc" + output = target_out_dir + "/downloadfilebutton_abc.o" + dep = ":gen_downloadfilebutton_abc" +} + +gen_obj("downloadfilebutton_abc_preview") { + input = get_label_info(":gen_downloadfilebutton_abc", "target_out_dir") + "/downloadfilebutton.abc" + output = target_out_dir + "/downloadfilebutton_abc.c" + snapshot_dep = [ ":gen_downloadfilebutton_abc" ] +} + +ohos_shared_library("downloadfilebutton") { + sources = [ "downloadfilebutton.cpp" ] + if (use_mingw_win || use_mac || use_linux) { + deps = [ ":gen_obj_src_downloadfilebutton_abc_preview" ] + } else { + deps = [ ":downloadfilebutton_abc" ] + } + external_deps = [ + "hilog:libhilog", + "napi:ace_napi", + ] + relative_install_dir = "module/file" + subsystem_name = "filemanagement" + part_name = "user_file_service" +} diff --git a/interfaces/kits/downloadfilebutton/downloadfilebutton.js b/interfaces/kits/downloadfilebutton/downloadfilebutton.js new file mode 100644 index 00000000..0412fe98 --- /dev/null +++ b/interfaces/kits/downloadfilebutton/downloadfilebutton.js @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export var DownloadIconStyle; +(function (o2) { + o2[o2["FULL_FILLED"] = 0] = "FULL_FILLED"; + o2[o2["LINES"] = 1] = "LINES"; +})(DownloadIconStyle || (DownloadIconStyle = {})); +export var DownloadDescription; +(function (n2) { + n2[n2["DOWNLOAD"] = 0] = "DOWNLOAD"; + n2[n2["DOWNLOAD_FILE"] = 1] = "DOWNLOAD_FILE"; + n2[n2["SAVE"] = 2] = "SAVE"; + n2[n2["SAVE_IMAGE"] = 3] = "SAVE_IMAGE"; + n2[n2["SAVE_FILE"] = 4] = "SAVE_FILE"; + n2[n2["DOWNLOAD_AND_SHARE"] = 5] = "DOWNLOAD_AND_SHARE"; + n2[n2["RECEIVE"] = 6] = "RECEIVE"; + n2[n2["CONTINUE_TO_RECEIVE"] = 7] = "CONTINUE_TO_RECEIVE"; +})(DownloadDescription || (DownloadDescription = {})); +export var DownloadLayoutDirection; +(function (m2) { + m2[m2["HORIZONTAL"] = 0] = "HORIZONTAL"; + m2[m2["VERTICAL"] = 1] = "VERTICAL"; +})(DownloadLayoutDirection || (DownloadLayoutDirection = {})); +const downloadDescriptionResourceMap = new Map([ + [DownloadDescription.DOWNLOAD, { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_download"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }], + [DownloadDescription.DOWNLOAD_FILE, { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_download_file"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }], + [DownloadDescription.SAVE, { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_save"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }], + [DownloadDescription.SAVE_IMAGE, { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_save_image"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }], + [DownloadDescription.SAVE_FILE, { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_save_file"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }], + [DownloadDescription.DOWNLOAD_AND_SHARE, { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_download_and_share"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }], + [DownloadDescription.RECEIVE, { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_receive"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }], + [DownloadDescription.CONTINUE_TO_RECEIVE, { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_continue_to_receive"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }] +]); +export class DownloadFileButton extends ViewPU { + constructor(g2, h2, i2, j2 = -1, k2 = undefined, l2) { + super(g2, i2, j2, l2); + if (typeof k2 === "function") { + this.paramsGenerator_ = k2; + } + this.__contentOption = new ObservedPropertyObjectPU({}, this, "contentOption"); + this.__styleOption = new ObservedPropertyObjectPU({ + iconSize: '16vp', + layoutDirection: DownloadLayoutDirection.HORIZONTAL, + fontSize: '16fp', + fontStyle: FontStyle.Normal, + fontWeight: FontWeight.Medium, + fontFamily: 'HarmonyOS Sans', + fontColor: '#ffffffff', + iconColor: '#ffffffff', + textIconSpace: '4vp' + }, this, "styleOption"); + this.setInitiallyProvidedValue(h2); + } + setInitiallyProvidedValue(f2) { + if (f2.contentOption !== undefined) { + this.contentOption = f2.contentOption; + } + if (f2.styleOption !== undefined) { + this.styleOption = f2.styleOption; + } + } + updateStateVars(e2) { + } + purgeVariableDependenciesOnElmtId(d2) { + this.__contentOption.purgeDependencyOnElmtId(d2); + this.__styleOption.purgeDependencyOnElmtId(d2); + } + aboutToBeDeleted() { + this.__contentOption.aboutToBeDeleted(); + this.__styleOption.aboutToBeDeleted(); + SubscriberManager.Get().delete(this.id__()); + this.aboutToBeDeletedInternal(); + } + get contentOption() { + return this.__contentOption.get(); + } + set contentOption(c2) { + this.__contentOption.set(c2); + } + get styleOption() { + return this.__styleOption.get(); + } + set styleOption(b2) { + this.__styleOption.set(b2); + } + initialRender() { + this.observeComponentCreation2((z1, a2) => { + Row.create(); + }, Row); + this.observeComponentCreation2((k, l) => { + If.create(); + if (this.contentOption.text === undefined && this.contentOption.icon === undefined) { + this.ifElseBranchUpdateFunction(0, () => { + this.observeComponentCreation2((s1, t1) => { + If.create(); + if (this.styleOption.layoutDirection === DownloadLayoutDirection.HORIZONTAL) { + this.ifElseBranchUpdateFunction(0, () => { + this.downloadImage.bind(this)(); + this.downloadText.bind(this)(); + }); + } + else { + this.ifElseBranchUpdateFunction(1, () => { + this.observeComponentCreation2((w1, x1) => { + Column.create(); + }, Column); + this.downloadImage.bind(this)(); + this.downloadText.bind(this)(); + Column.pop(); + }); + } + }, If); + If.pop(); + }); + } + else { + this.ifElseBranchUpdateFunction(1, () => { + this.observeComponentCreation2((o, p) => { + If.create(); + if (this.styleOption.layoutDirection === DownloadLayoutDirection.HORIZONTAL) { + this.ifElseBranchUpdateFunction(0, () => { + this.observeComponentCreation2((m1, n1) => { + If.create(); + if (this.contentOption.icon !== undefined) { + this.ifElseBranchUpdateFunction(0, () => { + this.downloadImage.bind(this)(); + }); + } + else { + this.ifElseBranchUpdateFunction(1, () => { + }); + } + }, If); + If.pop(); + this.observeComponentCreation2((i1, j1) => { + If.create(); + if (this.contentOption.text !== undefined) { + this.ifElseBranchUpdateFunction(0, () => { + this.downloadText.bind(this)(); + }); + } + else { + this.ifElseBranchUpdateFunction(1, () => { + }); + } + }, If); + If.pop(); + }); + } + else if (this.styleOption.layoutDirection === DownloadLayoutDirection.VERTICAL) { + this.ifElseBranchUpdateFunction(1, () => { + this.observeComponentCreation2((d1, e1) => { + Column.create(); + }, Column); + this.observeComponentCreation2((z, a1) => { + If.create(); + if (this.contentOption.icon !== undefined) { + this.ifElseBranchUpdateFunction(0, () => { + this.downloadImage.bind(this)(); + }); + } + else { + this.ifElseBranchUpdateFunction(1, () => { + }); + } + }, If); + If.pop(); + this.observeComponentCreation2((v, w) => { + If.create(); + if (this.contentOption.text !== undefined) { + this.ifElseBranchUpdateFunction(0, () => { + this.downloadText.bind(this)(); + }); + } + else { + this.ifElseBranchUpdateFunction(1, () => { + }); + } + }, If); + If.pop(); + Column.pop(); + }); + } + else { + this.ifElseBranchUpdateFunction(2, () => { + }); + } + }, If); + If.pop(); + }); + } + }, If); + If.pop(); + Row.pop(); + } + downloadImage(e = null) { + this.observeComponentCreation2((g, h) => { + Image.create(this.contentOption.icon === DownloadIconStyle.LINES + ? { "id": -1, "type": 20000, params: ["sys.media.ohos_ic_public_save_button_line"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" } : { "id": -1, "type": 20000, params: ["sys.media.ohos_ic_public_save_button_filled"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" }); + Image.size({ width: this.styleOption.iconSize, height: this.styleOption.iconSize }); + Image.fillColor(this.styleOption.iconColor); + }, Image); + } + downloadText(a = null) { + this.observeComponentCreation2((c, d) => { + Text.create(!this.contentOption.text || !downloadDescriptionResourceMap.get(this.contentOption.text) ? { "id": -1, "type": 10003, params: ["sys.string.ohos_id_text_save_button_description_download"], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" } : + downloadDescriptionResourceMap.get(this.contentOption.text)); + Text.fontSize(this.styleOption.fontSize); + Text.fontColor(this.styleOption.fontColor); + Text.fontStyle(this.styleOption.fontStyle); + Text.fontWeight(this.styleOption.fontWeight); + Text.fontFamily(this.styleOption.fontFamily); + Text.margin({ + top: this.styleOption.layoutDirection === DownloadLayoutDirection.VERTICAL ? this.styleOption.textIconSpace : + '0vp', + left: this.styleOption.layoutDirection === DownloadLayoutDirection.HORIZONTAL ? + this.styleOption.textIconSpace : + '0vp' + }); + }, Text); + Text.pop(); + } + rerender() { + this.updateDirtyElements(); + } +} \ No newline at end of file diff --git a/interfaces/kits/downloadfilebutton/native_module_ohos_downloadfilebutton.cpp b/interfaces/kits/downloadfilebutton/native_module_ohos_downloadfilebutton.cpp new file mode 100644 index 00000000..a5703b9a --- /dev/null +++ b/interfaces/kits/downloadfilebutton/native_module_ohos_downloadfilebutton.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "native_engine/native_engine.h" +extern const char _binary_downloadfilebutton_abc_start[]; +extern const char _binary_downloadfilebutton_abc_end[]; + +extern "C" __attribute__((visibility("default"))) void NAPI_file_DownloadFileButton_GetABCCode( + const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_downloadfilebutton_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_downloadfilebutton_abc_end - _binary_downloadfilebutton_abc_start; + } +} +/* + * Module define + */ +static napi_module DownloadFileButtonModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_modname = "file.DownloadFileButton", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; +/* + * Module registerfunction + */ +extern "C" __attribute__((constructor)) void DownloadFileButtonRegisterModule(void) +{ + napi_module_register(&DownloadFileButtonModule); +} \ No newline at end of file diff --git a/interfaces/kits/downloadfilebutton/src/DownloadFileButton.ets b/interfaces/kits/downloadfilebutton/src/DownloadFileButton.ets new file mode 100644 index 00000000..b391c7bf --- /dev/null +++ b/interfaces/kits/downloadfilebutton/src/DownloadFileButton.ets @@ -0,0 +1,125 @@ +export enum DownloadIconStyle { + FULL_FILLED = 0, + LINES = 1 +} + +export enum DownloadDescription { + DOWNLOAD = 0, + DOWNLOAD_FILE = 1, + SAVE = 2, + SAVE_IMAGE = 3, + SAVE_FILE = 4, + DOWNLOAD_AND_SHARE = 5, + RECEIVE = 6, + CONTINUE_TO_RECEIVE = 7 +} + +export enum DownloadLayoutDirection { + HORIZONTAL, + VERTICAL +} + +const downloadDescriptionResourceMap = new Map([ + [DownloadDescription.DOWNLOAD, $r("sys.string.ohos_id_text_save_button_description_download")], + [DownloadDescription.DOWNLOAD_FILE, $r("sys.string.ohos_id_text_save_button_description_download_file")], + [DownloadDescription.SAVE, $r("sys.string.ohos_id_text_save_button_description_save")], + [DownloadDescription.SAVE_IMAGE, $r("sys.string.ohos_id_text_save_button_description_save_image")], + [DownloadDescription.SAVE_FILE, $r("sys.string.ohos_id_text_save_button_description_save_file")], + [DownloadDescription.DOWNLOAD_AND_SHARE, $r("sys.string.ohos_id_text_save_button_description_download_and_share")], + [DownloadDescription.RECEIVE, $r("sys.string.ohos_id_text_save_button_description_receive")], + [DownloadDescription.CONTINUE_TO_RECEIVE, $r("sys.string.ohos_id_text_save_button_description_continue_to_receive")] +]); + +export interface DownloadContentOption { + icon?: DownloadIconStyle; + text?: DownloadDescription; +} + +export interface DownloadStyleOption { + iconSize?: Dimension; + layoutDirection?: DownloadLayoutDirection; + fontSize?: Dimension; + fontStyle?: FontStyle; + fontWeight?: number | FontWeight | string; + fontFamily?: string | Resource; + fontColor?: ResourceColor; + iconColor?: ResourceColor; + textIconSpace?: Dimension; +} + +@Component +export struct DownloadFileButton { + @State contentOption: DownloadContentOption = {}; + @State styleOption: DownloadStyleOption = { + iconSize: '16vp', + layoutDirection: DownloadLayoutDirection.HORIZONTAL, + fontSize: '16fp', + fontStyle: FontStyle.Normal, + fontWeight: FontWeight.Medium, + fontFamily: 'HarmonyOS Sans', + fontColor: '#ffffffff', + iconColor: '#ffffffff', + textIconSpace: '4vp' + }; + + build() { + Row() { + if (this.contentOption.text === undefined && this.contentOption.icon === undefined) { + if (this.styleOption.layoutDirection === DownloadLayoutDirection.HORIZONTAL) { + this.downloadImage(); + this.downloadText(); + } else { + Column() { + this.downloadImage(); + this.downloadText(); + } + } + } else { + if (this.styleOption.layoutDirection === DownloadLayoutDirection.HORIZONTAL) { + if (this.contentOption.icon !== undefined) { + this.downloadImage(); + } + if (this.contentOption.text !== undefined) { + this.downloadText(); + } + } else if (this.styleOption.layoutDirection === DownloadLayoutDirection.VERTICAL) { + Column() { + if (this.contentOption.icon !== undefined) { + this.downloadImage(); + } + if (this.contentOption.text !== undefined) { + this.downloadText(); + } + } + } + } + } + } + + @Builder + downloadImage() { + Image(this.contentOption.icon === DownloadIconStyle.LINES + ? $r("sys.media.ohos_ic_public_save_button_line") : $r("sys.media.ohos_ic_public_save_button_filled")) + .size({ width: this.styleOption.iconSize, height: this.styleOption.iconSize }) + .fillColor(this.styleOption.iconColor) + } + + @Builder + downloadText() { + Text(!this.contentOption.text || !downloadDescriptionResourceMap.get(this.contentOption.text) ? + $r("sys.string.ohos_id_text_save_button_description_download") : + downloadDescriptionResourceMap.get(this.contentOption.text)) + .fontSize(this.styleOption.fontSize) + .fontColor(this.styleOption.fontColor) + .fontStyle(this.styleOption.fontStyle) + .fontWeight(this.styleOption.fontWeight) + .fontFamily(this.styleOption.fontFamily) + .margin({ + top: this.styleOption.layoutDirection === DownloadLayoutDirection.VERTICAL ? this.styleOption.textIconSpace : + '0vp', + left: this.styleOption.layoutDirection === DownloadLayoutDirection.HORIZONTAL ? + this.styleOption.textIconSpace : + '0vp' + }) + } +} -- Gitee