diff --git a/crates/ui/src/app.rs b/crates/ui/src/app.rs index 3eb9b04..5391515 100644 --- a/crates/ui/src/app.rs +++ b/crates/ui/src/app.rs @@ -180,6 +180,14 @@ impl eframe::App for Dashboard { fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { controls(self, ui); + if let (Some(view), Some(error)) = (&self.view, &self.error) { + lost_contact( + ui.ctx(), + error, + view.generated_at, + ui.available_rect_before_wrap().width(), + ); + } egui::CentralPanel::default().show(ui, |ui| { egui::ScrollArea::vertical() .auto_shrink([false; 2]) @@ -188,15 +196,12 @@ impl eframe::App for Dashboard { let Self { range, view, - error, hidden_languages, .. } = self; match view { - None => waiting(ui, error.as_deref(), self.refresh_secs), - Some(view) => { - page(ui, view, *range, error.as_deref(), hidden_languages) - } + None => waiting(ui, self.error.as_deref(), self.refresh_secs), + Some(view) => page(ui, view, *range, hidden_languages), } }); }); @@ -397,23 +402,39 @@ fn waiting(ui: &mut egui::Ui, error: Option<&str>, refresh_secs: u64) { } /// The whole page below the controls. -fn page( - ui: &mut egui::Ui, - view: &View, - asked: Range, - error: Option<&str>, - hidden_languages: &mut Vec, -) { +/// A notice that the server can't be reached, shown over the page rather than +/// in it, so what is already on screen keeps its place and height. +fn lost_contact(ctx: &egui::Context, error: &str, from: i64, page_width: f32) { + let [_, _, bad] = style::marks(ctx.theme() == egui::Theme::Dark); + let when = fmt::moment_text(from + fmt::local_offset()); + egui::Area::new(egui::Id::unique("lost-contact")) + .anchor(egui::Align2::RIGHT_TOP, egui::vec2(-8.0, 52.0)) + .order(egui::Order::Foreground) + .show(ctx, |ui| { + let width = (page_width - 16.0).min(440.0); + egui::Frame::NONE + .fill(bad.gamma_multiply(0.25)) + .stroke(egui::Stroke::new(1.0, bad)) + .corner_radius(6.0) + .inner_margin(8.0) + .show(ui, |ui| { + ui.set_max_width(width); + ui.add( + egui::Label::new( + egui::RichText::new(format!( + "Can't reach the server: {error}. These figures are from {when}." + )) + .strong(), + ) + .wrap(), + ); + }); + }); +} + +fn page(ui: &mut egui::Ui, view: &View, asked: Range, hidden_languages: &mut Vec) { let dark = ui.visuals().dark_mode; let [_, _, bad] = style::marks(dark); - if let Some(error) = error { - let from = fmt::moment_text(view.generated_at + fmt::local_offset()); - style::banner( - ui, - format!("Lost contact with the server: {error}. These figures are from {from}."), - bad, - ); - } if let Some(error) = &view.server.error { style::banner(ui, format!("sccache is not working: {error}"), bad); } @@ -432,6 +453,8 @@ fn page( /// One of the three big figures. struct Figure<'a> { value: String, + /// How big the figure is written: 30 for numbers, less for words. + size: f32, color: egui::Color32, caption: String, /// Smaller print under the figure. @@ -453,7 +476,9 @@ impl Figure<'_> { |ui| { ui.set_width(width); ui.add( - egui::Label::new(egui::RichText::new(&self.value).size(30.0).color(self.color)) + egui::Label::new( + egui::RichText::new(&self.value).size(self.size).color(self.color), + ) .truncate(), ); ui.add(egui::Label::new(egui::RichText::new(&self.caption).small()).truncate()); @@ -510,6 +535,7 @@ fn headline(ui: &mut egui::Ui, view: &View, asked: Range, dark: bool) { ui.horizontal_wrapped(|ui| { Figure { value: fmt::percent(totals.hit_rate()), + size: 30.0, color: style::hit_rate_color(totals.hit_rate(), dark), caption: "from cache".into(), small: format!("{} of all compiler calls", fmt::percent(totals.hit_rate_of_all())), @@ -518,22 +544,43 @@ fn headline(ui: &mut egui::Ui, view: &View, asked: Range, dark: bool) { small2: String::new(), } .draw(ui, column); + // A saving is never written in milliseconds; and when no language's build time is + // known yet, the figure says so instead of showing nothing. + let seconds = view.time_saved.seconds; let left_out = view.time_saved.hits_left_out; - // The 90% range goes first; the hits left out are the line under it. + let left_out_line = if left_out > 0 { + format!("{} hits left out of this figure", fmt::count(left_out)) + } else { + "every hit counted".to_owned() + }; let interval = view .time_saved .interval_90 .map(|(low, high)| format!("{} – {} (90% range)", fmt::duration(low), fmt::duration(high))); + let (value, size, small, small2) = if seconds >= 1.0 { + (format!("~{}", fmt::duration(seconds)), 30.0, interval.unwrap_or_default(), left_out_line) + } else if seconds > 0.0 { + ("~under 1 s".to_owned(), 30.0, interval.unwrap_or_default(), left_out_line) + } else if left_out > 0 { + ( + "not enough data yet".to_owned(), + 22.0, + String::new(), + format!( + "{} hits not counted: no build time known yet for their language", + fmt::count(left_out) + ), + ) + } else { + ("nothing saved yet".to_owned(), 22.0, String::new(), String::new()) + }; Figure { - value: format!("~{}", fmt::duration(view.time_saved.seconds)), + value, + size, color: text_color, caption: "saved (rough)".into(), - small: interval.unwrap_or_default(), - small2: if left_out > 0 { - format!("{} hits left out of this figure", fmt::count(left_out)) - } else { - "every hit counted".into() - }, + small, + small2, trend: Some((&saved, saved_top)), hover: Some(view.time_saved.note.clone()), } @@ -546,6 +593,7 @@ fn headline(ui: &mut egui::Ui, view: &View, asked: Range, dark: bool) { } else { format!("{} problems", fmt::count(problems as u64)) }, + size: 30.0, color: problem_color, caption: worst .map(|problem| problem.title.clone()) diff --git a/crates/ui/src/cards.rs b/crates/ui/src/cards.rs index d7b04b0..eba26fc 100644 --- a/crates/ui/src/cards.rs +++ b/crates/ui/src/cards.rs @@ -1,13 +1,14 @@ -//! The six compact cards under the headline. +//! The six compact cards under the headline: all the same size, tops in a line. -use eframe::egui::{self, Color32, RichText, Ui}; +use eframe::egui::{self, Color32, RichText, Sense, StrokeKind, Ui}; use sccache_dash_api::{Range, View}; use crate::fmt; use crate::style; -/// Every card is this tall, whatever it has to say. +/// Every card is exactly this tall and as wide as its share of the row. const HEIGHT: f32 = 78.0; +const MARGIN: f32 = 8.0; pub fn row(ui: &mut Ui, view: &View, dark: bool) { let totals = &view.totals; @@ -98,28 +99,35 @@ struct Card { } impl Card { + /// A box of exactly `width` by `HEIGHT`, so every card lines up whatever it says. fn draw(self, ui: &mut Ui, width: f32, range: Range, [good, bad]: [Color32; 2]) { - let margin = 8.0; - // Laid out from the top down inside exactly `width`, whatever row it sits in. - ui.allocate_ui_with_layout(egui::vec2(width, HEIGHT), egui::Layout::top_down(egui::Align::Min), |ui| { - egui::Frame::group(ui.style()) - .inner_margin(margin) - .show(ui, |ui| { - ui.set_width(ui.available_rect_before_wrap().width() - 2.0); - ui.set_min_height(HEIGHT - 2.0 * margin); - ui.add(egui::Label::new(RichText::new(self.title).small()).truncate()); - let color = self.color.unwrap_or_else(|| ui.visuals().text_color()); - ui.add(egui::Label::new(RichText::new(fmt::count(self.count)).size(20.0).color(color)).truncate()); - let detail = ui.add( - egui::Label::new(RichText::new(&self.detail).small().weak()).truncate(), - ); - if !self.detail.is_empty() { - detail.on_hover_text(self.detail.clone()); - } - let (change, color) = change(self.count, self.previous, range, self.good_when_up, [good, bad]); - ui.add(egui::Label::new(RichText::new(change).small().color(color)).truncate()); - }); - }); + let (rect, _) = ui.allocate_exact_size(egui::vec2(width, HEIGHT), Sense::hover()); + let visuals = ui.visuals(); + ui.painter().rect( + rect, + 6.0, + visuals.faint_bg_color, + visuals.widgets.noninteractive.bg_stroke, + StrokeKind::Inside, + ); + let mut card = ui.new_child( + egui::UiBuilder::new() + .max_rect(rect.shrink(MARGIN)) + .layout(egui::Layout::top_down(egui::Align::Min)), + ); + card.add(egui::Label::new(RichText::new(self.title).small()).truncate()); + let color = self.color.unwrap_or_else(|| card.visuals().text_color()); + card.add( + egui::Label::new(RichText::new(fmt::count(self.count)).size(20.0).color(color)).truncate(), + ); + let detail = card.add( + egui::Label::new(RichText::new(&self.detail).small().weak()).truncate(), + ); + if !self.detail.is_empty() { + detail.on_hover_text(self.detail.clone()); + } + let (change, color) = change(self.count, self.previous, range, self.good_when_up, [good, bad]); + card.add(egui::Label::new(RichText::new(change).small().color(color)).truncate()); } } diff --git a/crates/ui/src/extras.rs b/crates/ui/src/extras.rs index bbf6f82..ed4e697 100644 --- a/crates/ui/src/extras.rs +++ b/crates/ui/src/extras.rs @@ -15,16 +15,11 @@ pub fn section(ui: &mut Ui, view: &View) { ui.add_space(6.0); } ui.label(RichText::new(&extra.title).strong()); - let label_width = if ui.available_rect_before_wrap().width() < 480.0 { - 104.0 - } else { - 160.0 - }; for (label, value) in &extra.rows { style::table_row( ui, vec![ - style::text_cell(RichText::new(label.as_str()).strong(), label_width, false, false), + style::text_cell(RichText::new(label.as_str()).strong(), 0.3, false, false), style::text_cell(RichText::new(value.as_str()), f32::INFINITY, false, true), ], ); diff --git a/crates/ui/src/overtime.rs b/crates/ui/src/overtime.rs index 6a4bfde..599c6d7 100644 --- a/crates/ui/src/overtime.rs +++ b/crates/ui/src/overtime.rs @@ -1,9 +1,11 @@ //! Over time: hit rate and compile volume per bucket, with a language filter. +//! Both charts always show the whole selected range; time nothing was recorded +//! in is shaded and labelled once. use eframe::egui::{self, Color32, RichText, Stroke, Ui}; use egui_plot::{ - Bar, BarChart, CoordinatesFormatter, Corner, GridInput, GridMark, Legend, Line, LineStyle, - Plot, PlotBounds, PlotPoint, PlotPoints, PlotResponse, PlotUi, Span, VLine, + Bar, BarChart, CoordinatesFormatter, Corner, GridInput, GridMark, Line, LineStyle, Plot, + PlotBounds, PlotPoint, PlotPoints, PlotResponse, PlotUi, Span, VLine, }; use sccache_dash_api::{Bucket, Range, View}; @@ -21,41 +23,24 @@ pub fn section(ui: &mut Ui, view: &View, hidden: &mut Vec, dark: bool) { style::section(ui, "overtime", "Over time", |ui| { filter(ui, view, hidden); - let tz = fmt::local_offset(); + let tz = fmt::local_offset() as f64; let step = bucket_step(&view.buckets, view.range); - let window = view - .buckets - .iter() - .filter(|bucket| !bucket.gap) - .map(|bucket| (bucket.start + tz) as f64) - .fold(None::<(f64, f64)>, |window, start| { - Some(match window { - Some((first, last)) => (first.min(start), last.max(start)), - None => (start, start), - }) - }); + let axes = Axes { + window: range_window(view, step), + step, + tz, + dark, + }; - let axes = Axes { window, step, tz }; ui.label(RichText::new("Hit rate").strong()); - hit_rate_chart(ui, view, hidden, axes, dark); + hit_rate_chart(ui, view, hidden, axes); ui.add_space(6.0); ui.label(RichText::new("Compiles").strong()); - volume_chart(ui, view, hidden, axes, dark); + volume_chart(ui, view, hidden, axes); }); } -/// What both charts share: the time window, the bucket width and the time zone. -#[derive(Clone, Copy)] -struct Axes { - /// First and last bucket start shown, if any bucket was recorded. - window: Option<(f64, f64)>, - /// Seconds per bucket. - step: f64, - /// Seconds to add to a Unix time to get local clock time. - tz: i64, -} - /// Checkboxes that narrow both charts to some languages. fn filter(ui: &mut Ui, view: &View, hidden: &mut Vec) { ui.horizontal_wrapped(|ui| { @@ -108,26 +93,72 @@ fn bucket_step(buckets: &[Bucket], range: Option) -> f64 { } } -/// A chart with a time axis: clock times as ticks and a readout of where the -/// pointer is. It can't be zoomed or dragged, so the wheel and a finger keep -/// scrolling the page; that also means it can fit its data afresh every frame, -/// so new buckets are never cut off. +/// The whole selected range as Unix seconds (start, end): a fixed-length range +/// runs back from now by its length, whatever the buckets cover; "since reset" +/// runs from the first bucket to the last one's end. +fn range_window(view: &View, step: f64) -> (f64, f64) { + let now = view.generated_at as f64; + let (first, last) = match (view.buckets.first(), view.buckets.last()) { + (Some(first), Some(last)) => (first.start as f64, last.start as f64 + step), + _ => (now - step, now), + }; + match view.range { + Some(Range::Hour) => (now - 3_600.0, now), + Some(Range::Day) => (now - 86_400.0, now), + Some(Range::Week) => (now - 604_800.0, now), + Some(Range::Month) => (now - 2_592_000.0, now), + _ => (first, last.max(first + step)), + } +} + +/// The stretches of the window no recorded bucket covers. +fn unrecorded(buckets: &[Bucket], window: (f64, f64), step: f64) -> Vec<(f64, f64)> { + let mut stretches = Vec::new(); + let mut cursor = window.0; + for bucket in buckets.iter().filter(|bucket| !bucket.gap) { + let (from, to) = (bucket.start as f64, bucket.start as f64 + step); + if from > cursor { + stretches.push((cursor, from)); + } + cursor = cursor.max(to); + } + if cursor < window.1 { + stretches.push((cursor, window.1)); + } + stretches +} + +/// What both charts share. +#[derive(Clone, Copy)] +struct Axes { + /// Start and end of the range in Unix seconds. + window: (f64, f64), + /// Seconds per bucket. + step: f64, + /// Seconds to add to a Unix time to get local clock time. + tz: f64, + dark: bool, +} + +/// A chart with a time axis over the whole range: clock times as ticks and a +/// readout of where the pointer is. It can't be zoomed or dragged, so the wheel +/// and a finger keep scrolling the page; that also means it can fit its data +/// afresh every frame, so new buckets are never cut off. fn time_plot<'a>( name: &'static str, axes: Axes, readout: impl Fn(&PlotPoint) -> String + 'a, ) -> Plot<'a> { - let (first, last) = axes.window.unwrap_or((0.0, axes.step)); - let step = axes.step; - let tick = fmt::tick_step(last - first + 2.0 * step); - let plot = Plot::new(name) + let (first, last) = (axes.window.0 + axes.tz, axes.window.1 + axes.tz); + let tick = fmt::tick_step(last - first); + Plot::new(name) .height(CHART_HEIGHT) .allow_scroll(false) .allow_drag(false) .allow_zoom(false) .allow_boxed_zoom(false) - .include_x(first - step) - .include_x(last + step) + // Exactly the range, edge to edge, with no air either side. + .default_x_bounds(first, last) .x_grid_spacer(move |input: GridInput| ticks(input.bounds.0, input.bounds.1, tick)) .x_axis_formatter(move |mark: GridMark, _range| { fmt::tick_label(mark.value.round() as i64, tick) @@ -135,8 +166,8 @@ fn time_plot<'a>( .coordinates_formatter( Corner::LeftTop, CoordinatesFormatter::new(move |point: &PlotPoint, _bounds: &PlotBounds| readout(point)), - ); - plot.reset() + ) + .reset() } fn ticks(from: f64, to: f64, tick: i64) -> Vec { @@ -154,31 +185,53 @@ fn ticks(from: f64, to: f64, tick: i64) -> Vec { marks } -/// Resets as dashed lines and gaps as grey bands, so a gap never reads as zero. -fn marks(plot: &mut PlotUi<'_>, buckets: &[Bucket], step: f64, dark: bool, tz: i64) { - let warn = style::marks(dark)[1]; - let shade = if dark { - Color32::WHITE.gamma_multiply(0.08) +/// The shade for time nothing was recorded in. +fn shade(dark: bool) -> Color32 { + if dark { + Color32::WHITE.gamma_multiply(0.14) } else { - Color32::BLACK.gamma_multiply(0.08) - }; + Color32::BLACK.gamma_multiply(0.12) + } +} + +/// Resets as dashed lines, and everything unrecorded as one grey band per stretch. +fn marks(plot: &mut PlotUi<'_>, buckets: &[Bucket], axes: Axes) { + let warn = style::marks(axes.dark)[1]; for bucket in buckets { - let start = (bucket.start + tz) as f64; if bucket.reset { plot.vline( - VLine::new("Reset", start) + VLine::new("Reset", bucket.start as f64 + axes.tz) .stroke(Stroke::new(1.5, warn)) .style(LineStyle::Dashed { length: 12.0 }), ); } - if bucket.gap { - plot.span( - Span::new("Not recorded", start..=start + step) - .fill(shade) - .border_width(0.0), - ); - } } + for (from, to) in unrecorded(buckets, axes.window, axes.step) { + plot.span( + Span::new("Not recorded", (from + axes.tz)..=(to + axes.tz)) + .fill(shade(axes.dark)) + .border_width(0.0), + ); + } +} + +/// Whether there is anything to mark: a reset, or time nothing was recorded in. +fn marked(buckets: &[Bucket], axes: Axes) -> (bool, bool) { + let any_reset = buckets.iter().any(|bucket| bucket.reset); + let any_gap = !unrecorded(buckets, axes.window, axes.step).is_empty(); + (any_reset, any_gap) +} + +/// The key to a chart, drawn under it so it never sits on the data. +fn legend(ui: &mut Ui, entries: Vec<(style::Mark, &'static str)>) { + ui.add_space(4.0); + ui.horizontal_wrapped(|ui| { + ui.spacing_mut().item_spacing.x = 14.0; + for (mark, text) in entries { + style::key(ui, mark); + ui.label(RichText::new(text).small().weak()); + } + }); } fn overlay(ui: &mut Ui, chart: &PlotResponse<()>, text: &str) { @@ -206,21 +259,17 @@ fn split(bucket: &Bucket, hidden: &[String]) -> (u64, u64) { } } -fn hit_rate_chart( - ui: &mut Ui, - view: &View, - hidden: &[String], - axes: Axes, - dark: bool, -) { - let (step, tz) = (axes.step, axes.tz); +fn hit_rate_chart(ui: &mut Ui, view: &View, hidden: &[String], axes: Axes) { // One line per stretch of buckets that have a hit rate. let mut runs: Vec> = Vec::new(); let mut run: Vec = Vec::new(); for bucket in view.buckets.iter().filter(|bucket| !bucket.gap) { let (hits, misses) = split(bucket, hidden); match hit_rate(hits, misses) { - Some(rate) => run.push(PlotPoint::new((bucket.start + tz) as f64, rate * 100.0)), + Some(rate) => run.push(PlotPoint::new( + bucket.start as f64 + axes.tz, + rate * 100.0, + )), None if !run.is_empty() => runs.push(std::mem::take(&mut run)), None => {} } @@ -246,7 +295,7 @@ fn hit_rate_chart( }) .y_axis_formatter(|mark: GridMark, _| format!("{:.0}%", mark.value)); let chart = plot.show(ui, |plot| { - marks(plot, &view.buckets, step, dark, tz); + marks(plot, &view.buckets, axes); for points in &runs { plot.line( Line::new("Hit rate", PlotPoints::Owned(points.clone())) @@ -258,16 +307,18 @@ fn hit_rate_chart( if runs.is_empty() { overlay(ui, &chart, "No cacheable compiles in this range"); } + let (any_reset, any_gap) = marked(&view.buckets, axes); + let mut entries = vec![(style::Mark::Line(style::CACHE), "Hit rate")]; + if any_reset { + entries.push((style::Mark::Dashed(style::marks(axes.dark)[1]), "Reset")); + } + if any_gap { + entries.push((style::Mark::Band(axes.dark), "Not recorded")); + } + legend(ui, entries); } -fn volume_chart( - ui: &mut Ui, - view: &View, - hidden: &[String], - axes: Axes, - dark: bool, -) { - let (step, tz) = (axes.step, axes.tz); +fn volume_chart(ui: &mut Ui, view: &View, hidden: &[String], axes: Axes) { // The tallest stack of one bucket, with a little air above it. let top = view .buckets @@ -283,15 +334,14 @@ fn volume_chart( axes, |point| fmt::moment_text(point.x.round() as i64), ) - .legend(Legend::default().position(Corner::RightTop)) .default_y_bounds(0.0, top) .y_axis_formatter(|mark: GridMark, _| fmt::count(mark.value.max(0.0).round() as u64)); - let width = step * 0.8; + let width = axes.step * 0.8; let (mut from_cache, mut built, mut other) = (vec![], vec![], vec![]); let mut any = false; for bucket in view.buckets.iter().filter(|bucket| !bucket.gap) { - let x = (bucket.start + tz) as f64; + let x = bucket.start as f64 + axes.tz; let (hits, misses) = split(bucket, hidden); for (bars, count) in [ (&mut from_cache, hits), @@ -306,7 +356,7 @@ fn volume_chart( } let chart = plot.show(ui, |plot| { - marks(plot, &view.buckets, step, dark, tz); + marks(plot, &view.buckets, axes); let from_cache = BarChart::new("From cache", from_cache) .width(width) .color(style::CACHE); @@ -327,4 +377,19 @@ fn volume_chart( if !any { overlay(ui, &chart, "Nothing recorded in this range"); } + let (any_reset, any_gap) = marked(&view.buckets, axes); + let mut entries = vec![ + (style::Mark::Fill(style::CACHE), "From cache"), + (style::Mark::Fill(style::BUILT), "Built"), + ]; + if hidden.is_empty() { + entries.push((style::Mark::Fill(style::NOT_CACHEABLE), "Not cacheable and other")); + } + if any_reset { + entries.push((style::Mark::Dashed(style::marks(axes.dark)[1]), "Reset")); + } + if any_gap { + entries.push((style::Mark::Band(axes.dark), "Not recorded")); + } + legend(ui, entries); } diff --git a/crates/ui/src/style.rs b/crates/ui/src/style.rs index bcb7e15..4f2ae29 100644 --- a/crates/ui/src/style.rs +++ b/crates/ui/src/style.rs @@ -128,7 +128,14 @@ pub fn sparkline(ui: &mut Ui, height: f32, values: &[Option], top: f64, col 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(); - painter.rect_filled(rect, height / 2.0, ui.visuals().extreme_bg_color); + // 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( @@ -193,13 +200,14 @@ pub fn log_fraction(ms: f64) -> f64 { position.clamp(0.02, 1.0) } -/// A cell of [`table_row`]: how wide (`f32::INFINITY` for the width that is left), -/// which side its content lines up with, and what draws it. +/// 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, width: f32, right: bool, wrap: bool) -> Cell<'static> { +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| { @@ -207,40 +215,93 @@ pub fn text_cell(text: RichText, width: f32, right: bool, wrap: bool) -> Cell<'s let label = if wrap { label.wrap() } else { label.truncate() }; ui.add(label).on_hover_text(hover); }); - (width, align, draw) + (share, align, draw) } -/// The width an `f32::INFINITY` cell gets in a row whose other cells are `fixed` wide. -pub fn leftover(ui: &Ui, fixed: &[f32]) -> f32 { +/// 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 - fixed.iter().sum::() - ROW_SPACING * fixed.len() as f32).max(0.0) + (all * (1.0 - fixed) - ROW_SPACING * cells as f32).max(0.0) } pub const ROW_SPACING: f32 = 4.0; -/// One table row: cells of fixed widths from the left, one under another across rows. +/// 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(|(width, _, _)| width.is_finite()) - .map(|(width, _, _)| *width) - .sum(); - let rest = (total - fixed - ROW_SPACING * cells.len() as f32).max(0.0); - ui.allocate_ui_with_layout( - egui::vec2(total, line), - Layout::left_to_right(Align::Center), - |ui| { - ui.spacing_mut().item_spacing.x = ROW_SPACING; - for (width, align, draw) in cells { - let width = if width.is_finite() { width } else { rest }; - let layout = match align { - Align::Max => Layout::right_to_left(Align::Center), - _ => Layout::left_to_right(Align::Center), - }; - ui.allocate_ui_with_layout(egui::vec2(width, line), layout, |ui| draw(ui)); - } - }, + .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); + } + } } diff --git a/crates/ui/src/where_went.rs b/crates/ui/src/where_went.rs index 67f8dbc..de095fa 100644 --- a/crates/ui/src/where_went.rs +++ b/crates/ui/src/where_went.rs @@ -48,15 +48,26 @@ pub fn section(ui: &mut Ui, view: &View, _dark: bool) { if !view.not_cached.is_empty() { ui.add_space(6.0); ui.label(RichText::new("Why compiles could not be cached").strong()); + let heading = |text: &'static str, share: f32, right: bool| { + style::text_cell(RichText::new(text).small().weak(), share, right, false) + }; + style::table_row( + ui, + vec![ + heading("Reason", f32::INFINITY, false), + heading("Compiles", 0.16, true), + heading("Share", 0.12, true), + ], + ); for (reason, count) in &view.not_cached { style::table_row( ui, vec![ style::text_cell(RichText::new(reason.as_str()), f32::INFINITY, false, false), - style::text_cell(RichText::new(fmt::count(*count)), 72.0, true, false), + style::text_cell(RichText::new(fmt::count(*count)), 0.16, true, false), style::text_cell( RichText::new(fmt::share(*count, totals.not_cacheable)), - 56.0, + 0.12, true, false, ), @@ -73,28 +84,28 @@ pub fn section(ui: &mut Ui, view: &View, _dark: bool) { }); } +/// Column shares for the language table: name, hits, misses, errors, hit rate. +/// The bar takes what is left, so the columns line up at every width. +const LANGUAGE_COLUMNS: [f32; 5] = [0.2, 0.16, 0.16, 0.13, 0.12]; + /// One row per language: hits, misses, errors, hit rate and a small bar. fn languages(ui: &mut Ui, view: &View) { let dark = ui.visuals().dark_mode; let text_color = ui.visuals().text_color(); let [_, _, bad] = style::marks(dark); - // Narrower cells on a phone, so the whole row fits. - let (name, hits, misses, errors, rate) = if ui.available_rect_before_wrap().width() < 480.0 { - (68.0, 56.0, 64.0, 52.0, 48.0) - } else { - (130.0, 80.0, 80.0, 72.0, 76.0) + let [name, hits, misses, errors, rate] = LANGUAGE_COLUMNS; + let bar = style::leftover(ui, LANGUAGE_COLUMNS.iter().sum(), 6) - style::ROW_SPACING; + let heading = |text: &'static str, share: f32, right: bool| { + style::text_cell(RichText::new(text).small().weak(), share, right, false) }; - let widths = [name, hits, misses, errors, rate]; - let bar = (style::leftover(ui, &widths) - style::ROW_SPACING).max(0.0); - let weak = |text: &'static str, width| style::text_cell(RichText::new(text).small().weak(), width, true, false); style::table_row( ui, vec![ - style::text_cell(RichText::new("").small(), name, false, false), - weak("Hits", hits), - weak("Misses", misses), - weak("Errors", errors), - weak("Hit rate", rate), + heading("", name, false), + heading("Hits", hits, true), + heading("Misses", misses, true), + heading("Errors", errors, true), + heading("Hit rate", rate, true), (f32::INFINITY, egui::Align::Min, Box::new(|_ui| {})), ], );