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 16import { AddFormOptions } from '@kit.ArkUI' 17import { componentSnapshot, componentUtils } from '@kit.ArkUI'; 18import { hilog } from '@kit.PerformanceAnalysisKit'; 19import { image } from '@kit.ImageKit'; 20import { util } from '@kit.ArkTS'; 21import { Want } from '@kit.AbilityKit'; 22 23const tag = 'AddFormMenuItem::ets::'; 24 25const querySnapshotAsync = async (want: Want, componentId: string): Promise<void> => { 26 let compInfo = componentUtils.getRectangleById(componentId); 27 let imagePackageApi = null; 28 try { 29 imagePackageApi: image.ImagePacker = image.createImagePacker(); 30 const packOpts: image.PackingOption = { 31 format: 'image/webp', 32 quality: 50, 33 }; 34 let packPixmap: image.PixelMap = await componentSnapshot.get(componentId); 35 let arrayBuffer: ArrayBuffer = await imagePackageApi.packing(packPixmap, packOpts); 36 let base64Helper = new util.Base64Helper(); 37 let uint8Arr = new Uint8Array(arrayBuffer); 38 let pixelStr = base64Helper.encodeToStringSync(uint8Arr); 39 !want.parameters && (want.parameters = {}); 40 want.parameters['ohos.extra.param.key.add_form_to_host_width'] = compInfo.size.width.toFixed(2); 41 want.parameters['ohos.extra.param.key.add_form_to_host_height'] = compInfo.size.height.toFixed(2); 42 want.parameters['ohos.extra.param.key.add_form_to_host_screenx'] = compInfo.screenOffset.x.toFixed(2); 43 want.parameters['ohos.extra.param.key.add_form_to_host_screeny'] = compInfo.screenOffset.y.toFixed(2); 44 want.parameters['ohos.extra.param.key.add_form_to_host_snapshot'] = pixelStr; 45 hilog.info(0x3900, tag, 'pixelStr length:' + pixelStr.length); 46 } catch (err) { 47 hilog.error(0x3900, tag, 'get pixelmap string error:' + err); 48 } finally { 49 imagePackageApi?.release(); 50 } 51} 52 53const querySnapshot = (want: Want, componentId: string): boolean => { 54 querySnapshotAsync(want, componentId); 55 return true; 56} 57 58@Builder 59AddFormMenuItem(want: Want, componentId: string, options?: AddFormOptions): void { 60 if (querySnapshot(want, componentId)) { 61 } 62 FormMenuItem(options?.style?.options ? options.style.options : { 63 startIcon: $r('sys.media.ic_form_menu_add'), 64 content: $r('sys.string.ohos_add_form_to_desktop') 65 }) 66 .onRegClick(want, componentId, options?.formBindingData?.data, options?.callback) 67}