diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index fcf3484f75..0af0c98de7 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -399,7 +399,7 @@ pub fn commands() -> Vec { r##""barOutline"?: {"color"?, "width"?: points, 0 to 5, a wider one held to 5, the keys not given keeping the outline's}|null (null or a width of 0 for none), "##, r##""hideLabels"?: bool (chart, as in layout.addElement: null leaves each other one as it is; refused for other elements unless null), "##, r##""rows"?, "##, - r##""fields"?: [field names] (table, as in layout.addElement, of the layer it will show; [] returns to every field, null leaves them; another "layer" without them drops them), "##, + r##""fields"?: [field names] (table, as in layout.addElement, of the layer it will show, plus any the table already shows of that layer and it has lost, spelled the same; [] returns to every field, null leaves them; another "layer" without them drops them), "##, r##""header"?: bool (table, as in layout.addElement; null leaves it as it is), "##, r##""image"?: base64 (picture: its new image, its file no longer remembered, which is said when it had one), "##, r##""path"?: a PNG or JPEG file (picture: read again, its image replacing the one it has, and remembered as its file; a file missing, over 16 MB, not a PNG or JPEG, or one that can't be drawn is refused and the picture left as it was; image and path together are refused; refused in the browser, which has no file paths: give its image; null leaves each as it is; refused for other elements), "##, diff --git a/crates/engine/src/cmd/layout/elements.rs b/crates/engine/src/cmd/layout/elements.rs index d406d72f1b..5f62984abd 100644 --- a/crates/engine/src/cmd/layout/elements.rs +++ b/crates/engine/src/cmd/layout/elements.rs @@ -83,19 +83,27 @@ pub(in crate::cmd::layout) fn table_fields(p: &Value) -> Result } /// `names` as the fields of `ds` they name, spelled as it spells them: an error for one it hasn't, -/// or one named twice. -pub(in crate::cmd::layout) fn table_columns(ds: &rustgis_data::Dataset, names: &[&str]) -> Result, String> { +/// or one named twice. A name the table already shows of this layer (`held`, spelled so) that no +/// field is spelled as stays as it is, as the table keeps a field the layer loses and draws one +/// added since under another case as lost too. +pub(in crate::cmd::layout) fn table_columns(ds: &rustgis_data::Dataset, names: &[&str], held: &[String]) -> Result, String> { let mut taken = std::collections::HashSet::new(); names .iter() - .map(|name| { - let at = rustgis_map::layout::table_field(ds, name); - let (i, field) = - at.and_then(|i| ds.fields.get(i).map(|f| (i, f))).ok_or_else(|| format!("no field '{name}' in {}", ds.name))?; - if !taken.insert(i) { - return Err(format!("fields names '{}' twice", field.name)); + .map(|&name| { + let exact = ds.fields.iter().find(|f| f.name == name); + let field = match exact { + Some(f) => f.name.as_str(), + None if held.iter().any(|h| h == name) => name, + None => rustgis_map::layout::table_field(ds, name) + .and_then(|i| ds.fields.get(i)) + .map(|f| f.name.as_str()) + .ok_or_else(|| format!("no field '{name}' in {}", ds.name))?, + }; + if !taken.insert(field) { + return Err(format!("fields names '{field}' twice")); } - Ok(field.name.clone()) + Ok(field.to_string()) }) .collect() } @@ -585,7 +593,7 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu let layer = frame_layer_name(s, l, frame, p)?; let rows = table_rows(p)?.unwrap_or(12); let fields = match table_fields(p)? { - Some(names) => table_columns(frame_dataset(s, l, frame, &layer).ok_or("no such layer")?, &names)?, + Some(names) => table_columns(frame_dataset(s, l, frame, &layer).ok_or("no such layer")?, &names, &[])?, None => Vec::new(), }; let text_size = text_size_param(p, "textSize", TABLE_TEXT_PT)?.unwrap_or(TABLE_TEXT_PT); diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index b875c84b28..65ed73bf66 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -981,10 +981,12 @@ fn table_update( checked: Option<&(String, Option)>, ) -> Result>, String> { let Some(l) = s.project.layouts.get(li) else { return Ok(None) }; - let Some(ElementKind::Table { frame, .. }) = l.element(id).map(|e| &e.kind) else { return Ok(None) }; + let Some(ElementKind::Table { frame, layer: shown, fields, .. }) = l.element(id).map(|e| &e.kind) else { return Ok(None) }; let Some(names) = table_fields(p)? else { return Ok(None) }; let Some((layer, _)) = checked else { return Err("fields need the table's layer".into()) }; - Ok(Some(table_columns(frame_dataset(s, l, *frame, layer).ok_or("no such layer")?, &names)?)) + // Another layer's fields aren't held for this one. + let held: &[String] = if layer.eq_ignore_ascii_case(shown) { fields } else { &[] }; + Ok(Some(table_columns(frame_dataset(s, l, *frame, layer).ok_or("no such layer")?, &names, held)?)) } /// Validates a table/chart's effective layer and field before mutation; input binding uses this same choice. diff --git a/crates/engine/src/tests/layout_table.rs b/crates/engine/src/tests/layout_table.rs index 2329247bb5..a4feb98952 100644 --- a/crates/engine/src/tests/layout_table.rs +++ b/crates/engine/src/tests/layout_table.rs @@ -68,6 +68,45 @@ fn a_field_the_layer_does_not_have_or_named_twice_is_refused() { } } +/// A field removed after the table chose it keeps its column through later changes of the list; +/// a name the table doesn't hold is still refused (#3409). +#[test] +fn a_field_the_table_holds_stays_in_a_new_list_after_the_layer_loses_it() { + let (mut s, id) = tabled(); + let [b, a] = two_fields(&s); + update(&mut s, id, json!({"fields": [b, a]})).unwrap(); + s.run("edit.deleteField", &json!({"layer": "Countries", "field": b})).unwrap(); + update(&mut s, id, json!({"fields": [a, b]})).unwrap(); + assert_eq!(table(&s, id)["fields"], json!([a, b])); + // Only as the table spells it, once, and no other name the layer lacks. + for (fields, said) in [(json!([a, b.to_uppercase()]), "no field"), (json!([b, b]), "twice"), (json!([b, "nope"]), "no field 'nope'")] { + refused(&mut s, "layout.update", json!({"element": id, "fields": fields}), said); + } + // A new table, or another layer, holds no such name. + refused(&mut s, "layout.addElement", json!({"kind": "table", "layer": "Countries", "fields": [b]}), "no field"); + refused(&mut s, "layout.update", json!({"element": id, "layer": "Cities", "fields": [b]}), "no field"); + update(&mut s, id, json!({"fields": [a]})).unwrap(); + refused(&mut s, "layout.update", json!({"element": id, "fields": [a, b]}), "no field"); +} + +/// A field added under another case isn't the one a table lost: its column stays as it is, as it +/// is drawn, while a name the table doesn't hold is still read in any case (#3409). +#[test] +fn a_lost_column_is_not_given_to_a_field_of_another_case() { + let (mut s, id) = tabled(); + let [b, a] = two_fields(&s); + update(&mut s, id, json!({"fields": [b, a]})).unwrap(); + s.run("edit.deleteField", &json!({"layer": "Countries", "field": b})).unwrap(); + let other = b.to_uppercase(); + s.run("edit.addField", &json!({"layer": "Countries", "name": other, "type": "text"})).unwrap(); + update(&mut s, id, json!({"fields": [a, b]})).unwrap(); + assert_eq!(table(&s, id)["fields"], json!([a, b])); + update(&mut s, id, json!({"fields": [b, other]})).unwrap(); + assert_eq!(table(&s, id)["fields"], json!([b, other])); + update(&mut s, id, json!({"fields": [a.to_uppercase()]})).unwrap(); + assert_eq!(table(&s, id)["fields"], json!([a])); +} + #[test] fn fields_of_the_wrong_type_are_refused() { let (mut s, id) = tabled(); diff --git a/crates/ui-egui/src/layoutview.rs b/crates/ui-egui/src/layoutview.rs index b2184f2e35..56dd738e7b 100644 --- a/crates/ui-egui/src/layoutview.rs +++ b/crates/ui-egui/src/layoutview.rs @@ -19,6 +19,8 @@ mod refused_tests; mod section; mod snap; #[cfg(test)] +mod table_tests; +#[cfg(test)] mod tests; pub(crate) use actions::locked_delete; diff --git a/crates/ui-egui/src/layoutview/format.rs b/crates/ui-egui/src/layoutview/format.rs index b31c628ce7..afeb89b704 100644 --- a/crates/ui-egui/src/layoutview/format.rs +++ b/crates/ui-egui/src/layoutview/format.rs @@ -1,6 +1,7 @@ //! The Format pane of the selected layout element; each element's own rows are in its file under `format/`. mod chart; +mod checklist; mod colorbar; mod frame; mod legend; @@ -364,6 +365,7 @@ pub(super) fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: }); text_section(app, ui, layout, li, &e, &update); legend_layers(app, ui, layout, li, &e, &update); + table_columns(app, ui, layout, &e, &update); frame_sections(app, ui, layout, li, &e, &update); // Its background (#2257): a box behind its content, which a map frame, drawing its own paper, // doesn't take. diff --git a/crates/ui-egui/src/layoutview/format/checklist.rs b/crates/ui-egui/src/layoutview/format/checklist.rs new file mode 100644 index 0000000000..bc2b632273 --- /dev/null +++ b/crates/ui-egui/src/layoutview/format/checklist.rs @@ -0,0 +1,240 @@ +//! A section of named rows to tick and to move up or down, below an element's grid: a legend's +//! layers and a table's fields (#3409). + +use egui::{Ui, vec2}; + +/// Over this many rows, the section has a box that filters them by name. +const FILTER_OVER: usize = 8; + +/// The height its rows scroll in, in points. +const LIST_HEIGHT: f32 = 320.0; + +/// A row of the section. +pub(super) struct Row { + /// What its arrows are named for. + pub(super) name: String, + /// What its focus and its expansion follow as it moves: one per row. + pub(super) key: String, + /// What its checkbox shows. + pub(super) text: String, + /// Another text the filter finds it by, besides its name; empty for none. + pub(super) also: String, + pub(super) ticked: bool, + /// Whether ▲ and ▼ move it, past other movable rows. + pub(super) movable: bool, +} + +/// What the section says and allows. Its texts come translated. +pub(super) struct Spec<'a> { + pub(super) title: &'a str, + /// What its ids come from: one per layout and element. + pub(super) id: egui::Id, + pub(super) open: bool, + pub(super) filter: &'a str, + pub(super) no_match: &'a str, + pub(super) tick: &'a str, + pub(super) select_all: &'a str, + pub(super) select_none: &'a str, + /// Reset's hover, while there is something to put back. + pub(super) reset: Option<&'a str>, + /// One row at least stays ticked. + pub(super) keep_one: bool, +} + +/// What the section was asked for, by the rows' places. +pub(super) enum Pick { + /// A row ticked (true) or unticked. + Tick(usize, bool), + /// Every row shown, ticked or unticked. + Shown(Vec, bool), + /// Two rows to change places, the upper first. + Swap(usize, usize), + Reset, +} + +/// What a row opens below itself. +pub(super) struct Details<'a> { + /// The hovers of its arrow, closed and open. + pub(super) show: &'a str, + pub(super) hide: &'a str, + /// The name of its arrow, open or closed, for the row named. + pub(super) named: &'a dyn Fn(bool, &str) -> String, + /// Draws what the row at this place opens. + pub(super) body: &'a mut dyn FnMut(&mut Ui, usize), +} + +/// `text` as the filter compares it: lower case a character at a time, as `str::to_lowercase` +/// turns a capital sigma into one letter or another by its place, and the final sigma as the other. +fn folded(text: &str) -> String { + text.chars().flat_map(char::to_lowercase).map(|c| if c == 'ς' { 'σ' } else { c }).collect() +} + +/// The section: collapsible, its filter, Select All and Select None, the rows and Reset. `rows` +/// is called only while it's open; what was asked for comes with the rows it was asked of. +pub(super) fn checklist( + ui: &mut Ui, + spec: &Spec<'_>, + rows: impl FnOnce() -> Vec, + mut details: Option>, +) -> Option<(Pick, Vec)> { + let mut pick = None; + ui.add_space(4.0); + let shown_rows = egui::CollapsingHeader::new(spec.title).id_salt(spec.id).default_open(spec.open).show(ui, |ui| { + let rows = rows(); + // The filter is the view's, not the project's: kept by egui, and read only while its box + // shows, so a list grown short again hides nothing. + let mut needle = String::new(); + if rows.len() > FILTER_OVER { + let key = spec.id.with("filter"); + let mut text = ui.data(|d| d.get_temp::(key)).unwrap_or_default(); + let r = ui.add(egui::TextEdit::singleline(&mut text).hint_text(spec.filter).desired_width(f32::INFINITY)); + // Named once typed in too, when its hint is gone. + r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::TextInput, true, spec.filter)); + needle = folded(text.trim()); + ui.data_mut(|d| d.insert_temp(key, text)); + } + // The places of the rows shown. + let found = |r: &Row| needle.is_empty() || folded(&r.name).contains(&needle) || folded(&r.also).contains(&needle); + let shown: Vec = rows.iter().enumerate().filter(|(_, r)| found(r)).map(|(i, _)| i).collect(); + if !needle.is_empty() && shown.is_empty() { + ui.label(spec.no_match); + } + let ticked = rows.iter().filter(|r| r.ticked).count(); + // Every row shown at once, the matches alone under a filter. + if !shown.is_empty() { + ui.horizontal(|ui| { + let visible = || shown.iter().filter_map(|&i| rows.get(i)); + let some_out = visible().any(|r| !r.ticked); + let ticked_shown = visible().filter(|r| r.ticked).count(); + let may_untick = ticked_shown > 0 && (!spec.keep_one || ticked_shown < ticked); + if ui.add_enabled(some_out, egui::Button::new(tl!("Select All"))).on_hover_text(spec.select_all).clicked() { + pick = Some(Pick::Shown(shown.clone(), true)); + } + if ui.add_enabled(may_untick, egui::Button::new(tl!("Select None"))).on_hover_text(spec.select_none).clicked() { + pick = Some(Pick::Shown(shown.clone(), false)); + } + }); + } + // The places a row swaps with the one shown above or below it. + let mut swap = None; + let movable = |&&j: &&usize| rows.get(j).is_some_and(|r| r.movable); + // A bar beside the rows, not floating over their arrows. + ui.scope(|ui| { + ui.spacing_mut().scroll = egui::style::ScrollStyle::solid(); + egui::ScrollArea::vertical().id_salt(spec.id.with("scroll")).max_height(LIST_HEIGHT).auto_shrink([false, true]).show( + ui, + |ui| { + for (k, (i, row)) in shown.iter().filter_map(|&i| rows.get(i).map(|r| (i, r))).enumerate() { + // A row that doesn't move looks for neither: a table's fields can be many. + let above = row.movable.then(|| shown.iter().take(k).rev().find(movable).copied()).flatten(); + let below = row.movable.then(|| shown.iter().skip(k + 1).find(movable).copied()).flatten(); + // Ids from the row's key, not its place (`push_id` would mix it in), so the + // keyboard's focus and the row's expansion move with it. + let row_id = ui.make_persistent_id(&row.key); + ui.scope_builder(egui::UiBuilder::new().scope_id(row_id), |ui| { + let mut opened = + egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), row_id.with("details"), false); + // The arrows on the right, the name in what they leave. + let size = vec2(ui.available_width(), ui.spacing().interact_size.y); + ui.allocate_ui_with_layout(size, egui::Layout::right_to_left(egui::Align::Center), |ui| { + // Named for its row, so each row's arrows tell apart to a screen reader. + let arrow = |ui: &mut Ui, glyph: &str, on: bool, hover: &str, label: String| { + let r = ui.add_enabled(on, egui::Button::new(glyph).small()); + r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, &label)); + r.on_hover_text(hover).clicked() + }; + let args = [("name", row.name.as_str())]; + if arrow(ui, "▼", below.is_some(), tl!("Move down"), crate::i18n::fmt("Move {name} down", &args)) { + swap = below.map(|j| (i, j)); + } + if arrow(ui, "▲", above.is_some(), tl!("Move up"), crate::i18n::fmt("Move {name} up", &args)) { + swap = above.map(|j| (j, i)); + } + ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| { + ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate); + if let Some(d) = &details { + let open = opened.is_open(); + let (glyph, hover) = if open { ("▾", d.hide) } else { ("▸", d.show) }; + let label = (d.named)(open, &row.name); + let toggle = ui.add(egui::Button::new(glyph).small().frame(false)); + toggle.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &label)); + if toggle.on_hover_text(hover).clicked() { + opened.toggle(ui); + } + } + let mut on = row.ticked; + let free = !(spec.keep_one && row.ticked && ticked == 1); + if ui.add_enabled(free, egui::Checkbox::new(&mut on, &row.text)).on_hover_text(spec.tick).changed() { + pick = Some(Pick::Tick(i, on)); + } + }); + }); + if let Some(d) = &mut details { + if opened.is_open() { + (d.body)(ui, i); + } + opened.store(ui.ctx()); + } + }); + } + }, + ); + }); + if let Some((a, b)) = swap { + pick = Some(Pick::Swap(a, b)); + } + if let Some(hover) = spec.reset + && ui.button(tl!("Reset")).on_hover_text(hover).clicked() + { + pick = Some(Pick::Reset); + } + rows + }); + pick.zip(shown_rows.body_returned) +} + +#[cfg(test)] +mod tests { + use super::*; + use egui_kittest::kittest::Queryable; + use std::cell::Cell; + use std::rc::Rc; + + /// A collapsed section builds no rows; opened, it does. + #[test] + fn a_collapsed_section_builds_no_rows() { + let built = Rc::new(Cell::new(0)); + let count = built.clone(); + let mut fonts = false; + let mut h = egui_kittest::Harness::new_ui(move |ui| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + let spec = Spec { + title: "Rows", + id: egui::Id::unique("rows"), + open: false, + filter: "Filter", + no_match: "None", + tick: "Tick", + select_all: "All", + select_none: "None of them", + reset: None, + keep_one: false, + }; + let rows = || { + count.set(count.get() + 1); + vec![Row { name: "a".into(), key: "a".into(), text: "a".into(), also: String::new(), ticked: true, movable: true }] + }; + checklist(ui, &spec, rows, None); + }); + h.run_steps(3); + assert_eq!(built.get(), 0); + h.get_by_label("Rows").click(); + h.run_steps(10); + assert!(built.get() > 0); + assert!(h.query_by_label("a").is_some()); + } +} diff --git a/crates/ui-egui/src/layoutview/format/legend.rs b/crates/ui-egui/src/layoutview/format/legend.rs index c0281636f9..db363fcde9 100644 --- a/crates/ui-egui/src/layoutview/format/legend.rs +++ b/crates/ui-egui/src/layoutview/format/legend.rs @@ -1,5 +1,6 @@ //! A legend's Format rows. +use super::checklist::{Details, Pick, Row, Spec, checklist}; use super::*; use crate::RustGisApp; use crate::widgets::Dragged; @@ -132,22 +133,9 @@ pub(super) fn legend_property_rows(app: &mut RustGisApp, ui: &mut Ui, li: usize, } } -/// Over this many layers, a legend's Layers section has a box that filters them by name. -const FILTER_OVER: usize = 8; - -/// The height its rows scroll in, in points. -const LAYERS_HEIGHT: f32 = 320.0; - /// The share of a label box's row its caption takes. const CAPTION_WIDTH: f32 = 0.4; -/// `text` as the Layers filter compares it: lower case a character at a time, as -/// `str::to_lowercase` turns a capital sigma into one letter or another by its place, and the final -/// sigma as the other. -fn folded(text: &str) -> String { - text.chars().flat_map(char::to_lowercase).map(|c| if c == 'ς' { 'σ' } else { c }).collect() -} - /// A box holding the legend's own text for a layer or one of its classes, `hint` (the text shown /// without one) while it's empty. What it sends is saved as any Format box's is (#3348). fn label_box(app: &mut RustGisApp, ui: &mut Ui, key: &DraftKey, current: &str, hint: &str, name: &str) -> Option { @@ -220,185 +208,106 @@ pub(super) fn legend_layers( let mut texts = Vec::new(); // With none to list, still there to reset the legend's own order or the names left out. if !names.is_empty() || !layer_order.is_empty() || !kept.is_empty() { - ui.add_space(4.0); - let open = !exclude.is_empty() || !layer_order.is_empty(); - // By layout too, as element ids repeat: the section's rows, scroll and open classes - // take their ids from it. - egui::CollapsingHeader::new(tl!("Layers")).id_salt(("legend_layers", &layout.name, id)).default_open(open).show(ui, |ui| { - let left_out = |n: &str| exclude.iter().any(|x| x.eq_ignore_ascii_case(n)); - // The filter is the view's, not the project's: kept by egui, and read only while - // its box shows, so a list grown short again hides nothing. - let mut needle = String::new(); - if names.len() > FILTER_OVER { - // By layout too: element ids repeat from one to another. - let key = egui::Id::unique(("legend_layers_filter", &layout.name, id)); - let mut text = ui.data(|d| d.get_temp::(key)).unwrap_or_default(); - let r = ui.add(egui::TextEdit::singleline(&mut text).hint_text(tl!("Filter layers")).desired_width(f32::INFINITY)); - // Named once typed in too, when its hint is gone. - r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::TextInput, true, tl!("Filter layers"))); - needle = folded(text.trim()); - ui.data_mut(|d| d.insert_temp(key, text)); - } - // The places in `names` of the rows shown. - let found = |n: &str| folded(n).contains(&needle) || folded(&legend_text(&e.kind, n, None)).contains(&needle); - let shown: Vec = names.iter().enumerate().filter(|(_, n)| found(n)).map(|(i, _)| i).collect(); - if !needle.is_empty() && shown.is_empty() { - ui.label(tl!("No layers match your search.")); - } - // Every layer shown at once, the matches alone under a filter. - let rows: Vec<&String> = shown.iter().filter_map(|&i| names.get(i)).collect(); - if !rows.is_empty() { + let left_out = |n: &str| exclude.iter().any(|x| x.eq_ignore_ascii_case(n)); + let rows = || { + names + .iter() + .map(|n| { + // By the legend's own text for it, still naming its layer: to a screen reader, + // the keyboard and among equal texts. + let own = legend_text(&e.kind, n, None); + let text = if own.is_empty() || own == *n { n.clone() } else { format!("{own} ({n})") }; + Row { name: n.clone(), key: n.to_ascii_lowercase(), text, also: own, ticked: !left_out(n), movable: true } + }) + .collect() + }; + let reset = !layer_order.is_empty() || !kept.is_empty(); + let spec = Spec { + title: tl!("Layers"), + // By layout too, as element ids repeat: the section's filter, rows, scroll and + // open classes take their ids from it. + id: egui::Id::unique(("legend_layers", &layout.name, id)), + open: !exclude.is_empty() || !layer_order.is_empty(), + filter: tl!("Filter layers"), + no_match: tl!("No layers match your search."), + tick: tl!("List this layer in the legend"), + select_all: tl!("List every layer shown"), + select_none: tl!("Leave out every layer shown"), + // The legend as it started: its texts for layers and classes stay. + reset: reset.then(|| tl!("List every layer again in the map's order")), + keep_one: false, + }; + let mut body = |ui: &mut Ui, i: usize| { + let Some(n) = names.get(i) else { return }; + let args = [("name", n.as_str())]; + let layer: Arc = n.as_str().into(); + // The classes of the map's layers of this name (a web layer has none), read before + // a box changes the app. + let classes: Vec = frame_map + .and_then(|name| app.session.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(name))) + .into_iter() + .flat_map(|m| &m.layers) + .filter(|l| l.name.eq_ignore_ascii_case(n)) + .flat_map(class_row_labels) + .collect(); + let mut entry = |ui: &mut Ui, class: Option<&str>, hint: &str, name: String| { + let field = Field::Label { layer: layer.clone(), class: class.map(Arc::from) }; + let current = legend_text(&e.kind, n, class); + drawn.insert(field.clone()); + let key = (li, id, field); ui.horizontal(|ui| { - let some_out = rows.iter().any(|n| left_out(n)); - let some_listed = rows.iter().any(|n| !left_out(n)); - let others = || kept.iter().filter(|x| !rows.iter().any(|n| x.eq_ignore_ascii_case(n))); - if ui - .add_enabled(some_out, egui::Button::new(tl!("Select All"))) - .on_hover_text(tl!("List every layer shown")) - .clicked() - { - change = Some(json!({"exclude": others().collect::>()})); - } - if ui - .add_enabled(some_listed, egui::Button::new(tl!("Select None"))) - .on_hover_text(tl!("Leave out every layer shown")) - .clicked() - { - change = Some(json!({"exclude": others().chain(rows.iter().copied()).collect::>()})); + // Its caption, in a column of one width: a class's always, as a box + // holding any text shows no hint. + let column = vec2(ui.available_width() * CAPTION_WIDTH, ui.spacing().interact_size.y); + ui.allocate_ui_with_layout(column, egui::Layout::left_to_right(egui::Align::Center), |ui| { + ui.set_min_width(column.x); + ui.add(egui::Label::new(class.unwrap_or(tl!("Label"))).truncate()); + }); + if let Some(text) = label_box(app, ui, &key, ¤t, hint, &name) { + texts.push(field_change(&key.2, text)); } }); - } - // The places a row swaps with the one shown above or below it. - let mut swap = None; - // A bar beside the rows, not floating over their arrows. - ui.scope(|ui| { - ui.spacing_mut().scroll = egui::style::ScrollStyle::solid(); - egui::ScrollArea::vertical() - .id_salt(("legend_layers_scroll", id)) - .max_height(LAYERS_HEIGHT) - .auto_shrink([false, true]) - .show(ui, |ui| { - for (k, (i, n)) in shown.iter().filter_map(|&i| names.get(i).map(|n| (i, n))).enumerate() { - let above = k.checked_sub(1).and_then(|k| shown.get(k)).copied(); - let below = shown.get(k + 1).copied(); - // Ids from the layer, not the row's place (`push_id` would mix it in), so - // the keyboard's focus and the row's expansion move with its layer. - let row_id = ui.make_persistent_id(n.to_ascii_lowercase()); - ui.scope_builder(egui::UiBuilder::new().scope_id(row_id), |ui| { - let mut labels = egui::collapsing_header::CollapsingState::load_with_default_open( - ui.ctx(), - row_id.with("labels"), - false, - ); - // The arrows on the right, the name in what they leave. - let row = vec2(ui.available_width(), ui.spacing().interact_size.y); - ui.allocate_ui_with_layout(row, egui::Layout::right_to_left(egui::Align::Center), |ui| { - // Named for its layer, so each row's arrows tell apart to a screen reader. - let arrow = |ui: &mut Ui, glyph: &str, on: bool, hover: &str, label: String| { - let r = ui.add_enabled(on, egui::Button::new(glyph).small()); - r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, &label)); - r.on_hover_text(hover).clicked() - }; - let args = [("name", n.as_str())]; - if arrow(ui, "▼", below.is_some(), tl!("Move down"), crate::i18n::fmt("Move {name} down", &args)) - { - swap = below.map(|j| (i, j)); - } - if arrow(ui, "▲", above.is_some(), tl!("Move up"), crate::i18n::fmt("Move {name} up", &args)) { - swap = above.map(|j| (j, i)); - } - ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| { - ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate); - let open = labels.is_open(); - let (glyph, hover, label) = if open { - ("▾", tl!("Hide labels"), crate::i18n::fmt("Hide the labels of {name}", &args)) - } else { - ("▸", tl!("Show labels"), crate::i18n::fmt("Show the labels of {name}", &args)) - }; - let toggle = ui.add(egui::Button::new(glyph).small().frame(false)); - toggle.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &label)); - if toggle.on_hover_text(hover).clicked() { - labels.toggle(ui); - } - let mut listed = !left_out(n); - // By the legend's own text for it, still naming its layer: - // to a screen reader, the keyboard and among equal texts. - let own = legend_text(&e.kind, n, None); - let text = if own.is_empty() || own == *n { n.clone() } else { format!("{own} ({n})") }; - if ui.checkbox(&mut listed, text).on_hover_text(tl!("List this layer in the legend")).changed() - { - let mut next: Vec<&String> = kept.iter().filter(|x| !x.eq_ignore_ascii_case(n)).collect(); - if !listed { - next.push(n); - } - change = Some(json!({"exclude": next})); - } - }); - }); - if labels.is_open() { - let args = [("name", n.as_str())]; - let layer: Arc = n.as_str().into(); - // The classes of the map's layers of this name (a web layer - // has none), read before a box changes the app. - let classes: Vec = frame_map - .and_then(|name| app.session.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(name))) - .into_iter() - .flat_map(|m| &m.layers) - .filter(|l| l.name.eq_ignore_ascii_case(n)) - .flat_map(class_row_labels) - .collect(); - let mut entry = |ui: &mut Ui, class: Option<&str>, hint: &str, name: String| { - let field = Field::Label { layer: layer.clone(), class: class.map(Arc::from) }; - let current = legend_text(&e.kind, n, class); - drawn.insert(field.clone()); - let key = (li, id, field); - ui.horizontal(|ui| { - // Its caption, in a column of one width: a class's - // always, as a box holding any text shows no hint. - let column = vec2(ui.available_width() * CAPTION_WIDTH, ui.spacing().interact_size.y); - ui.allocate_ui_with_layout( - column, - egui::Layout::left_to_right(egui::Align::Center), - |ui| { - ui.set_min_width(column.x); - ui.add(egui::Label::new(class.unwrap_or(tl!("Label"))).truncate()); - }, - ); - if let Some(text) = label_box(app, ui, &key, ¤t, hint, &name) { - texts.push(field_change(&key.2, text)); - } - }); - }; - ui.indent("labels", |ui| { - entry(ui, None, n, crate::i18n::fmt("Label of {name}", &args)); - // One box a label: classes that share one share its text. - let mut seen = HashSet::new(); - for class in classes.iter().filter(|c| seen.insert(c.as_str())) { - let args = [("class", class.as_str()), ("name", n.as_str())]; - ui.push_id(class, |ui| { - entry(ui, Some(class), class, crate::i18n::fmt("Label of {class} in {name}", &args)); - }); - } - }); - } - labels.store(ui.ctx()); - }); - } + }; + ui.indent("labels", |ui| { + entry(ui, None, n, crate::i18n::fmt("Label of {name}", &args)); + // One box a label: classes that share one share its text. + let mut seen = HashSet::new(); + for class in classes.iter().filter(|c| seen.insert(c.as_str())) { + let args = [("class", class.as_str()), ("name", n.as_str())]; + ui.push_id(class, |ui| { + entry(ui, Some(class), class, crate::i18n::fmt("Label of {class} in {name}", &args)); }); + } }); - if let Some((a, b)) = swap { + }; + let named = |open: bool, name: &str| { + let args = [("name", name)]; + if open { + crate::i18n::fmt("Hide the labels of {name}", &args) + } else { + crate::i18n::fmt("Show the labels of {name}", &args) + } + }; + let details = Details { show: tl!("Show labels"), hide: tl!("Hide labels"), named: &named, body: &mut body }; + // The names left out once `these` are listed or not, the rest as they are. + let exclude_with = |these: &[&String], listed: bool| { + let mut next: Vec<&String> = kept.iter().filter(|x| !these.iter().any(|n| x.eq_ignore_ascii_case(n))).collect(); + if !listed { + next.extend(these); + } + json!({"exclude": next}) + }; + change = checklist(ui, &spec, rows, Some(details)).map(|(pick, _)| match pick { + Pick::Tick(i, on) => exclude_with(&names.get(i).into_iter().collect::>(), on), + Pick::Shown(shown, on) => exclude_with(&shown.iter().filter_map(|&i| names.get(i)).collect::>(), on), + Pick::Swap(a, b) => { let mut next: Vec<&String> = names.iter().collect(); next.swap(a, b); // Those ordered but not listed stay ordered, after these. next.extend(&unlisted); - change = Some(json!({"layerOrder": next})); - } - // The legend as it started: its texts for layers and classes stay. - if (!layer_order.is_empty() || !kept.is_empty()) - && ui.button(tl!("Reset")).on_hover_text(tl!("List every layer again in the map's order")).clicked() - { - change = Some(json!({"exclude": [], "layerOrder": []})); + json!({"layerOrder": next}) } + Pick::Reset => json!({"exclude": [], "layerOrder": []}), }); } for change in texts.into_iter().chain(change) { diff --git a/crates/ui-egui/src/layoutview/format/table.rs b/crates/ui-egui/src/layoutview/format/table.rs index 901dec534d..4a06b834c7 100644 --- a/crates/ui-egui/src/layoutview/format/table.rs +++ b/crates/ui-egui/src/layoutview/format/table.rs @@ -1,11 +1,13 @@ -//! A table's Format rows. +//! A table's Format rows and its Columns section. +use super::checklist::{Pick, Row, Spec, checklist}; use super::*; use crate::RustGisApp; use crate::widgets::Dragged; use egui::Ui; -use rustgis_engine::map::layout::{Element, ElementKind}; +use rustgis_engine::map::layout::{Element, ElementKind, Layout}; use serde_json::{Value, json}; +use std::collections::HashSet; /// A table's rows, text size and header in the grid. pub(super) fn table_look(app: &mut RustGisApp, ui: &mut Ui, li: usize, e: &Element, update: &dyn Fn(&mut RustGisApp, Value)) { @@ -31,3 +33,75 @@ pub(super) fn table_look(app: &mut RustGisApp, ui: &mut Ui, li: usize, e: &Eleme } ui.end_row(); } + +/// A table's columns, below the grid: every field of its layer, those it shows ticked and first, +/// in the order drawn (#3409). +pub(super) fn table_columns(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, e: &Element, update: &dyn Fn(&mut RustGisApp, Value)) { + let ElementKind::Table { frame, layer, fields, .. } = &e.kind else { return }; + let map = match layout.element(*frame).map(|f| &f.kind) { + Some(ElementKind::MapFrame { map, .. }) => app.session.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)), + _ => None, + }; + let Some(ds) = map.and_then(|m| m.layer_by_name(layer)).map(|l| &l.dataset) else { return }; + let all = || ds.fields.iter().map(|f| f.name.as_str()); + let rows = || { + let row = |name: &str, ticked: bool| Row { + name: name.into(), + key: name.into(), + text: name.into(), + also: String::new(), + ticked, + movable: ticked, + }; + // Without a choice, every field in the layer's order. A chosen one keeps the name the + // table stores, as it is drawn: one the layer has lost stays listed. + if fields.is_empty() { + return all().map(|n| row(n, true)).collect(); + } + // The names listed, in a set as a layer's fields can be many. + let mut taken = HashSet::new(); + let mut rows: Vec = fields.iter().map(String::as_str).filter(|n| taken.insert(*n)).map(|n| row(n, true)).collect(); + rows.extend(all().filter(|n| !taken.contains(n)).map(|n| row(n, false))); + rows + }; + let spec = Spec { + title: tl!("Columns"), + // By layout too, as element ids repeat. + id: egui::Id::unique(("table_columns", &layout.name, e.id)), + open: !fields.is_empty(), + filter: tl!("Filter fields"), + no_match: tl!("No fields match your search."), + tick: tl!("Show this field in the table"), + select_all: tl!("Show every field listed"), + select_none: tl!("Leave out every field listed"), + reset: (!fields.is_empty()).then(|| tl!("Show every field again in the layer's order")), + // An empty list would show every field. + keep_one: true, + }; + let Some((pick, rows)) = checklist(ui, &spec, rows, None) else { return }; + let mut next: Vec<&str> = rows.iter().filter(|r| r.ticked).map(|r| r.name.as_str()).collect(); + // The rows named `these` ticked (added after the others) or unticked. + let mut tick = |these: &[usize], on: bool| { + for r in these.iter().filter_map(|&i| rows.get(i)) { + if !on { + next.retain(|n| *n != r.name); + } else if !r.ticked { + next.push(&r.name); + } + } + }; + match pick { + Pick::Tick(i, on) => tick(&[i], on), + Pick::Shown(shown, on) => tick(&shown, on), + // Ticked rows come first, in their order, so their places are the list's. + Pick::Swap(a, b) if a < next.len() && b < next.len() => next.swap(a, b), + Pick::Swap(..) => return, + Pick::Reset => next.clear(), + } + // Every field in the layer's order is no choice: the table saves none. + if next.iter().copied().eq(all()) { + next.clear(); + } + let change = json!({"fields": next}); + update(app, change); +} diff --git a/crates/ui-egui/src/layoutview/table_tests.rs b/crates/ui-egui/src/layoutview/table_tests.rs new file mode 100644 index 0000000000..b1c427bf93 --- /dev/null +++ b/crates/ui-egui/src/layoutview/table_tests.rs @@ -0,0 +1,234 @@ +//! Tests of a table's Columns section in the Format pane (#3409). + +use crate::RustGisApp; +use egui::accesskit::Role; +use egui::{Key, Modifiers, vec2}; +use egui_kittest::kittest::{NodeT, Queryable}; +use rustgis_engine::map::layout::ElementKind; +use serde_json::{Value, json}; + +type Harness = egui_kittest::Harness<'static, RustGisApp>; + +/// The Format pane of a new table of Countries, its Columns section open: the harness, the +/// layout's place, the table's id and the layer's fields. `extra` fields are added to the layer +/// first, and `params` set on the table. +fn columns(extra: u32, params: Value) -> (Harness, usize, u32, Vec) { + let mut app = RustGisApp::with_sample(); + for i in 0..extra { + app.run("edit.addField", json!({"layer": "Countries", "name": format!("extra{i:02}"), "type": "text"})).unwrap(); + } + app.run("layout.new", json!({"name": "Table"})).unwrap(); + let li = app.session.active_layout.unwrap(); + let added = app.run("layout.addElement", json!({"layout": li, "kind": "table", "layer": "Countries"})).unwrap(); + let id = added["element"].as_u64().unwrap() as u32; + let mut p = params; + p["layout"] = json!(li); + p["element"] = json!(id); + app.run("layout.update", p).unwrap(); + let fields = app.session.map().layer_by_name("Countries").unwrap().dataset.fields.iter().map(|f| f.name.clone()).collect(); + app.layout.selected = Some(id); + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(vec2(420.0, 1600.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + let layout = app.session.project.layouts[li].clone(); + egui::ScrollArea::vertical().show(ui, |ui| super::format::properties(app, ui, &layout, li)); + }, + app, + ); + h.run_steps(3); + // A table with chosen fields opens it already. + if h.query_by_label("Select All").is_none() { + h.get_by_label("Columns").click(); + h.run_steps(10); + } + (h, li, id, fields) +} + +/// The fields the table keeps. +fn kept(h: &Harness, li: usize, id: u32) -> Vec { + match &h.state().session.project.layouts[li].element(id).unwrap().kind { + ElementKind::Table { fields, .. } => fields.clone(), + k => panic!("{k:?}"), + } +} + +/// Whether the box of field `name` is listed, and ticked. +fn ticked(h: &Harness, name: &str) -> Option { + let node = h.query_by_role_and_label(Role::CheckBox, name)?; + Some(node.accesskit_node().toggled() == Some(egui::accesskit::Toggled::True)) +} + +fn disabled(h: &Harness, label: &str) -> bool { + h.get_by_label(label).accesskit_node().is_disabled() +} + +fn box_disabled(h: &Harness, name: &str) -> bool { + h.get_by_role_and_label(Role::CheckBox, name).accesskit_node().is_disabled() +} + +/// Clicks the button `label`: one undo step. +fn press(h: &mut Harness, label: &str) { + let depth = h.state().session.undo_depth(); + h.get_by_label(label).click(); + h.run_steps(3); + assert_eq!(h.state().session.undo_depth(), depth + 1, "{label}: one undo step"); +} + +/// Ticks or unticks the field `name`: one undo step. +fn tick(h: &mut Harness, name: &str) { + let depth = h.state().session.undo_depth(); + h.get_by_role_and_label(Role::CheckBox, name).click(); + h.run_steps(3); + assert_eq!(h.state().session.undo_depth(), depth + 1, "{name}: one undo step"); +} + +/// The filter box, if the section shows one. +fn filter<'a>(h: &'a egui_kittest::Harness<'_, RustGisApp>) -> Option> { + h.query_by_role_and_label(Role::TextInput, "Filter fields") +} + +fn type_filter(h: &mut Harness, text: &str) { + filter(h).unwrap().focus(); + h.run_steps(2); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.key_press(Key::Backspace); + h.run_steps(2); + filter(h).unwrap().type_text(text); + h.run_steps(3); +} + +#[test] +fn a_table_without_fields_lists_every_one_ticked() { + let (h, li, id, fields) = columns(0, json!({})); + assert!(fields.len() > 1); + for f in &fields { + assert_eq!(ticked(&h, f), Some(true), "{f}"); + } + let top = |f: &str| h.get_by_role_and_label(Role::CheckBox, f).rect().top(); + assert!(fields.windows(2).all(|w| top(&w[0]) < top(&w[1])), "in the layer's order"); + assert!(h.query_by_label("Reset").is_none()); + assert_eq!(filter(&h).is_some(), fields.len() > 8); + assert!(disabled(&h, "Select All") && disabled(&h, "Select None"), "none can be left out at once"); + assert!(kept(&h, li, id).is_empty()); +} + +#[test] +fn unticking_keeps_the_others_and_ticking_back_gives_every_field() { + let (mut h, li, id, fields) = columns(0, json!({})); + tick(&mut h, &fields[0]); + assert_eq!(kept(&h, li, id), fields[1..]); + h.state_mut().run("edit.undo", json!({})).unwrap(); + h.run_steps(3); + assert!(kept(&h, li, id).is_empty()); + // Ticked back at the end, the order isn't the layer's: the list stays. + tick(&mut h, &fields[0]); + tick(&mut h, &fields[0]); + let mut moved = fields[1..].to_vec(); + moved.push(fields[0].clone()); + assert_eq!(kept(&h, li, id), moved); + // The last one unticked and ticked again ends in the layer's order. + h.state_mut().run("layout.update", json!({"layout": li, "element": id, "fields": fields[..fields.len() - 1]})).unwrap(); + h.run_steps(3); + tick(&mut h, fields.last().unwrap()); + assert!(kept(&h, li, id).is_empty()); +} + +#[test] +fn ticked_fields_move_and_unticked_ones_do_not() { + let (_, _, _, all) = columns(0, json!({})); + let [a, b, c] = [&all[0], &all[1], &all[2]]; + let (mut h, li, id, _) = columns(0, json!({"fields": [b, a]})); + assert_eq!(ticked(&h, c), Some(false)); + assert!(disabled(&h, &format!("Move {b} up")), "the first ticked"); + assert!(disabled(&h, &format!("Move {a} down")), "the last ticked"); + assert!(disabled(&h, &format!("Move {c} up")) && disabled(&h, &format!("Move {c} down"))); + press(&mut h, &format!("Move {a} up")); + assert_eq!(kept(&h, li, id), [a.clone(), b.clone()]); + press(&mut h, &format!("Move {a} down")); + assert_eq!(kept(&h, li, id), [b.clone(), a.clone()]); + press(&mut h, "Reset"); + assert!(kept(&h, li, id).is_empty()); + assert!(h.query_by_label("Reset").is_none()); +} + +#[test] +fn the_last_ticked_field_stays_ticked() { + let (_, _, _, all) = columns(0, json!({})); + let (h, ..) = columns(0, json!({"fields": [all[1]]})); + assert!(box_disabled(&h, &all[1])); + assert!(!box_disabled(&h, &all[0])); + assert!(disabled(&h, "Select None")); + assert!(!disabled(&h, "Select All")); +} + +/// Over eight fields the section filters them; the buttons and arrows act on those shown. +#[test] +fn fields_are_filtered_by_name() { + let (_, _, _, all) = columns(9, json!({})); + let (mut h, li, id, _) = columns(9, json!({"fields": ["extra01", all[0], "extra00"]})); + type_filter(&mut h, "no such field"); + assert!(h.query_by_label("No fields match your search.").is_some()); + assert!(h.query_by_label("Select All").is_none() && h.query_by_label("Select None").is_none()); + type_filter(&mut h, " EXTRA0 "); + assert_eq!(ticked(&h, &all[0]), None); + // Over the hidden field between them. + press(&mut h, "Move extra00 up"); + assert_eq!(kept(&h, li, id), ["extra00".to_string(), all[0].clone(), "extra01".to_string()]); + press(&mut h, "Select None"); + assert_eq!(kept(&h, li, id), [all[0].clone()]); + press(&mut h, "Select All"); + let mut expected = vec![all[0].clone()]; + expected.extend((0..9).map(|i| format!("extra{i:02}"))); + assert_eq!(kept(&h, li, id), expected); +} + +/// A field the layer has lost stays listed and ticked, others change around it, and unticking +/// removes it. +#[test] +fn a_field_the_layer_lost_is_listed_ticked() { + let (_, _, _, all) = columns(1, json!({})); + let (mut h, li, id, _) = columns(1, json!({"fields": ["extra00", all[0]]})); + h.state_mut().run("edit.deleteField", json!({"layer": "Countries", "field": "extra00"})).unwrap(); + h.run_steps(3); + assert_eq!(ticked(&h, "extra00"), Some(true)); + tick(&mut h, &all[1]); + assert_eq!(kept(&h, li, id), ["extra00".to_string(), all[0].clone(), all[1].clone()]); + press(&mut h, "Move extra00 down"); + assert_eq!(kept(&h, li, id), [all[0].clone(), "extra00".to_string(), all[1].clone()]); + tick(&mut h, "extra00"); + assert_eq!(kept(&h, li, id), [all[0].clone(), all[1].clone()]); + assert_eq!(ticked(&h, "extra00"), None); +} + +/// A field added under another case has its own row; the lost one keeps its name and its place. +#[test] +fn a_lost_field_is_not_listed_as_one_of_another_case() { + let (_, _, _, all) = columns(1, json!({})); + let (mut h, li, id, _) = columns(1, json!({"fields": ["extra00", all[0]]})); + h.state_mut().run("edit.deleteField", json!({"layer": "Countries", "field": "extra00"})).unwrap(); + h.state_mut().run("edit.addField", json!({"layer": "Countries", "name": "EXTRA00", "type": "text"})).unwrap(); + h.run_steps(3); + assert_eq!(ticked(&h, "extra00"), Some(true)); + assert_eq!(ticked(&h, "EXTRA00"), Some(false)); + press(&mut h, "Move extra00 down"); + assert_eq!(kept(&h, li, id), [all[0].clone(), "extra00".to_string()]); +} + +#[test] +fn another_layer_lists_its_own_fields_and_a_lost_one_shows_no_section() { + let (mut h, li, id, countries) = columns(0, json!({})); + h.state_mut().run("layout.update", json!({"layout": li, "element": id, "layer": "Cities"})).unwrap(); + h.run_steps(3); + let cities: Vec = + h.state().session.map().layer_by_name("Cities").unwrap().dataset.fields.iter().map(|f| f.name.clone()).collect(); + assert!(cities.iter().all(|f| ticked(&h, f) == Some(true))); + assert!(countries.iter().filter(|f| !cities.contains(f)).all(|f| ticked(&h, f).is_none())); + h.state_mut().session.map_mut().layers.retain(|l| l.name != "Cities"); + h.run_steps(3); + assert!(h.query_by_label("Columns").is_none() && h.query_by_label("Select All").is_none()); +} diff --git a/crates/ui-egui/src/layoutview/tests.rs b/crates/ui-egui/src/layoutview/tests.rs index 9c36dbbd93..3c82c2abb1 100644 --- a/crates/ui-egui/src/layoutview/tests.rs +++ b/crates/ui-egui/src/layoutview/tests.rs @@ -3046,6 +3046,10 @@ fn a_legends_layer_filter_finds_its_own_text() { assert!(h.query_by_role_and_label(Role::CheckBox, "Waterways (Rivers)").is_some(), "{typed}"); assert!(h.query_by_role_and_label(Role::CheckBox, "Cities").is_none(), "{typed}"); } + // Its own text or its name, not the two as the row shows them. + type_layer_filter(&mut h, "(riv"); + assert!(h.query_by_label("No layers match your search.").is_some()); + type_layer_filter(&mut h, "river"); // The layer keeps its name for every legend and command, so its box sets `labels` under it. h.get_by_label("Show the labels of Rivers").click(); h.run_steps(3); diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index bfee8bbde1..7c7376ff5d 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -865,12 +865,18 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu `layout.addElement` takes it too. - **Table frame**: a layer's attribute table. Its properties set the **Rows** (1 to 500), the **Text size** (4 to 100 points, 8 unless set; rows and columns follow it) and the **Header**, the - row of field names. It shows the layer's fields in their order, as many as its box holds; - `"fields"` picks which and their order, from a command: + row of field names. It shows the layer's fields in their order, as many as its box holds. The + **Columns** section below picks which and their order: tick or untick each field, and move a + ticked one with ▲ and ▼, each one undo step. Ticked fields are listed first, as drawn, and a + field ticked goes after them. **Select All** and **Select None** tick or untick every field + shown, one at least staying ticked, and **Reset** shows every field in the layer's order again. + Over eight fields **Filter fields** keeps those whose name contains the text, in any case; the + buttons and arrows then act on those. From a command, `"fields"` does the same: `layout.update={"element":6,"fields":["NAME","POP"],"textSize":10,"header":false}`. `"fields":[]` shows every field again, and another layer without fields of its own does too. A field removed or renamed afterwards keeps its column, headed by that name over a dash in every row, until the - layer has a field of exactly that name again, which the column then shows. When its box doesn't + layer has a field of exactly that name again, which the column then shows; a new list of + `"fields"` may still name it, spelled so. When its box doesn't hold the rows and columns it is asked to show, adding or changing the table says how many it shows, and so does an export or a report. - **Chart frame**: a bar chart of a layer of the frame's map. Its properties pick the **Layer**