Change label of `Toggler` to optional

This commit is contained in:
Kaiden42 2020-09-24 16:31:39 +02:00 committed by Héctor Ramón
parent aa18a6e0d5
commit 7a626f3b7b
3 changed files with 53 additions and 32 deletions

View File

@ -33,7 +33,7 @@ where
bounds: Rectangle, bounds: Rectangle,
is_active: bool, is_active: bool,
is_mouse_over: bool, is_mouse_over: bool,
(label, _): Self::Output, label: Option<Self::Output>,
style_sheet: &Self::Style, style_sheet: &Self::Style,
) -> Self::Output { ) -> Self::Output {
let style = if is_mouse_over { let style = if is_mouse_over {
@ -82,7 +82,12 @@ where
( (
Primitive::Group { Primitive::Group {
primitives: vec![label, toggler_background, toggler_foreground], primitives: match label {
Some((l, _)) => {
vec![l, toggler_background, toggler_foreground]
}
None => vec![toggler_background, toggler_foreground],
},
}, },
if is_mouse_over { if is_mouse_over {
mouse::Interaction::Pointer mouse::Interaction::Pointer

View File

@ -299,7 +299,7 @@ impl toggler::Renderer for Null {
_bounds: Rectangle, _bounds: Rectangle,
_is_checked: bool, _is_checked: bool,
_is_mouse_over: bool, _is_mouse_over: bool,
_label: Self::Output, _label: Option<Self::Output>,
_style: &Self::Style, _style: &Self::Style,
) { ) {
} }

View File

@ -20,14 +20,14 @@ use crate::{
/// ///
/// let is_active = true; /// let is_active = true;
/// ///
/// Toggler::new(is_active, "Toggle me!", |b| Message::TogglerToggled(b)); /// Toggler::new(is_active, String::from("Toggle me!"), |b| Message::TogglerToggled(b));
/// ``` /// ```
/// ///
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Toggler<Message, Renderer: self::Renderer + text::Renderer> { pub struct Toggler<Message, Renderer: self::Renderer + text::Renderer> {
is_active: bool, is_active: bool,
on_toggle: Box<dyn Fn(bool) -> Message>, on_toggle: Box<dyn Fn(bool) -> Message>,
label: String, label: Option<String>,
width: Length, width: Length,
size: u16, size: u16,
text_size: Option<u16>, text_size: Option<u16>,
@ -44,13 +44,17 @@ impl<Message, Renderer: self::Renderer + text::Renderer>
/// ///
/// It expects: /// It expects:
/// * a boolean describing whether the [`Toggler`] is checked or not /// * a boolean describing whether the [`Toggler`] is checked or not
/// * the label of 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<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,
{ {
@ -143,25 +147,30 @@ where
renderer: &Renderer, renderer: &Renderer,
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
Row::<(), Renderer>::new() let mut row = Row::<(), Renderer>::new()
.width(self.width) .width(self.width)
.spacing(self.spacing) .spacing(self.spacing)
.align_items(Align::Center) .align_items(Align::Center);
.push(
Text::new(&self.label) if let Some(label) = &self.label {
row = row.push(
Text::new(label)
.horizontal_alignment( .horizontal_alignment(
self.text_align.unwrap_or(HorizontalAlignment::Left), self.text_align.unwrap_or(HorizontalAlignment::Left),
) )
.font(self.font) .font(self.font)
.width(self.width) .width(self.width)
.size(self.text_size.unwrap_or(renderer.default_size())), .size(self.text_size.unwrap_or(renderer.default_size())),
) );
.push( }
row = row.push(
Row::new() Row::new()
.width(Length::Units(2 * self.size)) .width(Length::Units(2 * self.size))
.height(Length::Units(self.size)), .height(Length::Units(self.size)),
) );
.layout(renderer, limits)
row.layout(renderer, limits)
} }
fn on_event( fn on_event(
@ -200,21 +209,28 @@ where
let bounds = layout.bounds(); let bounds = layout.bounds();
let mut children = layout.children(); let mut children = layout.children();
let label = match &self.label {
Some(label) => {
let label_layout = children.next().unwrap(); let label_layout = children.next().unwrap();
let toggler_layout = children.next().unwrap();
let toggler_bounds = toggler_layout.bounds();
let label = text::Renderer::draw( Some(text::Renderer::draw(
renderer, renderer,
defaults, defaults,
label_layout.bounds(), label_layout.bounds(),
&self.label, &label,
self.text_size.unwrap_or(renderer.default_size()), self.text_size.unwrap_or(renderer.default_size()),
self.font, self.font,
None, None,
self.text_align.unwrap_or(HorizontalAlignment::Left), self.text_align.unwrap_or(HorizontalAlignment::Left),
VerticalAlignment::Center, VerticalAlignment::Center,
); ))
}
None => None,
};
let toggler_layout = children.next().unwrap();
let toggler_bounds = toggler_layout.bounds();
let is_mouse_over = bounds.contains(cursor_position); let is_mouse_over = bounds.contains(cursor_position);
@ -267,7 +283,7 @@ pub trait Renderer: crate::Renderer {
bounds: Rectangle, bounds: Rectangle,
is_active: bool, is_active: bool,
is_mouse_over: bool, is_mouse_over: bool,
label: Self::Output, label: Option<Self::Output>,
style: &Self::Style, style: &Self::Style,
) -> Self::Output; ) -> Self::Output;
} }