2019-08-29 01:35:37 +02:00
|
|
|
//! Write your own renderer.
|
2024-03-22 05:41:15 +01:00
|
|
|
#[cfg(debug_assertions)]
|
2019-12-29 12:29:47 +01:00
|
|
|
mod null;
|
2023-03-04 05:37:11 +01:00
|
|
|
|
2025-10-25 00:07:13 +02:00
|
|
|
use crate::image;
|
2023-10-24 05:34:03 +02:00
|
|
|
use crate::{
|
2024-12-14 03:49:24 +01:00
|
|
|
Background, Border, Color, Font, Pixels, Rectangle, Shadow, Size,
|
|
|
|
|
Transformation, Vector,
|
2023-10-24 05:34:03 +02:00
|
|
|
};
|
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.
|
2024-03-21 05:52:48 +01:00
|
|
|
pub trait Renderer {
|
2024-03-21 22:27:17 +01:00
|
|
|
/// Starts recording a new layer.
|
2024-04-03 21:07:54 +02:00
|
|
|
fn start_layer(&mut self, bounds: Rectangle);
|
2024-03-21 22:27:17 +01:00
|
|
|
|
|
|
|
|
/// Ends recording a new layer.
|
|
|
|
|
///
|
|
|
|
|
/// The new layer will clip its contents to the provided `bounds`.
|
2024-04-09 22:25:16 +02:00
|
|
|
fn end_layer(&mut self);
|
2024-03-21 22:27:17 +01: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`.
|
2024-03-21 22:27:17 +01:00
|
|
|
fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) {
|
2024-04-03 21:07:54 +02:00
|
|
|
self.start_layer(bounds);
|
2024-03-21 22:27:17 +01:00
|
|
|
f(self);
|
2024-04-09 22:25:16 +02:00
|
|
|
self.end_layer();
|
2024-03-21 22:27:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Starts recording with a new [`Transformation`].
|
2024-04-03 21:07:54 +02:00
|
|
|
fn start_transformation(&mut self, transformation: Transformation);
|
2024-03-21 22:27:17 +01:00
|
|
|
|
|
|
|
|
/// Ends recording a new layer.
|
|
|
|
|
///
|
|
|
|
|
/// The new layer will clip its contents to the provided `bounds`.
|
2024-04-09 22:25:16 +02:00
|
|
|
fn end_transformation(&mut self);
|
2021-10-25 16:16:35 +07:00
|
|
|
|
2023-10-24 05:34:03 +02:00
|
|
|
/// Applies a [`Transformation`] to the primitives recorded in the given closure.
|
|
|
|
|
fn with_transformation(
|
|
|
|
|
&mut self,
|
|
|
|
|
transformation: Transformation,
|
|
|
|
|
f: impl FnOnce(&mut Self),
|
2024-03-21 22:27:17 +01:00
|
|
|
) {
|
2024-04-03 21:07:54 +02:00
|
|
|
self.start_transformation(transformation);
|
2024-03-21 22:27:17 +01:00
|
|
|
f(self);
|
2024-04-09 22:25:16 +02:00
|
|
|
self.end_transformation();
|
2024-03-21 22:27:17 +01:00
|
|
|
}
|
2023-10-24 05:34:03 +02: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),
|
2023-10-24 05:34:03 +02:00
|
|
|
) {
|
|
|
|
|
self.with_transformation(
|
|
|
|
|
Transformation::translate(translation.x, translation.y),
|
|
|
|
|
f,
|
|
|
|
|
);
|
|
|
|
|
}
|
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
|
|
|
|
2025-08-17 00:58:37 +02:00
|
|
|
/// Resets the [`Renderer`] to start drawing in the `new_bounds` from scratch.
|
|
|
|
|
fn reset(&mut self, new_bounds: Rectangle);
|
2025-10-25 00:07:13 +02:00
|
|
|
|
|
|
|
|
/// Creates an [`image::Allocation`] for the given [`image::Handle`] and calls the given callback with it.
|
|
|
|
|
fn allocate_image(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &image::Handle,
|
2025-10-28 21:19:25 +01:00
|
|
|
callback: impl FnOnce(Result<image::Allocation, image::Error>)
|
|
|
|
|
+ Send
|
|
|
|
|
+ 'static,
|
2025-10-25 00:07:13 +02:00
|
|
|
);
|
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
|
|
|
|
2024-06-29 21:39:12 +01:00
|
|
|
/// The [`Border`] of the [`Quad`]. The border is drawn on the inside of the [`Quad`].
|
2024-01-20 13:29:25 +01:00
|
|
|
pub border: Border,
|
2021-11-05 15:31:33 +07:00
|
|
|
|
2024-01-20 13:29:25 +01:00
|
|
|
/// The [`Shadow`] of the [`Quad`].
|
|
|
|
|
pub shadow: Shadow,
|
2025-05-30 00:30:23 +02:00
|
|
|
|
|
|
|
|
/// Whether the [`Quad`] should be snapped to the pixel grid.
|
|
|
|
|
pub snap: bool,
|
2024-01-20 12:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Quad {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
bounds: Rectangle::with_size(Size::ZERO),
|
2024-01-20 13:29:25 +01:00
|
|
|
border: Border::default(),
|
|
|
|
|
shadow: Shadow::default(),
|
2025-05-30 00:30:23 +02:00
|
|
|
snap: cfg!(feature = "crisp"),
|
2024-01-20 12:11:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-14 03:49:24 +01:00
|
|
|
|
|
|
|
|
/// A headless renderer is a renderer that can render offscreen without
|
|
|
|
|
/// a window nor a compositor.
|
|
|
|
|
pub trait Headless {
|
|
|
|
|
/// Creates a new [`Headless`] renderer;
|
2025-03-24 01:28:39 +01:00
|
|
|
fn new(
|
|
|
|
|
default_font: Font,
|
|
|
|
|
default_text_size: Pixels,
|
2025-03-24 20:18:24 +01:00
|
|
|
backend: Option<&str>,
|
2025-03-24 01:28:39 +01:00
|
|
|
) -> impl Future<Output = Option<Self>>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
2024-12-14 03:49:24 +01:00
|
|
|
|
2025-03-24 20:18:24 +01:00
|
|
|
/// Returns the unique name of the renderer.
|
|
|
|
|
///
|
|
|
|
|
/// This name may be used by testing libraries to uniquely identify
|
|
|
|
|
/// snapshots.
|
|
|
|
|
fn name(&self) -> String;
|
|
|
|
|
|
2024-12-14 03:49:24 +01:00
|
|
|
/// Draws offscreen into a screenshot, returning a collection of
|
|
|
|
|
/// bytes representing the rendered pixels in RGBA order.
|
|
|
|
|
fn screenshot(
|
|
|
|
|
&mut self,
|
|
|
|
|
size: Size<u32>,
|
|
|
|
|
scale_factor: f32,
|
|
|
|
|
background_color: Color,
|
|
|
|
|
) -> Vec<u8>;
|
|
|
|
|
}
|