Skip to main content

ToggleGroup

Struct ToggleGroup 

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

A group of pressable toggle buttons for selection (shadcn/ui Toggle Group)

Supports single selection (radio-like) or multiple selection (checkbox-like).

§Example

// Single selection — clicking one deselects the others
let mut selected = vec![true, false, false];
ToggleGroup::new(ToggleGroupType::Single)
    .variant(ToggleGroupVariant::Outline)
    .show(ui, &["Bold", "Italic", "Underline"], &mut selected);

// Multiple selection — each item toggles independently
let mut selected = vec![false, false, false];
ToggleGroup::new(ToggleGroupType::Multiple)
    .show(ui, &["Bold", "Italic", "Underline"], &mut selected);

Implementations§

Source§

impl ToggleGroup

Source

pub const fn new(group_type: ToggleGroupType) -> Self

Create a new toggle group

Source

pub fn id(self, id: impl Into<Id>) -> Self

Set ID for state persistence

Source

pub const fn variant(self, variant: ToggleGroupVariant) -> Self

Set the visual variant

Source

pub const fn size(self, size: ToggleGroupSize) -> Self

Set the size

Source

pub const fn spacing(self, spacing: f32) -> Self

Set spacing between items (0 = joined, >0 = separated)

Source

pub const fn padding(self, padding: f32) -> Self

Override horizontal padding around each item’s text. When set, this takes precedence over the size-based default padding.

Source

pub const fn vertical(self, vertical: bool) -> Self

Set vertical orientation

Source

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

Set disabled state

Source

pub const fn item_width(self, width: f32) -> Self

Set explicit uniform item width.

Required when using show_ui for proper layout. For text-based show, items auto-size to the widest label.

Source

pub fn show( self, ui: &mut Ui, items: &[&str], selected: &mut Vec<bool>, ) -> ToggleGroupResponse

Show the toggle group

selected is a bool per item. Will be resized to match items.len().

  • Single: clicking an item deselects all others (radio behavior)
  • Multiple: each item toggles independently
Source

pub fn show_ui( self, ui: &mut Ui, count: usize, selected: &mut Vec<bool>, render_item: impl Fn(usize, &mut Ui, &ContentContext), ) -> ToggleGroupResponse

Show the toggle group with custom content for each item.

The closure receives the item index, a &mut Ui, and a ContentContext. Use item_width to set uniform item width. If not set, items default to square (height x height).

§Example
let mut selected = vec![false, false, false];
ToggleGroup::new(ToggleGroupType::Single)
    .item_width(40.0)
    .show_ui(ui, 3, &mut selected, |index, ui, ctx| {
        // Render icon for item `index` using ctx.color
    });

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