2020-02-06 00:28:25 +01:00
|
|
|
//! Create choices using radio buttons.
|
2020-02-06 00:33:26 +01:00
|
|
|
use crate::{Bus, Css, Element, Widget};
|
|
|
|
|
|
|
|
|
|
pub use iced_style::radio::{Style, StyleSheet};
|
2019-09-15 18:53:13 +02:00
|
|
|
|
|
|
|
|
use dodrio::bumpalo;
|
2019-09-14 20:54:50 +02:00
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
/// A circular button representing a choice.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use iced_web::Radio;
|
|
|
|
|
///
|
|
|
|
|
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
/// pub enum Choice {
|
|
|
|
|
/// A,
|
|
|
|
|
/// B,
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// #[derive(Debug, Clone, Copy)]
|
|
|
|
|
/// pub enum Message {
|
|
|
|
|
/// RadioSelected(Choice),
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let selected_choice = Some(Choice::A);
|
|
|
|
|
///
|
|
|
|
|
/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected);
|
|
|
|
|
///
|
|
|
|
|
/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// 
|
2019-11-22 22:14:04 +01:00
|
|
|
#[allow(missing_debug_implementations)]
|
2019-11-21 13:47:20 +01:00
|
|
|
pub struct Radio<Message> {
|
|
|
|
|
is_selected: bool,
|
|
|
|
|
on_click: Message,
|
|
|
|
|
label: String,
|
2020-04-12 00:51:17 -06:00
|
|
|
id: String,
|
|
|
|
|
name: String,
|
2020-02-06 00:33:26 +01:00
|
|
|
style: Box<dyn StyleSheet>,
|
2019-11-21 13:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Message> Radio<Message> {
|
|
|
|
|
/// Creates a new [`Radio`] button.
|
|
|
|
|
///
|
|
|
|
|
/// It expects:
|
|
|
|
|
/// * the value related to the [`Radio`] button
|
|
|
|
|
/// * the label of the [`Radio`] button
|
|
|
|
|
/// * the current selected value
|
|
|
|
|
/// * a function that will be called when the [`Radio`] is selected. It
|
|
|
|
|
/// receives the value of the radio and must produce a `Message`.
|
|
|
|
|
///
|
|
|
|
|
/// [`Radio`]: struct.Radio.html
|
2020-04-14 07:41:35 +02:00
|
|
|
pub fn new<F, V>(
|
|
|
|
|
value: V,
|
|
|
|
|
label: impl Into<String>,
|
|
|
|
|
selected: Option<V>,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> Self
|
2019-11-21 13:47:20 +01:00
|
|
|
where
|
|
|
|
|
V: Eq + Copy,
|
|
|
|
|
F: 'static + Fn(V) -> Message,
|
|
|
|
|
{
|
|
|
|
|
Radio {
|
|
|
|
|
is_selected: Some(value) == selected,
|
|
|
|
|
on_click: f(value),
|
2020-04-14 07:41:35 +02:00
|
|
|
label: label.into(),
|
2020-04-12 00:51:17 -06:00
|
|
|
id: Default::default(),
|
|
|
|
|
name: Default::default(),
|
2020-02-06 00:33:26 +01:00
|
|
|
style: Default::default(),
|
2019-11-21 13:47:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 00:33:26 +01:00
|
|
|
/// Sets the style of the [`Radio`] button.
|
2019-11-21 13:47:20 +01:00
|
|
|
///
|
|
|
|
|
/// [`Radio`]: struct.Radio.html
|
2020-02-06 00:33:26 +01:00
|
|
|
pub fn style(mut self, style: impl Into<Box<dyn StyleSheet>>) -> Self {
|
|
|
|
|
self.style = style.into();
|
2019-11-21 13:47:20 +01:00
|
|
|
self
|
|
|
|
|
}
|
2020-04-12 00:51:17 -06:00
|
|
|
|
|
|
|
|
/// Sets the name attribute of the [`Radio`] button.
|
|
|
|
|
///
|
|
|
|
|
/// [`Radio`]: struct.Radio.html
|
|
|
|
|
pub fn name(mut self, name: impl Into<String>) -> Self {
|
|
|
|
|
self.name = name.into();
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the id of the [`Radio`] button.
|
|
|
|
|
///
|
|
|
|
|
/// [`Radio`]: struct.Radio.html
|
|
|
|
|
pub fn id(mut self, id: impl Into<String>) -> Self {
|
|
|
|
|
self.id = id.into();
|
|
|
|
|
self
|
|
|
|
|
}
|
2019-11-21 13:47:20 +01:00
|
|
|
}
|
2019-09-14 20:54:50 +02:00
|
|
|
|
2019-09-15 18:53:13 +02:00
|
|
|
impl<Message> Widget<Message> for Radio<Message>
|
|
|
|
|
where
|
2019-11-23 20:23:38 +01:00
|
|
|
Message: 'static + Clone,
|
2019-09-15 18:53:13 +02:00
|
|
|
{
|
|
|
|
|
fn node<'b>(
|
|
|
|
|
&self,
|
|
|
|
|
bump: &'b bumpalo::Bump,
|
|
|
|
|
bus: &Bus<Message>,
|
2020-02-05 00:23:22 +01:00
|
|
|
_style_sheet: &mut Css<'b>,
|
2019-09-15 18:53:13 +02:00
|
|
|
) -> dodrio::Node<'b> {
|
|
|
|
|
use dodrio::builder::*;
|
|
|
|
|
|
2020-04-12 00:51:17 -06:00
|
|
|
let radio_label =
|
|
|
|
|
bumpalo::format!(in bump, "{}", self.label).into_bump_str();
|
|
|
|
|
let radio_name =
|
|
|
|
|
bumpalo::format!(in bump, "{}", self.name).into_bump_str();
|
|
|
|
|
let radio_id = bumpalo::format!(in bump, "{}", self.id).into_bump_str();
|
2019-09-15 18:53:13 +02:00
|
|
|
|
|
|
|
|
let event_bus = bus.clone();
|
2019-11-23 20:23:38 +01:00
|
|
|
let on_click = self.on_click.clone();
|
2019-09-15 18:53:13 +02:00
|
|
|
|
2019-09-21 13:38:14 +02:00
|
|
|
// TODO: Complete styling
|
2019-09-15 18:53:13 +02:00
|
|
|
label(bump)
|
2019-11-23 20:23:38 +01:00
|
|
|
.attr("style", "display: block; font-size: 20px")
|
2020-04-12 00:51:17 -06:00
|
|
|
.attr("for", radio_id)
|
2019-09-15 18:53:13 +02:00
|
|
|
.children(vec![
|
|
|
|
|
input(bump)
|
|
|
|
|
.attr("type", "radio")
|
2020-04-12 00:51:17 -06:00
|
|
|
.attr("id", radio_id)
|
|
|
|
|
.attr("name", radio_name)
|
2019-11-23 20:23:38 +01:00
|
|
|
.attr("style", "margin-right: 10px")
|
2019-09-15 18:53:13 +02:00
|
|
|
.bool_attr("checked", self.is_selected)
|
2020-02-04 03:28:47 +01:00
|
|
|
.on("click", move |_root, _vdom, _event| {
|
|
|
|
|
event_bus.publish(on_click.clone());
|
2019-09-15 18:53:13 +02:00
|
|
|
})
|
|
|
|
|
.finish(),
|
2020-04-12 00:51:17 -06:00
|
|
|
text(radio_label),
|
2019-09-15 18:53:13 +02:00
|
|
|
])
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-14 20:54:50 +02:00
|
|
|
|
|
|
|
|
impl<'a, Message> From<Radio<Message>> for Element<'a, Message>
|
|
|
|
|
where
|
2019-11-23 20:23:38 +01:00
|
|
|
Message: 'static + Clone,
|
2019-09-14 20:54:50 +02:00
|
|
|
{
|
|
|
|
|
fn from(radio: Radio<Message>) -> Element<'a, Message> {
|
|
|
|
|
Element::new(radio)
|
|
|
|
|
}
|
|
|
|
|
}
|