Skip to content

Commit 25d8c9a

Browse files
authored
Merge pull request #10237 from mendix/qt-menuapireview
Review Menu API
2 parents 6b19ec2 + efad1d3 commit 25d8c9a

File tree

1 file changed

+46
-46
lines changed
  • content/en/docs/apidocs-mxsdk/apidocs/studio-pro-11/extensibility-api/web/web-extensions-howtos

1 file changed

+46
-46
lines changed

content/en/docs/apidocs-mxsdk/apidocs/studio-pro-11/extensibility-api/web/web-extensions-howtos/menu-api.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,40 @@ url: /apidocs-mxsdk/apidocs/web-extensibility-api-11/menu-api/
66

77
## Introduction
88

9-
This how-to shows you how to create both a simple menu item and a menu item with subsidiary items beneath it using the web extension API.
9+
This how-to describes how to create menus using the web extensibility API. In this example, you will:
10+
11+
* Create a simple menu item
12+
* Add menu items with sub-menus
13+
* Update a menu
1014

1115
## Prerequisites
1216

13-
This how-to uses the results of [Get Started with the Web Extensibility API](/apidocs-mxsdk/apidocs/web-extensibility-api-11/getting-started/). Please complete that how-to before starting this one.
17+
This how-to uses the results of [Get Started with the Web Extensibility API](/apidocs-mxsdk/apidocs/web-extensibility-api-11/getting-started/). Make sure to complete that how-to before starting this one.
1418

15-
## Creating a Simple Menu
19+
## Menu Properties
1620

17-
In this section, you will add a simple menu to your extension.
21+
A menu has the following properties:
1822

19-
The code below will:
23+
| Property | Description |
24+
|----------------------|-------------------------------------------------------------------------------|
25+
| `caption` | The text of the menu item |
26+
| `menuId` | A unique identifier for the menu item |
27+
| `subMenus` | A list of sub-menu items |
28+
| `hasSeparatorBefore` <br> (default: `false`) | Adds a visual separator before the item |
29+
| `hasSeparatorAfter` <br> (default: `false`) | Adds a visual separator after the item |
30+
| `enabled` <br> (default: `true`) | Indicates that the menu item notifies the listener when clicked |
2031

21-
* create a menu item with the caption "My First Menu"
22-
* show a dialog when the menu is clicked
32+
33+
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/web/menus/grouped_menus.png" width="300" >}}
34+
35+
## Creating a Simple Menu
36+
37+
The code below adds a simple menu to your extension. The code will:
38+
39+
* Create a menu item with the caption *My First Menu*
40+
* Show a dialog when the menu is clicked
41+
* Import `menuApi` from `studioPro.ui.extensionsMenu` to allow you to use the menu API
42+
* Import `messageBoxApi` from `studioPro.ui.messageBoxes` to show a dialog
2343

2444
Replace your `src/main/index.ts` file with the following:
2545

@@ -56,43 +76,28 @@ export const component: IComponent = {
5676
}
5777
```
5878

59-
The code imports the following:
60-
61-
* `menuApi` from `studioPro.ui.extensionsMenu` to allow you to use the menu API
62-
* `messageBoxApi` from `studioPro.ui.messageBoxes` to show a dialog.
79+
When this code is added, it does the following:
6380

64-
It starts listening to the `menuItemActivated` endpoint which will notify the extension when **My First Menu** is clicked.
65-
66-
The `menuItemActivated` listener event handles the actual function of the menu. The arguments `args` contain the data returned by Studio Pro to the extension, notifying it which menu item was activated (clicked). This is passed as the `menuId` that was used when creating the menu item and the `menuId` in the `if` statement is used to identify this. In this example it is `"my-menu-unique-id"` and the handler calls the `messageBoxApi` to show an information dialog.
81+
1. Studio Pro starts listening to the `menuItemActivated` endpoint. This notifies the extension when **My First Menu** is clicked.
82+
2. The `args` parameter contains the information sent from Studio Pro to the extension, indicating which menu item was clicked.
83+
3. The listener checks if the clicked `menuId` matches your defined ID. If it does, it calls `messageBoxApi.show()`.
84+
4. Studio Pro displays an information dialog with the message you provided.
6785

6886
Your extensions should now appear like this:
6987

70-
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/web/menus/my_first_menu.png" >}}
71-
72-
## Menu Properties
73-
74-
The menu has the following properties:
75-
76-
* `caption` – the text of the menu item
77-
* `menuId` – a unique id for the menu item
78-
* `subMenus` – a list of menus subsidiary to this menu item
79-
* `hasSeparatorBefore` (default: `false`) – shows a visual separator before this menu item
80-
* `hasSeparatorAfter` (default: `false`) – shows a visual separator after this menu item
81-
* `enabled` (default: `true`) – indicates that this menu item notifies the listener when clicked
82-
83-
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/web/menus/grouped_menus.png" >}}
88+
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/web/menus/my_first_menu.png" width="200" >}}
8489

8590
## Creating a Menu with Submenus
8691

87-
You can also have a number of submenus that branch out your menu.
92+
You can also include multiple sub-menus to expand your menu structure.
8893

89-
To do so, add additional menu items to your code and add these to the `subMenus` array for the relevant menu item. These child menus can in turn have their own submenus, and so on. Only parent menus (menus that are not sub menus to any others) should be added through the `await menuApi.add()` call, as shown in the code sample below.
94+
To do this, add additional menu items to your code and add them to the `subMenus` array for the relevant menu item. These child menus can have their own sub-menus, and so on. Only parent menus (menus that are not sub-menus to any others) should be added through the `await menuApi.add()` call, as shown in the code sample below.
9095

9196
{{% alert color="info" %}}
92-
Parent menus (with `subMenus`) do not create `menuItemActivated` events. These only get sent when a leaf menu (a menu that does not have any submenus) is clicked.
97+
Parent menus (with `subMenus`) do not create `menuItemActivated` events. These are only sent when a leaf menu (a menu that does not have any sub-menus) is clicked.
9398
{{% /alert %}}
9499

95-
The following `src/main/index.ts` generates one menu item with sub menus and one menu item without sub menus.
100+
The following `src/main/index.ts` generates one menu item with sub-menus, and one menu item without sub-menus.
96101

97102
```typescript
98103
import { IComponent, Menu, getStudioProApi } from "@mendix/extensions-api";
@@ -142,15 +147,15 @@ export const component: IComponent = {
142147
}
143148
```
144149

145-
The menu hierarchy will then be displayed like this:
150+
The menu hierarchy will be displayed like this:
146151

147152
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/web/menus/child_menus.png" >}}
148153

149-
## Updating a menu
154+
## Updating a Menu
150155

151-
Sometimes you might want to disable a menu or update its caption depending on a condition. You can do so by calling the menu API's `updateMenu` method.
156+
You can disable a menu or update its caption, depending on a condition. You can do this by calling the menu API's `updateMenu` method.
152157

153-
An example is shown in the code below. If you click on the menu item, it will be disabled and its caption will be updated.
158+
An example is shown in the code below. If you click the menu item, it will be disabled and its caption will be updated.
154159

155160
{{% alert color="info" %}}
156161
Only `caption` and `enabled` can be updated.
@@ -191,23 +196,18 @@ export const component: IComponent = {
191196
}
192197
```
193198

194-
The disabled state is shown in the image below.
199+
The disabled state is shown in the image below:
195200

196-
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/web/menus/disabled_menu.png" >}}
201+
{{< figure src="/attachments/apidocs-mxsdk/apidocs/extensibility-api/web/menus/disabled_menu.png" width="300" >}}
197202

198203
## Attaching a Command to the Menu
199204

200205
Instead of listening to the `menuItemActivated` event, it is possible to register a command, then attach the `commandId` of the new command to your menu. When the menu is clicked, if its `commandId` property has been set, the backend will execute the command instead of firing the `menuItemActivated` event.
201206

202-
For a full explanation on how to register commands, see the [Commands API](/apidocs-mxsdk/apidocs/web-extensibility-api-11/command-api/).
203-
204-
## Conclusion
205-
206-
You have seen how to create simple menu items and menu items with sub menus.
207-
You can also dynamically change the enabled status and caption of a menu item.
207+
For more information on how to register commands, see the [Commands API](/apidocs-mxsdk/apidocs/web-extensibility-api-11/command-api/).
208208

209209
## Extensibility Feedback
210210

211-
If you would like to provide us with some additional feedback you can complete a small [Survey](https://survey.alchemer.eu/s3/90801191/Extensibility-Feedback)
211+
If you would like to provide additional feedback, you can complete a small [survey](https://survey.alchemer.eu/s3/90801191/Extensibility-Feedback).
212212

213-
Any feedback is much appreciated.
213+
Any feedback is appreciated.

0 commit comments

Comments
 (0)