From 668f627532dacd5441f6990a39c327db1da40083 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Mon, 17 Feb 2020 17:40:01 -0600 Subject: [PATCH 1/2] Add size, spacing, and text_size properties to Checkbox --- native/src/widget/checkbox.rs | 45 +++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 951659977e..669437bc83 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -30,7 +30,10 @@ pub struct Checkbox { is_checked: bool, on_toggle: Box Message>, label: String, + size: u16, width: Length, + spacing: u16, + text_size: u16, style: Renderer::Style, } @@ -53,11 +56,22 @@ impl Checkbox { is_checked, on_toggle: Box::new(f), label: String::from(label), + size: 20, width: Length::Shrink, + spacing: 15, + text_size: 20, style: Renderer::Style::default(), } } + /// Sets the size of the [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn size(mut self, size: u16) -> Self { + self.size = size; + self + } + /// Sets the width of the [`Checkbox`]. /// /// [`Checkbox`]: struct.Checkbox.html @@ -66,6 +80,22 @@ impl Checkbox { self } + /// Sets the spacing between the [`Checkbox`] and the text. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn spacing(mut self, spacing: u16) -> Self { + self.spacing = spacing; + self + } + + /// Sets the text size of the [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn text_size(mut self, text_size: u16) -> Self { + self.text_size = text_size; + self + } + /// Sets the style of the [`Checkbox`]. /// /// [`Checkbox`]: struct.Checkbox.html @@ -93,18 +123,19 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let size = self::Renderer::default_size(renderer); - Row::<(), Renderer>::new() .width(self.width) - .spacing(15) + .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)) + .push( + Text::new(&self.label) + .width(self.width) + .size(self.text_size)) .layout(renderer, limits) } @@ -151,7 +182,7 @@ where defaults, label_layout.bounds(), &self.label, - text::Renderer::default_size(renderer), + self.text_size, Font::Default, None, HorizontalAlignment::Left, From 692216042307c0ff0d792d1bba6928e499a65d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 18 Feb 2020 02:28:15 +0100 Subject: [PATCH 2/2] Pull `Checkbox` default constants from its `Renderer` --- native/src/renderer/null.rs | 9 +++------ native/src/widget/checkbox.rs | 26 +++++++++++++++++--------- native/src/widget/radio.rs | 2 +- native/src/widget/text.rs | 8 ++++---- wgpu/src/renderer/widget/checkbox.rs | 7 ++----- wgpu/src/renderer/widget/text.rs | 7 +------ 6 files changed, 28 insertions(+), 31 deletions(-) diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 2e0b6f3286..0fcce5ada6 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -47,9 +47,7 @@ impl row::Renderer for Null { } impl text::Renderer for Null { - fn default_size(&self) -> u16 { - 20 - } + const DEFAULT_SIZE: u16 = 20; fn measure( &self, @@ -179,9 +177,8 @@ impl radio::Renderer for Null { impl checkbox::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/checkbox.rs b/native/src/widget/checkbox.rs index 669437bc83..b36d10a491 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -26,18 +26,20 @@ use crate::{ /// /// ![Checkbox drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/checkbox.png?raw=true) #[allow(missing_debug_implementations)] -pub struct Checkbox { +pub struct Checkbox { is_checked: bool, on_toggle: Box Message>, label: String, - size: u16, width: Length, + size: u16, spacing: u16, text_size: u16, style: Renderer::Style, } -impl Checkbox { +impl + Checkbox +{ /// Creates a new [`Checkbox`]. /// /// It expects: @@ -56,10 +58,10 @@ impl Checkbox { is_checked, on_toggle: Box::new(f), label: String::from(label), - size: 20, width: Length::Shrink, - spacing: 15, - text_size: 20, + size: ::DEFAULT_SIZE, + spacing: Renderer::DEFAULT_SPACING, + text_size: ::DEFAULT_SIZE, style: Renderer::Style::default(), } } @@ -135,7 +137,8 @@ where .push( Text::new(&self.label) .width(self.width) - .size(self.text_size)) + .size(self.text_size), + ) .layout(renderer, limits) } @@ -217,10 +220,15 @@ pub trait Renderer: crate::Renderer { /// The style supported by this renderer. type Style: Default; - /// Returns the default size of a [`Checkbox`]. + /// The default size of a [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + const DEFAULT_SIZE: u16; + + /// The default spacing of a [`Checkbox`]. /// /// [`Checkbox`]: struct.Checkbox.html - fn default_size(&self) -> u32; + const DEFAULT_SPACING: u16; /// Draws a [`Checkbox`]. /// diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 99743ec3c0..cdc4862cd1 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -149,7 +149,7 @@ where defaults, label_layout.bounds(), &self.label, - text::Renderer::default_size(renderer), + ::DEFAULT_SIZE, Font::Default, None, HorizontalAlignment::Left, diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index e4490fb6d7..7d8cad6ebe 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -131,7 +131,7 @@ where ) -> layout::Node { let limits = limits.width(self.width).height(self.height); - let size = self.size.unwrap_or(renderer.default_size()); + let size = self.size.unwrap_or(Renderer::DEFAULT_SIZE); let bounds = limits.max(); @@ -154,7 +154,7 @@ where defaults, layout.bounds(), &self.content, - self.size.unwrap_or(renderer.default_size()), + self.size.unwrap_or(Renderer::DEFAULT_SIZE), self.font, self.color, self.horizontal_alignment, @@ -179,10 +179,10 @@ where /// [renderer]: ../../renderer/index.html /// [`UserInterface`]: ../../struct.UserInterface.html pub trait Renderer: crate::Renderer { - /// Returns the default size of the [`Text`]. + /// The default size of [`Text`]. /// /// [`Text`]: struct.Text.html - fn default_size(&self) -> u16; + const DEFAULT_SIZE: u16; /// Measures the [`Text`] in the given bounds and returns the minimum /// boundaries that can fit the contents. diff --git a/wgpu/src/renderer/widget/checkbox.rs b/wgpu/src/renderer/widget/checkbox.rs index 17121eea51..1a0585d395 100644 --- a/wgpu/src/renderer/widget/checkbox.rs +++ b/wgpu/src/renderer/widget/checkbox.rs @@ -3,14 +3,11 @@ use iced_native::{ checkbox, HorizontalAlignment, MouseCursor, Rectangle, VerticalAlignment, }; -const SIZE: f32 = 28.0; - impl checkbox::Renderer for Renderer { type Style = Box; - fn default_size(&self) -> u32 { - SIZE as u32 - } + const DEFAULT_SIZE: u16 = 20; + const DEFAULT_SPACING: u16 = 15; fn draw( &mut self, diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index d61c5523d2..33e549cd40 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -6,13 +6,8 @@ use iced_native::{ use std::f32; -// TODO: Obtain from renderer configuration -const DEFAULT_TEXT_SIZE: f32 = 20.0; - impl text::Renderer for Renderer { - fn default_size(&self) -> u16 { - DEFAULT_TEXT_SIZE as u16 - } + const DEFAULT_SIZE: u16 = 20; fn measure( &self,