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
- User taps on either label
- Label emits its configured event (bubbles to parent via bus)
- Toggle intercepts this event in its handle_event()
- Toggle updates internal state (flips enabled)
- Toggle updates the SelectionBox child to reposition
- Toggle triggers a re-render
- 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 viewrect- The rectangular bounds of the togglechildren- Contains 4 children: [Label, Filler, Label, SelectionBox]enabled- true = first option selected, false = second option selectedevent- Event to emit and intercept when togglingleft_label_index- Index of left label in children vecright_label_index- Index of right label in children vecselection_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: i32Implementations§
Source§impl Toggle
impl Toggle
Sourcepub fn new(
rect: Rectangle,
text_enabled: &str,
text_disabled: &str,
enabled: bool,
event: Event,
fonts: &mut Fonts,
align: Align,
) -> Toggle
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 toggletext_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 toggledalign- Alignment to apply to the right label
§Returns
A new Toggle instance with two labels separated by a vertical line, right-aligned
fn request_rerender(&mut self, rq: &mut RenderQueue)
fn update_selection_box(&mut self, rq: &mut RenderQueue)
Trait Implementations§
Source§impl View for Toggle
impl View for Toggle
fn handle_event( &mut self, evt: &Event, _hub: &Hub, bus: &mut Bus, rq: &mut RenderQueue, _context: &mut Context, ) -> bool
fn render( &self, _fb: &mut dyn Framebuffer, _rect: Rectangle, _fonts: &mut Fonts, )
fn rect(&self) -> &Rectangle
fn rect_mut(&mut self) -> &mut Rectangle
fn children(&self) -> &Vec<Box<dyn View>>
fn children_mut(&mut self) -> &mut Vec<Box<dyn View>>
fn id(&self) -> Id
fn render_rect(&self, _rect: &Rectangle) -> Rectangle
fn resize( &mut self, rect: Rectangle, _hub: &Hub, _rq: &mut RenderQueue, _context: &mut Context, )
fn child(&self, index: usize) -> &dyn View
fn child_mut(&mut self, index: usize) -> &mut dyn View
fn len(&self) -> usize
fn might_skip(&self, _evt: &Event) -> bool
fn might_rotate(&self) -> bool
fn is_background(&self) -> bool
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> 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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.