pub struct MenuItem {
pub label: String,
pub role: MenuRole,
pub enabled: bool,
pub id: Option<Value>,
pub checked: bool,
pub accel: Option<String>,
pub submenu: Vec<MenuItem>,
}Expand description
One row of a menu.
Fields§
§label: StringWhat the row reads. Empty takes the role’s default wording.
role: MenuRole§enabled: boolA disabled row is drawn dimmed, is not focusable, and cannot be chosen — Paste with an empty clipboard, Copy with no selection. Present rather than absent on purpose: a menu whose rows move depending on what is possible is a menu nobody builds muscle memory for.
id: Option<Value>Posted as the event payload when the row is chosen. A Custom
item without one posts its label.
checked: boolDrawn with a checkmark, and the platform’s own check state where a host renders the menu itself. A setting the row is rather than a command it runs — View ▸ Show Sidebar — and inert for every other row, which is why it is a flag beside the label and not a role.
accel: Option<String>Drawn right-aligned and dimmed; the core binds nothing to it. The
keyboard shortcut is the app’s or the platform’s, and an
accelerator here only says which one it is. A standard row that
declares none takes its role’s (MenuRole::default_accel), the
way an empty label takes the role’s wording.
The rows of the menu this row opens: empty for an ordinary row. A
row with a submenu is drawn with a chevron and is never chosen
itself — hovering it, clicking it, Enter or the Right arrow opens
its menu beside it, and Left or Escape closes it again — and what
a chosen row inside posts is that row’s own menu event, on the
node the outermost menu is about (backlog F128). Nests to any
depth. MenuItem::submenu(label, items) builds one.
Implementations§
Source§impl MenuItem
impl MenuItem
Sourcepub const KEYS: [&'static str; 7]
pub const KEYS: [&'static str; 7]
The keys a row map may carry — everything Self::from_value
reads. A binding that drops the rest of a map on the floor checks
against this first and raises crate::diag::unknown_menu_item_key
for what it dropped, so {label, disabled: true} is not silently a
row that is enabled.
Sourcepub const NAME: &'static str = "menuItem"
pub const NAME: &'static str = "menuItem"
The name a binding reports a row’s dropped keys under
(diag::unknown_prop routes it to diag::unknown_menu_item_key):
a row is not an element, so it is not in schema::ELEMENTS, and
the spelling is the type’s in JSX (MenuItemInput).
Sourcepub fn from_value(v: &Value) -> Result<Self, String>
pub fn from_value(v: &Value) -> Result<Self, String>
A row from plain data: a map with label, role (a wire name;
absent is custom), enabled (default true), checked (default
false), id, accel and items (a submenu’s rows, the same maps).
A custom row needs a label, since the label is what it posts when it
has no id. Every binding funnels its rows through here —
openMenu’s list and a menu bar’s alike — so a row can never mean
two things.
Sourcepub fn list_from_value(v: &Value) -> Result<Vec<Self>, String>
pub fn list_from_value(v: &Value) -> Result<Vec<Self>, String>
A menu’s rows from plain data: a list of Self::from_value maps.
Sourcepub fn options_from_value(v: &Value) -> Result<Vec<Self>, String>
pub fn options_from_value(v: &Value) -> Result<Vec<Self>, String>
A select’s options from plain data (widgets::select_items in the
bindings): a list whose entries are strings — an option by its
label, posting it — or Self::from_value maps, for an option
that posts an id of its own or is disabled. An empty list is
refused: a select with nothing to choose from is a field that opens
a menu of no rows, which only Escape leaves.
A row that opens a menu of items beside it: “Move to ▸”, “Sort
by ▸”. It is chosen through, never itself — see the submenu
field — so it needs no id, and an accel on it is not drawn (the
chevron is where it would go). A submenu with no rows is an ordinary
row (Self::has_submenu).
use kui_core::MenuItem;
let sort = MenuItem::submenu("Sort by", vec![
MenuItem::new("Name").id("sort.name").checked(true),
MenuItem::new("Date modified").id("sort.date"),
]);
assert!(sort.has_submenu());Whether the row opens a menu rather than being chosen (the
submenu field). A row declared with an empty submenu is an
ordinary row.
Sourcepub fn at_path<'a>(
items: &'a [MenuItem],
path: &[usize],
) -> Option<&'a MenuItem>
pub fn at_path<'a>( items: &'a [MenuItem], path: &[usize], ) -> Option<&'a MenuItem>
The row at path in items: [2] is the third row, [2, 0] the
first row of the third row’s submenu. None past the end of any
level, for an empty path, and through a row with no submenu.
pub fn separator() -> Self
pub fn enabled(self, on: bool) -> Self
Sourcepub fn checked(self, on: bool) -> Self
pub fn checked(self, on: bool) -> Self
Draws a checkmark beside the row (and sets the platform’s check state where a host renders the menu).
pub fn id(self, id: impl Into<Value>) -> Self
pub fn accel(self, a: impl Into<String>) -> Self
Sourcepub fn accel_text(&self) -> Option<&str>
pub fn accel_text(&self) -> Option<&str>
The accelerator the row declares: its own, or the role’s. None is
a row with neither, which is every Custom one the app did not
spell a shortcut for. As declared — Self::accel_label is what is
drawn.
Sourcepub fn accel_label(&self) -> Option<Cow<'_, str>>
pub fn accel_label(&self) -> Option<Cow<'_, str>>
What the row draws on its right: Self::accel_text in the
platform’s spelling when kui can parse it ("mod+shift+n" reads
⇧⌘N on a Mac and Ctrl+Shift+N elsewhere), and exactly as
written when it cannot ("gd", an app’s own hint). See
Accel::label.
Sourcepub fn selectable(&self) -> bool
pub fn selectable(&self) -> bool
Whether the row takes focus and can be chosen — or, for a row with a submenu, opened.