From 03b34931383e701c39c653a7662a616fe21a0947 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Oct 2021 16:07:22 +0700 Subject: Remove trait-specific draw logic in `iced_native` --- native/src/widget/button.rs | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index c469a0e5..1c7f80ef 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -248,17 +248,7 @@ where layout: Layout<'_>, cursor_position: Point, _viewport: &Rectangle, - ) -> Renderer::Output { - renderer.draw( - defaults, - layout.bounds(), - cursor_position, - self.on_press.is_none(), - self.state.is_pressed, - &self.style, - &self.content, - layout.children().next().unwrap(), - ) + ) { } fn hash_layout(&self, state: &mut Hasher) { @@ -289,19 +279,6 @@ pub trait Renderer: crate::Renderer + Sized { /// The style supported by this renderer. type Style: Default; - - /// Draws a [`Button`]. - fn draw( - &mut self, - defaults: &Self::Defaults, - bounds: Rectangle, - cursor_position: Point, - is_disabled: bool, - is_pressed: bool, - style: &Self::Style, - content: &Element<'_, Message, Self>, - content_layout: Layout<'_>, - ) -> Self::Output; } impl<'a, Message, Renderer> From> -- cgit From edea093350e1b576e2b7db50c525e7fa5c3bea9f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 15:19:04 +0700 Subject: Move `Defaults` from `iced_graphics` to `iced_native` --- native/src/widget/button.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 1c7f80ef..2aeffd03 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -5,6 +5,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::mouse; use crate::overlay; +use crate::renderer; use crate::touch; use crate::{ Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle, @@ -244,7 +245,7 @@ where fn draw( &self, renderer: &mut Renderer, - defaults: &Renderer::Defaults, + style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, _viewport: &Rectangle, -- cgit From 3140cdc4babcefc444f1c1d30eb0f5f4ed1df054 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 16:02:30 +0700 Subject: Wire up styling to `Button` in `iced_native` --- native/src/widget/button.rs | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 2aeffd03..77148673 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -11,8 +11,11 @@ use crate::{ Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle, Widget, }; + use std::hash::Hash; +pub use iced_style::button::{Style, StyleSheet}; + /// A generic widget that produces a message when pressed. /// /// ``` @@ -54,7 +57,7 @@ use std::hash::Hash; /// } /// ``` #[allow(missing_debug_implementations)] -pub struct Button<'a, Message, Renderer: self::Renderer> { +pub struct Button<'a, Message, Renderer> { state: &'a mut State, content: Element<'a, Message, Renderer>, on_press: Option, @@ -63,13 +66,13 @@ pub struct Button<'a, Message, Renderer: self::Renderer> { min_width: u32, min_height: u32, padding: Padding, - style: Renderer::Style, + style: &'a dyn StyleSheet, } impl<'a, Message, Renderer> Button<'a, Message, Renderer> where Message: Clone, - Renderer: self::Renderer, + Renderer: crate::Renderer, { /// Creates a new [`Button`] with some local [`State`] and the given /// content. @@ -85,8 +88,8 @@ where height: Length::Shrink, min_width: 0, min_height: 0, - padding: Renderer::DEFAULT_PADDING, - style: Renderer::Style::default(), + padding: Padding::new(5), + style: Default::default(), } } @@ -128,8 +131,8 @@ where } /// Sets the style of the [`Button`]. - pub fn style(mut self, style: impl Into) -> Self { - self.style = style.into(); + pub fn style(mut self, style: &'a dyn StyleSheet) -> Self { + self.style = style; self } } @@ -151,7 +154,7 @@ impl<'a, Message, Renderer> Widget for Button<'a, Message, Renderer> where Message: Clone, - Renderer: self::Renderer, + Renderer: crate::Renderer, { fn width(&self) -> Length { self.width @@ -268,25 +271,11 @@ where } } -/// The renderer of a [`Button`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Button`] in your user interface. -/// -/// [renderer]: crate::renderer -pub trait Renderer: crate::Renderer + Sized { - /// The default padding of a [`Button`]. - const DEFAULT_PADDING: Padding; - - /// The style supported by this renderer. - type Style: Default; -} - impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where Message: 'a + Clone, - Renderer: 'a + self::Renderer, + Renderer: 'a + crate::Renderer, { fn from( button: Button<'a, Message, Renderer>, -- cgit From 7a876c8b2918ae90cedfd82d1881cf6406811eeb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 16:10:16 +0700 Subject: Implement `Widget::draw` for `Button` --- native/src/widget/button.rs | 67 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 7 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 77148673..a05210fd 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -8,8 +8,8 @@ use crate::overlay; use crate::renderer; use crate::touch; use crate::{ - Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle, - Widget, + Background, Clipboard, Color, Element, Hasher, Layout, Length, Padding, + Point, Rectangle, Vector, Widget, }; use std::hash::Hash; @@ -66,7 +66,7 @@ pub struct Button<'a, Message, Renderer> { min_width: u32, min_height: u32, padding: Padding, - style: &'a dyn StyleSheet, + style_sheet: &'a dyn StyleSheet, } impl<'a, Message, Renderer> Button<'a, Message, Renderer> @@ -89,7 +89,7 @@ where min_width: 0, min_height: 0, padding: Padding::new(5), - style: Default::default(), + style_sheet: Default::default(), } } @@ -131,8 +131,8 @@ where } /// Sets the style of the [`Button`]. - pub fn style(mut self, style: &'a dyn StyleSheet) -> Self { - self.style = style; + pub fn style(mut self, style_sheet: &'a dyn StyleSheet) -> Self { + self.style_sheet = style_sheet; self } } @@ -248,11 +248,64 @@ where fn draw( &self, renderer: &mut Renderer, - style: &renderer::Style, + _style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, _viewport: &Rectangle, ) { + let bounds = layout.bounds(); + let content_layout = layout.children().next().unwrap(); + + let is_mouse_over = bounds.contains(cursor_position); + + let styling = if self.on_press.is_none() { + self.style_sheet.disabled() + } else if is_mouse_over { + if self.state.is_pressed { + self.style_sheet.pressed() + } else { + self.style_sheet.hovered() + } + } else { + self.style_sheet.active() + }; + + if styling.background.is_some() || styling.border_width > 0.0 { + if styling.shadow_offset != Vector::default() { + // TODO: Implement proper shadow support + renderer.fill_rectangle(renderer::Quad { + bounds: Rectangle { + x: bounds.x + styling.shadow_offset.x, + y: bounds.y + styling.shadow_offset.y, + ..bounds + }, + background: Background::Color([0.0, 0.0, 0.0, 0.5].into()), + border_radius: styling.border_radius, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }); + } + + renderer.fill_rectangle(renderer::Quad { + bounds, + background: styling + .background + .unwrap_or(Background::Color(Color::TRANSPARENT)), + border_radius: styling.border_radius, + border_width: styling.border_width, + border_color: styling.border_color, + }); + } + + self.content.draw( + renderer, + &renderer::Style { + text_color: styling.text_color, + }, + content_layout, + cursor_position, + &bounds, + ); } fn hash_layout(&self, state: &mut Hasher) { -- cgit From 4f5f444d7c507105d025471fbe930e50653c05bf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 16:59:42 +0700 Subject: Implement `Widget::mouse_interaction` for `Button` --- native/src/widget/button.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index a05210fd..a654cf12 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -245,6 +245,22 @@ where event::Status::Ignored } + fn mouse_interaction( + &self, + layout: Layout<'_>, + _viewport: &Rectangle, + cursor_position: Point, + ) -> mouse::Interaction { + let is_mouse_over = layout.bounds().contains(cursor_position); + let is_disabled = self.on_press.is_none(); + + if is_mouse_over && !is_disabled { + mouse::Interaction::Pointer + } else { + mouse::Interaction::default() + } + } + fn draw( &self, renderer: &mut Renderer, @@ -257,8 +273,9 @@ where let content_layout = layout.children().next().unwrap(); let is_mouse_over = bounds.contains(cursor_position); + let is_disabled = self.on_press.is_none(); - let styling = if self.on_press.is_none() { + let styling = if is_disabled { self.style_sheet.disabled() } else if is_mouse_over { if self.state.is_pressed { -- cgit From 0aafcde0ef1533c9eeba0379de8c0082e30c7504 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 15:35:12 +0700 Subject: Remove `widget` module re-exports in `iced_native` --- native/src/widget/button.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index a654cf12..a577fe84 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -19,10 +19,10 @@ pub use iced_style::button::{Style, StyleSheet}; /// A generic widget that produces a message when pressed. /// /// ``` -/// # use iced_native::{button, Text}; +/// # use iced_native::widget::{button, Text}; /// # /// # type Button<'a, Message> = -/// # iced_native::Button<'a, Message, iced_native::renderer::Null>; +/// # iced_native::widget::Button<'a, Message, iced_native::renderer::Null>; /// # /// #[derive(Clone)] /// enum Message { @@ -38,10 +38,10 @@ pub use iced_style::button::{Style, StyleSheet}; /// be disabled: /// /// ``` -/// # use iced_native::{button, Text}; +/// # use iced_native::widget::{button, Text}; /// # /// # type Button<'a, Message> = -/// # iced_native::Button<'a, Message, iced_native::renderer::Null>; +/// # iced_native::widget::Button<'a, Message, iced_native::renderer::Null>; /// # /// #[derive(Clone)] /// enum Message { -- cgit From d36ce33a95c277ee8fe45555df17daf21ef02ef8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 16:53:18 +0700 Subject: Reintroduce `Box` for `style_sheet` in `Button` --- native/src/widget/button.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index a577fe84..588833a4 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -66,7 +66,7 @@ pub struct Button<'a, Message, Renderer> { min_width: u32, min_height: u32, padding: Padding, - style_sheet: &'a dyn StyleSheet, + style_sheet: Box, } impl<'a, Message, Renderer> Button<'a, Message, Renderer> @@ -131,8 +131,11 @@ where } /// Sets the style of the [`Button`]. - pub fn style(mut self, style_sheet: &'a dyn StyleSheet) -> Self { - self.style_sheet = style_sheet; + pub fn style( + mut self, + style_sheet: impl Into>, + ) -> Self { + self.style_sheet = style_sheet.into(); self } } -- cgit From 631e95ee0be01dc7f5e5183e1429972aee37787f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 2 Nov 2021 15:03:29 +0700 Subject: Move `viewport` argument to last position in `mouse_interaction` methods This keeps the order of the arguments consistent with `draw`. --- native/src/widget/button.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 588833a4..0d9eeebd 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -251,8 +251,8 @@ where fn mouse_interaction( &self, layout: Layout<'_>, - _viewport: &Rectangle, cursor_position: Point, + _viewport: &Rectangle, ) -> mouse::Interaction { let is_mouse_over = layout.bounds().contains(cursor_position); let is_disabled = self.on_press.is_none(); -- cgit From 023aded2772f0cd6abd716fe5c8624d5d22e21fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 Nov 2021 19:22:29 +0700 Subject: Rename `fill_rectangle` to `fill_quad` in `Renderer` --- native/src/widget/button.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'native/src/widget/button.rs') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 0d9eeebd..1d785f35 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -293,28 +293,32 @@ where if styling.background.is_some() || styling.border_width > 0.0 { if styling.shadow_offset != Vector::default() { // TODO: Implement proper shadow support - renderer.fill_rectangle(renderer::Quad { - bounds: Rectangle { - x: bounds.x + styling.shadow_offset.x, - y: bounds.y + styling.shadow_offset.y, - ..bounds + renderer.fill_quad( + renderer::Quad { + bounds: Rectangle { + x: bounds.x + styling.shadow_offset.x, + y: bounds.y + styling.shadow_offset.y, + ..bounds + }, + border_radius: styling.border_radius, + border_width: 0.0, + border_color: Color::TRANSPARENT, }, - background: Background::Color([0.0, 0.0, 0.0, 0.5].into()), - border_radius: styling.border_radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }); + Background::Color([0.0, 0.0, 0.0, 0.5].into()), + ); } - renderer.fill_rectangle(renderer::Quad { - bounds, - background: styling + renderer.fill_quad( + renderer::Quad { + bounds, + border_radius: styling.border_radius, + border_width: styling.border_width, + border_color: styling.border_color, + }, + styling .background .unwrap_or(Background::Color(Color::TRANSPARENT)), - border_radius: styling.border_radius, - border_width: styling.border_width, - border_color: styling.border_color, - }); + ); } self.content.draw( -- cgit