Module Webview_desktop.Menu

The native menu bar.

The system's own menu bar — the strip at the top of the screen on macOS, the one under the title bar on Linux and Windows — described as an ordinary OCaml value and installed in one call:

  let quit () = Webview.terminate w in
  Webview_desktop.Menu.set w
    [
      ("MyApp", [ Menu.item "Quit" ~key:'q' ~modifiers:[ Cmd ] quit ]);
      ( "File",
        [
          Menu.item "Open…" ~key:'o' ~modifiers:[ Cmd ] pick_file;
          Menu.separator;
          Menu.submenu "Recent" [ Menu.item "example.txt" reopen ];
        ] );
    ]

How to call it

set must run on the UI thread — the thread running Webview.run — and on macOS only once the application is active, which it becomes after Webview.run has started. The reliable place is therefore a Webview.dispatch callback, the same pattern Icon.set_app_icon needs:

  Webview.dispatch w (fun w -> Menu.set w menus);
  Webview.run w

Item callbacks are invoked on the UI thread too, so they may drive the webview directly — Webview.eval, Webview.terminate — with no Webview.dispatch in between. An exception escaping a callback is dropped: there is no OCaml handler on the native stack below it.

What differs between platforms

type modifier =
  1. | Cmd
    (*

    The platform's primary modifier: Command on macOS, Control on Linux and Windows, where there is no Command key.

    *)
  2. | Ctrl
    (*

    Control, on every platform.

    *)
  3. | Alt
    (*

    Option on macOS, Alt elsewhere.

    *)
  4. | Shift

A keyboard modifier.

type item

One entry in a menu: a clickable item, a separator, or a nested submenu.

val item : ?key:char -> ?modifiers:modifier list -> string -> (unit -> unit) -> item

item label on_click is a clickable entry that calls on_click () on the UI thread when chosen.

?key and ?modifiers give it a keyboard shortcut: ~key:'o' ~modifiers:[ Cmd ] is Command-O on macOS and Control-O elsewhere. Use a lowercase letter; with no ?key the item has no shortcut, and ?modifiers is then ignored.

val separator : item

A horizontal rule between groups of items.

val submenu : string -> item list -> item

submenu label items nests a menu inside another one.

val set : Webview.t -> (string * item list) list -> unit

set w menus replaces the whole menu bar, menus being the top-level menus in order, each a title and its contents.

Calling it again replaces the previous bar entirely; the callbacks of the old one are released. Passing [] installs an empty bar.