Actions
Toggle
A button that stays pressed while an option is on.


use iced::widget::{column, row, text};
use iced::{Element, Length};
use iced_cube::lucide;
use iced_cube::primitives::toggle;
#[derive(Debug, Clone)]
pub enum Message {
Grid(bool),
Rulers(bool),
Snap(bool),
}
#[derive(Debug)]
pub struct Example {
grid: bool,
rulers: bool,
snap: bool,
}
impl Default for Example {
fn default() -> Self {
Self {
grid: true,
rulers: false,
snap: false,
}
}
}
impl Example {
pub fn update(&mut self, message: Message) {
match message {
Message::Grid(on) => self.grid = on,
Message::Rulers(on) => self.rulers = on,
Message::Snap(on) => self.snap = on,
}
}
pub fn view(&self) -> Element<'_, Message> {
let toggles = row![
toggle("Grid")
.icon(lucide!(Grid3x3))
.pressed(self.grid)
.on_toggle(Message::Grid),
toggle("Rulers")
.icon(lucide!(Ruler))
.pressed(self.rulers)
.on_toggle(Message::Rulers),
toggle("Snap to grid")
.icon(lucide!(Magnet))
.pressed(self.snap)
.on_toggle(Message::Snap),
]
.spacing(4)
.wrap();
let showing: Vec<&str> = [(self.grid, "grid"), (self.rulers, "rulers")]
.into_iter()
.filter_map(|(on, name)| on.then_some(name))
.collect();
let status = match showing.as_slice() {
[] => String::from("Showing the canvas only"),
names => format!("Showing the {}", names.join(" and ")),
};
column![toggles, text(status).size(14)]
.spacing(12)
.width(Length::Fill)
.max_width(340)
.into()
}
}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.


use iced::widget::{column, row, text};
use iced::{Alignment, Element};
use iced_cube::lucide;
use iced_cube::primitives::toggle::{Variant, toggle};
#[derive(Debug, Clone)]
pub enum Message {
Pinned(bool),
}
#[derive(Debug, Default)]
pub struct Example {
pinned: bool,
}
impl Example {
pub fn update(&mut self, message: Message) {
let Message::Pinned(pinned) = message;
self.pinned = pinned;
}
pub fn view(&self) -> Element<'_, Message> {
let line = |variant: Variant| {
row![
text(format!("{variant:?}")).size(14).width(64),
toggle("Pin")
.icon(lucide!(Pin))
.variant(variant)
.pressed(self.pinned)
.on_toggle(Message::Pinned),
toggle("Locked")
.icon(lucide!(Lock))
.variant(variant)
.pressed(true),
]
.spacing(8)
.align_y(Alignment::Center)
};
column![line(Variant::Default), line(Variant::Outline)]
.spacing(16)
.into()
}
}Keyboard shortcuts
Each toggle's keymap swaps the default Space for its own chord.


use iced::widget::{column, row, text};
use iced::{Element, Length, Subscription};
use iced_cube::keys::{self, Chord, Keymap};
use iced_cube::lucide;
use iced_cube::primitives::toggle::{self, Action, toggle};
#[derive(Debug, Clone)]
pub enum Message {
Bold(bool),
Italic(bool),
Key(keys::Event),
}
#[derive(Debug)]
pub struct Example {
bold: bool,
italic: bool,
bold_keys: Keymap<Action>,
italic_keys: Keymap<Action>,
}
impl Default for Example {
fn default() -> Self {
// Each toggle gets its own chord in place of the default Space.
let only = |chord: Chord| toggle::default_keymap().clear().bind(chord, Action::Toggle);
Self {
bold: false,
italic: false,
bold_keys: only(Chord::character('b').alt()),
italic_keys: only(Chord::character('i').alt()),
}
}
}
impl Example {
pub fn update(&mut self, message: Message) {
match message {
Message::Bold(on) => self.bold = on,
Message::Italic(on) => self.italic = on,
Message::Key(key) => {
if let Some(action) = self.bold_keys.resolve_event(&key) {
self.bold = action.apply(self.bold);
}
if let Some(action) = self.italic_keys.resolve_event(&key) {
self.italic = action.apply(self.italic);
}
}
}
}
pub fn view(&self) -> Element<'_, Message> {
column![
row![
toggle("Bold")
.icon(lucide!(Bold))
.pressed(self.bold)
.on_toggle(Message::Bold),
toggle("Italic")
.icon(lucide!(Italic))
.pressed(self.italic)
.on_toggle(Message::Italic),
]
.spacing(4),
text("Alt+B and Alt+I switch the toggles from anywhere.").size(14),
]
.spacing(12)
.width(Length::Fill)
.max_width(360)
.into()
}
pub fn subscription(&self) -> Subscription<Message> {
keys::subscription().map(Message::Key)
}
}API
| Item | Description |
|---|---|
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:: | The default shortcuts as a Keymap<Action>. Give each toggle its own chord by clearing the map and binding one. |
Action:: | 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.
| Keys | Action |
|---|---|
| Space | Toggle Switches the toggle on or off. |