Skip to main content

Checkbox

Struct Checkbox 

Source
pub struct Checkbox<S, M> { /* private fields */ }
Expand description

A boolean control: marker left, label right, the whole row one hit target.

A click anywhere on the row toggles, as do Enter and Space while focused. Hover and focus raise the row the way a small ghost button raises, so pointer and keyboard users both always see what they are on.

The checked state lives in app state and arrives through checked; without that binding the checkbox paints but is not focusable and answers no events. With the markers chosen freely, the same component serves as a switch ([ON]/[off]) or an ASCII toggle ([x]/[ ]) — see the checkbox demo.

Its markers answer to checked/unchecked where the selection controls say selected/unselected: a checkbox holds one row whose two states are its content, not a choice among many.

Implementations§

Source§

impl<S, M> Checkbox<S, M>

Source

pub fn new(label: impl Into<String>) -> Self

Construct a checkbox labelled label.

Source

pub fn checked_marker(self, marker: impl Into<String>) -> Self

The marker shown while checked.

Any string works, including multi-character pairs like [x]; the marker column takes the wider of the pair, so the label holds still as the state flips. Pair it with unchecked_marker so both states read as one control — [ON]/[off] is a switch, [x]/[ ] an ASCII checkbox.

Source

pub fn unchecked_marker(self, marker: impl Into<String>) -> Self

The marker shown while unchecked.

Source

pub fn checked( self, read: impl Fn(&S) -> bool + 'static, on_change: impl Fn(bool) -> M + 'static, ) -> Self

Bind the checked state and the message that flips it.

read runs against current app state during rendering and event handling. on_change receives the requested state — true after a toggle onto checked, false after one onto unchecked. Without this binding the checkbox is not focusable and answers no events.

Source

pub const fn disabled(self, disabled: bool) -> Self

Paint and answer muted, ignoring every event.

Source

pub fn style(self, style: impl Fn(&Theme) -> CheckboxStyle + 'static) -> Self

Supply exact colors, taking precedence over the theme.

Source

pub fn width(&self) -> u16

Columns this checkbox paints: the marker column (the wider of the two markers), one space, the label — the same in both states, so a row sized from this holds its label still. See CheckboxWidget::width.

Trait Implementations§

Source§

impl<S: 'static, M: 'static> Component<S, M> for Checkbox<S, M>

Source§

fn prepare(&mut self, state: &S)

Prepare this component from the state it is being declared with. Read more
Source§

fn declare(&mut self, _ctx: &mut DeclareCtx<'_, S, M>)

Declare the component: lay out its area, declare its descendants, and record whatever handle_event will need to read back. Read more
Source§

fn paint(&mut self, ctx: &mut PaintCtx<'_, S>)

Paint the component. ctx carries the paint surface, area, app state, theme, and interaction state. Read more
Source§

fn handle_event( &mut self, event: &Event, _state: &S, _ctx: &mut EventCtx<'_>, ) -> EventResult<M>

Offer this component an event. Read more
Source§

fn scope_options(&self) -> ScopeOptions

The scope this component opens around its descendants. Read once, before declare, so it cannot depend on paint.
Source§

fn interaction_area(&self, area: Rect) -> Rect

Return the area used for focus, hit-testing, and event routing. Read more
Source§

fn reveal_in_viewport( &mut self, _target: Rect, _state: &State, _ctx: &mut EventCtx<'_>, )

Bring target into view inside this component’s viewport. Read more
Source§

impl<S, M> Debug for Checkbox<S, M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: 'static, M: 'static> MeasuredComponent<S, M> for Checkbox<S, M>

Source§

fn measure(&self) -> Size

The component’s preferred width and height in terminal cells. Read more

Auto Trait Implementations§

§

impl<S, M> !RefUnwindSafe for Checkbox<S, M>

§

impl<S, M> !Send for Checkbox<S, M>

§

impl<S, M> !Sync for Checkbox<S, M>

§

impl<S, M> !UnwindSafe for Checkbox<S, M>

§

impl<S, M> Freeze for Checkbox<S, M>
where Option<(Rc<dyn Fn(&S) -> bool>, Rc<dyn Fn(bool) -> M>)>: Freeze,

§

impl<S, M> Unpin for Checkbox<S, M>
where Option<(Rc<dyn Fn(&S) -> bool>, Rc<dyn Fn(bool) -> M>)>: Unpin,

§

impl<S, M> UnsafeUnpin for Checkbox<S, M>
where Option<(Rc<dyn Fn(&S) -> bool>, Rc<dyn Fn(bool) -> M>)>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.