Menus (src/ui/popups.fm)
A menu is a floating panel of rows, named by its id as a widget is, opened at a point or under a rect
(menu_open(id, at)), and built while it is open:
if sig.right_clicked { ui.menu_open("file_menu", ui.mouse_pos()) }
if ui.menu("file_menu") {
if ui.menu_item("Copy", "Ctrl+C").clicked { ... }
ui.menu_separator()
if ui.menu_submenu("Show as") {
if ui.menu_item("QR code", "Ctrl+Shift+Q").clicked { ... }
}
}The menu is the block of its if. ui.menu ends the menu where the scope of its call ends (Formula's
deferred_none), so an early return does not work:
// wrong: the scope of the call is the `if`'s; the menu ends before the items, which are built outside it
if !ui.menu("file_menu") { return }
ui.menu_item("Copy", "Ctrl+C")
// right: the items in the block, or in a procedure called there
if ui.menu("file_menu") { file_menu_items(app) }
// right too: the call as a statement; the menu ends with the procedure
open := ui.menu("file_menu")
if !open { return }
ui.menu_item("Copy", "Ctrl+C")The same holds for every call that opens a block: menu_submenu, tooltip, tooltip_near, menu_control.
A block that is no button (a message, a card, an image) takes a right press with ui.context_take(box), called after
what is in it is built: the innermost that asks keeps the press, one box a build; a field that opened its own menu on that
press keeps it; nothing drawn over the pointer (a dialog, a floating panel) lets it through.
if ui.context_take(card) { app.menu_for = card_id; ui.menu_open("card_menu", ui.mouse_pos()) }| A context menu | The same, co typed |
|---|---|
![]() | ![]() |
What is typed filters it
Every menu filters what is typed: the context menus, a menu bar's, a tool bar's, a drop-down's list, a field's menu.
A field at its top (search_band, as the palette's) shows the letters. The items that do not have them in order go,
a submenu's items come up into the menu, and the first match is selected: Enter chooses it.
- A letter typed while a submenu is open closes the submenu and goes to its menu's field. A nested menu has its own field.
- Until something is typed, Space chooses the selected item, and Left, Right, Home and End move in the menu.
- A drop-down's list opens on its current value: Enter keeps it while nothing is typed.
- A panel opened as a menu with no item (a slider's) has no field: it has nothing to filter.
There is no mode where a typed letter selects the next item that starts with it: the filter's first match gives the same, and one way to type in every menu is one thing to learn.
Its procedures
| Procedure | What it does |
|---|---|
menu_open(id, at, anchor = 0, from_keyboard = false, nested = false, align = .Start) | opens the menu id at at, a Menu_At: a point (Vec2, its top-left corner) or a rect (Rect: below it, above it when there is no room). It replaces any open menu. anchor: the box that opens it (a press on it is not a press outside: the widget toggles the menu). from_keyboard: its first item starts selected. nested: in the block of an open menu, the menu opens over it and that menu stays open behind it. align (Menu_Align): below a rect, the edge the menu lines up with, .Start its left, .End its right (a button at the end of a toolbar: the toolkit knows the menu's width) |
menu(id, width = 0) | if menu(id) { ... } builds the menu while it is open, a nested one in the block of the menu that opened it. A tool bar's tool with a menu, folded into its "…" menu, opens id as a submenu of its row: the same block builds it there |
menu_item(text, shortcut, checked, icon, disabled, keep_open, icon_font) | one row; disabled (or a disabled scope): muted, skipped by the keyboard |
menu_submenu(text, icon, disabled, icon_font) | if menu_submenu("Copy", lucide.COPY, icon_font = ui.lucide_icons()) { ... } a row that opens a submenu (supports arbitrary nesting, safe-triangle diagonal hover, keyboard Right/Enter/Left/Escape, and automatic screen-edge flipping) |
menu_control(label, id = "") | if menu_control("Width") { ui.slider(...) } a row that holds a widget: label at its left, in line with the items' labels, the widget at its right. The widgets of every control row of a level start at one column, after the widest label. Not an item: the keyboard skips it, and using its widget does not close the menu |
menu_heading(text), menu_separator() | a small weak title above a group of rows; a line between two groups (neither is an item) |
menu_close(), menu_is_open(id), menu_any_open() | closes every level; whether id is open (any level); whether a menu is open |
menu_bar_button(id) -> string | an entry of a menu bar: the id of its menu, for menu. id's words mark the access letter with & ("&File", "Ed&it"; && is an &); without one, the first letter is it. The keys, as on Windows and GTK: Alt+letter opens that entry's menu, its first item selected, the letters underlined while the bar is lit, a keyboard menu is open, or Alt is held alone past MENU_BAR_ALT_DELAY_MS (150 ms, so chords like Alt+Up/Alt+Down do not blink); F10, or Alt pressed and let go alone, lights the first entry (Left, Right, Home, End move; Down, Up, Enter, Space or a letter open; Escape, F10, Alt or a press of the pointer give the keys back); in an open menu of the bar, Left and Right go to the menu beside it (Right on an item with a submenu opens it), Escape closes it and leaves its entry lit. AltGr is no Alt (AltGr+E types €), and a focused widget that claims the keys (keys_claim) keeps Alt+letter, F10 and Alt alone |
A menu's id is the window's, not that of where it is built: two menus of one id are one menu. A menu of each row puts the
row in its id (fmt.tprintf("row_menu_%d", row.id)).
Open with
ui.Open_With lists the applications that open a file, with their icons, inside a program's submenu, and opens the file
with the one chosen. Its last item, always there, is "Choose an application…": a file of a type no application
declares still opens. The State builds its chooser at the end of the build: the program has nothing more to call.
ow: ui.Open_With // one per menu that offers it; zero is ready
ui.open_with_ask(&ow, s, path) // when the menu opens: a chore reads the handlers and their icons
if ui.menu_submenu(i18n.tr("Open with")) {
if ui.open_with_items(&ow, "ow") { ui.open_with_run(&ow) }
}
ui.open_with_destroy(&ow)| Procedure | What it does |
|---|---|
open_with_ask(ow, s, path) | reads the handlers of the file at path (desktop.handlers_for_file) and each one's icon (desktop.handler_icon, OPEN_WITH_ICON_PX) on a chore; the answer comes to s. What was read before goes |
open_with_items(ow, id_prefix = "ow") -> (chosen: bool) | the items, in the block of a menu_submenu: "Reading…" while they are read, each application with its icon (the default first, "Name (default)"), then "Choose an application…" (open_with_choose). True in the build where an application is clicked. The items are named <id_prefix>_<index>, the last <id_prefix>_choose |
open_with_choose(ow) | lets the person choose any application for the file asked: on Windows the system's dialog (desktop.open_with_dialog), which opens the file itself; elsewhere the chooser, which the State of the ask builds in end_build from then on (State.open_with_chooser), its list (desktop.applications) read on a chore. The chooser: "Open <name> with", a search (it narrows the list when its text changes, at most OPEN_WITH_CHOOSER_ROWS rows built), the applications, "Always use for this type" (desktop.set_default_handler, on a chore; a toast when it fails), Cancel and Open. Its parts are named owc_search, owc_<row>, owc_always, owc_open. The application chosen opens the file (open_with_run), or goes to on_chosen |
open_with_chosen(ow) -> (h: desktop.Handler, ok: bool) | the application chosen last, in the menu or the chooser: a program that prepares the file first runs desktop.open_with itself, on its worker |
open_with_run(ow, path = "") | opens the file (path "": the one asked) with the handler chosen, on a chore; a toast says when it does not start |
open_with_destroy(ow) | frees the list, its icons and their textures; an answer on its way is dropped |
Open_With.state is .Idle, .Reading or .Ready. The hooks handlers_of, icon_of, open, applications_of,
set_default and dialog replace the system's calls (nil: the desktop's); a test gives a fixed list. They run on a
chore. A dialog hook takes the place of the chooser, as Windows' dialog does. on_chosen (with user) is the
program's own start for the application chosen in the chooser, on the interface's thread; nil: open_with_run.
Its width
A menu is as wide as its widest row: the icon column, the label, the shortcut or a submenu's arrow, and their padding. One
procedure gives a row's width from the same numbers the row is laid out with, in the window's style (the menu's rows
are built in it, whatever the style where menu is called): a theme of a wider text_pad makes the menu wider, never a
label cut.
menu(id, width):widthis a minimum (a drop-down's list is as wide as its button at least). A row wider than it widens the menu.- A menu wider than the window is as wide as the window. Only then is a label cut ("..."), and its row shows the whole label as a tooltip when the pointer rests on it (0.5 s).
- A submenu, a nested menu and a
dropdown's list follow the same rule. - The command palette is as wide as the person makes it (a
modalof asize): a line it cuts shows its whole text as a tooltip in the same way.
The menu of a text field
edit_menu(id, target, owner, kind) builds the menu of a text field, id its menu: Undo, Redo, Cut, Copy,
Paste, Delete and Select all, each with its keys and its Lucide icon, and the field of every menu (search_band, as the palette's: widgets). target
is an Edit_Target: a line_edit's ^Line_Edit or a text_edit's ^Text_Edit; each edit goes through its own edit
history, as its keys do. owner is the field's widget, which a paste's text comes to (edit_paste). kind
(Line_Edit_Kind): a .Secret is neither copied nor cut; a .Read_Only field is only copied and selected.
line_edit and text_edit open it on a right press, the field taking the keyboard (unless their options' own_menu:
the program has its own).

