diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index c0bedff106..532f649453 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -1290,11 +1290,14 @@ const UNIQUE_KEEP_ORDER: &str = "unique-keep-order"; /// Whether saved JSON holds a Unique Values renderer that keeps its class order, wherever it is /// (a style's, a layer's, a map's or a project's), in JSON text a string carries too (a saved -/// step's inline project). +/// step's inline project). A saved step in it (a task's, a model's or the history's: its command +/// and `params`) is read as the command reads it, so its base64 `data` counts too. fn holds_kept_class_order(v: &Value) -> bool { match v { Value::Object(o) => { + let step = ["command", "tool", "id"].iter().find_map(|k| o.get(*k).and_then(Value::as_str)).zip(o.get("params")); o.get("Unique").and_then(|u| u.get("keep_order")).and_then(Value::as_bool).unwrap_or(false) + || step.is_some_and(|(id, params)| command_adds_kept_class_order(id, params)) || o.values().any(holds_kept_class_order) } Value::Array(a) => a.iter().any(holds_kept_class_order), @@ -2128,6 +2131,20 @@ mod tests { let mut outer: Value = serde_json::from_str(&sorted).unwrap(); outer["tasks"] = json!([{"name": "Open", "steps": [{"title": "Open", "command": "project.openText", "params": {"text": text}}]}]); assert_eq!(needs("project.openText", &outer.to_string()), ["unique-keep-order"]); + // So does one whose own saved step adds a style from base64 `data` (#3385 review). + let style_data = { + use base64::Engine; + base64::engine::general_purpose::STANDARD.encode(kept_order_style()) + }; + let step = |params: Value| json!([{"name": "Add", "steps": [{"title": "Add", "command": "style.add", "params": params}]}]); + outer["tasks"] = step(json!({"data": style_data})); + assert_eq!(needs("project.openText", &outer.to_string()), ["unique-keep-order"]); + let sorted_data = { + use base64::Engine; + base64::engine::general_purpose::STANDARD.encode(kept_order_style().replace("\"keep_order\":true", "\"keep_order\":false")) + }; + outer["tasks"] = step(json!({"data": sorted_data})); + assert!(needs("project.openText", &outer.to_string()).is_empty()); } /// `report.existingTemplate` imports a layout file as `layout.import` does, so a step of it diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index cd80a89420..35e11e4f76 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -5580,6 +5580,14 @@ fn legend_list(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { // the values as shown, each once, are what a move sends. let kept = matches!(&l.renderer, Renderer::Unique { keep_order: true, .. }); let listed = listed_values(&l.renderer); + // The values the rows drawn show, each once: a value listed twice takes two rows. + let drawn_values = match &l.renderer { + Renderer::Unique { classes, .. } => { + let shown = order.iter().take(MAX_EXPRESSION_CLASSES).filter_map(|&i| classes.get(i)).map(|(k, _)| k.as_str()); + shown.collect::>().len() + } + _ => 0, + }; // As many classes as can be drawn from an expression or a QGIS rule style // (MAX_EXPRESSION_CLASSES), then Unique Values' ``, the legend's // last row, whatever their number. A categorized style import can bring more: how @@ -5643,8 +5651,9 @@ fn legend_list(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { if ui.add_enabled(at > 0, egui::Button::new("▲").small()).on_hover_text(tl!("Move up")).clicked() { moved = Some(at - 1); } + // Not past the last class drawn, which would move out of sight. if ui - .add_enabled(at + 1 < listed.len(), egui::Button::new("▼").small()) + .add_enabled(at + 1 < drawn_values, egui::Button::new("▼").small()) .on_hover_text(tl!("Move down")) .clicked() { @@ -5739,8 +5748,8 @@ fn legend_list(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, t: &Tokens) { ui.label(egui::RichText::new(note).size(11.5).color(t.text_dim)); } let label_key = ticks_key.with("label"); - // A label typed for other ticks doesn't name these. - if ticked != ticks { + // A label typed for other ticks doesn't name these; the same ones moved in the list do. + if ticked.len() != ticks.len() || ticked.iter().any(|v| !ticks.contains(v)) { ui.data_mut(|d| d.remove::(label_key)); } if ticked.len() > 1 { @@ -10333,6 +10342,93 @@ pub(crate) mod tests { assert_eq!(labels(h.state()), sorted); } + /// Past the list's limit, the last class drawn can't move down out of sight (#3385 review), + /// with a value listed twice among those drawn too (#3410 review). + #[test] + fn the_last_class_drawn_does_not_move_out_of_the_list() { + use egui_kittest::kittest::{NodeT, Queryable}; + + let distinct: Vec = (0..MAX_EXPRESSION_CLASSES + 5).map(|i| format!("v{i:03}")).collect(); + let mut repeated = distinct.clone(); + repeated.insert(1, distinct[0].clone()); + for values in [distinct, repeated] { + let mut app = RustGisApp::with_sample(); + app.run("layer.symbology", json!({"layer": "Countries", "type": "unique", "field": "continent"})).unwrap(); + let id = layer(&app, "Countries"); + let twice = values.len() - MAX_EXPRESSION_CLASSES - 5; + if let Some(l) = app.session.map_mut().layer_mut(id) + && let Renderer::Unique { classes, keep_order, .. } = &mut l.renderer + { + let symbol = classes[0].1; + *classes = values.iter().map(|v| (v.clone(), symbol)).collect(); + *keep_order = true; + } + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(vec2(600.0, 900.0)).with_max_steps(8).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + let layer = app.session.map().layer(id).unwrap().clone(); + egui::ScrollArea::vertical().show(ui, |ui| legend_list(app, ui, &layer, &Tokens::light())); + }, + app, + ); + h.run_steps(2); + // A value's repeat is shown but not editable: it has no ▼ of its own. + let downs: Vec = h.query_all_by_label("▼").map(|n| n.accesskit_node().is_disabled()).collect(); + let drawn = MAX_EXPRESSION_CLASSES - twice; + assert_eq!(downs.len(), drawn, "one ▼ on each class drawn ({twice} repeated)"); + assert!(!downs[drawn - 2]); + assert!(downs[drawn - 1], "the last class drawn can't go down ({twice} repeated)"); + } + } + + /// A group label typed for the classes ticked stays when one of them moves (#3385 review). + #[test] + fn a_typed_group_label_stays_when_a_ticked_class_moves() { + use egui::accesskit::Role; + use egui_kittest::kittest::{NodeT, Queryable}; + + let mut app = RustGisApp::with_sample(); + app.run("layer.symbology", json!({"layer": "Countries", "type": "unique", "field": "continent"})).unwrap(); + app.run("layer.orderClasses", json!({"layer": "Countries", "values": ["Africa"]})).unwrap(); + let id = layer(&app, "Countries"); + let listed = app.session.map().layer(id).unwrap().renderer.class_entries(); + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(vec2(600.0, 900.0)).with_max_steps(8).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + let layer = app.session.map().layer(id).unwrap().clone(); + legend_list(app, ui, &layer, &Tokens::light()); + }, + app, + ); + h.run_steps(2); + h.get_by_role_and_label(Role::CheckBox, &listed[0].label).click(); + h.get_by_role_and_label(Role::CheckBox, &listed[1].label).click(); + h.run_steps(2); + let group_label = |h: &egui_kittest::Harness<'_, RustGisApp>| { + h.query_all_by_role(Role::TextInput).last().unwrap().accesskit_node().value().unwrap_or_default() + }; + h.query_all_by_role(Role::TextInput).last().unwrap().focus(); + h.run_steps(2); + h.query_all_by_role(Role::TextInput).last().unwrap().type_text("Regions"); + h.run_steps(2); + assert!(group_label(&h).contains("Regions"), "{}", group_label(&h)); + // The first ticked class moves below the second: the same two are ticked. + h.query_all_by_label("▼").next().unwrap().click(); + h.run_steps(2); + assert_eq!(h.state().session.map().layer(id).unwrap().renderer.class_entries()[1].label, listed[0].label); + assert!(group_label(&h).contains("Regions"), "{}", group_label(&h)); + } + /// Layer ids repeat across maps and projects: the ticks made on one layer aren't another's. #[test] fn class_ticks_stay_with_their_map() {