Implement Widget::draw for Rule

This commit is contained in:
Héctor Ramón Jiménez 2021-10-28 18:03:24 +07:00
parent d127dbd08e
commit f625797392
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
5 changed files with 66 additions and 57 deletions

View file

@ -1,10 +1,3 @@
//! Display a horizontal or vertical rule for dividing content. //! Display a horizontal or vertical rule for dividing content.
use crate::Renderer; pub use iced_graphics::rule::*;
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>;

View file

@ -1,18 +1,3 @@
//! Display a horizontal or vertical rule for dividing content. //! Display a horizontal or vertical rule for dividing content.
use crate::{Backend, Renderer}; pub use iced_native::rule::*;
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>;
}

View file

@ -1,4 +1,3 @@
use crate::progress_bar;
use crate::renderer::{self, Renderer}; use crate::renderer::{self, Renderer};
use crate::text; use crate::text;
use crate::toggler; use crate::toggler;

View file

@ -1,27 +1,32 @@
//! Display a horizontal or vertical rule for dividing content. //! Display a horizontal or vertical rule for dividing content.
use crate::layout; use crate::layout;
use crate::renderer; 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; use std::hash::Hash;
pub use iced_style::rule::{FillMode, Style, StyleSheet};
/// Display a horizontal or vertical rule for dividing content. /// Display a horizontal or vertical rule for dividing content.
#[derive(Debug, Copy, Clone)] #[allow(missing_debug_implementations)]
pub struct Rule<Renderer: self::Renderer> { pub struct Rule {
width: Length, width: Length,
height: Length, height: Length,
style: Renderer::Style,
is_horizontal: bool, 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. /// Creates a horizontal [`Rule`] for dividing content by the given vertical spacing.
pub fn horizontal(spacing: u16) -> Self { pub fn horizontal(spacing: u16) -> Self {
Rule { Rule {
width: Length::Fill, width: Length::Fill,
height: Length::from(Length::Units(spacing)), height: Length::from(Length::Units(spacing)),
style: Renderer::Style::default(),
is_horizontal: true, is_horizontal: true,
style_sheet: Default::default(),
} }
} }
@ -30,21 +35,24 @@ impl<Renderer: self::Renderer> Rule<Renderer> {
Rule { Rule {
width: Length::from(Length::Units(spacing)), width: Length::from(Length::Units(spacing)),
height: Length::Fill, height: Length::Fill,
style: Renderer::Style::default(),
is_horizontal: false, is_horizontal: false,
style_sheet: Default::default(),
} }
} }
/// Sets the style of the [`Rule`]. /// Sets the style of the [`Rule`].
pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self { pub fn style(
self.style = style.into(); mut self,
style_sheet: impl Into<Box<dyn StyleSheet>>,
) -> Self {
self.style_sheet = style_sheet.into();
self self
} }
} }
impl<Message, Renderer> Widget<Message, Renderer> for Rule<Renderer> impl<Message, Renderer> Widget<Message, Renderer> for Rule
where where
Renderer: self::Renderer, Renderer: crate::Renderer,
{ {
fn width(&self) -> Length { fn width(&self) -> Length {
self.width self.width
@ -67,13 +75,51 @@ where
fn draw( fn draw(
&self, &self,
renderer: &mut Renderer, renderer: &mut Renderer,
style: &renderer::Style, _style: &renderer::Style,
layout: Layout<'_>, layout: Layout<'_>,
_cursor_position: Point, _cursor_position: Point,
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
// TODO let bounds = layout.bounds();
// renderer.draw(layout.bounds(), &self.style, self.is_horizontal) 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) { fn hash_layout(&self, state: &mut Hasher) {
@ -85,19 +131,12 @@ where
} }
} }
/// The renderer of a [`Rule`]. impl<'a, Message, Renderer> From<Rule> for Element<'a, Message, Renderer>
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>
where where
Renderer: 'a + self::Renderer, Renderer: 'a + crate::Renderer,
Message: 'a, Message: 'a,
{ {
fn from(rule: Rule<Renderer>) -> Element<'a, Message, Renderer> { fn from(rule: Rule) -> Element<'a, Message, Renderer> {
Element::new(rule) Element::new(rule)
} }
} }

View file

@ -1,10 +1,3 @@
//! Display a horizontal or vertical rule for dividing content. //! Display a horizontal or vertical rule for dividing content.
use crate::Renderer; pub use iced_graphics::rule::*;
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>;