Skip to main content

MenuState

Struct MenuState 

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

State for a Menu component.

Implementations§

Source§

impl MenuState

Source

pub fn new(items: Vec<MenuItem>) -> Self

Creates a new menu with the given items.

§Example
use envision::component::{MenuState, MenuItem};

let state = MenuState::new(vec![
    MenuItem::new("File"),
    MenuItem::new("Edit"),
]);
assert_eq!(state.items().len(), 2);
Source

pub fn items(&self) -> &[MenuItem]

Returns the menu items.

§Examples
use envision::prelude::*;

let state = MenuState::new(vec![
    MenuItem::new("File"),
    MenuItem::new("Edit"),
]);
assert_eq!(state.items().len(), 2);
assert_eq!(state.items()[0].label(), "File");
Source

pub fn set_items(&mut self, items: Vec<MenuItem>)

Sets the menu items.

Resets selection to the first item if the current selection is out of bounds. Sets selection to None if the new items list is empty.

Source

pub fn add_item(&mut self, item: MenuItem)

Adds a menu item.

If this is the first item, it becomes selected.

Source

pub fn remove_item(&mut self, index: usize)

Removes a menu item by index.

If the index is out of bounds, this is a no-op. Adjusts the selection after removal:

  • If the removed item was the selected item, selects the previous item (or the first if at the beginning).
  • If the menu becomes empty, selection becomes None.
Source

pub fn selected_index(&self) -> Option<usize>

Returns the currently selected item index.

Returns None if the menu is empty.

§Examples
use envision::prelude::*;

let state = MenuState::new(vec![MenuItem::new("File"), MenuItem::new("Edit")]);
assert_eq!(state.selected_index(), Some(0));

let empty = MenuState::new(vec![]);
assert_eq!(empty.selected_index(), None);
Source

pub fn set_selected(&mut self, index: usize)

Sets the selected item index.

If the index is out of bounds, it will be clamped to the valid range. Has no effect on an empty menu.

Source

pub fn with_selected(self, index: usize) -> Self

Sets the selected item index (builder method).

If the index is out of bounds, it will be clamped to the valid range. Has no effect on an empty menu.

§Example
use envision::component::{MenuState, MenuItem};

let state = MenuState::new(vec![
    MenuItem::new("File"),
    MenuItem::new("Edit"),
    MenuItem::new("View"),
]).with_selected(1);
assert_eq!(state.selected_index(), Some(1));
Source

pub fn selected_item(&self) -> Option<&MenuItem>

Returns the currently selected item.

§Examples
use envision::prelude::*;

let state = MenuState::new(vec![MenuItem::new("File"), MenuItem::new("Edit")]);
assert_eq!(state.selected_item().unwrap().label(), "File");
Source

pub fn is_focused(&self) -> bool

Returns true if the menu is focused.

§Examples
use envision::prelude::*;

let state = MenuState::new(vec![MenuItem::new("File")]);
assert!(!state.is_focused());
Source

pub fn set_focused(&mut self, focused: bool)

Sets the focus state.

§Examples
use envision::prelude::*;

let mut state = MenuState::new(vec![MenuItem::new("File")]);
state.set_focused(true);
assert!(state.is_focused());
Source

pub fn is_disabled(&self) -> bool

Returns true if the menu is disabled.

§Examples
use envision::prelude::*;

let state = MenuState::new(vec![MenuItem::new("File")]);
assert!(!state.is_disabled());
Source

pub fn set_disabled(&mut self, disabled: bool)

Sets the disabled state.

§Examples
use envision::prelude::*;

let mut state = MenuState::new(vec![MenuItem::new("File")]);
state.set_disabled(true);
assert!(state.is_disabled());
Source

pub fn with_disabled(self, disabled: bool) -> Self

Sets the disabled state using builder pattern.

Source

pub fn handle_event(&self, event: &Event) -> Option<MenuMessage>

Maps an input event to a menu message.

Source

pub fn dispatch_event(&mut self, event: &Event) -> Option<MenuOutput>

Dispatches an event, updating state and returning any output.

Source

pub fn update(&mut self, msg: MenuMessage) -> Option<MenuOutput>

Updates the menu state with a message, returning any output.

Trait Implementations§

Source§

impl Clone for MenuState

Source§

fn clone(&self) -> MenuState

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for MenuState

Source§

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

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

impl Default for MenuState

Source§

fn default() -> MenuState

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

impl<'de> Deserialize<'de> for MenuState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for MenuState

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for MenuState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for MenuState

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

impl<T> StateExt for T

Source§

fn updated(self, cmd: Command<impl Clone>) -> UpdateResult<Self, impl Clone>

Updates self and returns a command.
Source§

fn unchanged(self) -> UpdateResult<Self, ()>

Returns self with no command.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,