Toggle

Struct Toggle 

Source
pub struct Toggle {
    id: Id,
    rect: Rectangle,
    children: Vec<Box<dyn View>>,
    enabled: bool,
    event: Event,
    left_label_index: usize,
    right_label_index: usize,
    selection_box_index: usize,
    left_text_width: i32,
    right_text_width: i32,
}
Expand description

A toggle component that displays two options side-by-side, separated by a vertical line.

The Toggle component provides a binary choice control where one option is highlighted with a minimal border box while the other appears without highlighting. Tapping either label toggles the state and emits a configured event.

§Implementation Note

Toggle uses a child view approach for the selection box. The SelectionBox is added as the 4th child and renders on top of the labels (due to z-order). When the toggle state changes, the SelectionBox is updated to reposition around the selected label.

§Visual Layout

┌─────────────────────────┐
│ ┌─────────┐ │           │
│ │Option A │ │ Option B  │ ← enabled = true (A selected)
│ └─────────┘ │           │
└─────────────────────────┘
     ↑             ↑
  Selected      Normal
  (border)   (no border)

§Event Flow

  1. User taps on either label
  2. Label emits its configured event (bubbles to parent via bus)
  3. Toggle intercepts this event in its handle_event()
  4. Toggle updates internal state (flips enabled)
  5. Toggle updates the SelectionBox child to reposition
  6. Toggle triggers a re-render
  7. Toggle re-emits the event to continue bubbling up

§Example

use cadmus_core::view::toggle::Toggle;
use cadmus_core::view::{Align, Event, ViewId, ToggleEvent};
use cadmus_core::font::Fonts;
use cadmus_core::rect;
use std::env;
use std::path::PathBuf;

let fonts = &mut Fonts::load_from(PathBuf::from(env::var("TEST_ROOT_DIR").unwrap())).unwrap();

let rect = rect![10, 100, 410, 160];
let wifi_toggle = Toggle::new(
    rect,
    "On",       // First option text
    "Off",      // Second option text
    true,       // Initial state (On selected)
    Event::NewToggle(ToggleEvent::View(ViewId::SettingsMenu)),
    fonts,
    Align::Right(10)
);

§Alignment Behavior

The right label uses the provided alignment, while the left label remains centered to avoid crowding the separator. This keeps the toggle right-aligned with other setting values while maintaining consistent padding to the edge.

§Fields

  • id - Unique identifier for this view
  • rect - The rectangular bounds of the toggle
  • children - Contains 4 children: [Label, Filler, Label, SelectionBox]
  • enabled - true = first option selected, false = second option selected
  • event - Event to emit and intercept when toggling
  • left_label_index - Index of left label in children vec
  • right_label_index - Index of right label in children vec
  • selection_box_index - Index of selection box in children vec

Fields§

§id: Id§rect: Rectangle§children: Vec<Box<dyn View>>§enabled: bool§event: Event§left_label_index: usize§right_label_index: usize§selection_box_index: usize§left_text_width: i32§right_text_width: i32

Implementations§

Source§

impl Toggle

Source

pub fn new( rect: Rectangle, text_enabled: &str, text_disabled: &str, enabled: bool, event: Event, fonts: &mut Fonts, align: Align, ) -> Toggle

Creates a new Toggle component.

§Arguments
  • rect - The rectangular bounds for the toggle
  • text_enabled - Text for the first option (shown with border when enabled=true)
  • text_disabled - Text for the second option (shown with border when enabled=false)
  • enabled - Initial state (true = first option selected)
  • event - Event to emit when toggled
  • align - Alignment to apply to the right label
§Returns

A new Toggle instance with two labels separated by a vertical line, right-aligned

Source

fn request_rerender(&mut self, rq: &mut RenderQueue)

Source

fn update_selection_box(&mut self, rq: &mut RenderQueue)

Trait Implementations§

Source§

impl View for Toggle

Source§

fn handle_event( &mut self, evt: &Event, _hub: &Hub, bus: &mut Bus, rq: &mut RenderQueue, _context: &mut Context, ) -> bool

Source§

fn render( &self, _fb: &mut dyn Framebuffer, _rect: Rectangle, _fonts: &mut Fonts, )

Source§

fn rect(&self) -> &Rectangle

Source§

fn rect_mut(&mut self) -> &mut Rectangle

Source§

fn children(&self) -> &Vec<Box<dyn View>>

Source§

fn children_mut(&mut self) -> &mut Vec<Box<dyn View>>

Source§

fn id(&self) -> Id

Source§

fn render_rect(&self, _rect: &Rectangle) -> Rectangle

Source§

fn resize( &mut self, rect: Rectangle, _hub: &Hub, _rq: &mut RenderQueue, _context: &mut Context, )

Source§

fn child(&self, index: usize) -> &dyn View

Source§

fn child_mut(&mut self, index: usize) -> &mut dyn View

Source§

fn len(&self) -> usize

Source§

fn might_skip(&self, _evt: &Event) -> bool

Source§

fn might_rotate(&self) -> bool

Source§

fn is_background(&self) -> bool

Source§

fn view_id(&self) -> Option<ViewId>

Auto Trait Implementations§

§

impl Freeze for Toggle

§

impl !RefUnwindSafe for Toggle

§

impl !Send for Toggle

§

impl !Sync for Toggle

§

impl Unpin for Toggle

§

impl !UnwindSafe for Toggle

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
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more