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 16const hilog = requireNapi('hilog'); 17const Want = requireNapi('app.ability.Want'); 18const componentSnapshot = requireNapi('arkui.componentSnapshot'); 19const componentUtils = requireNapi('arkui.componentUtils'); 20const image = requireNapi('multimedia.image'); 21const util = requireNapi('util'); 22 23const tag = 'AddFormMenuItem::js::'; 24 25async function querySnapshotAsync(want, componentId) { 26 let compInfo = componentUtils.getRectangleById(componentId); 27 try { 28 const imagePackageApi = image.createImagePacker(); 29 const packOpts = { 30 format: 'image/webp', 31 quality: 50, 32 }; 33 let packPixmap = await componentSnapshot.get(componentId); 34 let arrayBuffer = await imagePackageApi.packing(packPixmap, packOpts); 35 let base64Helper = new util.Base64Helper(); 36 let uint8Arr = new Uint8Array(arrayBuffer); 37 let pixelStr = base64Helper.encodeToStringSync(uint8Arr); 38 !want.parameters && (want.parameters = {}); 39 want.parameters['ohos.extra.param.key.add_form_to_host_width'] = compInfo.size.width.toFixed(2); 40 want.parameters['ohos.extra.param.key.add_form_to_host_height'] = compInfo.size.height.toFixed(2); 41 want.parameters['ohos.extra.param.key.add_form_to_host_screenx'] = compInfo.screenOffset.x.toFixed(2); 42 want.parameters['ohos.extra.param.key.add_form_to_host_screeny'] = compInfo.screenOffset.y.toFixed(2); 43 want.parameters['ohos.extra.param.key.add_form_to_host_snapshot'] = pixelStr; 44 hilog.info(0x3900, tag, 'pixelStr length:' + pixelStr.length); 45 } catch (err) { 46 hilog.error(0x3900, tag, 'get pixelmap string error:' + err); 47 } 48} 49 50function querySnapshot(want, componentId) { 51 querySnapshotAsync(want, componentId); 52 return true; 53} 54 55/** 56 * Build function of AddFormMenuItem. 57 * 58 * @param { Want } want - The want of the form to publish. 59 * @param { string } componentId - The id of the component used to get form snapshot. 60 * @param { AddFormOptions } [options] - Add form options. 61 * @syscap SystemCapability.ArkUI.ArkUI.Full 62 * @since 12 63 */ 64export function AddFormMenuItem(want, componentId, options, parent = null) { 65 this.observeComponentCreation2((elmtId, isInitialRender) => { 66 If.create(); 67 if (querySnapshot(want, componentId)) { 68 this.ifElseBranchUpdateFunction(0, () => { 69 }); 70 } 71 else { 72 this.ifElseBranchUpdateFunction(1, () => { 73 }); 74 } 75 }, If); 76 If.pop(); 77 this.observeComponentCreation2((elmtId, isInitialRender) => { 78 FormMenuItem.create(options?.style?.options ? options.style.options : { 79 startIcon: { 80 'id': 125832775, 81 'type': 20000, 82 params: ['sys.media.ic_form_menu_add'], 83 'bundleName': '', 84 'moduleName': '' 85 }, 86 content: { 87 'id': 125832726, 88 'type': 10003, 89 params: ['sys.string.ohos_add_form_to_desktop'], 90 'bundleName': '', 91 'moduleName': '' 92 } 93 }); 94 FormMenuItem.onRegClick(want, componentId, options?.formBindingData?.data, options?.callback); 95 }, FormMenuItem); 96 FormMenuItem.pop(); 97} 98 99export default { AddFormMenuItem }; 100