From 30643390a7bd0022ddf9ee7b7ae7756fe03d7f90 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 28 Nov 2026 18:23:28 -0700 Subject: [PATCH] Reasons: one line each, click to open; stack the small panels beside tall reasons Co-Authored-By: Claude Sonnet 5.5 --- crates/ui/src/app.rs | 47 +++++++++---- crates/ui/src/disclosure.rs | 88 ++++++++++++++++++++++++ crates/ui/src/lib.rs | 1 + crates/ui/src/where_went.rs | 131 +++++++++++++++++++++++++----------- 4 files changed, 217 insertions(+), 50 deletions(-) create mode 100644 crates/ui/src/disclosure.rs diff --git a/crates/ui/src/app.rs b/crates/ui/src/app.rs index 15889be..6ce2107 100644 --- a/crates/ui/src/app.rs +++ b/crates/ui/src/app.rs @@ -5,6 +5,7 @@ use std::time::Duration; use eframe::egui; use sccache_dash_api::{Range, View}; +use crate::disclosure; use crate::fmt; use crate::theme; use crate::{cards, extras, net, overtime, problems, raw, speed, style, where_went}; @@ -631,18 +632,40 @@ fn page(ui: &mut egui::Ui, view: &View, asked: Range, hidden_languages: &mut Vec 2 => { let half = style::span_width(ui, 2, 1); let reasons = where_went::reasons_height(ui, view, half); - let height = l::TRIPTYCH_HEIGHT.max(l::WHERE_FIXED_HEIGHT).max(reasons); - let [a, b] = row(ui, height, &[1, 1]).try_into().expect("two panels"); - let (_, a) = style::panel(ui, a, "Where compiles went", |ui| where_went::section(ui, view)); - let (_, b) = style::panel(ui, b, "Why compiles were not cacheable", |ui| where_went::reasons(ui, view)); - close_row(ui, &[a, b]); - let height = l::TRIPTYCH_HEIGHT - .max(table_height(l::LANGUAGES_FIXED_HEIGHT, view.languages.len())) - .max(problems_height(ui, view, half)); - let [a, b] = row(ui, height, &[1, 1]).try_into().expect("two panels"); - let (_, a) = style::panel(ui, a, "By language", |ui| where_went::languages(ui, view)); - let (_, b) = style::panel(ui, b, "Problems", |ui| problems::section(ui, view)); - close_row(ui, &[a, b]); + let (where_h, languages_h) = ( + l::TRIPTYCH_HEIGHT.max(l::WHERE_FIXED_HEIGHT), + table_height(l::LANGUAGES_FIXED_HEIGHT, view.languages.len()), + ); + let problems_h = problems_height(ui, view, half); + // Stacked, "By language" is its own row, so it also holds its head line. + let languages_h = languages_h.max(table_height(l::LANGUAGES_FIXED_HEIGHT, view.languages.len() + 1)); + let stacked = where_h + languages_h + problems_h + 2.0 * gap; + if disclosure::stack_beside(where_h, stacked, reasons) { + // The reasons are tall: the three small panels stack beside them, the last one filling what is left. + let [left, right] = row(ui, stacked.max(reasons), &[1, 1]).try_into().expect("two panels"); + let cut = |top: f32, bottom: f32| { + egui::Rect::from_min_max(egui::pos2(left.left(), top), egui::pos2(left.right(), bottom)) + }; + let a = cut(left.top(), left.top() + where_h); + let b = cut(a.bottom() + gap, a.bottom() + gap + languages_h); + let c = cut(b.bottom() + gap, left.bottom()); + let (_, a) = style::panel(ui, a, "Where compiles went", |ui| where_went::section(ui, view)); + let (_, b) = style::panel(ui, b, "By language", |ui| where_went::languages(ui, view)); + let (_, c) = style::panel(ui, c, "Problems", |ui| problems::section(ui, view)); + let (_, d) = style::panel(ui, right, "Why compiles were not cacheable", |ui| where_went::reasons(ui, view)); + close_row(ui, &[a, b, c, d]); + } else { + let height = l::TRIPTYCH_HEIGHT.max(l::WHERE_FIXED_HEIGHT).max(reasons); + let [a, b] = row(ui, height, &[1, 1]).try_into().expect("two panels"); + let (_, a) = style::panel(ui, a, "Where compiles went", |ui| where_went::section(ui, view)); + let (_, b) = style::panel(ui, b, "Why compiles were not cacheable", |ui| where_went::reasons(ui, view)); + close_row(ui, &[a, b]); + let height = l::TRIPTYCH_HEIGHT.max(languages_h).max(problems_h); + let [a, b] = row(ui, height, &[1, 1]).try_into().expect("two panels"); + let (_, a) = style::panel(ui, a, "By language", |ui| where_went::languages(ui, view)); + let (_, b) = style::panel(ui, b, "Problems", |ui| problems::section(ui, view)); + close_row(ui, &[a, b]); + } let [a] = row(ui, l::CHARTS_HEIGHT, &[2]).try_into().expect("one panel"); let (_, a) = style::panel(ui, a, "Over time", |ui| { overtime::section(ui, view, hidden_languages, theme::drawing::CHART_HEIGHT) diff --git a/crates/ui/src/disclosure.rs b/crates/ui/src/disclosure.rs new file mode 100644 index 0000000..389c784 --- /dev/null +++ b/crates/ui/src/disclosure.rs @@ -0,0 +1,88 @@ +//! The arithmetic of a list of lines that open: which start open, how tall the +//! list is, and whether stacking panels beside it wastes less room. No drawing +//! here, so it is tested natively. + +/// A line that opens to show a text beneath it, laid out for a width. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Entry { + /// The line itself. + pub row: f32, + pub open: bool, + /// The first paragraph's height; used only when open. + pub meaning: f32, + /// The second paragraph's height, `None` when there is none; used only when open. + pub action: Option, +} + +/// Whether a line is open: what was clicked, else open when `fixable`. +pub fn is_open(stored: Option, fixable: bool) -> bool { + stored.unwrap_or(fixable) +} + +/// How tall one line is: `gap` between its parts, `pad` under the open text. +pub fn entry_height(entry: &Entry, gap: f32, pad: f32) -> f32 { + if !entry.open { + return entry.row; + } + let action = entry.action.map_or(0.0, |height| gap + height); + entry.row + gap + entry.meaning + action + pad +} + +/// How tall the whole list is: its lines and a hairline `separator` tall between them. +pub fn list_height(entries: &[Entry], gap: f32, pad: f32, separator: f32) -> f32 { + let lines: f32 = entries.iter().map(|entry| entry_height(entry, gap, pad)).sum(); + lines + entries.len().saturating_sub(1) as f32 * separator +} + +/// Whether to stack the panels (`stacked` tall in all) beside a panel `beside` tall rather +/// than put the one `single` panel there: yes when that leaves less empty room. +pub fn stack_beside(single: f32, stacked: f32, beside: f32) -> bool { + (stacked - beside).abs() < (single - beside).abs() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn entry(open: bool) -> Entry { + Entry { row: 28.0, open, meaning: 45.0, action: Some(30.0) } + } + + #[test] + fn only_fixable_lines_start_open() { + assert!(is_open(None, true)); + assert!(!is_open(None, false)); + } + + #[test] + fn a_click_wins_over_the_default() { + assert!(!is_open(Some(false), true)); + assert!(is_open(Some(true), false)); + } + + #[test] + fn a_closed_line_is_one_row_and_an_open_one_adds_its_text() { + assert_eq!(entry_height(&entry(false), 3.0, 8.0), 28.0); + assert_eq!(entry_height(&entry(true), 3.0, 8.0), 28.0 + 3.0 + 45.0 + 3.0 + 30.0 + 8.0); + let no_action = Entry { action: None, ..entry(true) }; + assert_eq!(entry_height(&no_action, 3.0, 8.0), 28.0 + 3.0 + 45.0 + 8.0); + } + + #[test] + fn a_list_is_its_lines_and_the_hairlines_between() { + let list = [entry(true), entry(false), entry(false)]; + assert_eq!(list_height(&list, 3.0, 8.0, 1.0), 117.0 + 28.0 + 28.0 + 2.0); + assert_eq!(list_height(&[], 3.0, 8.0, 1.0), 0.0); + assert_eq!(list_height(&[entry(false)], 3.0, 8.0, 1.0), 28.0); + } + + #[test] + fn stacking_is_chosen_only_when_it_fills_the_room_better() { + // Everything closed: the lone panel (244) is close to the list (300); three stacked (500) are not. + assert!(!stack_beside(244.0, 500.0, 300.0)); + // Two open: the list (520) is nearer the stack. + assert!(stack_beside(244.0, 500.0, 520.0)); + // A tie keeps the simple layout. + assert!(!stack_beside(200.0, 400.0, 300.0)); + } +} diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index 5ade9b5..3299efb 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -1,6 +1,7 @@ //! The sccache-dash dashboard page. `trunk` compiles this to WebAssembly and //! the sccache-dash server serves it at http://127.0.0.1:8226. +pub mod disclosure; pub mod fmt; // Drawing needs eframe, which is only built for the browser so far; the rest diff --git a/crates/ui/src/where_went.rs b/crates/ui/src/where_went.rs index 04a9c94..9346501 100644 --- a/crates/ui/src/where_went.rs +++ b/crates/ui/src/where_went.rs @@ -6,8 +6,9 @@ use std::sync::Arc; use eframe::egui::{self, Color32, Sense, Stroke, Ui}; use eframe::epaint::text::Galley; -use sccache_dash_api::View; +use sccache_dash_api::{NotCached, View}; +use crate::disclosure; use crate::fmt; use crate::style::{self, Cell}; use crate::theme; @@ -57,32 +58,47 @@ pub fn section(ui: &mut Ui, view: &View) { )); } -/// One reason laid out for a width: its name, numbers and the two small paragraphs. +/// One reason laid out for a width: its line, and the two small paragraphs when it is open. struct Block { name: Arc, count: Arc, share: Arc, - meaning: Arc, - /// `None` when sccache gives nothing to do. + /// `None` while the reason is closed. + meaning: Option>, + /// `None` when closed, or when sccache gives nothing to do. action: Option>, - /// The first line: the name, or as tall as the name when it wraps. + /// The line: at least a control tall, taller when the name wraps. row: f32, + open: bool, } impl Block { - /// Its height, counting the room each part is given. - fn height(&self) -> f32 { - let line = theme::space::LINE; - let mut height = self.row + line + self.meaning.size().y + line; - if let Some(action) = &self.action { - height += action.size().y + line; + fn entry(&self) -> disclosure::Entry { + disclosure::Entry { + row: self.row, + open: self.open, + meaning: self.meaning.as_ref().map_or(0.0, |g| g.size().y), + action: self.action.as_ref().map(|g| g.size().y), } - height } } /// How far the name is from the numbers, and from the dot. const NAME_GAP: f32 = 8.0; +/// The side of the chevron's triangle box. +const CHEVRON: f32 = 8.0; +/// The hairline between two lines. +const SEPARATOR: f32 = 1.0; + +/// Where a reason remembers whether it is open. +fn open_id(reason: &str) -> egui::Id { + egui::Id::new(("reason-open", reason)) +} + +fn is_open(ctx: &egui::Context, reason: &NotCached) -> bool { + let stored = ctx.data(|data| data.get_temp::(open_id(&reason.reason))); + disclosure::is_open(stored, reason.fixable) +} /// Lay every reason out in `width`; counts and shares line up in columns, the name takes the rest. fn blocks(ui: &Ui, view: &View, width: f32) -> Vec { @@ -96,29 +112,43 @@ fn blocks(ui: &Ui, view: &View, width: f32) -> Vec { numbers.iter().map(|n| pick(n).size().x).fold(0.0, f32::max) }; let (count_width, share_width) = (widest(|n| &n.0), widest(|n| &n.1)); - let dot = theme::drawing::SEVERITY_DOT + NAME_GAP; - let name_width = (width - dot - count_width - share_width - 2.0 * NAME_GAP).max(40.0); + let indent = theme::drawing::SEVERITY_DOT + NAME_GAP; + let name_width = (width - indent - count_width - share_width - CHEVRON - 3.0 * NAME_GAP).max(40.0); view.not_cached .iter() .zip(numbers) .map(|(r, (count, share))| { let name = style::galley(ui, theme::body(r.name.clone()), true, name_width); - let small = |text: &str| style::galley(ui, theme::small(ui, text), true, width); + let open = is_open(ui.ctx(), r); + let small = |text: &str| style::galley(ui, theme::small(ui, text), true, width - indent); Block { - row: theme::space::ROW.max(name.size().y + 6.0), + row: theme::space::CONTROL_HEIGHT.max(name.size().y + 2.0 * theme::space::ITEM), name, count, share, - meaning: small(&r.meaning), - action: (!r.action.is_empty()).then(|| small(&r.action)), + meaning: open.then(|| small(&r.meaning)), + action: (open && !r.action.is_empty()).then(|| small(&r.action)), + open, } }) .collect() } -/// "Why compiles were not cacheable": each reason with a dot (the warning colour when a -/// setting would fix it, the muted one when it is expected), its plain name, its count and -/// share, and under it, in small print, what it means and what to do. +/// A triangle in the muted colour: pointing right when closed, down when open. +fn chevron(painter: &egui::Painter, center: egui::Pos2, open: bool, color: Color32) { + let h = CHEVRON / 2.0; + let points = if open { + vec![center + egui::vec2(-h, -h / 2.0), center + egui::vec2(h, -h / 2.0), center + egui::vec2(0.0, h / 2.0 + 1.0)] + } else { + vec![center + egui::vec2(-h / 2.0, -h), center + egui::vec2(-h / 2.0, h), center + egui::vec2(h / 2.0 + 1.0, 0.0)] + }; + painter.add(egui::Shape::convex_polygon(points, color, Stroke::NONE)); +} + +/// "Why compiles were not cacheable": one line per reason, each with a dot (the warning +/// colour when a setting would fix it, the muted one when it is expected), its plain name, +/// its count and share, and a chevron. Clicking the line shows, in small print under it, +/// what it means and what to do; the reasons a setting would fix start open. pub fn reasons(ui: &mut Ui, view: &View) { if view.not_cached.is_empty() { ui.label(theme::small(ui, "Every compile could be cached in this range.")); @@ -127,27 +157,53 @@ pub fn reasons(ui: &mut Ui, view: &View) { let p = theme::ui(ui); let width = ui.available_width(); let blocks = blocks(ui, view, width); + let indent = theme::drawing::SEVERITY_DOT + NAME_GAP; + // Every height is added by hand, so the list is as tall as `reasons_height` says. + ui.spacing_mut().item_spacing.y = 0.0; for (index, (reason, block)) in view.not_cached.iter().zip(&blocks).enumerate() { if index > 0 { - let (gap, _) = ui.allocate_exact_size(egui::vec2(width, theme::space::BETWEEN_PARTS), Sense::hover()); - ui.painter().hline(gap.x_range(), gap.center().y.round() + 0.5, Stroke::new(1.0, p.grid)); + let (gap, _) = ui.allocate_exact_size(egui::vec2(width, SEPARATOR), Sense::hover()); + ui.painter().rect_filled(gap, 0.0, p.grid); + } + let (row, response) = ui.allocate_exact_size(egui::vec2(width, block.row), Sense::click()); + let response = response.on_hover_cursor(egui::CursorIcon::PointingHand); + response.widget_info(|| { + egui::WidgetInfo::labeled(egui::WidgetType::Button, true, reason.name.as_str()) + }); + if response.clicked() { + ui.ctx().data_mut(|data| data.insert_temp(open_id(&reason.reason), !block.open)); + ui.ctx().request_repaint(); } - let (row, _) = ui.allocate_exact_size(egui::vec2(width, block.row), Sense::hover()); let painter = ui.painter(); + if response.hovered() || response.has_focus() { + painter.rect_filled(row, theme::space::CONTROL_RADIUS, p.hover); + } + if response.has_focus() { + painter.rect_stroke(row, theme::space::CONTROL_RADIUS, Stroke::new(1.0, p.muted), egui::StrokeKind::Inside); + } let side = theme::drawing::SEVERITY_DOT; let dot = if reason.fixable { p.warn } else { p.not_cacheable }; let name_top = row.top() + (block.row - block.name.size().y) / 2.0; - painter.circle_filled(egui::pos2(row.left() + side / 2.0, name_top + 8.0), side / 2.0, dot); - painter.galley(egui::pos2(row.left() + side + NAME_GAP, name_top), block.name.clone(), p.text); - let share_left = row.right() - block.share.size().x; - let at = |galley: &Arc, right: f32| { - egui::pos2(right - galley.size().x, row.top() + (block.row - galley.size().y) / 2.0) + painter.circle_filled(egui::pos2(row.left() + side / 2.0, row.center().y), side / 2.0, dot); + painter.galley(egui::pos2(row.left() + indent, name_top), block.name.clone(), p.text); + let chevron_left = row.right() - CHEVRON; + chevron(painter, egui::pos2(chevron_left + CHEVRON / 2.0, row.center().y), block.open, p.muted); + let share_left = chevron_left - NAME_GAP - block.share.size().x; + let at = |galley: &Arc, left: f32| { + egui::pos2(left, row.top() + (block.row - galley.size().y) / 2.0) }; - painter.galley(at(&block.share, row.right()), block.share.clone(), p.text); - painter.galley(at(&block.count, share_left - NAME_GAP), block.count.clone(), p.text); - for galley in std::iter::once(&block.meaning).chain(block.action.as_ref()) { - let (place, _) = ui.allocate_exact_size(galley.size(), Sense::hover()); - ui.painter().galley(place.min, galley.clone(), p.muted); + painter.galley(at(&block.share, share_left), block.share.clone(), p.text); + let count_left = share_left - NAME_GAP - block.count.size().x; + painter.galley(at(&block.count, count_left), block.count.clone(), p.text); + let mut opened = false; + for galley in block.meaning.iter().chain(block.action.as_ref()) { + ui.add_space(theme::space::LINE); + let (place, _) = ui.allocate_exact_size(egui::vec2(width, galley.size().y), Sense::hover()); + ui.painter().galley(egui::pos2(place.left() + indent, place.top()), galley.clone(), p.muted); + opened = true; + } + if opened { + ui.add_space(theme::space::ITEM); } } } @@ -155,10 +211,9 @@ pub fn reasons(ui: &mut Ui, view: &View) { /// How tall "Why compiles were not cacheable" is in a panel `panel_width` wide. pub fn reasons_height(ui: &Ui, view: &View, panel_width: f32) -> f32 { let inner = panel_width - 2.0 * (theme::space::PANEL_PADDING + 1.0); - let blocks = blocks(ui, view, inner); - let gaps = blocks.len().saturating_sub(1) as f32 * (theme::space::BETWEEN_PARTS + theme::space::LINE); - let content: f32 = blocks.iter().map(Block::height).sum(); - theme::layout::REASONS_FIXED_HEIGHT + content.max(theme::space::ROW) + gaps + let entries: Vec<_> = blocks(ui, view, inner).iter().map(Block::entry).collect(); + let list = disclosure::list_height(&entries, theme::space::LINE, theme::space::ITEM, SEPARATOR); + theme::layout::REASONS_FIXED_HEIGHT + list.max(theme::space::ROW) } /// "By language": one row per language, with hits, misses, errors, the hit rate