iced-yoda/widget/src/rule.rs

251 lines
6.7 KiB
Rust
Raw Normal View History

2020-08-13 12:54:34 -05:00
//! Display a horizontal or vertical rule for dividing content.
2024-03-05 22:38:27 +01:00
use crate::core::border::{self, Border};
use crate::core::layout;
2023-06-08 20:11:59 +02:00
use crate::core::mouse;
use crate::core::renderer;
use crate::core::widget::Tree;
use crate::core::{
2024-03-07 00:14:41 +01:00
Color, Element, Layout, Length, Pixels, Rectangle, Size, Theme, Widget,
};
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)]
2024-03-12 13:44:03 +01:00
pub struct Rule<'a, Theme = crate::Theme> {
2020-08-13 12:54:34 -05:00
width: Length,
height: Length,
is_horizontal: bool,
2024-03-12 13:44:03 +01:00
style: Style<'a, Theme>,
2020-08-13 12:54:34 -05:00
}
2024-03-12 13:44:03 +01:00
impl<'a, Theme> Rule<'a, Theme> {
/// Creates a horizontal [`Rule`] with the given height.
2024-03-05 22:38:27 +01:00
pub fn horizontal(height: impl Into<Pixels>) -> Self
where
2024-03-12 13:44:03 +01:00
Theme: DefaultStyle + 'a,
2024-03-05 22:38:27 +01:00
{
2020-08-13 12:54:34 -05:00
Rule {
width: Length::Fill,
height: Length::Fixed(height.into().0),
2020-08-13 12:54:34 -05:00
is_horizontal: true,
2024-03-12 13:44:03 +01:00
style: Box::new(Theme::default_style),
2020-08-13 12:54:34 -05:00
}
}
/// Creates a vertical [`Rule`] with the given width.
2024-03-05 22:38:27 +01:00
pub fn vertical(width: impl Into<Pixels>) -> Self
where
2024-03-12 13:44:03 +01:00
Theme: DefaultStyle + 'a,
2024-03-05 22:38:27 +01:00
{
2020-08-13 12:54:34 -05:00
Rule {
width: Length::Fixed(width.into().0),
2020-08-13 12:54:34 -05:00
height: Length::Fill,
is_horizontal: false,
2024-03-12 13:44:03 +01:00
style: Box::new(Theme::default_style),
2020-08-13 12:54:34 -05:00
}
}
/// Sets the style of the [`Rule`].
2024-03-12 13:44:03 +01:00
pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = Box::new(style);
2020-08-13 12:54:34 -05:00
self
}
}
2024-03-12 13:44:03 +01:00
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Rule<'a, Theme>
2020-08-13 12:54:34 -05:00
where
Renderer: crate::core::Renderer,
2020-08-13 12:54:34 -05:00
{
fn size(&self) -> Size<Length> {
Size {
width: self.width,
height: self.height,
}
2020-08-13 12:54:34 -05:00
}
fn layout(
&self,
_tree: &mut Tree,
2020-08-13 12:54:34 -05:00
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
layout::atomic(limits, self.width, self.height)
2020-08-13 12:54:34 -05:00
}
fn draw(
&self,
_state: &Tree,
2020-08-13 12:54:34 -05:00
renderer: &mut Renderer,
theme: &Theme,
2021-10-28 18:03:24 +07:00
_style: &renderer::Style,
2020-08-13 12:54:34 -05:00
layout: Layout<'_>,
2023-06-08 20:11:59 +02:00
_cursor: mouse::Cursor,
2020-09-08 00:53:03 +02:00
_viewport: &Rectangle,
) {
2021-10-28 18:03:24 +07:00
let bounds = layout.bounds();
let appearance = (self.style)(theme);
2021-10-28 18:03:24 +07:00
let bounds = if self.is_horizontal {
let line_y = (bounds.y + (bounds.height / 2.0)
2024-03-05 22:38:27 +01:00
- (appearance.width as f32 / 2.0))
2021-10-28 18:03:24 +07:00
.round();
2024-03-05 22:38:27 +01:00
let (offset, line_width) = appearance.fill_mode.fill(bounds.width);
2021-10-28 18:03:24 +07:00
let line_x = bounds.x + offset;
Rectangle {
x: line_x,
y: line_y,
width: line_width,
2024-03-05 22:38:27 +01:00
height: appearance.width as f32,
2021-10-28 18:03:24 +07:00
}
} else {
let line_x = (bounds.x + (bounds.width / 2.0)
2024-03-05 22:38:27 +01:00
- (appearance.width as f32 / 2.0))
2021-10-28 18:03:24 +07:00
.round();
2024-03-05 22:38:27 +01:00
let (offset, line_height) =
appearance.fill_mode.fill(bounds.height);
2021-10-28 18:03:24 +07:00
let line_y = bounds.y + offset;
Rectangle {
x: line_x,
y: line_y,
2024-03-05 22:38:27 +01:00
width: appearance.width as f32,
2021-10-28 18:03:24 +07:00
height: line_height,
}
};
renderer.fill_quad(
renderer::Quad {
bounds,
border: Border::rounded(appearance.radius),
..renderer::Quad::default()
},
2024-03-05 22:38:27 +01:00
appearance.color,
);
2020-08-13 12:54:34 -05:00
}
}
2024-03-12 13:44:03 +01:00
impl<'a, Message, Theme, Renderer> From<Rule<'a, Theme>>
for Element<'a, Message, Theme, Renderer>
2020-08-13 12:54:34 -05:00
where
Message: 'a,
2024-03-05 22:38:27 +01:00
Theme: 'a,
Renderer: 'a + crate::core::Renderer,
2020-08-13 12:54:34 -05:00
{
2024-03-12 13:44:03 +01:00
fn from(rule: Rule<'a, Theme>) -> Element<'a, Message, Theme, Renderer> {
2020-08-13 12:54:34 -05:00
Element::new(rule)
}
}
2024-03-05 22:38:27 +01:00
/// The appearance of a rule.
#[derive(Debug, Clone, Copy)]
pub struct Appearance {
/// The color of the rule.
pub color: Color,
/// The width (thickness) of the rule line.
pub width: u16,
/// The radius of the line corners.
pub radius: border::Radius,
/// The [`FillMode`] of the rule.
pub fill_mode: FillMode,
}
/// The fill mode of a rule.
#[derive(Debug, Clone, Copy)]
pub enum FillMode {
/// Fill the whole length of the container.
Full,
/// Fill a percent of the length of the container. The rule
/// will be centered in that container.
///
/// The range is `[0.0, 100.0]`.
Percent(f32),
/// Uniform offset from each end, length units.
Padded(u16),
/// Different offset on each end of the rule, length units.
/// First = top or left.
AsymmetricPadding(u16, u16),
}
impl FillMode {
/// Return the starting offset and length of the rule.
///
/// * `space` - The space to fill.
///
/// # Returns
///
/// * (`starting_offset`, `length`)
pub fn fill(&self, space: f32) -> (f32, f32) {
match *self {
FillMode::Full => (0.0, space),
FillMode::Percent(percent) => {
if percent >= 100.0 {
(0.0, space)
} else {
let percent_width = (space * percent / 100.0).round();
(((space - percent_width) / 2.0).round(), percent_width)
}
}
FillMode::Padded(padding) => {
if padding == 0 {
(0.0, space)
} else {
let padding = padding as f32;
let mut line_width = space - (padding * 2.0);
if line_width < 0.0 {
line_width = 0.0;
}
(padding, line_width)
}
}
FillMode::AsymmetricPadding(first_pad, second_pad) => {
let first_pad = first_pad as f32;
let second_pad = second_pad as f32;
let mut line_width = space - first_pad - second_pad;
if line_width < 0.0 {
line_width = 0.0;
}
(first_pad, line_width)
}
}
}
}
/// The style of a [`Rule`].
2024-03-12 13:44:03 +01:00
pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>;
/// The default style of a [`Rule`].
pub trait DefaultStyle {
/// Returns the default style of a [`Rule`].
2024-03-12 13:44:03 +01:00
fn default_style(&self) -> Appearance;
}
impl DefaultStyle for Theme {
2024-03-12 13:44:03 +01:00
fn default_style(&self) -> Appearance {
default(self)
}
2024-03-05 22:38:27 +01:00
}
impl DefaultStyle for Appearance {
2024-03-12 13:44:03 +01:00
fn default_style(&self) -> Appearance {
*self
2024-03-05 22:38:27 +01:00
}
}
/// The default styling of a [`Rule`].
pub fn default(theme: &Theme) -> Appearance {
let palette = theme.extended_palette();
Appearance {
color: palette.background.strong.color,
width: 1,
radius: 0.0.into(),
fill_mode: FillMode::Full,
}
}