iced-cube

Actions

Toggle group

A row of toggles where one, or several, can be on.

use iced_cube::toggle_group;
use iced_cube::primitives::toggle_group::{Event, State, Variant, item};
API reference
Single choice: An outlined group where one alignment is always on.Single choice: An outlined group where one alignment is always on.
Static preview
Single choice. An outlined group where one alignment is always on.

Usage

The app owns a toggle_group::State and routes events into it, as with tabs. In single mode pressing an item turns the others off; pressing the one that is on turns it off too, unless the group is .required(true), which suits a choice that always has an answer, such as text alignment or a date range.

Multiple mode suits formatting options and filters, where each item is independent. selected() returns the items that are on in the order they are shown, whatever order they were pressed in.

Separate toggles wrap onto new lines on a narrow screen. An outlined group stays in one row, so keep it to two to four short items.

Use a single-choice group for a few short options shown side by side, and a select when there are more than four or five. When the options switch between whole views rather than settings, tabs are the clearer choice.

The arrow keys are bound by default because a single-choice group behaves like tabs. They are app-wide, so an app showing tabs, a slider or another group on the same screen should rebind one of them.

Examples

Multiple choice

Formatting toggles that switch on and off independently, with a disabled item.

Multiple choice: Formatting toggles that switch on and off independently, with a disabled item.Multiple choice: Formatting toggles that switch on and off independently, with a disabled item.
Static preview

Keyboard shortcuts

The arrow keys move a single-choice group's selection through the default keymap.

Keyboard shortcuts: The arrow keys move a single-choice group's selection through the default keymap.Keyboard shortcuts: The arrow keys move a single-choice group's selection through the default keymap.
Static preview

API

ItemDescription
State::single(items) / State::multiple(items)Holds the items, with nothing on. Single mode keeps at most one item on; multiple mode switches each one by itself.
.required(true)Single mode only: the last item on stays on, and the first enabled item starts on when nothing else is.
.with_selected(ids)Switches these items on, skipping unknown and disabled ones. In single mode the first that applies replaces the selection.
item(id, label)Creates an item. Add .icon(glyph) or .disabled(true).
state.update(Event)Toggle(id), Next or Previous. Returns the ids that are on, if anything changed.
state.selected() / state.selected_one() / state.is_selected(id)The ids that are on in item order, the first of them, and a check for one id.
state.set_disabled(id, bool)Enables or disables an item at runtime. Whether it is on stays as it was.
toggle_group(&state)Renders the items. They render disabled until .on_event is set.
.variant(Variant)Default, separate toggles with a small gap, or Outline, attached segments inside one border. Defaults to Default.
.size(Size)The toggle Size: Sm, Md or Lg. Defaults to Md.
.on_event(f)Maps item events to your message, such as Message::Group.
toggle_group::default_keymap()The default shortcuts as a Keymap<Action>. Bind, unbind or clear chords to change them.
action.event(&state)Turns a resolved Action (Next or Previous) into the Event to pass to state.update. Multiple-mode groups get None.

Keyboard

Default shortcuts. Every one can be changed or removed with a keymap; see Keyboard shortcuts.

KeysAction
ArrowRightNext Turns on the next enabled item of a single-choice group, wrapping.
ArrowLeftPrevious Turns on the previous enabled item of a single-choice group, wrapping.
Esc