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/container.rs | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 0e86ab62..f43de2a5 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -179,16 +179,8 @@ where layout: Layout<'_>, cursor_position: Point, viewport: &Rectangle, - ) -> Renderer::Output { - renderer.draw( - defaults, - layout.bounds(), - cursor_position, - viewport, - &self.style, - &self.content, - layout.children().next().unwrap(), - ) + ) { + // TODO } fn hash_layout(&self, state: &mut Hasher) { @@ -221,18 +213,6 @@ where pub trait Renderer: crate::Renderer { /// The style supported by this renderer. type Style: Default; - - /// Draws a [`Container`]. - fn draw( - &mut self, - defaults: &Self::Defaults, - bounds: Rectangle, - cursor_position: Point, - viewport: &Rectangle, - style: &Self::Style, - content: &Element<'_, Message, Self>, - content_layout: Layout<'_>, - ) -> Self::Output; } impl<'a, Message, Renderer> From> -- cgit From 7c4738735b6cd5eafb544de01dbf5dd5091dc188 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Oct 2021 17:05:47 +0700 Subject: Implement `Widget::draw` for `Container` --- native/src/widget/container.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index f43de2a5..af40f2ab 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -180,7 +180,13 @@ where cursor_position: Point, viewport: &Rectangle, ) { - // TODO + self.content.draw( + renderer, + defaults, + layout.children().next().unwrap(), + cursor_position, + viewport, + ); } fn hash_layout(&self, state: &mut Hasher) { -- 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/container.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index af40f2ab..92869873 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,6 +5,7 @@ use crate::alignment::{self, Alignment}; use crate::event::{self, Event}; use crate::layout; use crate::overlay; +use crate::renderer; use crate::{ Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle, Widget, @@ -175,14 +176,14 @@ where fn draw( &self, renderer: &mut Renderer, - defaults: &Renderer::Defaults, + style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, viewport: &Rectangle, ) { self.content.draw( renderer, - defaults, + style, layout.children().next().unwrap(), cursor_position, viewport, -- cgit From d61cb58d92b6fcd520f665deb093f3747ffd5e5c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 15:36:32 +0700 Subject: Wire up `container` styling to `iced_native` --- native/src/widget/container.rs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 92869873..99bc3d49 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -13,11 +13,13 @@ use crate::{ use std::u32; +pub use iced_style::container::{Style, StyleSheet}; + /// An element decorating some content. /// /// It is normally used for alignment purposes. #[allow(missing_debug_implementations)] -pub struct Container<'a, Message, Renderer: self::Renderer> { +pub struct Container<'a, Message, Renderer> { padding: Padding, width: Length, height: Length, @@ -25,13 +27,13 @@ pub struct Container<'a, Message, Renderer: self::Renderer> { max_height: u32, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, - style: Renderer::Style, + style: &'a dyn StyleSheet, content: Element<'a, Message, Renderer>, } impl<'a, Message, Renderer> Container<'a, Message, Renderer> where - Renderer: self::Renderer, + Renderer: crate::Renderer, { /// Creates an empty [`Container`]. pub fn new(content: T) -> Self @@ -46,7 +48,7 @@ where max_height: u32::MAX, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, - style: Renderer::Style::default(), + style: Default::default(), content: content.into(), } } @@ -106,8 +108,8 @@ where } /// Sets the style of the [`Container`]. - 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 } } @@ -115,7 +117,7 @@ where impl<'a, Message, Renderer> Widget for Container<'a, Message, Renderer> where - Renderer: self::Renderer, + Renderer: crate::Renderer, { fn width(&self) -> Length { self.width @@ -211,21 +213,10 @@ where } } -/// The renderer of a [`Container`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Container`] in your user interface. -/// -/// [renderer]: crate::renderer -pub trait Renderer: crate::Renderer { - /// The style supported by this renderer. - type Style: Default; -} - impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Renderer: 'a + self::Renderer, + Renderer: 'a + crate::Renderer, Message: 'a, { fn from( -- cgit From 945f69e567d1766c82f77d3299d2d619d56d3add Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 15:44:03 +0700 Subject: Draw styling in `Widget::draw` for `Container` --- native/src/widget/container.rs | 44 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 99bc3d49..84c745e2 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -7,8 +7,8 @@ use crate::layout; use crate::overlay; use crate::renderer; use crate::{ - Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle, - Widget, + Background, Clipboard, Color, Element, Hasher, Layout, Length, Padding, + Point, Rectangle, Widget, }; use std::u32; @@ -27,7 +27,7 @@ pub struct Container<'a, Message, Renderer> { max_height: u32, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, - style: &'a dyn StyleSheet, + style_sheet: &'a dyn StyleSheet, content: Element<'a, Message, Renderer>, } @@ -48,7 +48,7 @@ where max_height: u32::MAX, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, - style: Default::default(), + style_sheet: Default::default(), content: content.into(), } } @@ -108,8 +108,8 @@ where } /// Sets the style of the [`Container`]. - 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 } } @@ -178,14 +178,22 @@ where fn draw( &self, renderer: &mut Renderer, - style: &renderer::Style, + renderer_style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, viewport: &Rectangle, ) { + let style = self.style_sheet.style(); + + draw_background(renderer, &style, layout.bounds()); + self.content.draw( renderer, - style, + &renderer::Style { + text_color: style + .text_color + .unwrap_or(renderer_style.text_color), + }, layout.children().next().unwrap(), cursor_position, viewport, @@ -213,6 +221,26 @@ where } } +pub fn draw_background( + renderer: &mut Renderer, + style: &Style, + bounds: Rectangle, +) where + Renderer: crate::Renderer, +{ + if style.background.is_some() || style.border_width > 0.0 { + renderer.fill_rectangle(renderer::Quad { + bounds, + background: style + .background + .unwrap_or(Background::Color(Color::TRANSPARENT)), + border_radius: style.border_radius, + border_width: style.border_width, + border_color: style.border_color, + }); + } +} + impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where -- cgit From 5130e98dc873f57279f7fa7acb0b7e6d12fb7edf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 16:45:53 +0700 Subject: Implement `Widget::mouse_interaction` for `Container` --- native/src/widget/container.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 84c745e2..006e07c6 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -4,6 +4,7 @@ use std::hash::Hash; use crate::alignment::{self, Alignment}; use crate::event::{self, Event}; use crate::layout; +use crate::mouse; use crate::overlay; use crate::renderer; use crate::{ @@ -175,6 +176,19 @@ where ) } + fn mouse_interaction( + &self, + layout: Layout<'_>, + viewport: &Rectangle, + cursor_position: Point, + ) -> mouse::Interaction { + self.content.widget.mouse_interaction( + layout.children().next().unwrap(), + viewport, + cursor_position, + ) + } + fn draw( &self, renderer: &mut Renderer, -- cgit From 40a5de581144886571504b762719f057dbb2e871 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 17:02:59 +0700 Subject: Reintroduce `Box` for `style_sheet` in `Container` --- native/src/widget/container.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 006e07c6..5ad07d6d 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -28,7 +28,7 @@ pub struct Container<'a, Message, Renderer> { max_height: u32, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, - style_sheet: &'a dyn StyleSheet, + style_sheet: Box, content: Element<'a, Message, Renderer>, } @@ -109,8 +109,11 @@ where } /// Sets the style of the [`Container`]. - 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/container.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 5ad07d6d..9e3d0193 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -182,13 +182,13 @@ where fn mouse_interaction( &self, layout: Layout<'_>, - viewport: &Rectangle, cursor_position: Point, + viewport: &Rectangle, ) -> mouse::Interaction { self.content.widget.mouse_interaction( layout.children().next().unwrap(), - viewport, cursor_position, + viewport, ) } -- 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/container.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'native/src/widget/container.rs') diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 9e3d0193..596af7fd 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -238,6 +238,7 @@ where } } +/// Draws the background of a [`Container`] given its [`Style`] and its `bounds`. pub fn draw_background( renderer: &mut Renderer, style: &Style, @@ -246,15 +247,17 @@ pub fn draw_background( Renderer: crate::Renderer, { if style.background.is_some() || style.border_width > 0.0 { - renderer.fill_rectangle(renderer::Quad { - bounds, - background: style + renderer.fill_quad( + renderer::Quad { + bounds, + border_radius: style.border_radius, + border_width: style.border_width, + border_color: style.border_color, + }, + style .background .unwrap_or(Background::Color(Color::TRANSPARENT)), - border_radius: style.border_radius, - border_width: style.border_width, - border_color: style.border_color, - }); + ); } } -- cgit