diff options
Diffstat (limited to 'native/src/widget/button.rs')
-rw-r--r-- | native/src/widget/button.rs | 97 |
1 files changed, 58 insertions, 39 deletions
diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 023c4ee8..f1d46936 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -6,7 +6,7 @@ //! [`State`]: struct.State.html use crate::{ input::{mouse, ButtonState}, - layout, Background, Element, Event, Hasher, Layout, Length, Point, + layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Rectangle, Widget, }; use std::hash::Hash; @@ -28,18 +28,22 @@ 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<Message>, width: Length, + height: Length, min_width: u32, + min_height: u32, padding: u16, - background: Option<Background>, - 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. /// @@ -54,10 +58,11 @@ impl<'a, Message, Renderer> Button<'a, Message, Renderer> { content: content.into(), on_press: None, width: Length::Shrink, + height: Length::Shrink, min_width: 0, - padding: 0, - background: None, - border_radius: 0, + min_height: 0, + padding: Renderer::DEFAULT_PADDING, + style: Renderer::Style::default(), } } @@ -69,36 +74,35 @@ impl<'a, Message, Renderer> Button<'a, Message, Renderer> { self } - /// Sets the minimum width of the [`Button`]. + /// Sets the height of the [`Button`]. /// /// [`Button`]: struct.Button.html - pub fn min_width(mut self, min_width: u32) -> Self { - self.min_width = min_width; + pub fn height(mut self, height: Length) -> Self { + self.height = height; self } - /// Sets the padding of the [`Button`]. + /// Sets the minimum width of the [`Button`]. /// /// [`Button`]: struct.Button.html - pub fn padding(mut self, padding: u16) -> Self { - self.padding = padding; + pub fn min_width(mut self, min_width: u32) -> Self { + self.min_width = min_width; self } - /// Sets the [`Background`] of the [`Button`]. + /// Sets the minimum height of the [`Button`]. /// /// [`Button`]: struct.Button.html - /// [`Background`]: ../../struct.Background.html - pub fn background(mut self, background: Background) -> Self { - self.background = Some(background); + pub fn min_height(mut self, min_height: u32) -> Self { + self.min_height = min_height; self } - /// Sets the border radius of the [`Button`]. + /// Sets the padding of the [`Button`]. /// /// [`Button`]: struct.Button.html - pub fn border_radius(mut self, border_radius: u16) -> Self { - self.border_radius = border_radius; + pub fn padding(mut self, padding: u16) -> Self { + self.padding = padding; self } @@ -109,6 +113,14 @@ impl<'a, Message, Renderer> Button<'a, Message, Renderer> { self.on_press = Some(msg); self } + + /// Sets the style of the [`Button`]. + /// + /// [`Button`]: struct.Button.html + pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self { + self.style = style.into(); + self + } } /// The local state of a [`Button`]. @@ -139,7 +151,7 @@ where } fn height(&self) -> Length { - Length::Shrink + self.height } fn layout( @@ -150,14 +162,13 @@ where let padding = f32::from(self.padding); let limits = limits .min_width(self.min_width) + .min_height(self.min_height) .width(self.width) - .height(Length::Shrink) + .height(self.height) .pad(padding); let mut content = self.content.layout(renderer, &limits); - - content.bounds.x = padding; - content.bounds.y = padding; + content.move_to(Point::new(padding, padding)); let size = limits.resolve(content.size()).pad(padding); @@ -171,6 +182,7 @@ where cursor_position: Point, messages: &mut Vec<Message>, _renderer: &Renderer, + _clipboard: Option<&dyn Clipboard>, ) { match event { Event::Mouse(mouse::Event::Input { @@ -205,22 +217,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.background, - self.border_radius, - content, + &self.style, + &self.content, + layout.children().next().unwrap(), ) } @@ -238,17 +247,27 @@ where /// [`Button`]: struct.Button.html /// [renderer]: ../../renderer/index.html pub trait Renderer: crate::Renderer + Sized { + /// The default padding of a [`Button`]. + /// + /// [`Button`]: struct.Button.html + const DEFAULT_PADDING: u16; + + /// The style supported by this renderer. + type Style: Default; + /// Draws a [`Button`]. /// /// [`Button`]: struct.Button.html - fn draw( + fn draw<Message>( &mut self, + defaults: &Self::Defaults, bounds: Rectangle, cursor_position: Point, + is_disabled: bool, is_pressed: bool, - background: Option<Background>, - border_radius: u16, - content: Self::Output, + style: &Self::Style, + content: &Element<'_, Message, Self>, + content_layout: Layout<'_>, ) -> Self::Output; } |