Allow custom renderers in Program and Application

This commit is contained in:
Héctor Ramón Jiménez 2024-03-22 07:09:51 +01:00
parent 4f2f40c68b
commit 5137d655e6
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
19 changed files with 162 additions and 122 deletions

View file

@ -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>;

View file

@ -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 = ();
}

View file

@ -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;

View file

@ -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;
}

29
graphics/src/settings.rs Normal file
View file

@ -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,
}
}
}