diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index aa1b9ca889..20663f0e3e 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2749,7 +2749,7 @@ fn catalog(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { // A large toolbox (the GeoLibre suite) opens one level at a time: // `GeoLibre › Raster › General`. if in_box.len() > 40 { - toolbox_levels(app, ui, &in_box, 1, &link); + toolbox_levels(app, ui, &in_box, 1, false, &link); } else { for tool in in_box { link(app, ui, tool); @@ -3020,27 +3020,41 @@ fn folder_browser(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } +/// The deepest toolbox level drawn as a group: a toolbox may hold up to 2,000 bytes of ` › `, and +/// every level filters its tools again. Tools below it are listed in their last group. +const MAX_TOOLBOX_LEVELS: usize = 8; + +/// The parts of a tool's toolbox (`Plugins › 1. Grid › Variograms`), trimmed and without empty +/// ones, down to [`MAX_TOOLBOX_LEVELS`]. +fn toolbox_parts(toolbox: &str) -> impl Iterator { + toolbox.split(" › ").map(str::trim).filter(|s| !s.is_empty()).take(MAX_TOOLBOX_LEVELS) +} + /// `tools` grouped by the `depth`-th part of their toolbox (`GeoLibre › Raster › General`), one -/// closed header per group and nested again below it; tools whose toolbox stops here are listed. +/// header per group (open at first when `open`) and nested again below it; tools whose toolbox +/// stops here are listed. `open` is in the headers' identity, so the groups drawn while browsing +/// (closed) and while searching (open) keep their own state: a header only reads `default_open` +/// the first time it is drawn. fn toolbox_levels( app: &mut RustGisApp, ui: &mut Ui, tools: &[&'static rustgis_engine::analysis::ToolSpec], depth: usize, - link: &dyn Fn(&mut RustGisApp, &mut Ui, &rustgis_engine::analysis::ToolSpec), + open: bool, + link: &dyn Fn(&mut RustGisApp, &mut Ui, &'static rustgis_engine::analysis::ToolSpec), ) { - let part = |x: &rustgis_engine::analysis::ToolSpec| x.toolbox.split(" › ").nth(depth); + let part = |x: &rustgis_engine::analysis::ToolSpec| toolbox_parts(x.toolbox).nth(depth); let mut groups: Vec<&str> = tools.iter().filter_map(|x| part(x)).collect(); groups.sort(); groups.dedup(); for g in groups { let inside: Vec<_> = tools.iter().copied().filter(|x| part(x) == Some(g)).collect(); - let path: Vec<&str> = inside.first().map(|x| x.toolbox.split(" › ").take(depth + 1).collect()).unwrap_or_default(); - egui::CollapsingHeader::new(crate::i18n::location(g)).id_salt(("toolbox", path)).default_open(false).show(ui, |ui| { - toolbox_levels(app, ui, &inside, depth + 1, link); + let path: Vec<&str> = inside.first().map(|x| toolbox_parts(x.toolbox).take(depth + 1).collect()).unwrap_or_default(); + egui::CollapsingHeader::new(crate::i18n::location(g)).id_salt(("toolbox", path, open)).default_open(open).show(ui, |ui| { + toolbox_levels(app, ui, &inside, depth + 1, open, link); }); } - for tool in tools.iter().filter(|x| part(x).is_none()) { + for tool in tools.iter().copied().filter(|x| part(x).is_none()) { link(app, ui, tool); } } @@ -4046,24 +4060,28 @@ fn geoprocessing(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) { } } }; - // Plugin tools, by toolbox ("Plugins › Vector Toolkit"): first when browsing (a user - // who just installed a plugin looks for it), with the matches when searching. + // Plugin tools, by toolbox ("Plugins › 1. Grid › Variograms"): first when browsing (a user + // who just installed a plugin looks for it), with the matches when searching. One group + // per toolbox name below "Plugins", nested by the ` › ` in it; the groups below the first + // open when searching, so the matches show. let plugin_tools: Vec<&'static rustgis_engine::analysis::ToolSpec> = rustgis_engine::analysis::external_tools_of(rustgis_engine::plugins::tools::SOURCE).iter().filter(|x| matches(x)).collect(); if !plugin_tools.is_empty() { ui.label(egui::RichText::new(tl!("Plugins")).size(11.0).color(t.text_dim)); - let mut boxes: Vec<&str> = plugin_tools.iter().map(|t| t.toolbox).collect(); - boxes.dedup(); - for b in boxes { - let in_box: Vec<_> = plugin_tools.iter().filter(|x| x.toolbox == b).collect(); - egui::CollapsingHeader::new(format!("{} ({})", b.rsplit(" › ").next().unwrap_or(b), in_box.len())) + let top = |x: &rustgis_engine::analysis::ToolSpec| toolbox_parts(x.toolbox).nth(1); + let mut tops: Vec<&str> = plugin_tools.iter().filter_map(|x| top(x)).collect(); + tops.sort(); + tops.dedup(); + for b in tops { + let in_box: Vec<_> = plugin_tools.iter().copied().filter(|x| top(x) == Some(b)).collect(); + egui::CollapsingHeader::new(format!("{} ({})", crate::i18n::location(b), in_box.len())) .id_salt(("gp-plugin", b)) .default_open(true) - .show(ui, |ui| { - for tool in in_box { - row(app, ui, tool); - } - }); + .show(ui, |ui| toolbox_levels(app, ui, &in_box, 2, !q.is_empty(), &row)); + } + // A toolbox of only separators names no group: its tools sit under "Plugins". + for tool in plugin_tools.iter().copied().filter(|x| top(x).is_none()) { + row(app, ui, tool); } ui.add_space(6.0); } @@ -7995,6 +8013,22 @@ pub(crate) fn layout_menu(app: &mut RustGisApp, ui: &mut Ui, n: &str) { pub(crate) mod tests { use super::*; + /// A toolbox's parts skip the empty ones and stop at the deepest level drawn, so a hostile + /// manifest's `toolbox` neither adds blank groups nor nests a thousand deep (#3022). + #[test] + fn toolbox_parts_skip_empty_ones_and_stop_at_the_deepest_level() { + fn parts(t: &str) -> Vec<&str> { + toolbox_parts(t).collect() + } + assert_eq!(parts("Plugins › 1. Grid › Variograms"), ["Plugins", "1. Grid", "Variograms"]); + assert_eq!(parts("Plugins › › Grid › › Examples › "), ["Plugins", "Grid", "Examples"]); + assert_eq!(parts("Plugins › A › B"), parts("Plugins › A › B "), "a part's own spaces are not part of its name"); + assert!(parts("").is_empty() && parts(" › › ").is_empty()); + let deep = vec!["a"; 300].join(" › "); + assert!(deep.len() < 2_000); + assert_eq!(toolbox_parts(&deep).count(), MAX_TOOLBOX_LEVELS); + } + /// A layer linked to its file says so after its name, with the file and what linking means /// (#2350); a copy, and the same layer once Load All Features copies it, don't. Another note /// (a change on disk) goes with it. diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs index acfa3044cf..f713a60862 100644 --- a/crates/ui-egui/tests/smoke.rs +++ b/crates/ui-egui/tests/smoke.rs @@ -2209,6 +2209,84 @@ fn plugins_can_have_a_tab_of_their_own() { rp::unload("ui-tab"); } +/// A plugin's toolbox path (`A. Grid › Variograms`) nests in the Tools pane, whatever the order of +/// its tools: one group per path, not one per run of neighbours (#3022). +#[test] +fn plugin_tools_nest_by_their_toolbox_path() { + let _turn = PLUGINS.lock().unwrap_or_else(|e| e.into_inner()); + use egui::accesskit::Role; + use rustgis_ui_egui::rustgis_engine::plugins as rp; + let mut m: serde_json::Value = serde_json::from_str(&rp::testing::manifest("ui-nest", &[])).unwrap(); + let tool = |id: &str, name: &str, toolbox: &str| json!({"id": id, "name": name, "toolbox": toolbox, "params": []}); + // Tools of one toolbox are not next to each other; one has none, one only separators. + m["tools"] = json!([ + tool("v1", "Nest Variogram One", "A. Grid › Variograms"), + tool("c1", "Nest Contours", "B. Topography"), + tool("e1", "Nest Example", "A. Grid › Examples"), + tool("v2", "Nest Variogram Two", "A. Grid › Variograms"), + tool("n1", "Nest Unplaced", ""), + tool("s1", "Nest Loose", " › › "), + ]); + let pkg = rp::testing::zip_of(&[("plugin.json", m.to_string().as_bytes()), ("plugin.wasm", &rp::testing::wasm())]); + rp::load(rp::Package::from_zip(&pkg).unwrap(), "test").unwrap(); + let mut h = harness_with_size(egui::vec2(1400.0, 2000.0)); + h.state_mut().run("ui.pane.geoprocessing", json!(null)).unwrap(); + for _ in 0..4 { + h.step(); + } + let headers = + |h: &egui_kittest::Harness<'_, RustGisApp>, label: &str| h.query_all_by_role_and_label(Role::DisclosureTriangle, label).count(); + // The parent of both nested groups, once; the groups under it, once each. + assert_eq!(headers(&h, "A. Grid (3)"), 1); + assert_eq!(headers(&h, "B. Topography (1)"), 1); + assert_eq!(headers(&h, "Test ui-nest (1)"), 1, "no toolbox: named after the plugin"); + assert_eq!(headers(&h, "Variograms"), 1); + assert_eq!(headers(&h, "Examples"), 1); + assert_eq!(h.query_all_by_label("Nest Variogram One").count(), 0, "a nested group opens when asked"); + h.get_by_role_and_label(Role::DisclosureTriangle, "Variograms").click(); + h.run(); + assert_eq!(h.query_all_by_label("Nest Variogram One").count(), 1); + assert_eq!(h.query_all_by_label("Nest Variogram Two").count(), 1); + assert_eq!(h.query_all_by_label("Nest Contours").count(), 1); + assert_eq!(h.query_all_by_label("Nest Loose").count(), 1); + rp::unload("ui-nest"); +} + +/// A nested group drawn closed while browsing opens when the user then searches, so the matches +/// show; clearing the search brings back the closed group (#3022). +#[test] +fn plugin_tool_groups_open_when_searching() { + let _turn = PLUGINS.lock().unwrap_or_else(|e| e.into_inner()); + use egui::accesskit::Role; + use rustgis_ui_egui::rustgis_engine::plugins as rp; + let mut m: serde_json::Value = serde_json::from_str(&rp::testing::manifest("ui-find", &[])).unwrap(); + m["tools"] = json!([ + {"id": "v1", "name": "Find Variogram One", "toolbox": "A. Grid › Variograms", "params": []}, + {"id": "e1", "name": "Find Example", "toolbox": "A. Grid › Examples", "params": []}, + ]); + let pkg = rp::testing::zip_of(&[("plugin.json", m.to_string().as_bytes()), ("plugin.wasm", &rp::testing::wasm())]); + rp::load(rp::Package::from_zip(&pkg).unwrap(), "test").unwrap(); + let mut h = harness_with_size(egui::vec2(1400.0, 2000.0)); + h.state_mut().run("ui.pane.geoprocessing", json!(null)).unwrap(); + for _ in 0..4 { + h.step(); + } + assert_eq!(h.query_all_by_role_and_label(Role::DisclosureTriangle, "Variograms").count(), 1); + assert_eq!(h.query_all_by_label("Find Variogram One").count(), 0, "closed while browsing"); + h.state_mut().gp.search = "Variogram One".into(); + for _ in 0..4 { + h.step(); + } + assert_eq!(h.query_all_by_label("Find Variogram One").count(), 1, "open while searching"); + assert_eq!(h.query_all_by_label("Find Example").count(), 0, "not a match"); + h.state_mut().gp.search.clear(); + for _ in 0..4 { + h.step(); + } + assert_eq!(h.query_all_by_label("Find Variogram One").count(), 0, "closed again after the search"); + rp::unload("ui-find"); +} + #[test] fn plugin_small_ribbon_columns_and_nested_menus_preserve_actions() { if in_own_process("plugin_small_ribbon_columns_and_nested_menus_preserve_actions") { diff --git a/docs/plugins.md b/docs/plugins.md index 57471a440c..57376c1ca8 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -198,6 +198,12 @@ cargo build --release --target wasm32-unknown-unknown Ask only for what you use. - `commands[].params`: a string describing the command's parameters for scripts and agents, not a JSON object or a form definition; it defaults to `"{}"`. +- `tools[].toolbox`: where the tool sits in the **Tools** pane, under **Plugins**. Separate levels + with ` › `: `1. Grid and isolines › Variograms` makes a **1. Grid and isolines** group with a + **Variograms** group in it, up to eight levels counting **Plugins**. A tool with no toolbox sits + in a group named after the plugin. Groups are listed alphabetically, in whatever order the tools + come in `plugin.json`; empty levels are skipped. Numbered groups sort as text, so write the + numbers with leading zeros (`01.`, `02.`, … `10.`) to keep their order. - `commands[].icon`: the name of one of RustGIS's own icons (`stats`, `zoom_full`, `bookmark`, `buffer`…), or a puzzle piece when it isn't one. - `commands[].form` (optional) gives the command a dialog of its own: its ribbon button (tab