Something went wrong. Try again.
usage — Vidya I/O gauges for disk & network throughput
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179//! Circular arc gauges for throughput and utilization.
use egui::{ epaint::PathStroke, Align2, Color32, FontId, Sense, Shape, Stroke, Ui, Vec2,};use std::f32::consts::{PI, TAU};use vidya::Theme;
/// Auto-scaling ceiling for a rate series (decays slowly so gauges stay readable).#[derive(Debug, Clone)]pub struct PeakScale { peak: f64, floor: f64,}
impl PeakScale { pub fn new(floor: f64) -> Self { Self { peak: floor, floor: floor.max(1.0), } }
pub fn observe(&mut self, value: f64) -> f64 { let v = value.max(0.0); if v > self.peak { self.peak = v; } else { // gentle decay toward floor so a spike doesn't lock the scale forever self.peak = (self.peak * 0.995).max(self.floor).max(v); } nice_ceiling(self.peak.max(self.floor)) }
pub fn current(&self) -> f64 { nice_ceiling(self.peak.max(self.floor)) }}
fn nice_ceiling(v: f64) -> f64 { if v <= 1.0 { return 1.0; } let exp = v.log10().floor(); let base = 10f64.powf(exp); let n = v / base; let nice = if n <= 1.0 { 1.0 } else if n <= 2.0 { 2.0 } else if n <= 5.0 { 5.0 } else { 10.0 }; nice * base}
/// Color by fill fraction: accent → warning → destructive.pub fn heat_color(theme: &Theme, fraction: f32) -> Color32 { let p = &theme.palette; let t = fraction.clamp(0.0, 1.0); if t < 0.55 { p.accent } else if t < 0.8 { p.warning } else { p.destructive }}
/// Draw a bottom-open circular gauge with center value + caption.pub fn arc_gauge( ui: &mut Ui, theme: &Theme, size: f32, fraction: f32, value_text: &str, label: &str, color: Color32,) { let fraction = fraction.clamp(0.0, 1.0); let (rect, _response) = ui.allocate_exact_size(Vec2::splat(size), Sense::hover()); if !ui.is_rect_visible(rect) { return; }
let painter = ui.painter_at(rect); let center = rect.center(); let radius = size * 0.42; let stroke_w = (size * 0.08).clamp(6.0, 14.0);
// egui angles (y-down): 0 = east, + = clockwise. // Bottom-open gauge: start SW, sweep 270° through north to SE. let start_angle = PI * 0.75; // 135° let sweep = PI * 1.5; // 270°
let track = theme.palette.border_soft; paint_arc( &painter, center, radius, start_angle, sweep, Stroke::new(stroke_w, track), ); if fraction > 0.001 { paint_arc( &painter, center, radius, start_angle, sweep * fraction, Stroke::new(stroke_w, color), ); }
if fraction > 0.001 { let a = start_angle + sweep * fraction; let tip = center + Vec2::angled(a) * radius; painter.circle_filled(tip, stroke_w * 0.45, color); }
let p = &theme.palette; painter.text( center - Vec2::new(0.0, size * 0.02), Align2::CENTER_CENTER, value_text, FontId::proportional(theme.type_scale.title_2), p.text, ); painter.text( center + Vec2::new(0.0, size * 0.14), Align2::CENTER_CENTER, label, FontId::proportional(theme.type_scale.caption), p.text_secondary, );}
fn paint_arc( painter: &egui::Painter, center: egui::Pos2, radius: f32, start: f32, sweep: f32, stroke: Stroke,) { if sweep.abs() < 1e-4 { return; } let steps = ((sweep.abs() / TAU) * 64.0).ceil().max(8.0) as usize; let mut points = Vec::with_capacity(steps + 1); for i in 0..=steps { let t = i as f32 / steps as f32; let a = start + sweep * t; points.push(center + Vec2::angled(a) * radius); } painter.add(Shape::line(points, PathStroke::from(stroke)));}
/// Compact horizontal bar used in device lists.pub fn mini_bar(ui: &mut Ui, theme: &Theme, fraction: f32, color: Color32, width: f32) { let height = 6.0; let (rect, _) = ui.allocate_exact_size(Vec2::new(width, height), Sense::hover()); if !ui.is_rect_visible(rect) { return; } let painter = ui.painter(); let r = theme.spacing.radius_sm; painter.rect_filled(rect, r, theme.palette.border_soft.gamma_multiply(0.5)); let f = fraction.clamp(0.0, 1.0); if f > 0.0 { let mut fill = rect; fill.set_width(rect.width() * f); painter.rect_filled(fill, r, color); }}