From 7a626f3b7b6871052e5fade697e120cfb7d726d7 Mon Sep 17 00:00:00 2001 From: Kaiden42 Date: Thu, 24 Sep 2020 16:31:39 +0200 Subject: [PATCH] Change label of `Toggler` to optional --- graphics/src/widget/toggler.rs | 9 ++++- native/src/renderer/null.rs | 2 +- native/src/widget/toggler.rs | 74 +++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/graphics/src/widget/toggler.rs b/graphics/src/widget/toggler.rs index a258443e..852d18ee 100644 --- a/graphics/src/widget/toggler.rs +++ b/graphics/src/widget/toggler.rs @@ -33,7 +33,7 @@ where bounds: Rectangle, is_active: bool, is_mouse_over: bool, - (label, _): Self::Output, + label: Option, 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 diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 89bb9433..bb57c163 100644 --- a/native/src/renderer/null.rs +++ b/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, _style: &Self::Style, ) { } diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index 63058d06..36e7d110 100644 --- a/native/src/widget/toggler.rs +++ b/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 { is_active: bool, on_toggle: Box Message>, - label: String, + label: Option, width: Length, size: u16, text_size: Option, @@ -44,13 +44,17 @@ impl /// /// 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(is_active: bool, label: impl Into, f: F) -> Self + pub fn new( + is_active: bool, + label: impl Into>, + 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::new() - .width(Length::Units(2 * self.size)) - .height(Length::Units(self.size)), - ) - .layout(renderer, limits) + ); + } + + row = row.push( + Row::new() + .width(Length::Units(2 * self.size)) + .height(Length::Units(self.size)), + ); + + row.layout(renderer, limits) } fn on_event( @@ -200,22 +209,29 @@ where let bounds = layout.bounds(); let mut children = layout.children(); - let label_layout = children.next().unwrap(); + let label = match &self.label { + Some(label) => { + let label_layout = children.next().unwrap(); + + Some(text::Renderer::draw( + renderer, + defaults, + label_layout.bounds(), + &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 label = text::Renderer::draw( - renderer, - defaults, - label_layout.bounds(), - &self.label, - self.text_size.unwrap_or(renderer.default_size()), - self.font, - None, - self.text_align.unwrap_or(HorizontalAlignment::Left), - VerticalAlignment::Center, - ); - let is_mouse_over = bounds.contains(cursor_position); self::Renderer::draw( @@ -267,7 +283,7 @@ pub trait Renderer: crate::Renderer { bounds: Rectangle, is_active: bool, is_mouse_over: bool, - label: Self::Output, + label: Option, style: &Self::Style, ) -> Self::Output; }