glass_switch

Function glass_switch 

Source
pub fn glass_switch(
    args: impl Into<GlassSwitchArgs>,
    state: Arc<RwLock<GlassSwitchState>>,
)
Expand description

A glass-like switch component for toggling a boolean state.

The glass_switch provides a visually appealing switch with a frosted glass effect. It animates smoothly between its “on” and “off” states and is fully customizable in terms of size, color, and border.

§Example

use std::sync::Arc;
use tessera_ui_basic_components::glass_switch::{glass_switch, GlassSwitchArgs, GlassSwitchArgsBuilder, GlassSwitchState};
use parking_lot::RwLock;

// In a real app, you would manage the state in shard state or elsewhere.
let state = Arc::new(RwLock::new(GlassSwitchState::new(false)));

glass_switch(
    GlassSwitchArgsBuilder::default()
        .on_toggle(Arc::new(|new_state| {
            // Update your application state here
            println!("Switch toggled to: {}", new_state);
        }))
        .build()
        .unwrap(),
    state.clone()
);

// Use the state to toggle the switch programmatically if needed.
state.write().toggle(); // Toggle the switch state
// or get the current on/off state
assert_eq!(state.read().is_checked(), true); // true here after toggle

§Arguments

  • args - An instance of GlassSwitchArgs which can be built using GlassSwitchArgsBuilder.
    • checked: A bool indicating the current state of the switch (true for on, false for off).
    • on_toggle: A callback Arc<dyn Fn(bool) + Send + Sync> that is called when the switch is clicked. It receives the new boolean state.
    • Other arguments for customization like width, height, track_on_color, track_off_color, etc. are also available.