pub struct MenuState { /* private fields */ }Expand description
State for a Menu component.
Implementations§
Source§impl MenuState
impl MenuState
Sourcepub fn new(items: Vec<MenuItem>) -> Self
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);Sourcepub fn items(&self) -> &[MenuItem]
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");Sourcepub fn set_items(&mut self, items: Vec<MenuItem>)
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.
Sourcepub fn add_item(&mut self, item: MenuItem)
pub fn add_item(&mut self, item: MenuItem)
Adds a menu item.
If this is the first item, it becomes selected.
Sourcepub fn remove_item(&mut self, index: usize)
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.
Sourcepub fn selected_index(&self) -> Option<usize>
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);Sourcepub fn set_selected(&mut self, index: usize)
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.
Sourcepub fn with_selected(self, index: usize) -> Self
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));Sourcepub fn selected_item(&self) -> Option<&MenuItem>
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");Sourcepub fn is_focused(&self) -> bool
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());Sourcepub fn set_focused(&mut self, focused: bool)
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());Sourcepub fn is_disabled(&self) -> bool
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());Sourcepub fn set_disabled(&mut self, disabled: bool)
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());Sourcepub fn with_disabled(self, disabled: bool) -> Self
pub fn with_disabled(self, disabled: bool) -> Self
Sets the disabled state using builder pattern.
Sourcepub fn handle_event(&self, event: &Event) -> Option<MenuMessage>
pub fn handle_event(&self, event: &Event) -> Option<MenuMessage>
Maps an input event to a menu message.
Sourcepub fn dispatch_event(&mut self, event: &Event) -> Option<MenuOutput>
pub fn dispatch_event(&mut self, event: &Event) -> Option<MenuOutput>
Dispatches an event, updating state and returning any output.
Sourcepub fn update(&mut self, msg: MenuMessage) -> Option<MenuOutput>
pub fn update(&mut self, msg: MenuMessage) -> Option<MenuOutput>
Updates the menu state with a message, returning any output.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for MenuState
impl<'de> Deserialize<'de> for MenuState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl StructuralPartialEq for MenuState
Auto Trait Implementations§
impl Freeze for MenuState
impl RefUnwindSafe for MenuState
impl Send for MenuState
impl Sync for MenuState
impl Unpin for MenuState
impl UnsafeUnpin for MenuState
impl UnwindSafe for MenuState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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