Introduce draw_with_bounds to canvas::Cache

Also:
  - Change `Rectangle::INFINITE` to have coordinates at `f32::NEG_INFINITY`
  - Change `Frame::with_clip` to _not_ adjust the coordinate system
  - Rename `Size::INFINITY` to `INFINITE`
This commit is contained in:
Héctor Ramón Jiménez 2025-08-17 22:31:58 +02:00
parent c1f7345ceb
commit f2aa570aac
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
13 changed files with 56 additions and 45 deletions

View file

@ -20,7 +20,7 @@ pub use crate::core::{Image, Svg};
pub use crate::gradient::{self, Gradient};
use crate::cache::Cached;
use crate::core::{self, Size};
use crate::core::{self, Rectangle};
/// A renderer capable of drawing some [`Self::Geometry`].
pub trait Renderer: core::Renderer {
@ -31,7 +31,7 @@ pub trait Renderer: core::Renderer {
type Frame: frame::Backend<Geometry = Self::Geometry>;
/// Creates a new [`Self::Frame`].
fn new_frame(&self, size: Size) -> Self::Frame;
fn new_frame(&self, bounds: Rectangle) -> Self::Frame;
/// Draws the given [`Self::Geometry`].
fn draw_geometry(&mut self, geometry: Self::Geometry);
@ -42,7 +42,7 @@ impl Renderer for () {
type Geometry = ();
type Frame = ();
fn new_frame(&self, _size: Size) -> Self::Frame {}
fn new_frame(&self, _bounds: Rectangle) -> Self::Frame {}
fn draw_geometry(&mut self, _geometry: Self::Geometry) {}
}

View file

@ -1,5 +1,5 @@
use crate::cache::{self, Cached};
use crate::core::Size;
use crate::core::{Rectangle, Size};
use crate::geometry::{self, Frame};
pub use cache::Group;
@ -17,7 +17,7 @@ where
#[derive(Debug, Clone)]
struct Data<T> {
bounds: Size,
bounds: Rectangle,
geometry: T,
}
@ -53,7 +53,7 @@ where
/// [`Cache`].
///
/// The closure will only be called when
/// - the bounds have changed since the previous draw call.
/// - the size has changed since the previous draw call.
/// - the [`Cache`] is empty or has been explicitly cleared.
///
/// Otherwise, the previously stored geometry will be returned. The
@ -62,7 +62,21 @@ where
pub fn draw(
&self,
renderer: &Renderer,
bounds: Size,
size: Size,
draw_fn: impl FnOnce(&mut Frame<Renderer>),
) -> Renderer::Geometry {
self.draw_with_bounds(renderer, Rectangle::with_size(size), draw_fn)
}
/// Draws geometry using the provided closure and stores it in the
/// [`Cache`].
///
/// Analogous to [`draw`](Self::draw), but takes a clipping [`Rectangle`] instead of
/// a [`Size`].
pub fn draw_with_bounds(
&self,
renderer: &Renderer,
bounds: Rectangle,
draw_fn: impl FnOnce(&mut Frame<Renderer>),
) -> Renderer::Geometry {
use std::ops::Deref;
@ -82,7 +96,7 @@ where
}
};
let mut frame = Frame::new(renderer, bounds);
let mut frame = Frame::with_bounds(renderer, bounds);
draw_fn(&mut frame);
let geometry = frame.into_geometry().cache(self.raw.group(), previous);

View file

@ -16,9 +16,17 @@ where
Renderer: geometry::Renderer,
{
/// Creates a new [`Frame`] with the given dimensions.
///
/// Any geometry drawn outside of the dimensions will be clipped.
/// If you need further control, use [`with_bounds`](Self::with_bounds).
pub fn new(renderer: &Renderer, size: Size) -> Self {
Self::with_bounds(renderer, Rectangle::with_size(size))
}
/// Creates a new [`Frame`] with the given clip bounds.
pub fn with_bounds(renderer: &Renderer, bounds: Rectangle) -> Self {
Self {
raw: renderer.new_frame(size),
raw: renderer.new_frame(bounds),
}
}