Skip to main content

MenuItem

Struct MenuItem 

Source
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: String

What the row reads. Empty takes the role’s default wording.

§role: MenuRole§enabled: bool

A 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: bool

Drawn 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.

§submenu: Vec<MenuItem>

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

Source

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.

Source

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).

Source

pub fn from_value(v: &Value) -> Result<MenuItem, 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.

Source

pub fn list_from_value(v: &Value) -> Result<Vec<MenuItem>, String>

A menu’s rows from plain data: a list of Self::from_value maps.

Source

pub fn stray_keys(rows: &Value) -> Vec<String>

The keys of rows — a list of row maps, as Self::list_from_value takes — that no row reads, a submenu’s rows’ included: what a binding raises crate::diag::unknown_menu_item_key for. A check of the outer rows alone let {label, disabled: true} inside a submenu be a row that is enabled (backlog RG150).

Source

pub fn stray_option_keys(options: &Value) -> Vec<String>

Self::stray_keys for a select’s options: items is one of them, since an option is chosen and never opens anything — Self::options_from_value leaves it out.

Source

pub fn options_from_value(v: &Value) -> Result<Vec<MenuItem>, 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 option’s items are left out (Self::stray_option_keys reports them): an option is chosen, never opened. 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.

Source

pub fn new(label: impl Into<String>) -> MenuItem

An item of the app’s own, by label.

Source

pub fn role(role: MenuRole) -> MenuItem

One of the standard items, with the role’s own wording.

Source

pub fn submenu(label: impl Into<String>, items: Vec<MenuItem>) -> MenuItem

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). Its rows want an id each: a row without one posts its label, which a row of the same name in another submenu posts too.

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());
Source

pub fn has_submenu(&self) -> bool

Whether the row opens a menu rather than being chosen (the submenu field). A row declared with an empty submenu is an ordinary row.

Source

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.

Source

pub fn separator() -> MenuItem

Source

pub fn enabled(self, on: bool) -> MenuItem

Source

pub fn checked(self, on: bool) -> MenuItem

Draws a checkmark beside the row (and sets the platform’s check state where a host renders the menu).

Source

pub fn id(self, id: impl Into<Value>) -> MenuItem

Source

pub fn accel(self, a: impl Into<String>) -> MenuItem

Source

pub fn text(&self) -> &str

What the row reads: its own label, or the role’s.

Source

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.

Source

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.

Source

pub fn selectable(&self) -> bool

Whether the row takes focus and can be chosen — or, for a row with a submenu, opened.

Trait Implementations§

Source§

impl Clone for MenuItem

Source§

fn clone(&self) -> MenuItem

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MenuItem

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for MenuItem

Source§

fn eq(&self, other: &MenuItem) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for MenuItem

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more