iced-yoda/native/src/widget/rule.rs

146 lines
3.7 KiB
Rust
Raw Normal View History

2020-08-13 12:54:34 -05:00
//! Display a horizontal or vertical rule for dividing content.
use crate::layout;
use crate::renderer;
use crate::widget::Tree;
use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget};
2020-08-13 12:54:34 -05:00
2022-06-01 01:56:46 +02:00
pub use iced_style::rule::{Appearance, FillMode, StyleSheet};
2021-10-28 18:03:24 +07:00
2020-08-13 12:54:34 -05:00
/// Display a horizontal or vertical rule for dividing content.
2021-10-28 18:03:24 +07:00
#[allow(missing_debug_implementations)]
2022-06-01 01:56:46 +02:00
pub struct Rule<Renderer>
where
Renderer: crate::Renderer,
Renderer::Theme: StyleSheet,
{
2020-08-13 12:54:34 -05:00
width: Length,
height: Length,
is_horizontal: bool,
2022-06-01 01:56:46 +02:00
style: <Renderer::Theme as StyleSheet>::Style,
2020-08-13 12:54:34 -05:00
}
2022-06-01 01:56:46 +02:00
impl<Renderer> Rule<Renderer>
where
Renderer: crate::Renderer,
Renderer::Theme: StyleSheet,
{
/// Creates a horizontal [`Rule`] with the given height.
pub fn horizontal(height: u16) -> Self {
2020-08-13 12:54:34 -05:00
Rule {
width: Length::Fill,
height: Length::Units(height),
2020-08-13 12:54:34 -05:00
is_horizontal: true,
2022-06-01 01:56:46 +02:00
style: Default::default(),
2020-08-13 12:54:34 -05:00
}
}
/// Creates a vertical [`Rule`] with the given width.
pub fn vertical(width: u16) -> Self {
2020-08-13 12:54:34 -05:00
Rule {
2022-07-04 01:17:29 +02:00
width: Length::Units(width),
2020-08-13 12:54:34 -05:00
height: Length::Fill,
is_horizontal: false,
2022-06-01 01:56:46 +02:00
style: Default::default(),
2020-08-13 12:54:34 -05:00
}
}
/// Sets the style of the [`Rule`].
2021-10-28 18:03:24 +07:00
pub fn style(
mut self,
2022-06-01 01:56:46 +02:00
style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
2021-10-28 18:03:24 +07:00
) -> Self {
2022-06-01 01:56:46 +02:00
self.style = style.into();
2020-08-13 12:54:34 -05:00
self
}
}
2022-06-01 01:56:46 +02:00
impl<Message, Renderer> Widget<Message, Renderer> for Rule<Renderer>
2020-08-13 12:54:34 -05:00
where
2021-10-28 18:03:24 +07:00
Renderer: crate::Renderer,
2022-06-01 01:56:46 +02:00
Renderer::Theme: StyleSheet,
2020-08-13 12:54:34 -05:00
{
fn width(&self) -> Length {
self.width
}
fn height(&self) -> Length {
self.height
}
fn layout(
&self,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
let limits = limits.width(self.width).height(self.height);
layout::Node::new(limits.resolve(Size::ZERO))
}
fn draw(
&self,
_state: &Tree,
2020-08-13 12:54:34 -05:00
renderer: &mut Renderer,
theme: &Renderer::Theme,
2021-10-28 18:03:24 +07:00
_style: &renderer::Style,
2020-08-13 12:54:34 -05:00
layout: Layout<'_>,
_cursor_position: Point,
2020-09-08 00:53:03 +02:00
_viewport: &Rectangle,
) {
2021-10-28 18:03:24 +07:00
let bounds = layout.bounds();
let style = theme.appearance(&self.style);
2021-10-28 18:03:24 +07:00
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_quad(
renderer::Quad {
bounds,
2022-11-03 00:35:01 +01:00
border_radius: style.radius.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
style.color,
);
2020-08-13 12:54:34 -05:00
}
}
2022-06-01 01:56:46 +02:00
impl<'a, Message, Renderer> From<Rule<Renderer>>
for Element<'a, Message, Renderer>
2020-08-13 12:54:34 -05:00
where
Message: 'a,
2022-06-01 01:56:46 +02:00
Renderer: 'a + crate::Renderer,
Renderer::Theme: StyleSheet,
2020-08-13 12:54:34 -05:00
{
2022-06-01 01:56:46 +02:00
fn from(rule: Rule<Renderer>) -> Element<'a, Message, Renderer> {
2020-08-13 12:54:34 -05:00
Element::new(rule)
}
}