From 863714602c1c0703e7872ecc014fc4a0aa33ecc4 Mon Sep 17 00:00:00 2001 From: Scott Harper Date: Fri, 15 May 2020 11:27:24 +0900 Subject: [PATCH] Updated `Radio` layout/fns to match `Checkbox` --- native/src/renderer/null.rs | 5 +-- native/src/widget/radio.rs | 75 +++++++++++++++++++++++++------ wgpu/src/renderer/widget/radio.rs | 5 +-- 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 2c7babdb..19689d30 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -161,9 +161,8 @@ impl button::Renderer for Null { impl radio::Renderer for Null { type Style = (); - fn default_size(&self) -> u32 { - 20 - } + const DEFAULT_SIZE: u16 = 20; + const DEFAULT_SPACING: u16 = 15; fn draw( &mut self, diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index ab5bcf32..d07a9012 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -34,14 +34,20 @@ use std::hash::Hash; /// /// ![Radio buttons drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/radio.png?raw=true) #[allow(missing_debug_implementations)] -pub struct Radio { +pub struct Radio { is_selected: bool, on_click: Message, label: String, + width: Length, + size: u16, + spacing: u16, + text_size: u16, style: Renderer::Style, } -impl Radio { +impl + Radio +{ /// Creates a new [`Radio`] button. /// /// It expects: @@ -66,10 +72,46 @@ impl Radio { is_selected: Some(value) == selected, on_click: f(value), label: label.into(), + width: Length::Shrink, + size: ::DEFAULT_SIZE, + spacing: Renderer::DEFAULT_SPACING, //15 + text_size: ::DEFAULT_SIZE, style: Renderer::Style::default(), } } + /// Sets the size of the [`Radio`] button. + /// + /// [`Radio`]: struct.Radio.html + pub fn size(mut self, size: u16) -> Self { + self.size = size; + self + } + + /// Sets the width of the [`Radio`] button. + /// + /// [`Radio`]: struct.Radio.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the spacing between the [`Radio`] button and the text. + /// + /// [`Radio`]: struct.Radio.html + pub fn spacing(mut self, spacing: u16) -> Self { + self.spacing = spacing; + self + } + + /// Sets the text size of the [`Radio`] button. + /// + /// [`Radio`]: struct.Radio.html + pub fn text_size(mut self, text_size: u16) -> Self { + self.text_size = text_size; + self + } + /// Sets the style of the [`Radio`] button. /// /// [`Radio`]: struct.Radio.html @@ -85,7 +127,7 @@ where Message: Clone, { fn width(&self) -> Length { - Length::Fill + self.width } fn height(&self) -> Length { @@ -97,18 +139,20 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let size = self::Renderer::default_size(renderer); - Row::<(), Renderer>::new() - .width(Length::Fill) - .spacing(15) + .width(self.width) + .spacing(self.spacing) .align_items(Align::Center) .push( Row::new() - .width(Length::Units(size as u16)) - .height(Length::Units(size as u16)), + .width(Length::Units(self.size)) + .height(Length::Units(self.size)), + ) + .push( + Text::new(&self.label) + .width(self.width) + .size(self.text_size), ) - .push(Text::new(&self.label)) .layout(renderer, limits) } @@ -150,7 +194,7 @@ where defaults, label_layout.bounds(), &self.label, - ::DEFAULT_SIZE, + self.text_size, Default::default(), None, HorizontalAlignment::Left, @@ -188,10 +232,15 @@ pub trait Renderer: crate::Renderer { /// The style supported by this renderer. type Style: Default; - /// Returns the default size of a [`Radio`] button. + /// The default size of a [`Radio`] button. /// /// [`Radio`]: struct.Radio.html - fn default_size(&self) -> u32; + const DEFAULT_SIZE: u16; + + /// The default spacing of a [`Radio`] button. + /// + /// [`Radio`]: struct.Radio.html + const DEFAULT_SPACING: u16; /// Draws a [`Radio`] button. /// diff --git a/wgpu/src/renderer/widget/radio.rs b/wgpu/src/renderer/widget/radio.rs index 2f1461db..cee0deb6 100644 --- a/wgpu/src/renderer/widget/radio.rs +++ b/wgpu/src/renderer/widget/radio.rs @@ -7,9 +7,8 @@ const DOT_SIZE: f32 = SIZE / 2.0; impl radio::Renderer for Renderer { type Style = Box; - fn default_size(&self) -> u32 { - SIZE as u32 - } + const DEFAULT_SIZE: u16 = SIZE as u16; + const DEFAULT_SPACING: u16 = 15; fn draw( &mut self,