How to mimic Mac OS menus or contextual menus?

Hi all,

How could it be possible to mimic Mac OS menus or contextual menus, where after displaying it, typing a modifier change some items?
For example, right-clicking on a folder displays “Open in a new Tab”, and keeping Option key while the menu is displayed, changes this menu item to “Open in a new Window”.

I can’t even imagine where to start for such a code… Probably with NS class?
Xojo code, MBS plugin or any other is welcome.

Thanks in advance.
Jean-Luc

Check NSMenuItemMBS class.
You need to set Alternate flag to true for second or third menu item.

So you may have multiple menu entries and only one of them shows.

Also, there are rules to follow to make it working. I’m not sure I’m up to date, but as far as I know, you should make sure the alternate items are following the “base” item (no other item between them) and the key of each item must be the same character with varying modifiers.
Probably obvious, but worth mentioning.

I think that rule may be more of a suggestion. Try the main :apple: menu and then press the “option” key :upside_down_face:

It took a bit of fiddling with @Christian_Schmitz’s suggestion (because there is no direct example in the MBS set that I could find), but the following works for me with existing Xojo DesktopMenuItem items.

In the MenuBarSelected event I did the following:

// closeWindowMenu is placed immediate before the following, in the Xojo Menu Designer
NSMenuItemMBS.MenuItem(closeAllWindowsMenu).Alternate = True

And because closeAllWindowsMenu is immediately after closeWindowMenu it will replace it, once the menu is displayed and “option” is pressed.

1 Like

There is also a Declares solution:

Declare Sub setAlternate Lib "Foundation" Selector "setAlternate:" (obj As Ptr, value As Boolean)
setAlternate(closeAllWindowsMenu.Handle, True)

Note: tested on macOS 14.6.1 (M1) and Xojo 2024.2.1

Is there also a possibility to add/remove a menu item when the menu is showing? I’d like to have something similar to the Finder where the option key shows/hides the Library menu item in the Go menu.

It depends how you interpret the result. In your example, I’d be tempted to say all items have the same character, which are possibly empty.
But it’s very probable my information is dating too much.

From a design point-of-view, I think you’re right. I also agree that if a character is used, the alternate menu should use the same character with a variation of the modifiers on the same character. Same with the alternate menu functionality - it should just be a variation of the same menu command.

I’m glad @Jean-Luc_ARNAUD posted this question, because I’m interested in the same feature but had not looked it up yet. But now I have, and have started implementing it in my current project.

I also found this link in the Human Interface Guidelines documentation, where this feature is called Dynamic Menu Items.

What Apple says about Dynamic Menus

In rare cases, it can make sense to present a dynamic menu item, which is a menu item that changes its behavior when people choose it while pressing a modifier key (Control, Option, Shift, or Command). For example, the Minimize item in the Window menu changes to Minimize Allwhen people press the Option key.

Avoid making a dynamic menu item the only way to accomplish a task. Dynamic menu items are hidden by default, so they’re best suited to offer shortcuts to advanced actions that people can accomplish in other ways. For example, if someone hasn’t discovered the Minimize Alldynamic menu item in the Window menu, they can still minimize each open window.

Use dynamic menu items primarily in menu bar menus. Adding a dynamic menu item to contextual or Dock menus can make the item even harder for people to discover.

Require only a single modifier key to reveal a dynamic menu item. It can be physically awkward to press more than one key while simultaneously opening a menu and choosing a menu item, in addition to reducing the discoverability of the dynamic behavior. For developer guidance, see isAlternate.

1 Like

Do what I say, not what I do.

As an example, you can fire an application from the alternate AppleMenu “About this computer” (or so). Only other way is to search it in Applications/Utilities…

Thanks for having searching for that. It looks like the part saying one should give the same character (if any) is gone.

1 Like