Merge pull request #345 from AberrantWolf/master

Update `Radio` to have the same layout members and fns as `Checkbox`
This commit is contained in:
Héctor Ramón 2020-05-15 14:58:28 +02:00 committed by GitHub
commit 33448508a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 19 deletions

View File

@ -161,9 +161,8 @@ impl button::Renderer for Null {
impl radio::Renderer for Null { impl radio::Renderer for Null {
type Style = (); type Style = ();
fn default_size(&self) -> u32 { const DEFAULT_SIZE: u16 = 20;
20 const DEFAULT_SPACING: u16 = 15;
}
fn draw( fn draw(
&mut self, &mut self,

View File

@ -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) /// ![Radio buttons drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/radio.png?raw=true)
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Radio<Message, Renderer: self::Renderer> { pub struct Radio<Message, Renderer: self::Renderer + text::Renderer> {
is_selected: bool, is_selected: bool,
on_click: Message, on_click: Message,
label: String, label: String,
width: Length,
size: u16,
spacing: u16,
text_size: u16,
style: Renderer::Style, style: Renderer::Style,
} }
impl<Message, Renderer: self::Renderer> Radio<Message, Renderer> { impl<Message, Renderer: self::Renderer + text::Renderer>
Radio<Message, Renderer>
{
/// Creates a new [`Radio`] button. /// Creates a new [`Radio`] button.
/// ///
/// It expects: /// It expects:
@ -66,10 +72,46 @@ impl<Message, Renderer: self::Renderer> Radio<Message, Renderer> {
is_selected: Some(value) == selected, is_selected: Some(value) == selected,
on_click: f(value), on_click: f(value),
label: label.into(), label: label.into(),
width: Length::Shrink,
size: <Renderer as self::Renderer>::DEFAULT_SIZE,
spacing: Renderer::DEFAULT_SPACING, //15
text_size: <Renderer as text::Renderer>::DEFAULT_SIZE,
style: Renderer::Style::default(), 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. /// Sets the style of the [`Radio`] button.
/// ///
/// [`Radio`]: struct.Radio.html /// [`Radio`]: struct.Radio.html
@ -85,7 +127,7 @@ where
Message: Clone, Message: Clone,
{ {
fn width(&self) -> Length { fn width(&self) -> Length {
Length::Fill self.width
} }
fn height(&self) -> Length { fn height(&self) -> Length {
@ -97,18 +139,20 @@ where
renderer: &Renderer, renderer: &Renderer,
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
let size = self::Renderer::default_size(renderer);
Row::<(), Renderer>::new() Row::<(), Renderer>::new()
.width(Length::Fill) .width(self.width)
.spacing(15) .spacing(self.spacing)
.align_items(Align::Center) .align_items(Align::Center)
.push( .push(
Row::new() Row::new()
.width(Length::Units(size as u16)) .width(Length::Units(self.size))
.height(Length::Units(size as u16)), .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) .layout(renderer, limits)
} }
@ -150,7 +194,7 @@ where
defaults, defaults,
label_layout.bounds(), label_layout.bounds(),
&self.label, &self.label,
<Renderer as text::Renderer>::DEFAULT_SIZE, self.text_size,
Default::default(), Default::default(),
None, None,
HorizontalAlignment::Left, HorizontalAlignment::Left,
@ -188,10 +232,15 @@ pub trait Renderer: crate::Renderer {
/// The style supported by this renderer. /// The style supported by this renderer.
type Style: Default; type Style: Default;
/// Returns the default size of a [`Radio`] button. /// The default size of a [`Radio`] button.
/// ///
/// [`Radio`]: struct.Radio.html /// [`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. /// Draws a [`Radio`] button.
/// ///

View File

@ -7,9 +7,8 @@ const DOT_SIZE: f32 = SIZE / 2.0;
impl radio::Renderer for Renderer { impl radio::Renderer for Renderer {
type Style = Box<dyn StyleSheet>; type Style = Box<dyn StyleSheet>;
fn default_size(&self) -> u32 { const DEFAULT_SIZE: u16 = SIZE as u16;
SIZE as u32 const DEFAULT_SPACING: u16 = 15;
}
fn draw( fn draw(
&mut self, &mut self,