• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, uiContext) {
26  let compInfo = uiContext.getComponentUtils().getRectangleById(componentId);
27  let imagePackageApi = null;
28  try {
29    imagePackageApi = image.createImagePacker();
30    const packOpts = {
31      format: 'image/webp',
32      quality: 50,
33    };
34    hilog.info(0x3900, tag, 'componentId:' + componentId);
35    let packPixmap = await uiContext.getComponentSnapshot().get(componentId);
36    let arrayBuffer = await imagePackageApi.packing(packPixmap, packOpts);
37    let base64Helper = new util.Base64Helper();
38    let uint8Arr = new Uint8Array(arrayBuffer);
39    let pixelStr = base64Helper.encodeToStringSync(uint8Arr);
40
41    !want.parameters && (want.parameters = {});
42    want.parameters['ohos.extra.param.key.add_form_to_host_width'] = compInfo.size.width.toFixed(2);
43    want.parameters['ohos.extra.param.key.add_form_to_host_height'] = compInfo.size.height.toFixed(2);
44    want.parameters['ohos.extra.param.key.add_form_to_host_screenx'] = compInfo.screenOffset.x.toFixed(2);
45    want.parameters['ohos.extra.param.key.add_form_to_host_screeny'] = compInfo.screenOffset.y.toFixed(2);
46    want.parameters['ohos.extra.param.key.add_form_to_host_snapshot'] = pixelStr;
47  } catch (err) {
48    hilog.error(0x3900, tag, 'get pixelmap string error:' + err);
49  } finally {
50    imagePackageApi?.release();
51  }
52}
53
54/**
55 * Build function of AddFormMenuItem.
56 *
57 * @param { Want } want - The want of the form to publish.
58 * @param { string } componentId - The id of the component used to get form snapshot.
59 * @param { AddFormOptions } [options] - Add form options.
60 * @syscap SystemCapability.ArkUI.ArkUI.Full
61 * @since 12
62 */
63export function AddFormMenuItem(want, componentId, options, parent = null) {
64  hilog.info(0x0000, tag, 'Add form menu item.');
65  this.observeComponentCreation2((elmtId, isInitialRender) => {
66    FormMenuItem.create(options?.style?.options ? options.style.options : {
67      startIcon: {
68        'id': 125832775,
69        'type': 20000,
70        params: ['sys.media.ic_form_menu_add'],
71        'bundleName': '',
72        'moduleName': ''
73      },
74      content: {
75        'id': 125832726,
76        'type': 10003,
77        params: ['sys.string.ohos_add_form_to_desktop'],
78        'bundleName': '',
79        'moduleName': ''
80      }
81    });
82
83    let uiContext = this.getUIContext();
84    FormMenuItem.onClick(async () => {
85      await querySnapshotAsync(want, componentId, uiContext);
86      uiContext.runScopedTask(() => {
87        FormMenuItem.onRequestPublishFormWithSnapshot(want, options?.formBindingData?.data, options?.callback);
88      });
89    });
90  }, FormMenuItem);
91  FormMenuItem.pop();
92}
93
94export default { AddFormMenuItem };
95