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 {
|
2022-05-14 01:47:55 +02:00
|
|
|
/// The supported theme of the [`Renderer`].
|
|
|
|
|
type Theme;
|
|
|
|
|
|
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.
|
2023-01-27 13:37:32 -07:00
|
|
|
fn layout<Message>(
|
2019-11-11 06:07:31 +01:00
|
|
|
&mut self,
|
2023-01-27 13:37:32 -07:00
|
|
|
element: &Element<'_, Message, Self>,
|
2020-01-10 03:10:58 +01:00
|
|
|
limits: &layout::Limits,
|
2019-11-11 06:07:31 +01:00
|
|
|
) -> layout::Node {
|
2022-07-27 06:49:20 +02:00
|
|
|
element.as_widget().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
|
|
|
/// Fills a [`Quad`] with the provided [`Background`].
|
|
|
|
|
fn fill_quad(&mut self, quad: Quad, background: impl Into<Background>);
|
2023-01-12 02:59:08 +01:00
|
|
|
|
|
|
|
|
/// Clears all of the recorded primitives in the [`Renderer`].
|
|
|
|
|
fn clear(&mut self);
|
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`].
|
2022-11-08 04:59:34 +01:00
|
|
|
pub border_radius: BorderRadius,
|
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
|
|
|
|
2022-11-08 04:59:34 +01:00
|
|
|
/// The border radi for the corners of a graphics primitive in the order:
|
2022-11-03 00:35:01 +01:00
|
|
|
/// top-left, top-right, bottom-right, bottom-left.
|
2022-11-08 04:59:34 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
|
|
|
|
pub struct BorderRadius([f32; 4]);
|
2022-11-03 00:35:01 +01:00
|
|
|
|
2022-11-08 04:59:34 +01:00
|
|
|
impl From<f32> for BorderRadius {
|
2022-11-03 00:35:01 +01:00
|
|
|
fn from(w: f32) -> Self {
|
|
|
|
|
Self([w; 4])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 04:59:34 +01:00
|
|
|
impl From<[f32; 4]> for BorderRadius {
|
2022-11-03 00:35:01 +01:00
|
|
|
fn from(radi: [f32; 4]) -> Self {
|
|
|
|
|
Self(radi)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 04:59:34 +01:00
|
|
|
impl From<BorderRadius> for [f32; 4] {
|
|
|
|
|
fn from(radi: BorderRadius) -> Self {
|
|
|
|
|
radi.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|