1//! Write your own renderer.
2#[cfg(debug_assertions)]
3mod null;
45use crate::{
6 Background, Border, Color, Rectangle, Shadow, Size, Transformation, Vector,
7};
89/// A component that can be used by widgets to draw themselves on a screen.
10pub trait Renderer {
11/// Starts recording a new layer.
12fn start_layer(&mut self, bounds: Rectangle);
1314/// Ends recording a new layer.
15 ///
16 /// The new layer will clip its contents to the provided `bounds`.
17fn end_layer(&mut self);
1819/// Draws the primitives recorded in the given closure in a new layer.
20 ///
21 /// The layer will clip its contents to the provided `bounds`.
22fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) {
23self.start_layer(bounds);
24 f(self);
25self.end_layer();
26 }
2728/// Starts recording with a new [`Transformation`].
29fn start_transformation(&mut self, transformation: Transformation);
3031/// Ends recording a new layer.
32 ///
33 /// The new layer will clip its contents to the provided `bounds`.
34fn end_transformation(&mut self);
3536/// Applies a [`Transformation`] to the primitives recorded in the given closure.
37fn with_transformation(
38&mut self,
39 transformation: Transformation,
40 f: impl FnOnce(&mut Self),
41 ) {
42self.start_transformation(transformation);
43 f(self);
44self.end_transformation();
45 }
4647/// Applies a translation to the primitives recorded in the given closure.
48fn with_translation(
49&mut self,
50 translation: Vector,
51 f: impl FnOnce(&mut Self),
52 ) {
53self.with_transformation(
54 Transformation::translate(translation.x, translation.y),
55 f,
56 );
57 }
5859/// Fills a [`Quad`] with the provided [`Background`].
60fn fill_quad(&mut self, quad: Quad, background: impl Into<Background>);
6162/// Clears all of the recorded primitives in the [`Renderer`].
63fn clear(&mut self);
64}
6566/// A polygon with four sides.
67#[derive(Debug, Clone, Copy, PartialEq)]
68pub struct Quad {
69/// The bounds of the [`Quad`].
70pub bounds: Rectangle,
7172/// The [`Border`] of the [`Quad`]. The border is drawn on the inside of the [`Quad`].
73pub border: Border,
7475/// The [`Shadow`] of the [`Quad`].
76pub shadow: Shadow,
77}
7879impl Default for Quad {
80fn default() -> Self {
81Self {
82 bounds: Rectangle::with_size(Size::ZERO),
83 border: Border::default(),
84 shadow: Shadow::default(),
85 }
86 }
87}
8889/// The styling attributes of a [`Renderer`].
90#[derive(Debug, Clone, Copy, PartialEq)]
91pub struct Style {
92/// The color to apply to symbolic icons.
93pub icon_color: Color,
94/// The text color
95pub text_color: Color,
96/// The scale factor
97pub scale_factor: f64,
98}
99100impl Default for Style {
101fn default() -> Self {
102 Style {
103 icon_color: Color::BLACK,
104 text_color: Color::BLACK,
105 scale_factor: 1.0,
106 }
107 }
108}