Skip to content

NativeMenuBar

Provides a fluent API for building native menus with runtime modification support.

Accessing the Menu Bar

csharp
var menuBar = window.MenuBar;

Methods

AddMenu

csharp
public NativeMenuBar AddMenu(string label, Action<NativeMenu> configure)

Add a top-level menu.

csharp
menuBar.AddMenu("File", file =>
{
    file.AddItem("New", "file.new")
        .AddItem("Open", "file.open");
});

Indexer

csharp
public NativeMenuItem this[string commandId] { get; }

Access a menu item by its command ID.

csharp
menuBar["file.save"].IsEnabled = document.IsDirty;

Events

ItemClicked

csharp
public event Action<string>? ItemClicked;

Fired when any menu item is clicked. The parameter is the command ID.

csharp
menuBar.ItemClicked += itemId =>
{
    switch (itemId)
    {
        case "file.new":
            CreateNewDocument();
            break;
        case "file.exit":
            window.Close();
            break;
    }
};

NativeMenu Methods

AddItem

csharp
public NativeMenu AddItem(string label, string commandId, Action<NativeMenuItem>? configure = null)

Add a menu item.

csharp
menu.AddItem("Save", "file.save", item => item
    .WithAccelerator("Ctrl+S")
    .WithEnabled(document.IsDirty));

AddSeparator

csharp
public NativeMenu AddSeparator()

Add a separator line.

AddSubmenu

csharp
public NativeMenu AddSubmenu(string label, string commandId, Action<NativeMenu> configure)

Add a submenu.

csharp
menu.AddSubmenu("Recent Files", "file.recent", recent =>
{
    recent.AddItem("Doc1.txt", "recent.1")
          .AddItem("Doc2.txt", "recent.2");
});

NativeMenuItem Methods

WithAccelerator

csharp
public NativeMenuItem WithAccelerator(string accelerator)

Set the keyboard shortcut.

csharp
item.WithAccelerator("Ctrl+S")       // Ctrl+S on Windows/Linux, Cmd+S on macOS
item.WithAccelerator("Ctrl+Shift+N") // Multiple modifiers
item.WithAccelerator("F5")           // Function key

WithEnabled

csharp
public NativeMenuItem WithEnabled(bool enabled)

Set whether the item is enabled.

WithChecked

csharp
public NativeMenuItem WithChecked(bool isChecked)

Set whether the item shows a checkmark.

NativeMenuItem Properties

IsEnabled

csharp
public bool IsEnabled { get; set; }

Get or set whether the item is enabled.

IsChecked

csharp
public bool IsChecked { get; set; }

Get or set whether the item is checked.

Label

csharp
public string Label { get; set; }

Get or set the item's label text.

Runtime Modification

Hermes supports modifying menus at runtime for plugin systems:

InsertItem

csharp
menuBar["Tools"].InsertItem(
    afterId: "tools.options",
    label: "My Plugin",
    commandId: "plugins.myplugin");

RemoveItem

csharp
menuBar["Tools"].RemoveItem("plugins.myplugin");

RemoveMenu

csharp
menuBar.RemoveMenu("Plugins");

Example: Plugin System

csharp
public void OnPluginLoaded(IPlugin plugin)
{
    menuBar.AddMenu(plugin.MenuName, menu =>
    {
        foreach (var command in plugin.Commands)
        {
            menu.AddItem(command.Label, command.Id);
        }
    });
}

public void OnPluginUnloaded(IPlugin plugin)
{
    menuBar.RemoveMenu(plugin.MenuName);
}

Platform Notes

  • Windows: Menus appear in the window
  • macOS: Menus appear in the system menu bar (top of screen)
  • Linux: Menus appear in the window

Accelerators are automatically translated:

  • Ctrl+NCmd+N on macOS

Released under the Elastic License 2.0