diff options
-rw-r--r-- | glow/src/widget/rule.rs | 9 | ||||
-rw-r--r-- | graphics/src/widget/rule.rs | 17 | ||||
-rw-r--r-- | native/src/renderer/null.rs | 1 | ||||
-rw-r--r-- | native/src/widget/rule.rs | 87 | ||||
-rw-r--r-- | wgpu/src/widget/rule.rs | 9 |
5 files changed, 66 insertions, 57 deletions
diff --git a/glow/src/widget/rule.rs b/glow/src/widget/rule.rs index faa2be86..40281773 100644 --- a/glow/src/widget/rule.rs +++ b/glow/src/widget/rule.rs @@ -1,10 +1,3 @@ //! Display a horizontal or vertical rule for dividing content. -use crate::Renderer; - -pub use iced_graphics::rule::{FillMode, Style, StyleSheet}; - -/// Display a horizontal or vertical rule for dividing content. -/// -/// This is an alias of an `iced_native` rule with an `iced_glow::Renderer`. -pub type Rule = iced_native::Rule<Renderer>; +pub use iced_graphics::rule::*; diff --git a/graphics/src/widget/rule.rs b/graphics/src/widget/rule.rs index 62766f82..57f4a530 100644 --- a/graphics/src/widget/rule.rs +++ b/graphics/src/widget/rule.rs @@ -1,18 +1,3 @@ //! Display a horizontal or vertical rule for dividing content. -use crate::{Backend, Renderer}; -use iced_native::rule; - -pub use iced_style::rule::{FillMode, Style, StyleSheet}; - -/// Display a horizontal or vertical rule for dividing content. -/// -/// This is an alias of an `iced_native` rule with an `iced_graphics::Renderer`. -pub type Rule<Backend> = iced_native::Rule<Renderer<Backend>>; - -impl<B> rule::Renderer for Renderer<B> -where - B: Backend, -{ - type Style = Box<dyn StyleSheet>; -} +pub use iced_native::rule::*; diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 92e64cf4..e91c4a80 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -1,4 +1,3 @@ -use crate::progress_bar; use crate::renderer::{self, Renderer}; use crate::text; use crate::toggler; 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<Renderer: self::Renderer> { +#[allow(missing_debug_implementations)] +pub struct Rule { width: Length, height: Length, - style: Renderer::Style, is_horizontal: bool, + style_sheet: Box<dyn StyleSheet>, } -impl<Renderer: self::Renderer> Rule<Renderer> { +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<Renderer: self::Renderer> Rule<Renderer> { 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<Renderer::Style>) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style_sheet: impl Into<Box<dyn StyleSheet>>, + ) -> Self { + self.style_sheet = style_sheet.into(); self } } -impl<Message, Renderer> Widget<Message, Renderer> for Rule<Renderer> +impl<Message, Renderer> Widget<Message, Renderer> 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<Rule<Renderer>> - for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From<Rule> for Element<'a, Message, Renderer> where - Renderer: 'a + self::Renderer, + Renderer: 'a + crate::Renderer, Message: 'a, { - fn from(rule: Rule<Renderer>) -> Element<'a, Message, Renderer> { + fn from(rule: Rule) -> Element<'a, Message, Renderer> { Element::new(rule) } } diff --git a/wgpu/src/widget/rule.rs b/wgpu/src/widget/rule.rs index 3f7bc67a..40281773 100644 --- a/wgpu/src/widget/rule.rs +++ b/wgpu/src/widget/rule.rs @@ -1,10 +1,3 @@ //! Display a horizontal or vertical rule for dividing content. -use crate::Renderer; - -pub use iced_graphics::rule::{FillMode, Style, StyleSheet}; - -/// Display a horizontal or vertical rule for dividing content. -/// -/// This is an alias of an `iced_native` rule with an `iced_wgpu::Renderer`. -pub type Rule = iced_native::Rule<Renderer>; +pub use iced_graphics::rule::*; |