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 StepAfter,
30}
31
32pub fn origin_point<T>(x: T, y: T, origin: Point<T>) -> Point<T>
33where
34 T: Default + Clone + Debug + PartialEq + Add<Output = T>,
35{
36 point(x, y) + origin
37}
38
39pub fn polygon<T>(points: &[Point<T>], bounds: &Bounds<Pixels>) -> Option<Path<Pixels>>
40where
41 T: Default + Clone + Copy + Debug + Into<f32> + PartialEq,
42{
43 let mut path = PathBuilder::stroke(px(1.));
44 let points = &points
45 .iter()
46 .map(|p| {
47 point(
48 px(p.x.into() + bounds.origin.x.as_f32()),
49 px(p.y.into() + bounds.origin.y.as_f32()),
50 )
51 })
52 .collect::<Vec<_>>();
53 path.add_polygon(points, false);
54 path.build().ok()
55}