• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# FormMenu
2
3The **FormMenu** component encapsulates an **Add to home screen** menu, which allows users to long-press a component in the application to add it to their home screen for direct access. During application usage, this component acts as a portal for retention and re-engagement, encouraging users to conveniently add features to their home screen.
4
5This component facilitates the quick addition of service widgets to the home screen through a long-press menu within the application:
6
71. You pass the widget data and the ID of the in-application functional component to the service widget framework.
8
92. The click event captures a snapshot and the position of the in-application functional component based on the component ID, which is used for the transition animation when the widget is added to the home screen.
10
113. The service widget framework triggers the addition of the service widget to the home screen by sending the addition data to the home screen, initiating the service widget placement operation.
12
13
14> **NOTE**
15>
16> This component is supported since API version 12. Updates will be marked with a superscript to indicate their earliest API version.
17
18
19## Modules to Import
20
21```
22import { AddFormMenuItem } from '@kit.ArkUI';
23```
24
25
26## Child Components
27
28Not supported
29
30## Attributes
31The [universal attributes](ts-component-general-attributes.md) are not supported.
32
33## AddFormMenuItem
34
35
36AddFormMenuItem(
37  want: Want,
38  componentId: string,
39  options?: AddFormOptions
40): void
41
42
43**Decorator**: @Builder
44
45**Atomic service API**: This API can be used in atomic services since API version 12.
46
47**System capability**: SystemCapability.ArkUI.ArkUI.Full
48
49**Parameters**
50
51| Name          | Type                       | Mandatory| Description                                                            |
52| -------------- | ------------------------------- | ---- | ---------------------------------------------------------------- |
53| want           | [Want](../../apis-ability-kit/js-apis-app-ability-want.md#want)                            | Yes  | Want information of the functional component.                                        |
54| componentId    | string                          | Yes  | In-application functional component ID, which corresponds to a UI similar to the UI of the service widget to add.|
55| options| [AddFormOptions](#addformoptions) | No  | Options for adding the service widget.                                                        |
56
57## AddFormOptions
58
59**Atomic service API**: This API can be used in atomic services since API version 12.
60
61**System capability**: SystemCapability.ArkUI.ArkUI.Full
62
63**Parameters**
64| Name            | Type               | Mandatory| Description                                                     |
65| --------------- | ---- | ---- | ---------------------------------------------------------------- |
66| formBindingData | [formBindingData.FormBindingData](../../apis-form-kit/js-apis-app-form-formBindingData.md#formbindingdata) | No| Service widget data.|
67| callback        | AsyncCallback\<string>                                                                                                | No| Callback used to return the result. |
68| style           | [FormMenuItemStyle](#formmenuitemstyle)                                                                              | No| Custom menu style.|
69
70
71## FormMenuItemStyle
72
73**Atomic service API**: This API can be used in atomic services since API version 12.
74
75**System capability**: SystemCapability.ArkUI.ArkUI.Full
76
77**Parameters**
78| Name           | Type          | Mandatory| Description|
79| --------------- | ----------------- | ---- | ---- |
80| options | [MenuItemOptions](ts-basic-components-menuitem.md#menuitemoptions) | No  | Information about the menu item.|
81
82> **NOTE**
83>
84> If **style** is left empty or unspecified, the default icon and menu text are applied.
85
86## Events
87Menu click events are supported.
88
89## Example
90
91```ts
92// index.ets
93import { AddFormMenuItem } from '@kit.ArkUI';
94import { formBindingData } from '@kit.FormKit';
95import { hilog } from '@kit.PerformanceAnalysisKit';
96
97const tag = 'AddFormMenuItem';
98
99@Entry
100@Component
101struct Index {
102  @State message: string = 'Long press show menu';
103  private compId: string = 'addforms@d46313145';
104
105  @Builder
106  MyMenu() {
107    Menu() {
108      AddFormMenuItem(
109        {
110          bundleName: 'com.example.myapplication', // Bundle name
111          abilityName: 'EntryFormAbility', // Module ability name.
112          parameters: {
113            'ohos.extra.param.key.form_dimension': 2,
114            'ohos.extra.param.key.form_name': 'widget',
115            'ohos.extra.param.key.module_name': 'entry'
116          },
117        },
118        this.compId,
119        {
120          formBindingData: formBindingData.createFormBindingData({}),
121          // formBindingData: formBindingData.createFormBindingData({ data: 'share' }),
122          callback: (error, formId) => {
123            hilog.info(0x3900, tag, `callback info: error = ${JSON.stringify(error)}, formId = ${formId}`);
124            if (error?.code === 0) {
125              hilog.info(0x3900, tag, "Added to the home screen.")
126            } else {
127              hilog.info(0x3900, tag, "Failed to add to the home screen. Try another method.")
128            }
129          },
130          style: {
131            // options: {
132            // startIcon: $r("app.media.icon"), // Menu icon, which can be provided by yourself. The default value is "sys.media.ic_public_add."
133            //   content: "Add to home screen",  //  Menu content, which can be provided by yourself. The default value is "sys.string.ohos_add_form_to_desktop."
134            //   endIcon: $r("app.media.icon") // Menu icon, which can be provided by yourself.
135            // }
136          }
137        }
138      )
139    }
140  }
141
142  build() {
143    Row() {
144      Column() {
145        Image($r("app.media.startIcon"))   // Custom image.
146          .id(this.compId)
147          .width(200)
148          .height(200)
149          .bindContextMenu(this.MyMenu, ResponseType.LongPress, {
150            placement: Placement.TopLeft
151          })
152      }
153      .width('100%')
154    }
155    .height('100%')
156  }
157}
158```
159
160```ts
161// WidgetCard.ets
162const local = new LocalStorage()
163
164@Entry(local)
165@Component
166struct WidgetCard {
167  @LocalStorageProp('data') data: string = 'defaultdata'; // Define the service widget data to be updated.
168  /*
169   * The action type.
170   */
171  readonly ACTION_TYPE: string = 'router';
172  /*
173   * The ability name.
174   */
175  readonly ABILITY_NAME: string = 'EntryAbility';
176  /*
177   * The message.
178   */
179  readonly MESSAGE: string = 'add detail';
180  /*
181   * The width percentage setting.
182   */
183  readonly FULL_WIDTH_PERCENT: string = '100%';
184  /*
185   * The height percentage setting.
186   */
187  readonly FULL_HEIGHT_PERCENT: string = '100%';
188
189  build() {
190    Row() {
191      Column() {
192        Text(this.data)
193          .fontSize($r('app.float.font_size'))
194          .fontWeight(FontWeight.Medium)
195          .fontColor($r('app.color.item_title_font'))
196      }
197      .width(this.FULL_WIDTH_PERCENT)
198    }
199    .height(this.FULL_HEIGHT_PERCENT)
200    .backgroundImage($r('app.media.startIcon'))
201    .backgroundImageSize({ width: '100%', height: '100%' })
202    .onClick(() => {
203      postCardAction(this, {
204        action: this.ACTION_TYPE,
205        abilityName: this.ABILITY_NAME,
206        params: {
207          message: this.MESSAGE
208        }
209      });
210    })
211  }
212}
213```
214
215**UI of the FormMenu component**
216
217![en-us_image_0000001616959836](figures/en-us_image_add_form_to_desktop.jpeg)
218
219**Result of using Add to home screen with the FormMenu component**
220
221The figure below shows the results when **formbindingdata** is empty (left), and when it is set to **{ data: 'share' }** (right).
222
223![en-us_image_0000001616959836](figures/en-us_image_add_form_to_desktop_result.jpeg)
224