Rename canvas::frame to canvas in iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2023-03-03 04:00:44 +01:00
parent bbeaf10c04
commit d13d19ba35
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
9 changed files with 14 additions and 153 deletions

View file

@ -86,14 +86,14 @@ pub enum Primitive {
/// The [`Gradient`] to apply to the mesh.
gradient: Gradient,
},
#[cfg(feature = "tiny_skia")]
#[cfg(feature = "tiny-skia")]
Fill {
path: tiny_skia::Path,
paint: tiny_skia::Paint<'static>,
rule: tiny_skia::FillRule,
transform: tiny_skia::Transform,
},
#[cfg(feature = "tiny_skia")]
#[cfg(feature = "tiny-skia")]
Stroke {
path: tiny_skia::Path,
paint: tiny_skia::Paint<'static>,

View file

@ -16,7 +16,7 @@ use crate::{Backend, Point, Rectangle, Size, Vector};
pub use crate::Geometry;
pub enum Frame {
Wgpu(iced_wgpu::widget::canvas::Frame),
Wgpu(iced_wgpu::canvas::Frame),
TinySkia(iced_tiny_skia::canvas::Frame),
}
@ -33,7 +33,7 @@ impl Frame {
pub fn new<Theme>(renderer: &crate::Renderer<Theme>, size: Size) -> Self {
match renderer.backend() {
Backend::Wgpu(_) => {
Frame::Wgpu(iced_wgpu::widget::canvas::Frame::new(size))
Frame::Wgpu(iced_wgpu::canvas::Frame::new(size))
}
Backend::TinySkia(_) => {
Frame::TinySkia(iced_tiny_skia::canvas::Frame::new(size))
@ -131,7 +131,7 @@ impl Frame {
pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) {
let mut frame = match self {
Self::Wgpu(_) => {
Self::Wgpu(iced_wgpu::widget::canvas::Frame::new(region.size()))
Self::Wgpu(iced_wgpu::canvas::Frame::new(region.size()))
}
Self::TinySkia(_) => Self::TinySkia(
iced_tiny_skia::canvas::Frame::new(region.size()),

View file

@ -256,6 +256,9 @@ impl Backend {
// Not supported!
// TODO: Draw a placeholder (?) / Log it (?)
}
_ => {
// Not supported!
}
}
}
}

View file

@ -1,9 +1,8 @@
use crate::primitive::{self, Primitive};
use crate::widget::canvas::fill::{self, Fill};
use crate::widget::canvas::{
use iced_graphics::primitive::{self, Primitive};
use iced_native::widget::canvas::fill::{self, Fill};
use iced_native::widget::canvas::{
LineCap, LineDash, LineJoin, Path, Stroke, Style, Text,
};
use iced_native::{Gradient, Point, Rectangle, Size, Vector};
use lyon::geom::euclid;

View file

@ -37,12 +37,13 @@
#![forbid(rust_2018_idioms)]
#![allow(clippy::inherent_to_string, clippy::type_complexity)]
#![cfg_attr(docsrs, feature(doc_cfg))]
pub mod layer;
pub mod settings;
pub mod widget;
pub mod window;
#[cfg(feature = "canvas")]
pub mod canvas;
mod backend;
mod buffer;
mod quad;

View file

@ -1,9 +0,0 @@
//! Use the graphical widgets supported out-of-the-box.
#[cfg(feature = "canvas")]
#[cfg_attr(docsrs, doc(cfg(feature = "canvas")))]
pub mod canvas;
#[cfg(feature = "canvas")]
#[doc(no_inline)]
pub use canvas::Canvas;

View file

@ -1,16 +0,0 @@
mod cache;
mod frame;
mod geometry;
pub use cache::Cache;
pub use frame::Frame;
pub use geometry::Geometry;
pub use iced_native::widget::canvas::event::{self, Event};
pub use iced_native::widget::canvas::fill::{self, Fill};
pub use iced_native::widget::canvas::gradient::{self, Gradient};
pub use iced_native::widget::canvas::path::{self, Path};
pub use iced_native::widget::canvas::stroke::{self, Stroke};
pub use iced_native::widget::canvas::{
Canvas, Cursor, LineCap, LineDash, LineJoin, Program, Renderer, Style, Text,
};

View file

@ -1,93 +0,0 @@
use crate::widget::canvas::{Frame, Geometry};
use crate::Primitive;
use iced_native::Size;
use std::{cell::RefCell, sync::Arc};
#[derive(Default)]
enum State {
#[default]
Empty,
Filled {
bounds: Size,
primitive: Arc<Primitive>,
},
}
/// 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.
#[derive(Debug, Default)]
pub struct Cache {
state: RefCell<State>,
}
impl Cache {
/// Creates a new empty [`Cache`].
pub fn new() -> Self {
Cache {
state: Default::default(),
}
}
/// Clears the [`Cache`], forcing a redraw the next time it is used.
pub fn clear(&self) {
*self.state.borrow_mut() = State::Empty;
}
/// Draws [`Geometry`] using the provided closure and stores it in the
/// [`Cache`].
///
/// The closure will only be called when
/// - the bounds have changed since the previous draw call.
/// - the [`Cache`] is empty or has been explicitly cleared.
///
/// Otherwise, the previously stored [`Geometry`] will be returned. The
/// [`Cache`] is not cleared in this case. In other words, it will keep
/// returning the stored [`Geometry`] if needed.
pub fn draw(
&self,
bounds: Size,
draw_fn: impl FnOnce(&mut Frame),
) -> Geometry {
use std::ops::Deref;
if let State::Filled {
bounds: cached_bounds,
primitive,
} = self.state.borrow().deref()
{
if *cached_bounds == bounds {
return Geometry::from_primitive(Primitive::Cache {
content: primitive.clone(),
});
}
}
let mut frame = Frame::new(bounds);
draw_fn(&mut frame);
let primitive = Arc::new(frame.into_primitive());
*self.state.borrow_mut() = State::Filled {
bounds,
primitive: primitive.clone(),
};
Geometry::from_primitive(Primitive::Cache { content: primitive })
}
}
impl std::fmt::Debug for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
State::Empty => write!(f, "Empty"),
State::Filled { primitive, bounds } => f
.debug_struct("Filled")
.field("primitive", primitive)
.field("bounds", bounds)
.finish(),
}
}
}

View file

@ -1,24 +0,0 @@
use crate::Primitive;
/// A bunch of shapes that can be drawn.
///
/// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
/// [`Cache`].
///
/// [`Frame`]: crate::widget::canvas::Frame
/// [`Cache`]: crate::widget::canvas::Cache
#[derive(Debug, Clone)]
pub struct Geometry(Primitive);
impl Geometry {
pub(crate) fn from_primitive(primitive: Primitive) -> Self {
Self(primitive)
}
/// Turns the [`Geometry`] into a [`Primitive`].
///
/// This can be useful if you are building a custom widget.
pub fn into_primitive(self) -> Primitive {
self.0
}
}