gpui-component/crates/ui/src/plot/mod.rs
Floyd Wang 3b759cd448
plot: Use f32 instead of f64 (#1052)
## Break Change
- Convert all `f64` parameters to `f32`.

Since the GPUI geometry module uses f32 for rendering, we are also
switching to f32. This helps avoid many as operators and unnecessary
conversions.
2025-07-08 16:24:59 +08:00

52 lines
1.2 KiB
Rust

mod axis;
mod grid;
pub mod label;
pub mod scale;
pub mod shape;
pub mod tooltip;
pub use gpui_component_macros::IntoPlot;
use std::{fmt::Debug, ops::Add};
use gpui::{point, px, App, Bounds, IntoElement, Path, PathBuilder, Pixels, Point, Window};
pub use axis::{Axis, AxisText, AXIS_GAP};
pub use grid::Grid;
pub use label::Label;
pub trait Plot: IntoElement {
fn paint(&mut self, bounds: Bounds<Pixels>, window: &mut Window, cx: &mut App);
}
#[derive(Clone, Copy, Default)]
pub enum StrokeStyle {
#[default]
Natural,
Linear,
}
pub fn origin_point<T>(x: T, y: T, origin: Point<T>) -> Point<T>
where
T: Default + Clone + Debug + PartialEq + Add<Output = T>,
{
point(x, y) + origin
}
pub fn polygon<T>(points: &[Point<T>], bounds: &Bounds<Pixels>) -> Option<Path<Pixels>>
where
T: Default + Clone + Copy + Debug + Into<f32> + PartialEq,
{
let mut path = PathBuilder::stroke(px(1.));
let points = &points
.iter()
.map(|p| {
point(
px(p.x.into() + bounds.origin.x.0),
px(p.y.into() + bounds.origin.y.0),
)
})
.collect::<Vec<_>>();
path.add_polygon(points, false);
path.build().ok()
}