Menu

Guides

The context menu

Edit the menu a right-click opens, add items of your own or let the page draw its own.

A right-click opens Kurogane’s menu and never Chromium’s. That holds in a window and in an embedded browser alike.

What the menu shows

  • In a text field the editing items: Undo, Redo, Cut, Copy, Paste, Paste as plain text and Select all. Chromium’s spelling suggestions come first on a misspelled word.
  • Copy on a selection.
  • Nothing elsewhere. An empty menu doesn’t show.
  • Inspect last in a debug build.

Add items of your own

App::on_context_menu edits the menu as it opens. App::on_context_menu_command runs when the user chooses one of your items:

use kurogane::{App, MenuItem};

fn main() {
    App::new("content")
        .on_context_menu(|menu, _app| {
            if menu.target().link_url().is_some() {
                menu.push(MenuItem::separator());
                menu.push(MenuItem::new("copy-link", "Copy link address"));
            }
        })
        .on_context_menu_command(|command, app| {
            if command.id() == "copy-link"
                && let Some(url) = command.target().link_url()
            {
                app.broadcast_json("copy-link", &url);
            }
        })
        .run_or_exit();
}

The page copies the address when the event arrives:

window.kurogane.on("copy-link", (json) => navigator.clipboard.writeText(JSON.parse(json)));

on_context_menu_command runs only for your items. It gets the same target the menu opened on. An item chosen after its page went away runs nothing. That happens when the page navigated while the menu stayed open.

Remove an item

ContextMenu::remove takes one of Kurogane’s items out, from submenus too:

use kurogane::{App, StandardItem};

fn main() {
    App::new("content")
        .on_context_menu(|menu, _app| {
            if menu.target().is_editable() {
                // No pasting formatted text into this app's fields
                menu.remove(StandardItem::Paste);
            }
        })
        .run_or_exit();
}

menu.clear() takes every item out. menu.items_mut() gives the items as a Vec to reorder or filter.

A standard item whose command App::on_chrome_command refuses is left out of the menu. It’s asked about again when chosen. Refusing DevTools there removes Inspect.

Disabled items, check marks and submenus

AppItem adds an enabled state and a check mark. MenuItem::submenu groups items under a label:

use std::sync::atomic::{AtomicBool, Ordering};

use kurogane::{App, AppItem, MenuItem};

static WRAP: AtomicBool = AtomicBool::new(true);

fn main() {
    App::new("content")
        .on_context_menu(|menu, _app| {
            menu.push(MenuItem::submenu(
                "View",
                [
                    MenuItem::App(AppItem::new("wrap", "Word wrap").checked(WRAP.load(Ordering::Relaxed))),
                    MenuItem::App(AppItem::new("zoom", "Zoom").enabled(false)),
                ],
            ));
        })
        .on_context_menu_command(|command, _app| {
            if command.id() == "wrap" {
                WRAP.fetch_xor(true, Ordering::Relaxed);
            }
        })
        .run_or_exit();
}

Choosing a check item doesn’t change its mark. Your app sets the mark each time the menu opens.

Separators at an edge or next to another aren’t shown. Neither are submenus left empty.

In a window’s menu an & in a label marks the letter that chooses the item from the keyboard. && shows an &. An embedded browser’s menu takes every & out. There a typed letter chooses the item whose label begins with it.

Know what was right-clicked

ContextMenuTarget describes it:

Method Gives
origin() the origin of the document right-clicked; opaque for a sandboxed document
page_url() the URL of the page the browser shows
frame_url() the URL of the document right-clicked, the page’s or a frame’s
link_url() the link’s URL
media(), media_url() the kind of media (MediaKind) and its URL
selection() the selected text
is_editable() whether it was in a text field or other editable content
misspelled_word() the misspelled word
position() where the right-click was in the browser’s view
browser() the browser right-clicked

Let the page draw its own menu

A page that draws its own menu calls preventDefault() on its contextmenu event. Kurogane’s menu doesn’t open then:

document.addEventListener("contextmenu", (event) => {
  event.preventDefault();
  showMyMenu(event.clientX, event.clientY);
});

Rules for the hooks

  • Both hooks run on the UI thread and must not block.
  • A panicking on_context_menu leaves Kurogane’s menu as it was. A panicking on_context_menu_command is logged.
  • DevTools’ menus are never asked about.
  • A later call replaces an earlier hook.