Fix format
This commit is contained in:
parent
e00fca6372
commit
2a5aa69024
|
@ -7,21 +7,21 @@ use dodrio::bumpalo;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
/// A toggler that can be toggled.
|
/// A toggler that can be toggled.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use iced_web::Toggler;
|
/// # use iced_web::Toggler;
|
||||||
///
|
///
|
||||||
/// pub enum Message {
|
/// pub enum Message {
|
||||||
/// TogglerToggled(bool),
|
/// TogglerToggled(bool),
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// let is_active = true;
|
/// let is_active = true;
|
||||||
///
|
///
|
||||||
/// Toggler::new(is_active, String::from("Toggle me!"), Message::TogglerToggled);
|
/// Toggler::new(is_active, String::from("Toggle me!"), Message::TogglerToggled);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Toggler<Message> {
|
pub struct Toggler<Message> {
|
||||||
is_active: bool,
|
is_active: bool,
|
||||||
|
@ -34,16 +34,20 @@ pub struct Toggler<Message> {
|
||||||
|
|
||||||
impl<Message> Toggler<Message> {
|
impl<Message> Toggler<Message> {
|
||||||
/// Creates a new [`Toggler`].
|
/// Creates a new [`Toggler`].
|
||||||
///
|
///
|
||||||
/// It expects:
|
/// It expects:
|
||||||
/// * a boolean describing whether the [`Toggler`] is active or not
|
/// * a boolean describing whether the [`Toggler`] is active or not
|
||||||
/// * An optional label for the [`Toggler`]
|
/// * An optional label for the [`Toggler`]
|
||||||
/// * a function that will be called when the [`Toggler`] is toggled. It
|
/// * a function that will be called when the [`Toggler`] is toggled. It
|
||||||
/// will receive the new state of the [`Toggler`] and must produce a
|
/// will receive the new state of the [`Toggler`] and must produce a
|
||||||
/// `Message`.
|
/// `Message`.
|
||||||
///
|
///
|
||||||
/// [`Toggler`]: struct.Toggler.html
|
/// [`Toggler`]: struct.Toggler.html
|
||||||
pub fn new<F>(is_active: bool, label: impl Into<Option<String>>, f: F) -> Self
|
pub fn new<F>(
|
||||||
|
is_active: bool,
|
||||||
|
label: impl Into<Option<String>>,
|
||||||
|
f: F,
|
||||||
|
) -> Self
|
||||||
where
|
where
|
||||||
F: 'static + Fn(bool) -> Message,
|
F: 'static + Fn(bool) -> Message,
|
||||||
{
|
{
|
||||||
|
@ -58,7 +62,7 @@ impl<Message> Toggler<Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the width of the [`Toggler`].
|
/// Sets the width of the [`Toggler`].
|
||||||
///
|
///
|
||||||
/// [`Toggler`]: struct.Toggler.html
|
/// [`Toggler`]: struct.Toggler.html
|
||||||
pub fn width(mut self, width: Length) -> Self {
|
pub fn width(mut self, width: Length) -> Self {
|
||||||
self.width = width;
|
self.width = width;
|
||||||
|
@ -66,7 +70,7 @@ impl<Message> Toggler<Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`Toggler`].
|
/// Sets the style of the [`Toggler`].
|
||||||
///
|
///
|
||||||
/// [`Toggler`]: struct.Toggler.html
|
/// [`Toggler`]: struct.Toggler.html
|
||||||
pub fn style(mut self, style: impl Into<Box<dyn StyleSheet>>) -> Self {
|
pub fn style(mut self, style: impl Into<Box<dyn StyleSheet>>) -> Self {
|
||||||
self.style = style.into();
|
self.style = style.into();
|
||||||
|
@ -74,7 +78,7 @@ impl<Message> Toggler<Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the id of the [`Toggler`].
|
/// Sets the id of the [`Toggler`].
|
||||||
///
|
///
|
||||||
/// [`Toggler`]: struct.Toggler.html
|
/// [`Toggler`]: struct.Toggler.html
|
||||||
pub fn id(mut self, id: impl Into<String>) -> Self {
|
pub fn id(mut self, id: impl Into<String>) -> Self {
|
||||||
self.id = Some(id.into());
|
self.id = Some(id.into());
|
||||||
|
@ -95,9 +99,10 @@ where
|
||||||
use dodrio::builder::*;
|
use dodrio::builder::*;
|
||||||
use dodrio::bumpalo::collections::String;
|
use dodrio::bumpalo::collections::String;
|
||||||
|
|
||||||
let toggler_label = &self.label.as_ref().map(|label| {
|
let toggler_label = &self
|
||||||
String::from_str_in(&label, bump).into_bump_str()
|
.label
|
||||||
});
|
.as_ref()
|
||||||
|
.map(|label| String::from_str_in(&label, bump).into_bump_str());
|
||||||
|
|
||||||
let event_bus = bus.clone();
|
let event_bus = bus.clone();
|
||||||
let on_toggle = self.on_toggle.clone();
|
let on_toggle = self.on_toggle.clone();
|
||||||
|
@ -125,9 +130,7 @@ where
|
||||||
})
|
})
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
let toggler = span(bump)
|
let toggler = span(bump).children(vec![span(bump).finish()]).finish();
|
||||||
.children(vec![span(bump).finish()])
|
|
||||||
.finish();
|
|
||||||
|
|
||||||
label
|
label
|
||||||
.attr(
|
.attr(
|
||||||
|
@ -140,7 +143,7 @@ where
|
||||||
bumpalo::format!(in bump, "width: {}; align-items: center", css::length(self.width))
|
bumpalo::format!(in bump, "width: {}; align-items: center", css::length(self.width))
|
||||||
.into_bump_str()
|
.into_bump_str()
|
||||||
)
|
)
|
||||||
.children(
|
.children(
|
||||||
if let Some(label) = toggler_label {
|
if let Some(label) = toggler_label {
|
||||||
vec![
|
vec![
|
||||||
text(label),
|
text(label),
|
||||||
|
@ -165,4 +168,4 @@ where
|
||||||
fn from(toggler: Toggler<Message>) -> Element<'a, Message> {
|
fn from(toggler: Toggler<Message>) -> Element<'a, Message> {
|
||||||
Element::new(toggler)
|
Element::new(toggler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue