pub struct Toggle<M> { /* private fields */ }Expand description
A switch: a track, a knob, and a boolean.
Reads better than a Checkbox at arm’s length, which is
what a wall panel is. Identical semantics otherwise, including the message
being a function of the new value:
enum Message { Muted(bool) }
Toggle::new("Mute", Message::Muted);§The knob slides
Flipping the switch requests animation for the 120 ms crossing and then the
widget stops asking — the bounded-transition contract described on
Widget::animate. Focus is not involved: the knob finishes its crossing
whether or not focus moves away mid-slide, which is what retired the
FocusLost-snaps-the-knob workaround this widget shipped with.
The animation is still a courtesy, not a mechanism: nothing about the value
depends on a frame arriving, and a Toggle on a tree whose application
never calls Ui::tick is merely a switch that jumps.
Implementations§
Source§impl<M> Toggle<M>
impl<M> Toggle<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 off switch 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 switch 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. The knob starts there rather than sliding into it.
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, and without animating.
Silent for the same reason Checkbox::set_checked is: the message
reports what a person did, and an application that assigned here and got
its own message back would either loop or have to guard against itself.
It does not animate because a setter has no way to request frames — the
tree grants them through an event or crate::Ui::request_animation,
and a widget cannot reach the tree from here. An application that wants
the slide can call request_animation itself; the default is the honest
jump.
Sourcepub fn preferred_width(&self, theme: &Theme, engine: &mut TextEngine) -> i32
pub fn preferred_width(&self, theme: &Theme, engine: &mut TextEngine) -> i32
Width this toggle needs for its track, its gap and its label.
Trait Implementations§
Source§impl<M> Describe for Toggle<M>
impl<M> Describe for Toggle<M>
Source§const DOC: &'static str = "The same on-or-off as a checkbox, shaped like a switch."
const DOC: &'static str = "The same on-or-off as a checkbox, shaped like a switch."
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 Toggle<M>
impl<M: 'static> Widget<M> for Toggle<M>
Source§fn snap(&mut self, now_ms: u64) -> Animation
fn snap(&mut self, now_ms: u64) -> Animation
The knob arrives at once. A switch is the clearest case for reduced motion: the slide was always a courtesy, and the value never depended on it.
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 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 moreSource§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 moreAuto Trait Implementations§
impl<M> Freeze for Toggle<M>
impl<M> RefUnwindSafe for Toggle<M>
impl<M> Send for Toggle<M>
impl<M> Sync for Toggle<M>
impl<M> Unpin for Toggle<M>
impl<M> UnsafeUnpin for Toggle<M>
impl<M> UnwindSafe for Toggle<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.