feat: enable secondary label for macOS menu (#46887)

* feat: enable secondary label for macOS menu

* Update shell/browser/ui/cocoa/electron_menu_controller.mm

Co-authored-by: Robo <hop2deep@gmail.com>

* fix for lint

* update docs for sublabel

---------

Co-authored-by: Robo <hop2deep@gmail.com>
This commit is contained in:
Michaela Laurencin 2025-05-09 12:05:26 -04:00 committed by GitHub
parent b9f0aebb2f
commit 10e4f9ad37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 1 deletions

View file

@ -19,7 +19,7 @@ See [`Menu`](menu.md) for examples.
* `type` string (optional) - Can be `normal`, `separator`, `submenu`, `checkbox` or
`radio`.
* `label` string (optional)
* `sublabel` string (optional)
* `sublabel` string (optional) _macOS_ - Available in macOS >= 14.4
* `toolTip` string (optional) _macOS_ - Hover text for this menu item.
* `accelerator` [Accelerator](accelerator.md) (optional)
* `icon` ([NativeImage](native-image.md) | string) (optional)

View file

@ -329,6 +329,27 @@ name, no matter what label you set. To change it, modify your app bundle's
[About Information Property List Files][AboutInformationPropertyListFiles]
for more information.
### Menu Sublabels
Menu sublabels, or [subtitles](https://developer.apple.com/documentation/appkit/nsmenuitem/subtitle?language=objc), can be added to menu items using the `sublabel` option. Below is an example based on the renderer example above:
```js @ts-expect-error=[12]
// main
ipcMain.on('show-context-menu', (event) => {
const template = [
{
label: 'Menu Item 1',
sublabel: 'Subtitle 1',
click: () => { event.sender.send('context-menu-command', 'menu-item-1') }
},
{ type: 'separator' },
{ label: 'Menu Item 2', sublabel: 'Subtitle 2', type: 'checkbox', checked: true }
]
const menu = Menu.buildFromTemplate(template)
menu.popup({ window: BrowserWindow.fromWebContents(event.sender) })
})
```
## Setting Menu for Specific Browser Window (_Linux_ _Windows_)
The [`setMenu` method][setMenu] of browser windows can set the menu of certain

View file

@ -315,12 +315,21 @@ NSArray* ConvertSharingItemToNS(const SharingItem& item) {
- (NSMenuItem*)makeMenuItemForIndex:(NSInteger)index
fromModel:(electron::ElectronMenuModel*)model {
std::u16string label16 = model->GetLabelAt(index);
auto rawSecondaryLabel = model->GetSecondaryLabelAt(index);
NSString* label = l10n_util::FixUpWindowsStyleLabel(label16);
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:label
action:@selector(itemSelected:)
keyEquivalent:@""];
if (!rawSecondaryLabel.empty()) {
if (@available(macOS 14.4, *)) {
NSString* secondary_label =
l10n_util::FixUpWindowsStyleLabel(rawSecondaryLabel);
item.subtitle = secondary_label;
}
}
// If the menu item has an icon, set it.
ui::ImageModel icon = model->GetIconAt(index);
if (icon.IsImage())