iced-cube

Actions

Toggle

A button that stays pressed while an option is on.

use iced_cube::{lucide, toggle};
use iced_cube::primitives::toggle::{Size, Variant};
API reference
Default: Toggles with icons that stay pressed while an option is on.Default: Toggles with icons that stay pressed while an option is on.
Static preview
Default. Toggles with icons that stay pressed while an option is on.

Usage

A toggle is a button with an on state, for options that sit among other controls rather than in a form: showing a grid, pinning a panel, bold text. The app holds the bool, passes it to .pressed, and flips it in the on_toggle message, as with a switch.

Use a switch for a setting that takes effect in a settings list, and a toggle where the option lives next to the content it changes. For an option with only an icon, such as bold in a compact toolbar, use a pressed icon button, which shows its label as a tooltip.

Shortcuts are app-wide, so each toggle needs its own chord. The keyboard example clears the default Space binding and binds Alt+B and Alt+I instead.

Examples

Variants

Default and outline toggles, each next to a disabled toggle that is on.

Variants: Default and outline toggles, each next to a disabled toggle that is on.Variants: Default and outline toggles, each next to a disabled toggle that is on.
Static preview

Keyboard shortcuts

Each toggle's keymap swaps the default Space for its own chord.

Keyboard shortcuts: Each toggle's keymap swaps the default Space for its own chord.Keyboard shortcuts: Each toggle's keymap swaps the default Space for its own chord.
Static preview

API

ItemDescription
toggle(label)Creates a toggle with a text label, switched off. It renders disabled until it has a message.
.pressed(bool)Whether the toggle is on. Pass the value your app holds.
.on_toggle(f) / .on_toggle_maybe(option)Sets the message for a click. The closure gets the new pressed state. None disables the toggle.
.icon(glyph)Adds a Lucide icon before the label.
.variant(Variant)Default, transparent until hovered, or Outline, with a border. Defaults to Default.
.size(Size)Sm, Md or Lg: 32, 36 or 40 pixels tall. Defaults to Md.
toggle::default_keymap()The default shortcuts as a Keymap<Action>. Give each toggle its own chord by clearing the map and binding one.
Action::Toggle.apply(pressed)The pressed state a click would send. Pass it to your on_toggle message.
colours(tokens, pressed, status) / style(tokens, variant, pressed, status)The resolved colours and iced style, for reuse in custom widgets.

Keyboard

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

KeysAction
SpaceToggle Switches the toggle on or off.
Esc