iced_core/
renderer.rs

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