gpui: Add more shapes for PathBuilder (#30904)

- Add `arc` for drawing elliptical arc.
- Add `polygon` support.

<img width="1136" alt="image"
src="https://github.com/user-attachments/assets/97032b02-e6ff-4985-a587-3689500bfd56"
/>

Release Notes:

- N/A
This commit is contained in:
Floyd Wang 2025-05-26 17:49:42 +08:00 committed by GitHub
parent e42cf21703
commit c73af0a52f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 110 additions and 5 deletions

View file

@ -1,6 +1,9 @@
use anyhow::Error;
use etagere::euclid::Vector2D;
use etagere::euclid::{Point2D, Vector2D};
use lyon::geom::Angle;
use lyon::math::{Vector, vector};
use lyon::path::traits::SvgPathBuilder;
use lyon::path::{ArcFlags, Polygon};
use lyon::tessellation::{
BuffersBuilder, FillTessellator, FillVertex, StrokeTessellator, StrokeVertex, VertexBuffers,
};
@ -56,6 +59,18 @@ impl From<Point<Pixels>> for lyon::math::Point {
}
}
impl From<Point<Pixels>> for Vector {
fn from(p: Point<Pixels>) -> Self {
vector(p.x.0, p.y.0)
}
}
impl From<Point<Pixels>> for Point2D<f32, Pixels> {
fn from(p: Point<Pixels>) -> Self {
Point2D::new(p.x.0, p.y.0)
}
}
impl Default for PathBuilder {
fn default() -> Self {
Self {
@ -116,6 +131,49 @@ impl PathBuilder {
.cubic_bezier_to(control_a.into(), control_b.into(), to.into());
}
/// Adds an elliptical arc.
pub fn arc_to(
&mut self,
radii: Point<Pixels>,
x_rotation: Pixels,
large_arc: bool,
sweep: bool,
to: Point<Pixels>,
) {
self.raw.arc_to(
radii.into(),
Angle::degrees(x_rotation.into()),
ArcFlags { large_arc, sweep },
to.into(),
);
}
/// Equivalent to `arc_to` in relative coordinates.
pub fn relative_arc_to(
&mut self,
radii: Point<Pixels>,
x_rotation: Pixels,
large_arc: bool,
sweep: bool,
to: Point<Pixels>,
) {
self.raw.relative_arc_to(
radii.into(),
Angle::degrees(x_rotation.into()),
ArcFlags { large_arc, sweep },
to.into(),
);
}
/// Adds a polygon.
pub fn add_polygon(&mut self, points: &[Point<Pixels>], closed: bool) {
let points = points.iter().copied().map(|p| p.into()).collect::<Vec<_>>();
self.raw.add_polygon(Polygon {
points: points.as_ref(),
closed,
});
}
/// Close the current sub-path.
#[inline]
pub fn close(&mut self) {