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};
#[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)]
pub struct CrossLine {
point: Point<Pixels>,
height: Option<f32>,
direction: CrossLineAxis,
}
impl CrossLine {
@ -16,9 +37,20 @@ impl CrossLine {
Self {
point,
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 {
self.height = Some(height);
self
@ -38,30 +70,34 @@ impl RenderOnce for CrossLine {
.absolute()
.top_0()
.left_0()
.child(
div()
.absolute()
.w(px(1.))
.bg(cx.theme().border)
.top_0()
.left(self.point.x)
.map(|this| {
if let Some(height) = self.height {
this.h(px(height))
} else {
this.h_full()
}
}),
)
.child(
div()
.absolute()
.w_full()
.h(px(1.))
.bg(cx.theme().border)
.left_0()
.top(self.point.y),
)
.when(self.direction.show_vertical(), |this| {
this.child(
div()
.absolute()
.w(px(1.))
.bg(cx.theme().border)
.top_0()
.left(self.point.x)
.map(|this| {
if let Some(height) = self.height {
this.h(px(height))
} else {
this.h_full()
}
}),
)
})
.when(self.direction.show_horizontal(), |this| {
this.child(
div()
.absolute()
.w_full()
.h(px(1.))
.bg(cx.theme().border)
.left_0()
.top(self.point.y),
)
})
}
}