104 lines
2.8 KiB
Rust
104 lines
2.8 KiB
Rust
use crate::{style, Bus, Color, Element, Widget};
|
|
|
|
use dodrio::bumpalo;
|
|
|
|
/// A box that can be checked.
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```
|
|
/// # use iced_web::Checkbox;
|
|
///
|
|
/// pub enum Message {
|
|
/// CheckboxToggled(bool),
|
|
/// }
|
|
///
|
|
/// let is_checked = true;
|
|
///
|
|
/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled);
|
|
/// ```
|
|
///
|
|
/// 
|
|
#[allow(missing_debug_implementations)]
|
|
pub struct Checkbox<Message> {
|
|
is_checked: bool,
|
|
on_toggle: Box<dyn Fn(bool) -> Message>,
|
|
label: String,
|
|
label_color: Option<Color>,
|
|
}
|
|
|
|
impl<Message> Checkbox<Message> {
|
|
/// Creates a new [`Checkbox`].
|
|
///
|
|
/// It expects:
|
|
/// * a boolean describing whether the [`Checkbox`] is checked or not
|
|
/// * the label of the [`Checkbox`]
|
|
/// * a function that will be called when the [`Checkbox`] is toggled. It
|
|
/// will receive the new state of the [`Checkbox`] and must produce a
|
|
/// `Message`.
|
|
///
|
|
/// [`Checkbox`]: struct.Checkbox.html
|
|
pub fn new<F>(is_checked: bool, label: &str, f: F) -> Self
|
|
where
|
|
F: 'static + Fn(bool) -> Message,
|
|
{
|
|
Checkbox {
|
|
is_checked,
|
|
on_toggle: Box::new(f),
|
|
label: String::from(label),
|
|
label_color: None,
|
|
}
|
|
}
|
|
|
|
/// Sets the color of the label of the [`Checkbox`].
|
|
///
|
|
/// [`Checkbox`]: struct.Checkbox.html
|
|
pub fn label_color<C: Into<Color>>(mut self, color: C) -> Self {
|
|
self.label_color = Some(color.into());
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<Message> Widget<Message> for Checkbox<Message>
|
|
where
|
|
Message: 'static + Clone,
|
|
{
|
|
fn node<'b>(
|
|
&self,
|
|
bump: &'b bumpalo::Bump,
|
|
bus: &Bus<Message>,
|
|
_style_sheet: &mut style::Sheet<'b>,
|
|
) -> dodrio::Node<'b> {
|
|
use dodrio::builder::*;
|
|
|
|
let checkbox_label = bumpalo::format!(in bump, "{}", self.label);
|
|
|
|
let event_bus = bus.clone();
|
|
let msg = (self.on_toggle)(!self.is_checked);
|
|
|
|
// TODO: Complete styling
|
|
label(bump)
|
|
.children(vec![
|
|
input(bump)
|
|
.attr("type", "checkbox")
|
|
.bool_attr("checked", self.is_checked)
|
|
.on("click", move |root, vdom, _event| {
|
|
event_bus.publish(msg.clone(), root);
|
|
|
|
vdom.schedule_render();
|
|
})
|
|
.finish(),
|
|
text(checkbox_label.into_bump_str()),
|
|
])
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
impl<'a, Message> From<Checkbox<Message>> for Element<'a, Message>
|
|
where
|
|
Message: 'static + Clone,
|
|
{
|
|
fn from(checkbox: Checkbox<Message>) -> Element<'a, Message> {
|
|
Element::new(checkbox)
|
|
}
|
|
}
|