Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"

[dependencies]
plotters-backend = "0.3.7"
gpui = { git = "https://github.com/zed-industries/zed" }
gpui = { git = "https://github.com/huacnlee/zed" }
parking_lot = "0.12.3"
plotters = { version = "0.3.7", optional = true }
tracing = "0.1"
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ provides interactivity and more stuff on top of plotters-gpui

## Show cases

<img width="300" alt="image" src="https://github.com/user-attachments/assets/276b75c2-d5fe-4b0e-93b1-1215317d4b73" /> <img width="300" alt="image" src="https://github.com/user-attachments/assets/8b1f7c80-ef09-4ffd-aff8-123315ecf1b3" />
<img width="300" alt="image" src="https://github.com/user-attachments/assets/7e9ec94e-a8f0-4e0d-97eb-9399f0145e39" /> <img width="300" alt="image" src="https://github.com/user-attachments/assets/03ea4351-d079-4372-af84-bd2429ccc098" />
<img width="300" alt="image" src="https://github.com/user-attachments/assets/56c29590-c120-4a5e-8b65-b272afe732dc" />
<img width="300" src="https://github.com/user-attachments/assets/58104fbd-35e7-40a1-be8d-ad18945acacb" />
<img width="300" src="https://github.com/user-attachments/assets/86c95b28-74db-44d3-8599-910d24adee55" />
<img width="300" src="https://github.com/user-attachments/assets/f599b6a8-946d-492a-a423-c2805fb22c4c" />
<img width="300" src="https://github.com/user-attachments/assets/066f3f92-9671-48cf-8383-9a55d1bf0ef7" />
<img width="300" src="https://github.com/user-attachments/assets/e837b24e-50c6-4ddb-ad06-d5084920b424" />
<img width="300" src="https://github.com/user-attachments/assets/fd61a1cb-ef51-4654-9a50-4154694de57e" />
<img width="300" src="https://github.com/user-attachments/assets/4cd02da9-bf8f-4e5e-9b4b-2423787ded0d" />

## Performance

Expand All @@ -54,4 +58,3 @@ Enables the Metal Performance HUD.
```shell
MTL_HUD_ENABLED=1 cargo run ...
```

32 changes: 25 additions & 7 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,19 @@ impl DrawingBackend for GpuiBackend<'_> {
let color = color_to_hsla(style.color());

if fill {
let mut path = gpui::Path::new(upper_left);
path.line_to(point(upper_left.x, bottom_right.y));
path.line_to(bottom_right);
path.line_to(point(bottom_right.x, upper_left.y));
path.line_to(upper_left);
let mut builder = gpui::PathBuilder::fill();
builder.move_to(upper_left);
builder.line_to(point(upper_left.x, bottom_right.y));
builder.line_to(bottom_right);
builder.line_to(point(bottom_right.x, upper_left.y));
builder.line_to(upper_left);
let path = builder.build().map_err(|err| {
DrawingErrorKind::DrawingError(std::io::Error::new(
std::io::ErrorKind::Other,
err.to_string(),
))
})?;

self.window.paint_path(path, color);
} else {
for (p1, p2) in [
Expand Down Expand Up @@ -129,10 +137,20 @@ impl DrawingBackend for GpuiBackend<'_> {
Some(start) => start,
None => return Ok(()),
};
let mut path = gpui::Path::new(coord_to_point(self.bounds.origin, start));

let mut builder = gpui::PathBuilder::fill();
builder.move_to(coord_to_point(self.bounds.origin, start));
for point in iter {
path.line_to(coord_to_point(self.bounds.origin, point));
builder.line_to(coord_to_point(self.bounds.origin, point));
}

let path = builder.build().map_err(|err| {
DrawingErrorKind::DrawingError(std::io::Error::new(
std::io::ErrorKind::Other,
err.to_string(),
))
})?;

let color = color_to_hsla(style.color());
self.window.paint_path(path, color);
Ok(())
Expand Down
27 changes: 8 additions & 19 deletions src/line.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use gpui::{point, Hsla, Path, Pixels, Point, Window};
use gpui::{px, Hsla, PathBuilder, Pixels, Point, Window};
use tracing::warn;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -50,29 +50,18 @@ impl Line {
return;
}

let width = self.width;

let (Some(first), Some(last)) = (self.points.first().copied(), self.points.last().copied())
else {
let mut builder = PathBuilder::stroke(px(self.width.0));
let Some(first_p) = self.points.first() else {
return;
};

let mut angle = f32::atan2(first.y.0 - last.y.0, first.x.0 - last.x.0);
angle += std::f32::consts::FRAC_PI_2;
let shift = point(width * f32::cos(angle), width * f32::sin(angle));

let mut reversed_points = vec![first + shift];
let mut path = Path::new(first);
for &p in &self.points[1..] {
path.line_to(p);
reversed_points.push(p + shift);
builder.move_to(*first_p);
for p in self.points.iter().skip(1) {
builder.line_to(*p);
}

// now do the reverse to close the path
for p in reversed_points.into_iter().rev() {
path.line_to(p);
if let Ok(path) = builder.build() {
window.paint_path(path, self.color);
}

window.paint_path(path, self.color);
}
}