Skip to main content

TabBarState

Struct TabBarState 

Source
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

Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Source§

fn clone(&self) -> TabBarState

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 TabBarState

Source§

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

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

impl Default for TabBarState

Source§

fn default() -> TabBarState

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

impl<'de> Deserialize<'de> for TabBarState

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 TabBarState

Source§

fn eq(&self, other: &Self) -> 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 TabBarState

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

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>,