Implement composable, type-safe renderer fallback

This commit is contained in:
Héctor Ramón Jiménez 2024-03-21 22:27:17 +01:00
parent 7e4ae8450e
commit 3645d34d6a
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
35 changed files with 1474 additions and 1210 deletions

View file

@ -7,7 +7,6 @@ pub use event::Event;
pub use program::Program;
pub use crate::graphics::geometry::*;
pub use crate::renderer::geometry::*;
use crate::core;
use crate::core::layout::{self, Layout};
@ -21,13 +20,19 @@ use crate::graphics::geometry;
use std::marker::PhantomData;
/// A simple cache that stores generated [`Geometry`] to avoid recomputation.
///
/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
/// change or it is explicitly cleared.
pub type Cache<Renderer = crate::Renderer> = geometry::Cache<Renderer>;
/// A widget capable of drawing 2D graphics.
///
/// ## Drawing a simple circle
/// If you want to get a quick overview, here's how we can draw a simple circle:
///
/// ```no_run
/// # use iced_widget::canvas::{self, Canvas, Fill, Frame, Geometry, Path, Program};
/// # use iced_widget::canvas::{self, frame, Canvas, Fill, Frame, Path, Program};
/// # use iced_widget::core::{Color, Rectangle};
/// # use iced_widget::core::mouse;
/// # use iced_widget::{Renderer, Theme};
@ -42,9 +47,9 @@ use std::marker::PhantomData;
/// impl Program<()> for Circle {
/// type State = ();
///
/// fn draw(&self, _state: &(), renderer: &Renderer, _theme: &Theme, bounds: Rectangle, _cursor: mouse::Cursor) -> Vec<Geometry>{
/// fn draw(&self, _state: &(), renderer: &mut Renderer, _theme: &Theme, bounds: Rectangle, _cursor: mouse::Cursor) {
/// // We prepare a new `Frame`
/// let mut frame = Frame::new(renderer, bounds.size());
/// let mut frame = frame(renderer, bounds.size());
///
/// // We create a `Path` representing a simple circle
/// let circle = Path::circle(frame.center(), self.radius);
@ -53,7 +58,7 @@ use std::marker::PhantomData;
/// frame.fill(&circle, Color::BLACK);
///
/// // Finally, we produce the geometry
/// vec![frame.into_geometry()]
/// renderer.draw_geometry([frame]);
/// }
/// }
///
@ -210,9 +215,7 @@ where
renderer.with_transformation(
Transformation::translate(bounds.x, bounds.y),
|renderer| {
renderer.draw(
self.program.draw(state, renderer, theme, bounds, cursor),
);
self.program.draw(state, renderer, theme, bounds, cursor);
},
);
}

View file

@ -37,22 +37,15 @@ where
(event::Status::Ignored, None)
}
/// Draws the state of the [`Program`], producing a bunch of [`Geometry`].
///
/// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
/// [`Cache`].
///
/// [`Geometry`]: crate::canvas::Geometry
/// [`Frame`]: crate::canvas::Frame
/// [`Cache`]: crate::canvas::Cache
/// Draws the state of the [`Program`] with the given [`Renderer`].
fn draw(
&self,
state: &Self::State,
renderer: &Renderer,
renderer: &mut Renderer,
theme: &Theme,
bounds: Rectangle,
cursor: mouse::Cursor,
) -> Vec<Renderer::Geometry>;
);
/// Returns the current mouse interaction of the [`Program`].
///
@ -90,12 +83,12 @@ where
fn draw(
&self,
state: &Self::State,
renderer: &Renderer,
renderer: &mut Renderer,
theme: &Theme,
bounds: Rectangle,
cursor: mouse::Cursor,
) -> Vec<Renderer::Geometry> {
T::draw(self, state, renderer, theme, bounds, cursor)
) {
T::draw(self, state, renderer, theme, bounds, cursor);
}
fn mouse_interaction(

View file

@ -8,7 +8,6 @@ use crate::core::{
Color, Element, Layout, Length, Point, Rectangle, Size, Theme, Vector,
Widget,
};
use crate::graphics::geometry::Renderer as _;
use crate::Renderer;
use std::cell::RefCell;
@ -92,6 +91,8 @@ impl<'a, Message, Theme> Widget<Message, Theme, Renderer>
_cursor: mouse::Cursor,
_viewport: &Rectangle,
) {
use canvas::Frame;
let state = tree.state.downcast_ref::<State>();
let bounds = layout.bounds();
@ -142,7 +143,7 @@ impl<'a, Message, Theme> Widget<Message, Theme, Renderer>
renderer.with_translation(
bounds.position() - Point::ORIGIN,
|renderer| {
renderer.draw(vec![geometry]);
renderer.draw_geometry(vec![geometry]);
},
);
}
@ -161,11 +162,11 @@ where
/// The data of a [`QRCode`].
///
/// It stores the contents that will be displayed.
#[derive(Debug)]
#[allow(missing_debug_implementations)]
pub struct Data {
contents: Vec<qrcode::Color>,
width: usize,
cache: canvas::Cache,
cache: canvas::Cache<Renderer>,
}
impl Data {