Skip to content

Checkbox ​

A labeled boolean control: the marker on the left, the label on the right, and the whole row as one hit target, so a click on the label checks the box exactly as a click on the marker does.

rust
use ratcn::{Checkbox, Theme};

// In draw(), declare one Checkbox per option:
ctx.component(
    "vim",
    Checkbox::new("Vim bindings").checked(|state| state.vim, Msg::SetVim),
    area,
);

The checked value is app-owned and arrives through .checked(read, on_change): read answers from app state each frame, and on_change receives the requested state after every toggle. Without the binding the checkbox paints but is not focusable and answers no events.

At rest a checkbox reads as text on the surface it sits on, with no chrome. Hover or focus lay the same quiet fill over the row that every other control uses, so both keyboard and pointer users can see what they are about to flip.

The markers are yours ​

The default pair is ■ / □, the boxes a multi-select List ticks its rows with. Because both markers are strings you choose, the same component covers every binary control:

rust
// An ASCII checkbox:
Checkbox::new("Telemetry")
    .checked_marker("[x]")
    .unchecked_marker("[ ]")

// A switch, where the words are the point:
Checkbox::new("Terminal bell")
    .checked_marker("[ON]")
    .unchecked_marker("[off]")

The marker column takes the wider of the pair, so an uneven pair like [ON]/[off] never moves the label as it flips. Checkbox::width() measures the whole row, the same in both states, for layouts that hug it, as the demo's rows do.

Two options are a Checkbox wearing its states as labels; three or more are a Cycle.

Disabled ​

.disabled(true) mutes the row, takes it out of Tab order, and ignores events. Pass the flag from app state, which is in scope while declaring.

Styling ​

Colors derive from the theme. To recolor one checkbox, pass .style(...) a closure that receives the active theme and returns a CheckboxStyle, usually built from CheckboxStyle::from_theme(theme).

Paint-only widget ​

CheckboxWidget draws one row without focus, events, or state:

rust
use ratcn::CheckboxWidget;

frame.render_widget(
    CheckboxWidget::new("Vim bindings", state.vim).themed(&theme),
    area,
);

You supply the interaction states with .focused(...), .hovered(...), and .disabled(...). .width() measures the columns it wants (marker column, space, label), the same in both states. Replace .themed(...) with .style(...) to supply exact colors.

Keyboard and mouse ​

InputDoes
Enter SpaceToggle the focused checkbox
Left click on the rowToggle the checkbox

Mouse input needs capture enabled in the host. See Mouse input.

Limits ​

  • A checkbox is one row. In a taller area only the first row paints and takes clicks.

Full API ​

See Checkbox, CheckboxWidget, and CheckboxStyle on docs.rs for the full API.

See also ​

Use Cycle when a setting has more than two options, or List with multi-selection when the user ticks any number of rows in a longer list.