Merge pull request #192 from clarkmoody/checkbox-settings

Customize Checkbox
This commit is contained in:
Héctor Ramón 2020-02-18 06:11:55 +01:00 committed by GitHub
commit 80fc8c286e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 33 deletions

View File

@ -47,9 +47,7 @@ impl row::Renderer for Null {
} }
impl text::Renderer for Null { impl text::Renderer for Null {
fn default_size(&self) -> u16 { const DEFAULT_SIZE: u16 = 20;
20
}
fn measure( fn measure(
&self, &self,
@ -179,9 +177,8 @@ impl radio::Renderer for Null {
impl checkbox::Renderer for Null { impl checkbox::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

@ -26,15 +26,20 @@ use crate::{
/// ///
/// ![Checkbox drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/checkbox.png?raw=true) /// ![Checkbox drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/checkbox.png?raw=true)
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Checkbox<Message, Renderer: self::Renderer> { pub struct Checkbox<Message, Renderer: self::Renderer + text::Renderer> {
is_checked: bool, is_checked: bool,
on_toggle: Box<dyn Fn(bool) -> Message>, on_toggle: Box<dyn Fn(bool) -> Message>,
label: String, label: String,
width: Length, width: Length,
size: u16,
spacing: u16,
text_size: u16,
style: Renderer::Style, style: Renderer::Style,
} }
impl<Message, Renderer: self::Renderer> Checkbox<Message, Renderer> { impl<Message, Renderer: self::Renderer + text::Renderer>
Checkbox<Message, Renderer>
{
/// Creates a new [`Checkbox`]. /// Creates a new [`Checkbox`].
/// ///
/// It expects: /// It expects:
@ -54,10 +59,21 @@ impl<Message, Renderer: self::Renderer> Checkbox<Message, Renderer> {
on_toggle: Box::new(f), on_toggle: Box::new(f),
label: String::from(label), label: String::from(label),
width: Length::Shrink, width: Length::Shrink,
size: <Renderer as self::Renderer>::DEFAULT_SIZE,
spacing: Renderer::DEFAULT_SPACING,
text_size: <Renderer as text::Renderer>::DEFAULT_SIZE,
style: Renderer::Style::default(), 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`]. /// Sets the width of the [`Checkbox`].
/// ///
/// [`Checkbox`]: struct.Checkbox.html /// [`Checkbox`]: struct.Checkbox.html
@ -66,6 +82,22 @@ impl<Message, Renderer: self::Renderer> Checkbox<Message, Renderer> {
self 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`]. /// Sets the style of the [`Checkbox`].
/// ///
/// [`Checkbox`]: struct.Checkbox.html /// [`Checkbox`]: struct.Checkbox.html
@ -93,18 +125,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(self.width) .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).width(self.width))
.layout(renderer, limits) .layout(renderer, limits)
} }
@ -151,7 +185,7 @@ where
defaults, defaults,
label_layout.bounds(), label_layout.bounds(),
&self.label, &self.label,
text::Renderer::default_size(renderer), self.text_size,
Font::Default, Font::Default,
None, None,
HorizontalAlignment::Left, HorizontalAlignment::Left,
@ -186,10 +220,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 [`Checkbox`]. /// The default size of a [`Checkbox`].
/// ///
/// [`Checkbox`]: struct.Checkbox.html /// [`Checkbox`]: struct.Checkbox.html
fn default_size(&self) -> u32; const DEFAULT_SIZE: u16;
/// The default spacing of a [`Checkbox`].
///
/// [`Checkbox`]: struct.Checkbox.html
const DEFAULT_SPACING: u16;
/// Draws a [`Checkbox`]. /// Draws a [`Checkbox`].
/// ///

View File

@ -149,7 +149,7 @@ where
defaults, defaults,
label_layout.bounds(), label_layout.bounds(),
&self.label, &self.label,
text::Renderer::default_size(renderer), <Renderer as text::Renderer>::DEFAULT_SIZE,
Font::Default, Font::Default,
None, None,
HorizontalAlignment::Left, HorizontalAlignment::Left,

View File

@ -131,7 +131,7 @@ where
) -> layout::Node { ) -> layout::Node {
let limits = limits.width(self.width).height(self.height); 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(); let bounds = limits.max();
@ -154,7 +154,7 @@ where
defaults, defaults,
layout.bounds(), layout.bounds(),
&self.content, &self.content,
self.size.unwrap_or(renderer.default_size()), self.size.unwrap_or(Renderer::DEFAULT_SIZE),
self.font, self.font,
self.color, self.color,
self.horizontal_alignment, self.horizontal_alignment,
@ -179,10 +179,10 @@ where
/// [renderer]: ../../renderer/index.html /// [renderer]: ../../renderer/index.html
/// [`UserInterface`]: ../../struct.UserInterface.html /// [`UserInterface`]: ../../struct.UserInterface.html
pub trait Renderer: crate::Renderer { pub trait Renderer: crate::Renderer {
/// Returns the default size of the [`Text`]. /// The default size of [`Text`].
/// ///
/// [`Text`]: struct.Text.html /// [`Text`]: struct.Text.html
fn default_size(&self) -> u16; const DEFAULT_SIZE: u16;
/// Measures the [`Text`] in the given bounds and returns the minimum /// Measures the [`Text`] in the given bounds and returns the minimum
/// boundaries that can fit the contents. /// boundaries that can fit the contents.

View File

@ -3,14 +3,11 @@ use iced_native::{
checkbox, HorizontalAlignment, MouseCursor, Rectangle, VerticalAlignment, checkbox, HorizontalAlignment, MouseCursor, Rectangle, VerticalAlignment,
}; };
const SIZE: f32 = 28.0;
impl checkbox::Renderer for Renderer { impl checkbox::Renderer for Renderer {
type Style = Box<dyn StyleSheet>; type Style = Box<dyn StyleSheet>;
fn default_size(&self) -> u32 { const DEFAULT_SIZE: u16 = 20;
SIZE as u32 const DEFAULT_SPACING: u16 = 15;
}
fn draw( fn draw(
&mut self, &mut self,

View File

@ -6,13 +6,8 @@ use iced_native::{
use std::f32; use std::f32;
// TODO: Obtain from renderer configuration
const DEFAULT_TEXT_SIZE: f32 = 20.0;
impl text::Renderer for Renderer { impl text::Renderer for Renderer {
fn default_size(&self) -> u16 { const DEFAULT_SIZE: u16 = 20;
DEFAULT_TEXT_SIZE as u16
}
fn measure( fn measure(
&self, &self,