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,30 +70,34 @@ impl RenderOnce for CrossLine {
.absolute() .absolute()
.top_0() .top_0()
.left_0() .left_0()
.child( .when(self.direction.show_vertical(), |this| {
div() this.child(
.absolute() div()
.w(px(1.)) .absolute()
.bg(cx.theme().border) .w(px(1.))
.top_0() .bg(cx.theme().border)
.left(self.point.x) .top_0()
.map(|this| { .left(self.point.x)
if let Some(height) = self.height { .map(|this| {
this.h(px(height)) if let Some(height) = self.height {
} else { this.h(px(height))
this.h_full() } else {
} this.h_full()
}), }
) }),
.child( )
div() })
.absolute() .when(self.direction.show_horizontal(), |this| {
.w_full() this.child(
.h(px(1.)) div()
.bg(cx.theme().border) .absolute()
.left_0() .w_full()
.top(self.point.y), .h(px(1.))
) .bg(cx.theme().border)
.left_0()
.top(self.point.y),
)
})
} }
} }