plot: Support multiple directions for the tooltip cross line (#1128)

This commit is contained in:
Floyd Wang 2025-08-12 14:16:31 +08:00 committed by GitHub
parent 7f64f8798e
commit e37369ea16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,10 +5,31 @@ use gpui::{
use crate::{v_flex, ActiveTheme}; use crate::{v_flex, ActiveTheme};
#[derive(Default)]
pub enum CrossLineAxis {
#[default]
Vertical,
Horizontal,
Both,
}
impl CrossLineAxis {
#[inline]
pub fn show_vertical(&self) -> bool {
matches!(self, CrossLineAxis::Vertical | CrossLineAxis::Both)
}
#[inline]
pub fn show_horizontal(&self) -> bool {
matches!(self, CrossLineAxis::Horizontal | CrossLineAxis::Both)
}
}
#[derive(IntoElement)] #[derive(IntoElement)]
pub struct CrossLine { pub struct CrossLine {
point: Point<Pixels>, point: Point<Pixels>,
height: Option<f32>, height: Option<f32>,
direction: CrossLineAxis,
} }
impl CrossLine { impl CrossLine {
@ -16,9 +37,20 @@ impl CrossLine {
Self { Self {
point, point,
height: None, height: None,
direction: Default::default(),
} }
} }
pub fn horizontal(mut self) -> Self {
self.direction = CrossLineAxis::Horizontal;
self
}
pub fn both(mut self) -> Self {
self.direction = CrossLineAxis::Both;
self
}
pub fn height(mut self, height: f32) -> Self { pub fn height(mut self, height: f32) -> Self {
self.height = Some(height); self.height = Some(height);
self self
@ -38,7 +70,8 @@ impl RenderOnce for CrossLine {
.absolute() .absolute()
.top_0() .top_0()
.left_0() .left_0()
.child( .when(self.direction.show_vertical(), |this| {
this.child(
div() div()
.absolute() .absolute()
.w(px(1.)) .w(px(1.))
@ -53,7 +86,9 @@ impl RenderOnce for CrossLine {
} }
}), }),
) )
.child( })
.when(self.direction.show_horizontal(), |this| {
this.child(
div() div()
.absolute() .absolute()
.w_full() .w_full()
@ -62,6 +97,7 @@ impl RenderOnce for CrossLine {
.left_0() .left_0()
.top(self.point.y), .top(self.point.y),
) )
})
} }
} }