Skip to main content

Switch

Struct Switch 

Source
pub struct Switch {
    pub on: bool,
    /* private fields */
}
Expand description

A toggle switch — the reference “premium widget” (Q0 quality-bar exemplar).

Ships beautiful with zero config and stays fully overridable. It layers every state a real toggle needs on top of one animated value:

  • Motion — the thumb slides (theme-eased) between off/on; because the thumb radius is derived from that same 0→1 position, it also grows as it settles on, Material-3 style, for free (one node scalar, see animate_to).
  • States — idle · hover · pressed (thumb stretches) · focus-visible (ring + state-layer halo) · disabled (dimmed, inert) · on/off.
  • Elevation — the thumb casts a soft drop shadow so it reads as a physical knob above the track.
  • Theming — track/thumb/halo all come from theme tokens (primary, surface_variant, on_primary, outline, shadow); light+dark adapt automatically. Any of them can be overridden per-instance.
  • A11yRole::Switch with an on/off value, a Tab-focusable node, and an optional label for screen readers.
  • Interactive-by-identity — always owns its hit region, wired or not, so a tap can never fall through to a pannable surface behind it.

Fields§

§on: bool

The current value.

Implementations§

Source§

impl Switch

Source

pub fn new(on: bool) -> Self

Source

pub fn on_change(self, f: impl Fn(bool) + Send + Sync + 'static) -> Self

Called with the NEW value when the switch is toggled (D094).

Source

pub fn disabled(self) -> Self

Non-interactive, dimmed, and inert (still absorbs the tap so nothing behind it reacts).

Source

pub fn disabled_if(self, c: bool) -> Self

Source

pub fn label(self, l: impl Into<String>) -> Self

Accessibility label announced by screen readers alongside the on/off value.

Source

pub fn size(self, width: f32, height: f32) -> Self

Override the track size (default 44×24). Proportions stay tasteful at any reasonable size.

Source

pub fn on_color(self, c: Color) -> Self

Override the on-track color (default: theme primary).

Source

pub fn off_color(self, c: Color) -> Self

Override the off-track color (default: theme surface_variant).

Source

pub fn thumb_color(self, c: Color) -> Self

Override the thumb color (default: theme on_primary on / outline off).

Trait Implementations§

Source§

impl Widget for Switch

Source§

fn layout(&self, ctx: &LayoutCtx<'_>) -> Size

Measure under ctx.constraints and return a size within them. Read more
Source§

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

Record draw commands for ctx.rect. Read more
Source§

fn children(&self) -> Children<'_>

Declare this widget’s children. Drives every default below.
Source§

fn flex_factor(&self) -> f32

Flex weight inside Row/Column. Wrappers are transparent by default.
Source§

fn into_element(self) -> Element
where Self: Sized + 'static,

Wrap this widget in an Element so it can be returned from Component::build().

Auto Trait Implementations§

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<W> FocusApi for W
where W: Widget + Send + Sync + 'static,

Source§

fn focus_node(self, node: FocusNode) -> WithFocus<Self>

Attach a focus node. This enables focus-ring rendering and explicit neighbor wiring.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<W> HeroApi for W
where W: Widget + Send + Sync + 'static,

Source§

fn hero_tag(self, tag: impl Into<String>) -> Hero<Self>

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<W> OverlayApi for W
where W: Widget + Send + Sync + 'static,

Source§

fn dropdown( self, open: Atom<bool>, content: impl Fn() -> BoxedWidget + Send + Sync + 'static, ) -> WithOverlay<Self>

Source§

fn sheet( self, open: Atom<bool>, content: impl Fn() -> BoxedWidget + Send + Sync + 'static, ) -> WithOverlay<Self>

Source§

fn dialog( self, open: Atom<bool>, content: impl Fn() -> BoxedWidget + Send + Sync + 'static, ) -> WithOverlay<Self>

Source§

fn rich_tooltip( self, content: impl Fn() -> BoxedWidget + Send + Sync + 'static, ) -> WithOverlay<Self>

Source§

fn toast( self, open: Atom<bool>, content: impl Fn() -> BoxedWidget + Send + Sync + 'static, ) -> WithOverlay<Self>

Source§

impl<W> PressApi for W
where W: Widget + Send + Sync + 'static,

Source§

fn on_press(self, f: impl Fn() + Send + Sync + 'static) -> Pressable<Self>

Source§

fn on_long_press( self, f: impl Fn() + Send + Sync + 'static, ) -> LongPressable<Self>

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<W> WidgetExt for W
where W: Widget + 'static,

Source§

fn tooltip(self, label: impl Into<String>) -> Tooltip

Show label while the pointer hovers this widget.