pub struct Checkbox<M> { /* private fields */ }Expand description
A checkbox with an optional label beside it.
The message is a function of the new value rather than a fixed value, so an application matches on what the checkbox became rather than looking the widget up afterwards. An enum’s tuple variant already is such a function:
enum Message { Muted(bool) }
Checkbox::new("Mute", Message::Muted);A plain fn pointer rather than a closure, so this needs no allocation, no
M: Clone, and works in no_std.
§Why there is no indeterminate state
The third state only means anything for a checkbox that summarises other
checkboxes — a parent over a list of children — and neither the list nor the
hierarchy exists here yet. It is also additive when it does: HTML keeps
indeterminate as a property separate from checked precisely because it is
a way of drawing a checkbox rather than a third value it can hold. So this
stays a bool and gains a flag later, rather than becoming an enum everybody
has to match on for a case nothing can currently produce.
Implementations§
Source§impl<M> Checkbox<M>
impl<M> Checkbox<M>
Sourcepub fn new(label: impl Into<String>, message: fn(bool) -> M) -> Self
pub fn new(label: impl Into<String>, message: fn(bool) -> M) -> Self
An unchecked box whose message is built from the value it changes to.
Sourcepub fn inert(label: impl Into<String>) -> Self
pub fn inert(label: impl Into<String>) -> Self
A checkbox that emits nothing, for a value the application reads rather than reacts to.
Sourcepub fn with_checked(self, checked: bool) -> Self
pub fn with_checked(self, checked: bool) -> Self
Sets the initial value.
Sourcepub fn with_role(self, role: Role) -> Self
pub fn with_role(self, role: Role) -> Self
Sets the colour role of the filled box. The tick comes from the theme’s pairing, so it stays readable whichever role and theme are chosen.
Sourcepub fn with_style(self, style: TextStyle) -> Self
pub fn with_style(self, style: TextStyle) -> Self
Sets the label’s font and size.
Sourcepub fn set_checked(&mut self, checked: bool)
pub fn set_checked(&mut self, checked: bool)
Sets the value without emitting anything.
The message reports what a person did. An application that assigns here and then receives its own message back would either loop or have to guard against itself, which is the bug this rule exists to prevent.
Sourcepub fn set_style(&mut self, style: TextStyle)
pub fn set_style(&mut self, style: TextStyle)
Replaces the label’s font and size.
For an application that registers a font after building its tree, which is the ordinary case: the tree has to exist before anyone knows whether the font file was there.
Sourcepub fn preferred_width(&self, theme: &Theme, engine: &mut TextEngine) -> i32
pub fn preferred_width(&self, theme: &Theme, engine: &mut TextEngine) -> i32
Width this checkbox needs for its box, its gap and its label.
Takes the theme because the box is a theme metric — a touch theme’s box is 28 logical pixels where a mouse theme’s is 20 — and the engine because with a proportional font the label’s width is not the character count times anything.
Trait Implementations§
Source§impl<M> Describe for Checkbox<M>
impl<M> Describe for Checkbox<M>
Source§const DOC: &'static str = "A box and a tick: one thing that is either on or off."
const DOC: &'static str = "A box and a tick: one thing that is either on or off."
Source§const ICON: &'static Icon
const ICON: &'static Icon
Source§const PROPERTIES: &'static [Property]
const PROPERTIES: &'static [Property]
Source§impl<M: 'static> Widget<M> for Checkbox<M>
impl<M: 'static> Widget<M> for Checkbox<M>
Source§fn describe(&self) -> Option<&dyn DynDescribe>
fn describe(&self) -> Option<&dyn DynDescribe>
Source§fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>
fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>
describe.Source§fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured
fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured
Source§fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)
fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)
canvas, which is already clipped to this widget’s bounds
intersected with the damage region being repainted. Read moreSource§fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled
fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled
Source§fn accepts_pointer(&self) -> bool
fn accepts_pointer(&self) -> bool
true if the pointer can hit this widget. Read moreSource§fn preserves_focus(&self) -> bool
fn preserves_focus(&self) -> bool
true if a press on this widget should leave focus exactly where
it is. Read moreSource§fn animate(&mut self, now_ms: u64) -> Animation
fn animate(&mut self, now_ms: u64) -> Animation
EventCtx::request_animation — and stops being called
the moment it answers Wake::Never. Read moreAuto Trait Implementations§
impl<M> Freeze for Checkbox<M>
impl<M> RefUnwindSafe for Checkbox<M>
impl<M> Send for Checkbox<M>
impl<M> Sync for Checkbox<M>
impl<M> Unpin for Checkbox<M>
impl<M> UnsafeUnpin for Checkbox<M>
impl<M> UnwindSafe for Checkbox<M>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DynDescribe for Twhere
T: Describe,
impl<T> DynDescribe for Twhere
T: Describe,
Source§fn kind(&self) -> &'static str
fn kind(&self) -> &'static str
Describe::KIND.Source§fn properties(&self) -> &'static [Property]
fn properties(&self) -> &'static [Property]
Describe::PROPERTIES.Source§fn get_property(&self, name: &str) -> Option<Value>
fn get_property(&self, name: &str) -> Option<Value>
Describe::get.Source§fn set_property(
&mut self,
name: &str,
value: Value,
) -> Result<(), PropertyError>
fn set_property( &mut self, name: &str, value: Value, ) -> Result<(), PropertyError>
Describe::set.