Page polish from the live service
- Both charts always span exactly the selected range, edge to edge. Time nothing was recorded in is shaded and labelled once, and each chart's key is drawn under the chart instead of over the data. - The time saved says "not enough data yet" or "nothing saved yet" when nothing can be counted, and a saving under a second reads "under 1 s", never in milliseconds. - The six cards are exact boxes of one size, so their tops line up. - Tables have headed columns at fixed shares of the width, text left and numbers right; each row is one rectangle with a place for every cell. - The cache usage bar has an outlined track. - When the server can't be reached, the notice is drawn over the page, so what is on screen keeps its place and height.
This commit is contained in:
1 parent
bcc178330b
commit
73c8138072
6 files changed
+367
-179
No files matched your search
+88
-27
@@ -128,7 +128,14 @@ pub fn sparkline(ui: &mut Ui, height: f32, values: &[Option<f64>], 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<dyn FnOnce(&mut Ui) + 'a>);
|
||||
|
||||
/// 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::<f32>() - 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<Cell<'_>>) {
|
||||
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::<f32>()
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user