gpui_component/plot/
mod.rs1mod axis;
2mod grid;
3pub mod label;
4pub mod scale;
5pub mod shape;
6pub mod tooltip;
7
8pub use gpui_component_macros::IntoPlot;
9
10use std::{fmt::Debug, ops::Add};
11
12use gpui::{point, px, App, Bounds, IntoElement, Path, PathBuilder, Pixels, Point, Window};
13
14use crate::PixelsExt;
15
16pub use axis::{Axis, AxisText, AXIS_GAP};
17pub use grid::Grid;
18pub use label::Label;
19
20pub trait Plot: IntoElement {
21 fn paint(&mut self, bounds: Bounds<Pixels>, window: &mut Window, cx: &mut App);
22}
23
24#[derive(Clone, Copy, Default)]
25pub enum StrokeStyle {
26 #[default]
27 Natural,
28 Linear,
29}
30
31pub fn origin_point<T>(x: T, y: T, origin: Point<T>) -> Point<T>
32where
33 T: Default + Clone + Debug + PartialEq + Add<Output = T>,
34{
35 point(x, y) + origin
36}
37
38pub fn polygon<T>(points: &[Point<T>], bounds: &Bounds<Pixels>) -> Option<Path<Pixels>>
39where
40 T: Default + Clone + Copy + Debug + Into<f32> + PartialEq,
41{
42 let mut path = PathBuilder::stroke(px(1.));
43 let points = &points
44 .iter()
45 .map(|p| {
46 point(
47 px(p.x.into() + bounds.origin.x.as_f32()),
48 px(p.y.into() + bounds.origin.y.as_f32()),
49 )
50 })
51 .collect::<Vec<_>>();
52 path.add_polygon(points, false);
53 path.build().ok()
54}