Skip to main content

MenuModel

Struct MenuModel 

Source
pub struct MenuModel { /* private fields */ }
Expand description

A declarative menu tree shared by the in-window MenuBar and the native OS menu bar. Cloneable by handle (Rc inside); a clone shares the same nodes and version signal, so mutating one updates every view.

Implementations§

Source§

impl MenuModel

Source

pub fn new() -> Self

An empty model.

Source

pub fn menu( self, title: impl Into<LocalizedString>, build: impl FnOnce(MenuItems) -> MenuItems, ) -> Self

Append a top-level menu with the given title and contents (auto id).

Source

pub fn menu_with_id( self, id: MenuItemId, title: impl Into<LocalizedString>, build: impl FnOnce(MenuItems) -> MenuItems, ) -> Self

Append a top-level menu with a caller-supplied id, so it can be addressed later by push_item / remove.

Source

pub fn standard(self, role: StandardMenuRole) -> Self

Append a platform-standard top-level menu (macOS App / Window / Help) with default (English) labels. Use standard_menu to supply localized labels.

Source

pub fn standard_menu(self, menu: StandardMenu) -> Self

Append a platform-standard top-level menu with localized labels.

Source

pub fn version(&self) -> Signal<u64>

A Signal<u64> bumped whenever the tree’s structure changes. The native bridge re-installs the menu on a bump; per-item state changes go through the finer-grained update_item path instead.

Source

pub fn nodes(&self) -> Ref<'_, Vec<MenuNode>>

Borrow the top-level nodes.

Source

pub fn modify(&self, f: impl FnOnce(&mut Vec<MenuNode>))

Mutate the node tree directly, then bump version. The escape hatch for any structural change the typed helpers don’t cover (reorder, retitle, bulk edits). MenuNode / MenuEntry are public, so the closure can build whatever it needs.

Source

pub fn push_menu( &self, title: impl Into<LocalizedString>, build: impl FnOnce(MenuItems) -> MenuItems, ) -> MenuItemId

Append a top-level menu at runtime, returning its id. Mirrors menu but takes &self.

Source

pub fn push_item(&self, into: MenuItemId, entry: MenuEntry) -> bool

Append entry to the submenu identified by into (a top-level menu or nested submenu id). Returns true if the submenu was found.

Source

pub fn push_separator(&self, into: MenuItemId) -> bool

Append a separator to the submenu identified by into. Returns true if the submenu was found.

Source

pub fn insert_menu_at( &self, index: usize, id: MenuItemId, title: impl Into<LocalizedString>, build: impl FnOnce(MenuItems) -> MenuItems, )

Insert a top-level menu at index, under a caller-supplied id.

push_menu appends, which puts a menu after Help — fine for something added once at startup, wrong for a menu that comes and goes, since a writer looking for it needs it in the same place every time. The id is the caller’s for the same reason: a menu that will be removed again has to be nameable before it exists.

index is clamped, so a model that has since grown or shrunk cannot panic a caller holding a stale position.

Source

pub fn contains(&self, id: MenuItemId) -> bool

Whether a node with this id is anywhere in the tree.

The companion to remove for callers that add and remove the same node as state changes: without it, “is it already there?” can only be answered by removing it and seeing what comes back, which bumps the version and re-installs the native menu for nothing.

Source

pub fn remove(&self, id: MenuItemId) -> bool

Remove the item or submenu with the given id, anywhere in the tree. Returns true if a node was removed.

Trait Implementations§

Source§

impl Clone for MenuModel

Source§

fn clone(&self) -> MenuModel

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 Default for MenuModel

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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
§

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

§

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
§

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

§

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

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

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

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

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> NoneValue for T
where T: Default,

§

type NoneType = T

§

fn null_value() -> T

The none-equivalent value.
§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> Upcast<T> for T

§

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

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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