diff options
author | 2024-03-22 07:09:51 +0100 | |
---|---|---|
committer | 2024-03-22 07:09:51 +0100 | |
commit | 5137d655e6bbd29581fc1469d0385515113f2999 (patch) | |
tree | 46a3ff77014d7e7bd2047ce7c6e7dfe9b3a596cd /graphics | |
parent | 4f2f40c68b4647f281d34034beb159a41422aa06 (diff) | |
download | iced-5137d655e6bbd29581fc1469d0385515113f2999.tar.gz iced-5137d655e6bbd29581fc1469d0385515113f2999.tar.bz2 iced-5137d655e6bbd29581fc1469d0385515113f2999.zip |
Allow custom renderers in `Program` and `Application`
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/src/backend.rs | 6 | ||||
-rw-r--r-- | graphics/src/compositor.rs | 24 | ||||
-rw-r--r-- | graphics/src/lib.rs | 2 | ||||
-rw-r--r-- | graphics/src/renderer.rs | 8 | ||||
-rw-r--r-- | graphics/src/settings.rs | 29 |
5 files changed, 58 insertions, 11 deletions
diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index e982b54a..aa7bf4e8 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -2,16 +2,16 @@ use crate::core::image; use crate::core::svg; use crate::core::Size; -use crate::Mesh; +use crate::{Compositor, Mesh, Renderer}; use std::borrow::Cow; /// The graphics backend of a [`Renderer`]. /// /// [`Renderer`]: crate::Renderer -pub trait Backend { +pub trait Backend: Sized { /// The compositor of this [`Backend`]. - type Compositor; + type Compositor: Compositor<Renderer = Renderer<Self>>; /// The custom kind of primitives this [`Backend`] supports. type Primitive: TryFrom<Mesh, Error = &'static str>; diff --git a/graphics/src/compositor.rs b/graphics/src/compositor.rs index 32cea46a..4d548f30 100644 --- a/graphics/src/compositor.rs +++ b/graphics/src/compositor.rs @@ -1,9 +1,9 @@ //! A compositor is responsible for initializing a renderer and managing window //! surfaces. -use crate::{Error, Viewport}; - +use crate::core; use crate::core::Color; use crate::futures::{MaybeSend, MaybeSync}; +use crate::{Error, Settings, Viewport}; use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; use std::future::Future; @@ -11,9 +11,6 @@ use thiserror::Error; /// A graphics compositor that can draw to windows. pub trait Compositor: Sized { - /// The settings of the backend. - type Settings: Default; - /// The iced renderer of the backend. type Renderer; @@ -22,7 +19,7 @@ pub trait Compositor: Sized { /// Creates a new [`Compositor`]. fn new<W: Window + Clone>( - settings: Self::Settings, + settings: Settings, compatible_window: W, ) -> impl Future<Output = Result<Self, Error>>; @@ -93,6 +90,12 @@ impl<T> Window for T where { } +/// A renderer that supports composition. +pub trait Renderer: core::Renderer { + /// The compositor of the renderer. + type Compositor: Compositor<Renderer = Self>; +} + /// Result of an unsuccessful call to [`Compositor::present`]. #[derive(Clone, PartialEq, Eq, Debug, Error)] pub enum SurfaceError { @@ -123,13 +126,13 @@ pub struct Information { pub backend: String, } +#[cfg(debug_assertions)] impl Compositor for () { - type Settings = (); type Renderer = (); type Surface = (); async fn new<W: Window + Clone>( - _settings: Self::Settings, + _settings: Settings, _compatible_window: W, ) -> Result<Self, Error> { Ok(()) @@ -182,3 +185,8 @@ impl Compositor for () { vec![] } } + +#[cfg(debug_assertions)] +impl Renderer for () { + type Compositor = (); +} diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index a682b89b..2e476f8c 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -20,6 +20,7 @@ mod antialiasing; mod cached; mod error; mod primitive; +mod settings; mod viewport; pub mod backend; @@ -47,6 +48,7 @@ pub use gradient::Gradient; pub use mesh::Mesh; pub use primitive::Primitive; pub use renderer::Renderer; +pub use settings::Settings; pub use viewport::Viewport; pub use iced_core as core; diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index eb720495..5de7f97f 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -1,5 +1,6 @@ //! Create a renderer from a [`Backend`]. use crate::backend::{self, Backend}; +use crate::compositor; use crate::core; use crate::core::image; use crate::core::renderer; @@ -259,3 +260,10 @@ where self.draw_primitive(geometry); } } + +impl<B> compositor::Renderer for Renderer<B> +where + B: Backend, +{ + type Compositor = B::Compositor; +} diff --git a/graphics/src/settings.rs b/graphics/src/settings.rs new file mode 100644 index 00000000..68673536 --- /dev/null +++ b/graphics/src/settings.rs @@ -0,0 +1,29 @@ +use crate::core::{Font, Pixels}; +use crate::Antialiasing; + +/// The settings of a Backend. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Settings { + /// The default [`Font`] to use. + pub default_font: Font, + + /// The default size of text. + /// + /// By default, it will be set to `16.0`. + pub default_text_size: Pixels, + + /// The antialiasing strategy that will be used for triangle primitives. + /// + /// By default, it is `None`. + pub antialiasing: Option<Antialiasing>, +} + +impl Default for Settings { + fn default() -> Settings { + Settings { + default_font: Font::default(), + default_text_size: Pixels(16.0), + antialiasing: None, + } + } +} |