use arc_motion::{arc_spring_with_options, spring::SpringOptions};
use arc_tokens::{ActiveArcTheme as _, Radii, Spacing, SpringSpec, Typography};
use gpui_kit::{
App, ElementId, IntoElement, ParentElement, RenderOnce, Role, SharedString, Styled, Window,
base::StyledExt as _, canvas, div, prelude::*, px,
};
use super::common::{ChartTone, build_clock_arc_stroke};
pub const GAUGE_START_DEG: f32 = -135.0;
pub const GAUGE_SPAN_DEG: f32 = 270.0;
#[derive(Clone, Debug, PartialEq)]
pub struct GaugeThreshold {
pub from: f64,
pub tone: ChartTone,
pub label: SharedString,
}
impl GaugeThreshold {
pub fn new(from: f64, tone: ChartTone, label: impl Into<SharedString>) -> Self {
Self {
from,
tone,
label: label.into(),
}
}
}
pub fn gauge_fraction(value: f64, min: f64, max: f64) -> f32 {
let safe_max = if max > min { max } else { min + 1.0 };
((value - min) / (safe_max - min)).clamp(0.0, 1.0) as f32
}
pub fn gauge_resolve_threshold(
value: f64,
thresholds: &[GaugeThreshold],
) -> Option<&GaugeThreshold> {
thresholds
.iter()
.filter(|t| value >= t.from - 1e-6)
.max_by(|a, b| a.from.total_cmp(&b.from))
}
#[derive(IntoElement)]
pub struct Gauge {
id: ElementId,
value: f64,
min: f64,
max: f64,
label: SharedString,
detail: Option<SharedString>,
tone: ChartTone,
thresholds: Vec<GaugeThreshold>,
diameter: f32,
}
impl Gauge {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>, value: f64) -> Self {
Self {
id: id.into(),
value,
min: 0.0,
max: 100.0,
label: label.into(),
detail: None,
tone: ChartTone::Accent,
thresholds: Vec::new(),
diameter: 148.0,
}
}
pub fn range(mut self, min: f64, max: f64) -> Self {
self.min = min;
self.max = max;
self
}
pub fn detail(mut self, detail: impl Into<SharedString>) -> Self {
self.detail = Some(detail.into());
self
}
pub fn tone(mut self, tone: ChartTone) -> Self {
self.tone = tone;
self
}
pub fn thresholds(mut self, thresholds: Vec<GaugeThreshold>) -> Self {
self.thresholds = thresholds;
self
}
pub fn diameter(mut self, px_val: f32) -> Self {
self.diameter = px_val.clamp(96.0, 260.0);
self
}
}
impl RenderOnce for Gauge {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.arc_theme().clone();
let colors = theme.colors;
let safe_max = if self.max > self.min {
self.max
} else {
self.min + 1.0
};
let target_frac = gauge_fraction(self.value, self.min, safe_max);
let sweep = arc_spring_with_options(
SharedString::from(format!("arc-gauge-sweep-{:?}", self.id)),
target_frac,
SpringOptions::new(SpringSpec::visual(0.55, 0.0)),
window,
cx,
)
.value
.clamp(0.0, 1.0);
let live_value = self.min + (sweep as f64) * (safe_max - self.min);
let pct_display = (sweep * 100.0).round() as i32;
let live_band = gauge_resolve_threshold(live_value, &self.thresholds);
let active_tone = live_band.map(|b| b.tone).unwrap_or(self.tone);
let status_label = live_band.map(|b| b.label.clone());
let tone_color = active_tone.color(&colors);
let track_color = colors.border;
let arc_alpha = (sweep / 0.02).clamp(0.0, 1.0);
let arc_color = tone_color.alpha(tone_color.a * arc_alpha);
let diam = self.diameter;
let visual_h = diam * 0.88;
let ring_canvas = canvas(
move |bounds, _w, _cx| bounds,
move |bounds, _, window, _cx| {
let left: f32 = bounds.origin.x.into();
let top: f32 = bounds.origin.y.into();
let w: f32 = bounds.size.width.into();
let h: f32 = bounds.size.height.into();
let cx_px = left + w * 0.5;
let cy_px = top + h * 0.54;
let stroke = (diam * 0.075).clamp(6.0, 12.0);
let radius = (w.min(h) * 0.44).max(16.0);
if let Some(track) = build_clock_arc_stroke(
cx_px,
cy_px,
radius,
GAUGE_START_DEG,
GAUGE_START_DEG + GAUGE_SPAN_DEG,
stroke,
56,
) {
window.paint_path(track, track_color);
}
if sweep > 0.002 {
let end_deg = GAUGE_START_DEG + GAUGE_SPAN_DEG * sweep;
if let Some(arc) = build_clock_arc_stroke(
cx_px,
cy_px,
radius,
GAUGE_START_DEG,
end_deg,
stroke,
56,
) {
window.paint_path(arc, arc_color);
}
}
},
)
.w(px(diam))
.h(px(visual_h));
div()
.id(self.id)
.role(Role::ProgressIndicator)
.p(Spacing::S4)
.rounded(Radii::PANEL)
.border_1()
.border_color(colors.border)
.bg(colors.surface)
.flex()
.flex_col()
.items_center()
.gap(Spacing::S2)
.child(
div()
.relative()
.w(px(diam))
.h(px(visual_h))
.flex()
.items_center()
.justify_center()
.child(ring_canvas)
.child(
div()
.absolute()
.inset_0()
.pt(px(diam * 0.14))
.flex()
.flex_col()
.items_center()
.justify_center()
.gap(px(2.0))
.child(
div()
.flex()
.items_baseline()
.gap(px(1.0))
.child(
div()
.text_size(Typography::TEXT_2XL)
.font_semibold()
.text_color(colors.foreground)
.child(format!("{pct_display}")),
)
.child(
div()
.text_size(Typography::TEXT_XS)
.font_medium()
.text_color(colors.text_muted)
.child("%"),
),
)
.when_some(status_label, |this, status| {
this.child(
div()
.text_size(Typography::TEXT_XS)
.font_medium()
.text_color(tone_color)
.child(status),
)
}),
),
)
.child(
div()
.flex()
.flex_col()
.items_center()
.gap(px(2.0))
.child(
div()
.text_size(Typography::TEXT_SM)
.font_medium()
.text_color(colors.foreground)
.child(self.label),
)
.when_some(self.detail, |this, detail| {
this.child(
div()
.text_size(Typography::TEXT_XS)
.text_color(colors.text_muted)
.child(detail),
)
}),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gauge_fraction_and_threshold_resolution() {
assert!((gauge_fraction(75.0, 0.0, 100.0) - 0.75).abs() < 1e-5);
assert!((gauge_fraction(-10.0, 0.0, 100.0) - 0.0).abs() < 1e-5);
assert!((gauge_fraction(150.0, 0.0, 100.0) - 1.0).abs() < 1e-5);
let bands = vec![
GaugeThreshold::new(0.0, ChartTone::Success, "Healthy"),
GaugeThreshold::new(70.0, ChartTone::Warning, "Elevated"),
GaugeThreshold::new(90.0, ChartTone::Danger, "Critical"),
];
assert_eq!(
gauge_resolve_threshold(42.0, &bands)
.unwrap()
.label
.as_ref(),
"Healthy"
);
assert_eq!(
gauge_resolve_threshold(70.0, &bands)
.unwrap()
.label
.as_ref(),
"Elevated"
);
assert_eq!(
gauge_resolve_threshold(96.0, &bands)
.unwrap()
.label
.as_ref(),
"Critical"
);
}
}