Change label of `Toggler` to optional
MODIFY
graphics/src/widget/toggler.rs
graphics/src/widget/toggler.rs
@@ -33,7 +33,7 @@ where
bounds: Rectangle,
is_active: bool,
is_mouse_over: bool,
- (label, _): Self::Output,
+ label: Option<Self::Output>,
style_sheet: &Self::Style,
) -> Self::Output {
let style = if is_mouse_over {
@@ -82,7 +82,12 @@ where
(
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 {
mouse::Interaction::Pointer

MODIFY
native/src/renderer/null.rs
native/src/renderer/null.rs
@@ -299,7 +299,7 @@ impl toggler::Renderer for Null {
_bounds: Rectangle,
_is_checked: bool,
_is_mouse_over: bool,
- _label: Self::Output,
+ _label: Option<Self::Output>,
_style: &Self::Style,
) {
}

MODIFY
native/src/widget/toggler.rs
native/src/widget/toggler.rs
@@ -20,14 +20,14 @@ use crate::{
///
/// 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)]
pub struct Toggler<Message, Renderer: self::Renderer + text::Renderer> {
is_active: bool,
on_toggle: Box<dyn Fn(bool) -> Message>,
- label: String,
+ label: Option<String>,
width: Length,
size: u16,
text_size: Option<u16>,
@@ -44,13 +44,17 @@ impl<Message, Renderer: self::Renderer + text::Renderer>
///
/// It expects:
/// * 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
/// will receive the new state of the [`Toggler`] and must produce a
/// `Message`.
///
/// [`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
F: 'static + Fn(bool) -> Message,
{
@@ -143,25 +147,30 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
- Row::<(), Renderer>::new()
+ let mut row = Row::<(), Renderer>::new()
.width(self.width)
.spacing(self.spacing)
- .align_items(Align::Center)
- .push(
- Text::new(&self.label)
+ .align_items(Align::Center);
+
+ if let Some(label) = &self.label {
+ row = row.push(
+ Text::new(label)
.horizontal_alignment(
self.text_align.unwrap_or(HorizontalAlignment::Left),
)
.font(self.font)
.width(self.width)
.size(self.text_size.unwrap_or(renderer.default_size())),
- )
- .push(
+ );
+ }
+
+ row = row.push(
Row::new()
.width(Length::Units(2 * self.size))
.height(Length::Units(self.size)),
- )
- .layout(renderer, limits)
+ );
+
+ row.layout(renderer, limits)
}
fn on_event(
@@ -200,21 +209,28 @@ where
let bounds = layout.bounds();
let mut children = layout.children();
+ let label = match &self.label {
+ Some(label) => {
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,
defaults,
label_layout.bounds(),
- &self.label,
+ &label,
self.text_size.unwrap_or(renderer.default_size()),
self.font,
None,
self.text_align.unwrap_or(HorizontalAlignment::Left),
VerticalAlignment::Center,
- );
+ ))
+ }
+
+ None => None,
+ };
+
+ let toggler_layout = children.next().unwrap();
+ let toggler_bounds = toggler_layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
@@ -267,7 +283,7 @@ pub trait Renderer: crate::Renderer {
bounds: Rectangle,
is_active: bool,
is_mouse_over: bool,
- label: Self::Output,
+ label: Option<Self::Output>,
style: &Self::Style,
) -> Self::Output;
}

Change label of `Toggler` to optional