diff options
author | 2021-10-28 18:03:24 +0700 | |
---|---|---|
committer | 2021-10-28 18:05:26 +0700 | |
commit | f6257973926233e7bb18ae3b4dee4385bfc6ab61 (patch) | |
tree | b3ee879f35bf2673ca6b524d78293fa1067c50c4 /native/src/widget/rule.rs | |
parent | d127dbd08ec314e6a2c65961134bdc7b28a1e4aa (diff) | |
download | iced-f6257973926233e7bb18ae3b4dee4385bfc6ab61.tar.gz iced-f6257973926233e7bb18ae3b4dee4385bfc6ab61.tar.bz2 iced-f6257973926233e7bb18ae3b4dee4385bfc6ab61.zip |
Implement `Widget::draw` for `Rule`
Diffstat (limited to 'native/src/widget/rule.rs')
-rw-r--r-- | native/src/widget/rule.rs | 87 |
1 files changed, 63 insertions, 24 deletions
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) } } |