diff --git a/crates/ui-egui/src/tools/analysis/data_points_labels_tests.rs b/crates/ui-egui/src/tools/analysis/data_points_labels_tests.rs new file mode 100644 index 0000000000..236a071a3a --- /dev/null +++ b/crates/ui-egui/src/tools/analysis/data_points_labels_tests.rs @@ -0,0 +1,97 @@ +//! #16345: Image › Analysis › Select Data Points draws every check box with its data point's name, and each column's +//! title above its boxes. + +use egui::accesskit::Role; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use photocraft_engine::analysis_cmds; +use serde_json::json; + +use super::sources; +use crate::PhotocraftApp; + +type H = Harness<'static, Box>; + +fn open_dialog() -> H { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_max_steps(64).with_context(app.ctx().clone()).build_ui_state( + |ui, app: &mut Box| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + crate::dialogs::run(app, ui); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, Default::default()); + let ctx = h.ctx.clone(); + crate::menus::invoke(h.state_mut(), &ctx, "image.analysis.selectDataPoints", json!({})).unwrap(); + h.run_steps(6); + h +} + +/// The shown name of data point `column`, as the engine lists it. +fn name(column: &str) -> &str { + analysis_cmds::COLUMNS.iter().find(|c| c.0 == column).map_or(column, |c| c.1) +} + +/// A check box of the dialog: where it is, its name, and where the text that names it is drawn. +struct Shown { + rect: egui::Rect, + name: String, + text: Option, +} + +fn rect(r: egui::accesskit::Rect) -> egui::Rect { + egui::Rect::from_min_max(egui::pos2(r.x0 as f32, r.y0 as f32), egui::pos2(r.x1 as f32, r.y1 as f32)) +} + +#[test] +fn every_check_box_draws_its_data_point_name_and_each_column_its_title_above() { + let h = open_dialog(); + let mut boxes: Vec = h + .query_all_by_role(Role::CheckBox) + .map(|b| { + let node = b.accesskit_node(); + let name = node.label().unwrap_or_default(); + let text = + node.labelled_by().find(|l| l.role() == Role::Label && l.value().as_deref() == Some(name.as_str())).and_then(|l| l.bounding_box()).map(rect); + Shown { rect: b.rect(), name, text } + }) + .collect(); + // In columns by their left edge, each column top to bottom. + boxes.sort_by(|a, b| a.rect.left().total_cmp(&b.rect.left()).then(a.rect.top().total_cmp(&b.rect.top()))); + let mut columns: Vec> = Vec::new(); + for shown in boxes { + match columns.last_mut() { + Some(column) if (column[0].rect.left() - shown.rect.left()).abs() < 0.5 => column.push(shown), + _ => columns.push(vec![shown]), + } + } + let names: Vec> = columns.iter().map(|c| c.iter().map(|s| s.name.as_str()).collect()).collect(); + assert_eq!(columns.len(), sources().len(), "one column of boxes per source: {names:?}"); + for (((source, title), column), shown) in sources().into_iter().zip(&columns).zip(&names) { + let want: Vec<&str> = analysis_cmds::available(source).into_iter().map(name).collect(); + assert_eq!(shown, &want, "{source}: each box is named after its data point, in order"); + // Each box draws its name: the text that names it stands after the box, on its line. + for b in column { + let text = b.text.unwrap_or_else(|| panic!("{source}: {:?} draws no name", b.name)); + assert!( + text.left() >= b.rect.right() - 0.5 && text.top() < b.rect.bottom() && text.bottom() > b.rect.top(), + "{source}: {:?} is named beside its box", + b.name + ); + } + // The title sits over its column's first box, under the dialog's "Data Points:" rather than on its line. + let heading = h.query_by_label("Data Points:").expect("\"Data Points:\" is drawn").rect(); + let first = column[0].rect; + let title = h + .query_all_by_label(title) + .find(|n| n.accesskit_node().role() != Role::CheckBox && n.rect().left() <= first.center().x && n.rect().right() >= first.center().x) + .unwrap_or_else(|| panic!("{title} heads its column")) + .rect(); + assert!(title.bottom() <= first.top() + 0.5, "{source}: the title {title:?} is above the first box {first:?}"); + assert!(heading.bottom() <= title.top() + 0.5, "{source}: \"Data Points:\" {heading:?} is above the title {title:?}"); + } +} diff --git a/crates/ui-egui/src/tools/analysis/dialogs.rs b/crates/ui-egui/src/tools/analysis/dialogs.rs index 9285426605..8247e31ac5 100644 --- a/crates/ui-egui/src/tools/analysis/dialogs.rs +++ b/crates/ui-egui/src/tools/analysis/dialogs.rs @@ -170,26 +170,32 @@ fn form(kind: Kind, fields: &mut DialogFields) -> ParamDialog<'_> { }) } Kind::DataPoints => { + // Full width, with the title over the columns: in a labelled row every check box is named by the row and + // draws no name of its own. let row = Param::custom("columns", tl!("Data Points"), |ui, fields, _| { - Row::new() + Stack::new() .show(ui, |ui, _| { - for (source, title) in sources() { - Stack::new().show(ui, |ui, _| { - Text::new(Label::from(title)).show(ui); - for column in analysis_cmds::available(source) { - let name = analysis_cmds::COLUMNS.iter().find(|c| c.0 == column).map_or(column, |c| c.1); - let field = point_key(source, column); - let mut on = fields.bool(&field).unwrap_or(false); - Checkbox::new(Bind::bound(&mut on)).label(name.to_owned()).show(ui); - if fields.bool(&field) != Some(on) { - fields.set(field, on); + Text::new(Label::from(tl!("Data Points:"))).show(ui); + Row::new().show(ui, |ui, _| { + for (source, title) in sources() { + Stack::new().show(ui, |ui, _| { + Text::new(Label::from(title)).show(ui); + for column in analysis_cmds::available(source) { + let name = analysis_cmds::COLUMNS.iter().find(|c| c.0 == column).map_or(column, |c| c.1); + let field = point_key(source, column); + let mut on = fields.bool(&field).unwrap_or(false); + Checkbox::new(Bind::bound(&mut on)).label(name.to_owned()).show(ui); + if fields.bool(&field) != Some(on) { + fields.set(field, on); + } } - } - }); - } + }); + } + }); }) .output - }); + }) + .full_width(); ParamDialog::command(key, tl!("Select Data Points"), vec![row]) } Kind::ScaleMarker => { @@ -211,3 +217,7 @@ fn form(kind: Kind, fields: &mut DialogFields) -> ParamDialog<'_> { }; dialog.values(Bind::bound(fields)) } + +#[cfg(test)] +#[path = "data_points_labels_tests.rs"] +mod data_points_labels_tests;