From c7b170da6d180f80e539910cccb543720fa3713c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 29 Dec 2019 10:57:01 +0100 Subject: Draft `Style` and `StyleSheet` for `Button` --- native/src/widget/button.rs | 47 +++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 2881105f..4a7187da 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -6,8 +6,8 @@ //! [`State`]: struct.State.html use crate::{ input::{mouse, ButtonState}, - layout, Background, Clipboard, Element, Event, Hasher, Layout, Length, - Point, Rectangle, Widget, + layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, + Rectangle, Widget, }; use std::hash::Hash; @@ -28,7 +28,7 @@ use std::hash::Hash; /// .on_press(Message::ButtonPressed); /// ``` #[allow(missing_debug_implementations)] -pub struct Button<'a, Message, Renderer> { +pub struct Button<'a, Message, Renderer: self::Renderer> { state: &'a mut State, content: Element<'a, Message, Renderer>, on_press: Option, @@ -37,11 +37,13 @@ pub struct Button<'a, Message, Renderer> { min_width: u32, min_height: u32, padding: u16, - background: Option, - border_radius: u16, + style: Renderer::Style, } -impl<'a, Message, Renderer> Button<'a, Message, Renderer> { +impl<'a, Message, Renderer> Button<'a, Message, Renderer> +where + Renderer: self::Renderer, +{ /// Creates a new [`Button`] with some local [`State`] and the given /// content. /// @@ -60,8 +62,7 @@ impl<'a, Message, Renderer> Button<'a, Message, Renderer> { min_width: 0, min_height: 0, padding: 0, - background: None, - border_radius: 0, + style: Renderer::Style::default(), } } @@ -105,23 +106,6 @@ impl<'a, Message, Renderer> Button<'a, Message, Renderer> { self } - /// Sets the [`Background`] of the [`Button`]. - /// - /// [`Button`]: struct.Button.html - /// [`Background`]: ../../struct.Background.html - pub fn background>(mut self, background: T) -> Self { - self.background = Some(background.into()); - self - } - - /// Sets the border radius of the [`Button`]. - /// - /// [`Button`]: struct.Button.html - pub fn border_radius(mut self, border_radius: u16) -> Self { - self.border_radius = border_radius; - self - } - /// Sets the message that will be produced when the [`Button`] is pressed. /// /// [`Button`]: struct.Button.html @@ -129,6 +113,11 @@ impl<'a, Message, Renderer> Button<'a, Message, Renderer> { self.on_press = Some(msg); self } + + pub fn style(mut self, style: impl Into) -> Self { + self.style = style.into(); + self + } } /// The local state of a [`Button`]. @@ -240,8 +229,7 @@ where layout.bounds(), cursor_position, self.state.is_pressed, - self.background, - self.border_radius, + &self.style, content, ) } @@ -260,6 +248,8 @@ where /// [`Button`]: struct.Button.html /// [renderer]: ../../renderer/index.html pub trait Renderer: crate::Renderer + Sized { + type Style: Default; + /// Draws a [`Button`]. /// /// [`Button`]: struct.Button.html @@ -268,8 +258,7 @@ pub trait Renderer: crate::Renderer + Sized { bounds: Rectangle, cursor_position: Point, is_pressed: bool, - background: Option, - border_radius: u16, + style: &Self::Style, content: Self::Output, ) -> Self::Output; } -- cgit From 8caa66be2708b1c83e20d905d69902c2567c4692 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 30 Dec 2019 12:14:26 +0100 Subject: Add `Renderer::Defaults` and style inheritance --- native/src/widget/button.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 4a7187da..75ef2693 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -216,21 +216,19 @@ where fn draw( &self, renderer: &mut Renderer, + defaults: &Renderer::Defaults, layout: Layout<'_>, cursor_position: Point, ) -> Renderer::Output { - let content = self.content.draw( - renderer, - layout.children().next().unwrap(), - cursor_position, - ); - renderer.draw( + defaults, layout.bounds(), cursor_position, + self.on_press.is_none(), self.state.is_pressed, &self.style, - content, + &self.content, + layout.children().next().unwrap(), ) } @@ -253,13 +251,16 @@ pub trait Renderer: crate::Renderer + Sized { /// Draws a [`Button`]. /// /// [`Button`]: struct.Button.html - fn draw( + fn draw( &mut self, + defaults: &Self::Defaults, bounds: Rectangle, cursor_position: Point, + is_disabled: bool, is_pressed: bool, style: &Self::Style, - content: Self::Output, + content: &Element<'_, Message, Self>, + content_layout: Layout<'_>, ) -> Self::Output; } -- cgit From 7b278755fc7929633b5771824beac4d39b16e82e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 9 Jan 2020 18:31:07 +0100 Subject: Write missing docs and reenable deny statements --- native/src/widget/button.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 75ef2693..51b02172 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -114,6 +114,9 @@ where self } + /// Sets the style of the [`Button`]. + /// + /// [`Button`]: struct.Button.html pub fn style(mut self, style: impl Into) -> Self { self.style = style.into(); self @@ -246,6 +249,7 @@ where /// [`Button`]: struct.Button.html /// [renderer]: ../../renderer/index.html pub trait Renderer: crate::Renderer + Sized { + /// The style supported by this renderer. type Style: Default; /// Draws a [`Button`]. -- cgit