mod axis;
mod element;
mod grid;
mod hover;
pub mod label;
mod path_cache;
pub mod scale;
pub mod shape;
use std::{fmt::Debug, ops::Add, time::Duration};
use gpui::{
AnyElement, App, Bounds, ElementId, IntoElement, Path, PathBuilder, Pixels, Point, Window,
point, px,
};
use crate::{Spring, motion::Transition};
#[allow(deprecated)]
pub use axis::AXIS_GAP;
pub use axis::{AxisLabelPlacement, AxisLabelSide, AxisText, PlotAxis, axis_gutter};
pub use element::PlotElement;
pub use grid::Grid;
pub use hover::{PlotHover, TooltipState, hover_progress, is_hover_entering, pointer_spring};
pub use label::PlotLabel;
pub use path_cache::{PathCache, PathCaches, ShapeKey};
pub use scale::PlotValue;
#[derive(Clone)]
pub struct PlotMotion {
pointer: Spring,
enter: Transition,
exit: Transition,
}
impl Default for PlotMotion {
fn default() -> Self {
Self {
pointer: Spring::new(Duration::ZERO),
enter: Transition::new(Duration::ZERO),
exit: Transition::new(Duration::ZERO),
}
}
}
impl PlotMotion {
pub fn with_pointer(mut self, pointer: Spring) -> Self {
self.pointer = pointer;
self
}
pub fn with_enter(mut self, enter: Transition) -> Self {
self.enter = enter;
self
}
pub fn with_exit(mut self, exit: Transition) -> Self {
self.exit = exit;
self
}
pub fn pointer(&self) -> Spring {
self.pointer
}
pub fn enter(&self) -> &Transition {
&self.enter
}
pub fn exit(&self) -> &Transition {
&self.exit
}
}
pub trait Plot: IntoElement {
fn prepaint(
&mut self,
_bounds: Bounds<Pixels>,
_window: &mut Window,
_cx: &mut App,
) -> Vec<AnyElement> {
vec![]
}
fn paint(&mut self, bounds: Bounds<Pixels>, window: &mut Window, cx: &mut App);
fn id(&self) -> Option<ElementId> {
None
}
fn tooltip_state(
&self,
_position: Point<Pixels>,
_bounds: Bounds<Pixels>,
_cx: &App,
) -> Option<TooltipState> {
None
}
fn hover(&mut self, _hover: Option<&PlotHover>, _window: &mut Window, _cx: &mut App) {}
fn tooltip(
&self,
_state: &TooltipState,
_cursor: Point<Pixels>,
_bounds: Bounds<Pixels>,
_window: &mut Window,
_cx: &mut App,
) -> Option<AnyElement> {
None
}
}
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub enum Curve {
#[default]
Natural,
Linear,
StepAfter,
}
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.as_f32()),
px(p.y.into() + bounds.origin.y.as_f32()),
)
})
.collect::<Vec<_>>();
path.add_polygon(points, false);
path.build().ok()
}