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/rule.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'native/src/widget/rule.rs') diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs index 18c88658..1fab77bc 100644 --- a/native/src/widget/rule.rs +++ b/native/src/widget/rule.rs @@ -72,8 +72,9 @@ where layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, - ) -> Renderer::Output { - renderer.draw(layout.bounds(), &self.style, self.is_horizontal) + ) { + // TODO + // renderer.draw(layout.bounds(), &self.style, self.is_horizontal) } fn hash_layout(&self, state: &mut Hasher) { @@ -89,19 +90,6 @@ where pub trait Renderer: crate::Renderer { /// The style supported by this renderer. type Style: Default; - - /// Draws a [`Rule`]. - /// - /// It receives: - /// * the bounds of the [`Rule`] - /// * the style of the [`Rule`] - /// * whether the [`Rule`] is horizontal (true) or vertical (false) - fn draw( - &mut self, - bounds: Rectangle, - style: &Self::Style, - is_horizontal: bool, - ) -> 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/rule.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'native/src/widget/rule.rs') diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs index 1fab77bc..24c4a51a 100644 --- a/native/src/widget/rule.rs +++ b/native/src/widget/rule.rs @@ -1,11 +1,10 @@ //! Display a horizontal or vertical rule for dividing content. +use crate::layout; +use crate::renderer; +use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget}; use std::hash::Hash; -use crate::{ - layout, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, -}; - /// Display a horizontal or vertical rule for dividing content. #[derive(Debug, Copy, Clone)] pub struct Rule { @@ -68,7 +67,7 @@ where fn draw( &self, renderer: &mut Renderer, - _defaults: &Renderer::Defaults, + style: &renderer::Style, layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, -- cgit From f6257973926233e7bb18ae3b4dee4385bfc6ab61 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 18:03:24 +0700 Subject: Implement `Widget::draw` for `Rule` --- native/src/widget/rule.rs | 87 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 24 deletions(-) (limited to 'native/src/widget/rule.rs') diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs index 24c4a51a..f0d50006 100644 --- a/native/src/widget/rule.rs +++ b/native/src/widget/rule.rs @@ -1,27 +1,32 @@ //! Display a horizontal or vertical rule for dividing content. use crate::layout; use crate::renderer; -use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget}; +use crate::{ + Background, Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, + Widget, +}; use std::hash::Hash; +pub use iced_style::rule::{FillMode, Style, StyleSheet}; + /// Display a horizontal or vertical rule for dividing content. -#[derive(Debug, Copy, Clone)] -pub struct Rule { +#[allow(missing_debug_implementations)] +pub struct Rule { width: Length, height: Length, - style: Renderer::Style, is_horizontal: bool, + style_sheet: Box, } -impl Rule { +impl Rule { /// Creates a horizontal [`Rule`] for dividing content by the given vertical spacing. pub fn horizontal(spacing: u16) -> Self { Rule { width: Length::Fill, height: Length::from(Length::Units(spacing)), - style: Renderer::Style::default(), is_horizontal: true, + style_sheet: Default::default(), } } @@ -30,21 +35,24 @@ impl Rule { Rule { width: Length::from(Length::Units(spacing)), height: Length::Fill, - style: Renderer::Style::default(), is_horizontal: false, + style_sheet: Default::default(), } } /// Sets the style of the [`Rule`]. - pub fn style(mut self, style: impl Into) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style_sheet: impl Into>, + ) -> Self { + self.style_sheet = style_sheet.into(); self } } -impl Widget for Rule +impl Widget for Rule where - Renderer: self::Renderer, + Renderer: crate::Renderer, { fn width(&self) -> Length { self.width @@ -67,13 +75,51 @@ where fn draw( &self, renderer: &mut Renderer, - style: &renderer::Style, + _style: &renderer::Style, layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, ) { - // TODO - // renderer.draw(layout.bounds(), &self.style, self.is_horizontal) + let bounds = layout.bounds(); + let style = self.style_sheet.style(); + + let bounds = if self.is_horizontal { + let line_y = (bounds.y + (bounds.height / 2.0) + - (style.width as f32 / 2.0)) + .round(); + + let (offset, line_width) = style.fill_mode.fill(bounds.width); + let line_x = bounds.x + offset; + + Rectangle { + x: line_x, + y: line_y, + width: line_width, + height: style.width as f32, + } + } else { + let line_x = (bounds.x + (bounds.width / 2.0) + - (style.width as f32 / 2.0)) + .round(); + + let (offset, line_height) = style.fill_mode.fill(bounds.height); + let line_y = bounds.y + offset; + + Rectangle { + x: line_x, + y: line_y, + width: style.width as f32, + height: line_height, + } + }; + + renderer.fill_rectangle(renderer::Quad { + bounds, + background: Background::Color(style.color), + border_radius: style.radius, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }); } fn hash_layout(&self, state: &mut Hasher) { @@ -85,19 +131,12 @@ where } } -/// The renderer of a [`Rule`]. -pub trait Renderer: crate::Renderer { - /// The style supported by this renderer. - type Style: Default; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> where - Renderer: 'a + self::Renderer, + Renderer: 'a + crate::Renderer, Message: 'a, { - fn from(rule: Rule) -> Element<'a, Message, Renderer> { + fn from(rule: Rule) -> Element<'a, Message, Renderer> { Element::new(rule) } } -- cgit From 48490c3d878da1e2760c7701e80586c3653d5bd8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 17:34:58 +0700 Subject: Introduce state lifetime for `style_sheet` in `Rule` --- native/src/widget/rule.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'native/src/widget/rule.rs') diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs index f0d50006..8f4e66a6 100644 --- a/native/src/widget/rule.rs +++ b/native/src/widget/rule.rs @@ -12,14 +12,14 @@ pub use iced_style::rule::{FillMode, Style, StyleSheet}; /// Display a horizontal or vertical rule for dividing content. #[allow(missing_debug_implementations)] -pub struct Rule { +pub struct Rule<'a> { width: Length, height: Length, is_horizontal: bool, - style_sheet: Box, + style_sheet: Box, } -impl Rule { +impl<'a> Rule<'a> { /// Creates a horizontal [`Rule`] for dividing content by the given vertical spacing. pub fn horizontal(spacing: u16) -> Self { Rule { @@ -43,14 +43,14 @@ impl Rule { /// Sets the style of the [`Rule`]. pub fn style( mut self, - style_sheet: impl Into>, + style_sheet: impl Into>, ) -> Self { self.style_sheet = style_sheet.into(); self } } -impl Widget for Rule +impl<'a, Message, Renderer> Widget for Rule<'a> where Renderer: crate::Renderer, { @@ -131,12 +131,12 @@ where } } -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where Renderer: 'a + crate::Renderer, Message: 'a, { - fn from(rule: Rule) -> Element<'a, Message, Renderer> { + fn from(rule: Rule<'a>) -> Element<'a, Message, Renderer> { Element::new(rule) } } -- 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/rule.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'native/src/widget/rule.rs') diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs index 8f4e66a6..7c8c5dbc 100644 --- a/native/src/widget/rule.rs +++ b/native/src/widget/rule.rs @@ -2,8 +2,7 @@ use crate::layout; use crate::renderer; use crate::{ - Background, Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, - Widget, + Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; use std::hash::Hash; @@ -113,13 +112,15 @@ where } }; - renderer.fill_rectangle(renderer::Quad { - bounds, - background: Background::Color(style.color), - border_radius: style.radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }); + renderer.fill_quad( + renderer::Quad { + bounds, + border_radius: style.radius, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }, + style.color, + ); } fn hash_layout(&self, state: &mut Hasher) { -- cgit