pub struct TabBarState { /* private fields */ }Expand description
State for a TabBar component.
Tracks the list of tabs, the active tab, scroll offset for overflow, an optional maximum tab width, and focus/disabled state.
§Example
use envision::component::{Tab, TabBarState};
let tabs = vec![
Tab::new("a", "Alpha"),
Tab::new("b", "Beta"),
Tab::new("c", "Gamma"),
];
let state = TabBarState::new(tabs);
assert_eq!(state.len(), 3);
assert_eq!(state.active_index(), Some(0));Implementations§
Source§impl TabBarState
impl TabBarState
Sourcepub fn new(tabs: Vec<Tab>) -> Self
pub fn new(tabs: Vec<Tab>) -> Self
Creates a new tab bar state with the first tab active.
If tabs is empty the active index is None.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![
Tab::new("1", "One"),
Tab::new("2", "Two"),
]);
assert_eq!(state.active_index(), Some(0));
assert_eq!(state.len(), 2);Sourcepub fn with_active(tabs: Vec<Tab>, active: usize) -> Self
pub fn with_active(tabs: Vec<Tab>, active: usize) -> Self
Creates a tab bar state with a specific tab active.
The index is clamped to the valid range. Empty tabs yield None active.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::with_active(
vec![Tab::new("a", "A"), Tab::new("b", "B"), Tab::new("c", "C")],
1,
);
assert_eq!(state.active_index(), Some(1));Sourcepub fn with_max_tab_width(self, max: Option<usize>) -> Self
pub fn with_max_tab_width(self, max: Option<usize>) -> Self
Sets the max tab width (builder).
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![Tab::new("a", "Alpha")])
.with_max_tab_width(Some(20));
assert_eq!(state.max_tab_width(), Some(20));Sourcepub fn tabs(&self) -> &[Tab]
pub fn tabs(&self) -> &[Tab]
Returns the tabs.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![
Tab::new("a", "Alpha"),
Tab::new("b", "Beta"),
]);
assert_eq!(state.tabs().len(), 2);
assert_eq!(state.tabs()[0].label(), "Alpha");Sourcepub fn tabs_mut(&mut self) -> &mut [Tab]
pub fn tabs_mut(&mut self) -> &mut [Tab]
Returns a mutable reference to the tabs.
§Example
use envision::component::{Tab, TabBarState};
let mut state = TabBarState::new(vec![Tab::new("a", "Alpha")]);
state.tabs_mut()[0].set_label("Renamed");
assert_eq!(state.tabs()[0].label(), "Renamed");Sourcepub fn active_index(&self) -> Option<usize>
pub fn active_index(&self) -> Option<usize>
Returns the currently active tab index, or None if empty.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![Tab::new("a", "A"), Tab::new("b", "B")]);
assert_eq!(state.active_index(), Some(0));
let empty = TabBarState::new(vec![]);
assert_eq!(empty.active_index(), None);Sourcepub fn active(&self) -> Option<usize>
pub fn active(&self) -> Option<usize>
Returns the active tab index, or None if the tab bar is empty.
This is the getter counterpart to set_active.
§Example
use envision::component::{Tab, TabBarState};
let mut state = TabBarState::new(vec![
Tab::new("a", "A"),
Tab::new("b", "B"),
]);
assert_eq!(state.active(), Some(0));
state.set_active(Some(1));
assert_eq!(state.active(), Some(1));
state.set_active(None);
assert_eq!(state.active(), None);Sourcepub fn active_tab(&self) -> Option<&Tab>
pub fn active_tab(&self) -> Option<&Tab>
Returns the currently active tab, or None if empty.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![Tab::new("a", "Alpha")]);
assert_eq!(state.active_tab().unwrap().label(), "Alpha");Sourcepub fn active_tab_mut(&mut self) -> Option<&mut Tab>
pub fn active_tab_mut(&mut self) -> Option<&mut Tab>
Returns a mutable reference to the active tab.
§Example
use envision::component::{Tab, TabBarState};
let mut state = TabBarState::new(vec![Tab::new("a", "Alpha")]);
state.active_tab_mut().unwrap().set_modified(true);
assert!(state.active_tab().unwrap().modified());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of tabs.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![Tab::new("a", "A"), Tab::new("b", "B")]);
assert_eq!(state.len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if there are no tabs.
§Example
use envision::component::TabBarState;
let state = TabBarState::new(vec![]);
assert!(state.is_empty());Sourcepub fn scroll_offset(&self) -> usize
pub fn scroll_offset(&self) -> usize
Returns the scroll offset.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![Tab::new("a", "A")]);
assert_eq!(state.scroll_offset(), 0);Sourcepub fn max_tab_width(&self) -> Option<usize>
pub fn max_tab_width(&self) -> Option<usize>
Returns the maximum tab width, if set.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![Tab::new("a", "A")]);
assert_eq!(state.max_tab_width(), None);
let state = state.with_max_tab_width(Some(20));
assert_eq!(state.max_tab_width(), Some(20));Sourcepub fn set_active(&mut self, index: Option<usize>)
pub fn set_active(&mut self, index: Option<usize>)
Sets the active tab by index.
None clears the selection. Some(i) is clamped to the valid range.
§Example
use envision::component::{Tab, TabBarState};
let mut state = TabBarState::new(vec![
Tab::new("a", "A"),
Tab::new("b", "B"),
]);
state.set_active(Some(1));
assert_eq!(state.active_index(), Some(1));
state.set_active(None);
assert_eq!(state.active_index(), None);Sourcepub fn set_scroll_offset(&mut self, offset: usize)
pub fn set_scroll_offset(&mut self, offset: usize)
Sets the scroll offset.
§Example
use envision::component::{Tab, TabBarState};
let mut state = TabBarState::new(vec![Tab::new("a", "A")]);
state.set_scroll_offset(5);
assert_eq!(state.scroll_offset(), 5);Sourcepub fn set_max_tab_width(&mut self, max: Option<usize>)
pub fn set_max_tab_width(&mut self, max: Option<usize>)
Sets the max tab width.
§Example
use envision::component::{Tab, TabBarState};
let mut state = TabBarState::new(vec![Tab::new("a", "A")]);
state.set_max_tab_width(Some(15));
assert_eq!(state.max_tab_width(), Some(15));Sourcepub fn update_tab(&mut self, index: usize, f: impl FnOnce(&mut Tab))
pub fn update_tab(&mut self, index: usize, f: impl FnOnce(&mut Tab))
Updates a tab at the given index via a closure.
No-ops if the index is out of bounds. This is safe because it does not change the number of tabs or their positions, so the active index remains valid.
§Example
use envision::component::{Tab, TabBarState};
let mut state = TabBarState::new(vec![
Tab::new("a", "Alpha"),
Tab::new("b", "Beta"),
]);
state.update_tab(1, |tab| tab.set_modified(true));
assert!(state.tabs()[1].modified());Sourcepub fn set_tabs(&mut self, tabs: Vec<Tab>)
pub fn set_tabs(&mut self, tabs: Vec<Tab>)
Replaces all tabs, clamping or clearing the active index.
§Example
use envision::component::{Tab, TabBarState};
let mut state = TabBarState::new(vec![Tab::new("a", "A")]);
state.set_tabs(vec![Tab::new("x", "X"), Tab::new("y", "Y")]);
assert_eq!(state.len(), 2);
assert_eq!(state.tabs()[0].label(), "X");Sourcepub fn find_tab_by_id(&self, id: &str) -> Option<(usize, &Tab)>
pub fn find_tab_by_id(&self, id: &str) -> Option<(usize, &Tab)>
Returns a tab by its id, if present.
§Example
use envision::component::{Tab, TabBarState};
let state = TabBarState::new(vec![
Tab::new("file1", "main.rs"),
Tab::new("file2", "lib.rs"),
]);
let (index, tab) = state.find_tab_by_id("file2").unwrap();
assert_eq!(index, 1);
assert_eq!(tab.label(), "lib.rs");
assert!(state.find_tab_by_id("missing").is_none());Sourcepub fn update(&mut self, msg: TabBarMessage) -> Option<TabBarOutput>
pub fn update(&mut self, msg: TabBarMessage) -> Option<TabBarOutput>
Updates the tab bar state with a message, returning any output.
§Example
use envision::component::{Tab, TabBarState, TabBarMessage, TabBarOutput};
let mut state = TabBarState::new(vec![
Tab::new("a", "A"),
Tab::new("b", "B"),
]);
let output = state.update(TabBarMessage::NextTab);
assert_eq!(output, Some(TabBarOutput::TabSelected(1)));
assert_eq!(state.active_index(), Some(1));Trait Implementations§
Source§impl Clone for TabBarState
impl Clone for TabBarState
Source§fn clone(&self) -> TabBarState
fn clone(&self) -> TabBarState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TabBarState
impl Debug for TabBarState
Source§impl Default for TabBarState
impl Default for TabBarState
Source§fn default() -> TabBarState
fn default() -> TabBarState
Source§impl<'de> Deserialize<'de> for TabBarState
impl<'de> Deserialize<'de> for TabBarState
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>,
Source§impl PartialEq for TabBarState
impl PartialEq for TabBarState
Auto Trait Implementations§
impl Freeze for TabBarState
impl RefUnwindSafe for TabBarState
impl Send for TabBarState
impl Sync for TabBarState
impl Unpin for TabBarState
impl UnsafeUnpin for TabBarState
impl UnwindSafe for TabBarState
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