Skip to content

Commit

Permalink
Fix canvas::Frame issuing a new layer for Mesh2D in with_clip
Browse files Browse the repository at this point in the history
Text will still be rendered in its own layer, until we fix the composition bottleneck in `glyph-brush`.
  • Loading branch information
hecrj committed Mar 10, 2022
1 parent a53fa91 commit ec8ed9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
42 changes: 32 additions & 10 deletions graphics/src/widget/canvas/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,30 @@ impl Frame {

f(&mut frame);

self.primitives.push(Primitive::Clip {
bounds: region,
content: Box::new(Primitive::Translate {
translation: Vector::new(region.x, region.y),
content: Box::new(frame.into_geometry().into_primitive()),
}),
let primitives = frame.into_primitives();

let (text, meshes) = primitives
.into_iter()
.partition(|primitive| matches!(primitive, Primitive::Text { .. }));

let translation = Vector::new(region.x, region.y);

self.primitives.push(Primitive::Group {
primitives: vec![
Primitive::Translate {
translation,
content: Box::new(Primitive::Group { primitives: meshes }),
},
Primitive::Translate {
translation,
content: Box::new(Primitive::Clip {
bounds: region,
content: Box::new(Primitive::Group {
primitives: text,
}),
}),
},
],
});
}

Expand Down Expand Up @@ -308,7 +326,13 @@ impl Frame {
}

/// Produces the [`Geometry`] representing everything drawn on the [`Frame`].
pub fn into_geometry(mut self) -> Geometry {
pub fn into_geometry(self) -> Geometry {
Geometry::from_primitive(Primitive::Group {
primitives: self.into_primitives(),
})
}

fn into_primitives(mut self) -> Vec<Primitive> {
if !self.buffers.indices.is_empty() {
self.primitives.push(Primitive::Mesh2D {
buffers: triangle::Mesh2D {
Expand All @@ -319,9 +343,7 @@ impl Frame {
});
}

Geometry::from_primitive(Primitive::Group {
primitives: self.primitives,
})
self.primitives
}
}

Expand Down
6 changes: 0 additions & 6 deletions graphics/src/widget/canvas/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,3 @@ impl Geometry {
self.0
}
}

impl From<Geometry> for Primitive {
fn from(geometry: Geometry) -> Primitive {
geometry.0
}
}

0 comments on commit ec8ed9f

Please sign in to comment.