//! Colours and small drawings shared by the sections. use eframe::egui::{self, Align, Color32, Layout, Pos2, RichText, Sense, Shape, Stroke, Ui}; /// Served from the cache. pub const CACHE: Color32 = Color32::from_rgb(0x4C, 0xAF, 0x50); /// Compiled because the cache had nothing. pub const BUILT: Color32 = Color32::from_rgb(0x42, 0xA5, 0xF5); pub const NOT_CACHEABLE: Color32 = Color32::from_rgb(0xFF, 0xA7, 0x26); pub const UNSUPPORTED: Color32 = Color32::from_rgb(0xAB, 0x47, 0xBC); pub const NOT_COMPILE: Color32 = Color32::from_rgb(0x26, 0xA6, 0x9A); /// Text colours that mean "good", "worth a look" and "bad", for the theme being drawn. pub fn marks(dark: bool) -> [Color32; 3] { if dark { [ Color32::from_rgb(0x3F, 0xB9, 0x50), Color32::from_rgb(0xD2, 0x99, 0x22), Color32::from_rgb(0xF8, 0x51, 0x49), ] } else { [ Color32::from_rgb(0x1A, 0x7F, 0x37), Color32::from_rgb(0x9A, 0x67, 0x00), Color32::from_rgb(0xCF, 0x22, 0x2E), ] } } /// The colour of a hit rate: green from 70%, yellow from 40%, red below. pub fn hit_rate_color(rate: Option, dark: bool) -> Color32 { let [good, warn, bad] = marks(dark); match rate { Some(rate) if rate >= 0.7 => good, Some(rate) if rate >= 0.4 => warn, Some(_) => bad, None => Color32::GRAY, } } /// A titled section that starts open and can be folded away. pub fn section( ui: &mut Ui, id: &'static str, title: impl Into, body: impl FnOnce(&mut Ui) -> R, ) -> Option { ui.add_space(8.0); // The body is indented, and then thinks it may run that much further right; // hold it to the width that is left so nothing is pushed off the page. let body_width = ui.available_rect_before_wrap().width() - ui.spacing().indent; egui::CollapsingHeader::new(title.into().strong().size(17.0)) .id_salt(id) .default_open(true) .show(ui, |ui| { ui.set_max_width(body_width); body(ui) }) .body_returned } /// A message that must be noticed, in the colour of how bad it is. pub fn banner(ui: &mut Ui, text: impl Into, color: Color32) { let mut text: RichText = text.into(); text = text.strong(); // As wide as there is room, minus a little: text that wraps measures itself a few // pixels wider, and a page that is told to fit exactly that wide grows each time. let width = ui.available_rect_before_wrap().width() - 2.0 * 8.0 - 6.0; egui::Frame::NONE .fill(color.gamma_multiply(0.22)) .stroke(Stroke::new(1.0, color)) .corner_radius(6.0) .inner_margin(8.0) .show(ui, |ui| { ui.set_min_width(width); ui.add(egui::Label::new(text).wrap()); }); ui.add_space(6.0); } /// A small coloured square in front of a legend entry. pub fn swatch(ui: &mut Ui, color: Color32) { let (rect, _) = ui.allocate_exact_size(egui::vec2(10.0, 10.0), Sense::hover()); ui.painter().rect_filled(rect, 2.0, color); } /// A trend line one point per bucket, drawn in a strip `height` tall. `None` /// points break the line, so a gap never looks like a zero. `top` is the value /// drawn at the top of the strip. pub fn sparkline(ui: &mut Ui, height: f32, values: &[Option], top: f64, color: Color32) { let (rect, _) = ui.allocate_exact_size(egui::vec2(ui.available_rect_before_wrap().width(), height), Sense::hover()); let painter = ui.painter(); painter.hline( rect.x_range(), rect.bottom() - 0.5, Stroke::new(1.0, ui.visuals().widgets.noninteractive.bg_stroke.color), ); if values.is_empty() || top <= 0.0 { return; } let at = |index: usize, value: f64| { Pos2::new( rect.left() + rect.width() * ((index as f32 + 0.5) / values.len() as f32), rect.bottom() - (rect.height() * (value / top) as f32).min(rect.height()), ) }; let mut run: Vec = Vec::new(); let flush = |run: &mut Vec, painter: &egui::Painter| match run.len() { 0 => {} 1 => { painter.circle_filled(run[0], 2.0, color); run.clear(); } _ => { painter.add(Shape::line(std::mem::take(run), Stroke::new(1.5, color))); } }; for (index, value) in values.iter().enumerate() { match value { Some(value) => run.push(at(index, *value)), None => flush(&mut run, painter), } } flush(&mut run, painter); } /// A bar filled from the left to `fraction` of its width (0 to 1), always `height` tall. pub fn meter(ui: &mut Ui, height: f32, fraction: f64, fill: Color32) { let (rect, _) = ui.allocate_exact_size(egui::vec2(ui.available_rect_before_wrap().width(), height), Sense::hover()); let painter = ui.painter(); // An outline, so the empty track can be seen when the bar is nearly empty. painter.rect( rect, height / 2.0, ui.visuals().extreme_bg_color, ui.visuals().widgets.noninteractive.bg_stroke, egui::StrokeKind::Inside, ); let width = (rect.width() * fraction.clamp(0.0, 1.0) as f32).min(rect.width()); if width > 0.5 { painter.rect_filled( egui::Rect::from_min_max(rect.min, egui::pos2(rect.left() + width, rect.bottom())), height / 2.0, fill, ); } } /// One bar split into coloured parts. The parts fill the whole width when /// their counts add up to `total`; a part with no count gets no width. pub fn stacked_bar(ui: &mut Ui, height: f32, parts: &[(&'static str, u64, Color32)], total: u64) { let (rect, response) = ui.allocate_exact_size(egui::vec2(ui.available_rect_before_wrap().width(), height), Sense::hover()); let painter = ui.painter(); painter.rect_filled(rect, 4.0, ui.visuals().extreme_bg_color); let mut x = rect.left(); let mut tip = String::new(); for (name, part, color) in parts { let width = if total > 0 { rect.width() * *part as f64 as f32 / total as f32 } else { 0.0 }; if width > 0.0 { let part_rect = egui::Rect::from_min_max(egui::pos2(x, rect.top()), egui::pos2(x + width, rect.bottom())); painter.rect_filled(part_rect, 4.0, *color); x += width; tip.push_str(&format!("{name}: {} ({})\n", crate::fmt::count(*part), crate::fmt::share(*part, total))); } } if !tip.is_empty() { response.clone().on_hover_text(tip.trim_end()); } } /// A short bar of fixed width showing hits against misses for one language. pub fn hit_bar(ui: &mut Ui, width: f32, hits: u64, misses: u64) { let (rect, _) = ui.allocate_exact_size(egui::vec2(width, 12.0), Sense::hover()); let painter = ui.painter(); painter.rect_filled(rect, 3.0, ui.visuals().extreme_bg_color); let total = hits + misses; if total > 0 { let hit_width = rect.width() * hits as f32 / total as f32; painter.rect_filled( egui::Rect::from_min_max(rect.min, egui::pos2(rect.left() + hit_width, rect.bottom())), 3.0, CACHE, ); } } /// Where a value between a tenth of a millisecond and 100 seconds sits on a /// scale that lets 0.7 ms and 8 s both be seen. pub fn log_fraction(ms: f64) -> f64 { const LOW: f64 = -1.0; const HIGH: f64 = 5.0; if !ms.is_finite() || ms <= 0.0 { return 0.0; } let position = (ms.log10() - LOW) / (HIGH - LOW); position.clamp(0.02, 1.0) } /// A cell of [`table_row`]: its width as a share of the row (0 to 1), or /// `f32::INFINITY` for the width that is left; which side its content lines up /// with; and what draws it. pub type Cell<'a> = (f32, egui::Align, Box); /// Text in a cell, cut short if it is too long (or wrapped when `wrap` is set); /// the whole text shows on hover. pub fn text_cell(text: RichText, share: f32, right: bool, wrap: bool) -> Cell<'static> { let hover = text.text().to_owned(); let align = if right { egui::Align::Max } else { egui::Align::Min }; let draw = Box::new(move |ui: &mut Ui| { let label = egui::Label::new(text); let label = if wrap { label.wrap() } else { label.truncate() }; ui.add(label).on_hover_text(hover); }); (share, align, draw) } /// The width an `f32::INFINITY` cell gets in a row whose other cells take `fixed` of the row. pub fn leftover(ui: &Ui, fixed: f32, cells: usize) -> f32 { let all = ui.available_rect_before_wrap().width(); (all * (1.0 - fixed) - ROW_SPACING * cells as f32).max(0.0) } pub const ROW_SPACING: f32 = 4.0; /// One table row. Each cell sits at a fixed share of the row's width, so the /// columns line up across rows whatever the numbers are. pub fn table_row(ui: &mut Ui, cells: Vec>) { let line = ui.text_style_height(&egui::TextStyle::Body) + 2.0; let total = ui.available_rect_before_wrap().width(); let fixed: f32 = cells .iter() .filter(|(share, _, _)| share.is_finite()) .map(|(share, _, _)| *share) .sum::() .min(1.0); let rest = (total * (1.0 - fixed) - ROW_SPACING * cells.len() as f32).max(0.0); // The row is one exact rectangle, and every cell gets its own place in it. let (rect, _) = ui.allocate_exact_size(egui::vec2(total, line), Sense::hover()); let mut row = ui.new_child( egui::UiBuilder::new() .max_rect(rect) .layout(Layout::left_to_right(Align::Center)), ); let mut left = rect.left(); for (share, align, draw) in cells { let width = if share.is_finite() { total * share } else { rest }; let cell = egui::Rect::from_min_max( egui::pos2(left, rect.top()), egui::pos2(left + width, rect.bottom()), ); let layout = match align { Align::Max => Layout::right_to_left(Align::Center), _ => Layout::left_to_right(Align::Center), }; let mut cell = row.new_child(egui::UiBuilder::new().max_rect(cell).layout(layout)); draw(&mut cell); left += width + ROW_SPACING; } } /// What an entry of a chart's key stands for. pub enum Mark { /// A bar. Fill(Color32), /// A line. Line(Color32), /// A dashed line: a reset. Dashed(Color32), /// Time nothing was recorded in. Band(bool), } /// The swatch for one entry of a chart's key. pub fn key(ui: &mut Ui, mark: Mark) { let (rect, _) = ui.allocate_exact_size(egui::vec2(16.0, 10.0), Sense::hover()); let painter = ui.painter(); let left = egui::pos2(rect.left(), rect.center().y); let right = egui::pos2(rect.right(), rect.center().y); match mark { Mark::Fill(color) => { painter.rect_filled(rect.shrink(1.0), 2.0, color); } Mark::Line(color) => { painter.add(Shape::line(vec![left, right], Stroke::new(2.0, color))); } Mark::Dashed(color) => { painter.add(Shape::dashed_line( &[left, right], Stroke::new(1.5, color), 4.0, 3.0, )); } Mark::Band(dark) => { let color = if dark { Color32::WHITE.gamma_multiply(0.14) } else { Color32::BLACK.gamma_multiply(0.12) }; painter.rect_filled(rect.shrink(1.0), 2.0, color); } } }