2019-08-29 01:35:37 +02:00
|
|
|
//! Write your own renderer.
|
2019-12-29 12:29:47 +01:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
mod null;
|
2019-11-21 13:47:20 +01:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
pub use null::Null;
|
2019-10-05 03:56:18 +02:00
|
|
|
|
2021-10-14 16:59:19 +07:00
|
|
|
use crate::layout;
|
2021-10-18 14:47:49 +07:00
|
|
|
use crate::{Background, Color, Element, Rectangle, Vector};
|
2019-11-11 06:07:31 +01:00
|
|
|
|
2022-04-30 14:20:52 +02:00
|
|
|
/// A component that can be used by widgets to draw themselves on a screen.
|
2019-11-11 06:07:31 +01:00
|
|
|
pub trait Renderer: Sized {
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Lays out the elements of a user interface.
|
|
|
|
|
///
|
|
|
|
|
/// You should override this if you need to perform any operations before or
|
|
|
|
|
/// after layouting. For instance, trimming the measurements cache.
|
2019-11-11 06:07:31 +01:00
|
|
|
fn layout<'a, Message>(
|
|
|
|
|
&mut self,
|
|
|
|
|
element: &Element<'a, Message, Self>,
|
2020-01-10 03:10:58 +01:00
|
|
|
limits: &layout::Limits,
|
2019-11-11 06:07:31 +01:00
|
|
|
) -> layout::Node {
|
2020-01-10 03:10:58 +01:00
|
|
|
element.layout(self, limits)
|
2019-11-11 06:07:31 +01:00
|
|
|
}
|
2020-04-16 13:22:00 +02:00
|
|
|
|
2021-11-04 19:22:29 +07:00
|
|
|
/// Draws the primitives recorded in the given closure in a new layer.
|
|
|
|
|
///
|
|
|
|
|
/// The layer will clip its contents to the provided `bounds`.
|
2021-10-25 16:16:35 +07:00
|
|
|
fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self));
|
|
|
|
|
|
2021-11-04 19:22:29 +07:00
|
|
|
/// Applies a `translation` to the primitives recorded in the given closure.
|
2021-10-25 16:16:35 +07:00
|
|
|
fn with_translation(
|
2021-10-14 17:15:29 +07:00
|
|
|
&mut self,
|
2021-10-25 16:16:35 +07:00
|
|
|
translation: Vector,
|
2021-10-14 17:15:29 +07:00
|
|
|
f: impl FnOnce(&mut Self),
|
|
|
|
|
);
|
2021-10-14 16:59:19 +07:00
|
|
|
|
2021-11-04 19:22:29 +07:00
|
|
|
/// Clears all of the recorded primitives in the [`Renderer`].
|
2021-10-14 16:59:19 +07:00
|
|
|
fn clear(&mut self);
|
2021-10-18 14:47:49 +07:00
|
|
|
|
2021-11-04 19:22:29 +07:00
|
|
|
/// Fills a [`Quad`] with the provided [`Background`].
|
|
|
|
|
fn fill_quad(&mut self, quad: Quad, background: impl Into<Background>);
|
2021-10-18 14:47:49 +07:00
|
|
|
}
|
|
|
|
|
|
2021-11-05 15:31:33 +07:00
|
|
|
/// A polygon with four sides.
|
2021-10-18 14:47:49 +07:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
|
pub struct Quad {
|
2021-11-05 15:31:33 +07:00
|
|
|
/// The bounds of the [`Quad`].
|
2021-10-18 14:47:49 +07:00
|
|
|
pub bounds: Rectangle,
|
2021-11-05 15:31:33 +07:00
|
|
|
|
|
|
|
|
/// The border radius of the [`Quad`].
|
2021-10-18 14:47:49 +07:00
|
|
|
pub border_radius: f32,
|
2021-11-05 15:31:33 +07:00
|
|
|
|
|
|
|
|
/// The border width of the [`Quad`].
|
2021-10-18 14:47:49 +07:00
|
|
|
pub border_width: f32,
|
2021-11-05 15:31:33 +07:00
|
|
|
|
|
|
|
|
/// The border color of the [`Quad`].
|
2021-10-18 14:47:49 +07:00
|
|
|
pub border_color: Color,
|
2019-10-05 03:56:18 +02:00
|
|
|
}
|
2021-10-18 15:19:04 +07:00
|
|
|
|
|
|
|
|
/// The styling attributes of a [`Renderer`].
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
|
pub struct Style {
|
|
|
|
|
/// The text color
|
|
|
|
|
pub text_color: Color,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Style {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Style {
|
|
|
|
|
text_color: Color::BLACK,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|