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
impl ToggleGroup
Sourcepub const fn new(group_type: ToggleGroupType) -> Self
pub const fn new(group_type: ToggleGroupType) -> Self
Create a new toggle group
Sourcepub const fn variant(self, variant: ToggleGroupVariant) -> Self
pub const fn variant(self, variant: ToggleGroupVariant) -> Self
Set the visual variant
Sourcepub const fn size(self, size: ToggleGroupSize) -> Self
pub const fn size(self, size: ToggleGroupSize) -> Self
Set the size
Sourcepub const fn spacing(self, spacing: f32) -> Self
pub const fn spacing(self, spacing: f32) -> Self
Set spacing between items (0 = joined, >0 = separated)
Sourcepub const fn padding(self, padding: f32) -> Self
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.
Sourcepub const fn item_width(self, width: f32) -> Self
pub const fn item_width(self, width: f32) -> Self
Sourcepub fn show(
self,
ui: &mut Ui,
items: &[&str],
selected: &mut Vec<bool>,
) -> ToggleGroupResponse
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
Sourcepub fn show_ui(
self,
ui: &mut Ui,
count: usize,
selected: &mut Vec<bool>,
render_item: impl Fn(usize, &mut Ui, &ContentContext),
) -> ToggleGroupResponse
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§
impl Freeze for ToggleGroup
impl RefUnwindSafe for ToggleGroup
impl Send for ToggleGroup
impl Sync for ToggleGroup
impl Unpin for ToggleGroup
impl UnsafeUnpin for ToggleGroup
impl UnwindSafe for ToggleGroup
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
Mutably borrows from an owned value. Read more