ui: leave out status statistics that would overlap the mode text (#4187)

This commit is contained in:
Madhav Agarwal
2026-11-15 15:07:26 +05:30
parent 2f52ae5c7e
commit c46a90c8c8
2 changed files with 61 additions and 1 deletions
+35 -1
View File
@@ -382,6 +382,23 @@ pub fn sheet_tabs(app: &mut SheetApp, ui: &mut Ui) {
});
}
/// How many statistics, given their widths left to right, fit when drawn from the right end of
/// `room` points with `gap` points of space after each. The leftmost (lowest-priority) labels are
/// the ones left out; a label wider than the whole room leaves none.
fn fitting_statistics(widths: &[f32], room: f32, gap: f32) -> usize {
let mut used = 0.0;
let mut kept = 0;
for w in widths.iter().rev() {
let need = w + gap;
if used + need > room {
break;
}
used += need;
kept += 1;
}
kept
}
/// Draws status mode/messages, zoom controls/view-mode buttons and selection statistics.
pub fn status_bar(app: &mut SheetApp, ui: &mut Ui) {
let t = Tokens::get(ui.ctx());
@@ -479,7 +496,14 @@ pub fn status_bar(app: &mut SheetApp, ui: &mut Ui) {
ui.add_space(16.0);
// The layout runs right to left, so the last statistic is drawn first.
// Clicking a statistic copies its value (without the label) to the clipboard.
for p in selection_statistics(app).into_iter().rev() {
// Only the rightmost statistics that fit are drawn: on a narrow window the rest
// would paint over the mode text such as "Ready" at the left end (#4187).
let stats = selection_statistics(app);
let font = theme::ui_font(12.5);
let widths: Vec<f32> = stats.iter().map(|p| ui.painter().layout_no_wrap(p.clone(), font.clone(), t.text_dim).size().x).collect();
let keep = fitting_statistics(&widths, ui.available_width(), 10.0);
let span = stats.len();
for p in stats.into_iter().skip(span - keep).rev() {
let value = p.split_once(": ").map_or(p.as_str(), |(_, v)| v).to_owned();
let resp = ui.add(
egui::Label::new(egui::RichText::new(p).font(theme::ui_font(12.5)).color(t.text_dim))
@@ -586,6 +610,16 @@ mod tests {
app.session.active().unwrap().wb.sheets.iter().map(|s| s.name.clone()).collect()
}
#[test]
fn statistics_that_do_not_fit_are_left_out() {
// Widths left to right (Average, Count, Sum), 10 points of gap after each. Only a suffix fits.
let widths = [60.0, 50.0, 40.0];
assert_eq!(super::fitting_statistics(&widths, 300.0, 10.0), 3);
assert_eq!(super::fitting_statistics(&widths, 159.0, 10.0), 2);
assert_eq!(super::fitting_statistics(&widths, 100.0, 10.0), 1);
assert_eq!(super::fitting_statistics(&widths, 49.0, 10.0), 0);
}
#[test]
fn status_bar_shows_the_statistics_that_are_turned_on_in_order() {
let mut session = gridcraft_engine::Session::default();
@@ -58,3 +58,29 @@ fn right_clicking_the_status_bar_text_opens_the_menu() {
assert!(h.query_by_label("Numerical Count").is_some(), "a right-click on {text:?} opens the statistics menu");
}
}
/// On a phone-width window the statistics are drawn from the right end; anything that does not
/// fit is left out instead of painting over the mode text at the left end (#4187).
#[test]
fn statistics_that_do_not_fit_stay_clear_of_the_mode_text() {
let mut session = Session::new();
session.new_workbook();
for (cell, input) in [("A1", "4"), ("A2", "-1"), ("A3", "9")] {
session.execute("cell.set", json!({"cell": cell, "input": input})).unwrap();
}
session.execute("selection.set", json!({"range": "A1:A3"})).unwrap();
let mut h = crate::common::builder().with_size(vec2(320.0, 640.0)).build_ui_state(
|ui, app: &mut SheetApp| {
app.logic(ui.ctx());
app.ui(ui);
},
crate::common::app(session),
);
h.run_steps(4);
let ready = h.get_by_label("Ready").rect();
for text in ["Average: 4", "Count: 3", "Sum: 12"] {
if let Some(label) = h.query_by_label(text) {
assert!(!label.rect().intersects(ready), "{text:?} overlaps the mode text: {:?} vs {ready:?}", label.rect());
}
}
}