diff --git a/crates/ui-egui/src/dialogs/svg_filter.rs b/crates/ui-egui/src/dialogs/svg_filter.rs index f092a55cc4..897cce464c 100644 --- a/crates/ui-egui/src/dialogs/svg_filter.rs +++ b/crates/ui-egui/src/dialogs/svg_filter.rs @@ -12,35 +12,233 @@ //! the document holds no copy of. //! //! Fields: `selected` (a filter's name), `code` (the code being edited), `editing` (`""`, `"new"` -//! or the name of the filter being edited), and with an applied effect `__index`, `__item` and -//! `missing` (the name of a deleted filter the effect names). +//! or the name of the filter being edited), `__action` (the button pressed, run once by [`pump`]), +//! `__list` (the filters listed, pushed by [`pump`]), and with an applied effect `__index`, +//! `__item` and `missing` (the name of a deleted filter the effect names). +use craft::host::{PickRequest, Picked}; +use craft_ui::buttons::Button; +use craft_ui::contract::{ActivateOn, Bind, CollectionEvent, DisplayOrder, ItemSource, SelectMode, Selection, Show as _, ShowCollection as _, ShowContainer as _}; +use craft_ui::dialog::dialog_id; +use craft_ui::layout_components::{Row, Stack}; +use craft_ui::list::{List, ListRow, RowInk}; +use craft_ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamResult}; +use craft_ui::runtime::env::Label; +use craft_ui::text::TextField; use serde_json::{Value, json}; use vectorcraft_effects::svg_filter::defaults::{DEFAULT_FILTERS, Listed, default_filter, listed}; use vectorcraft_engine::doc::SvgFilter; -use super::DialogSpec; -use crate::picks::PickRequest; -use crate::state::Dialog; -use crate::{FilePick, VectorcraftApp, widgets}; +use crate::file_asks::{self, Owner}; +use crate::menus::Node; +use crate::VectorcraftApp; +// PLAN: assumed the framework paths above (`craft_ui::contract` re-exporting the behavior types, `craft_ui::patterns::form_dialogs`) and `crate::menus::Node`. /// The dialog kind of Apply SVG Filter. pub const KIND: &str = "svgFilter"; -pub(super) const SPEC: DialogSpec = DialogSpec { - heading: |_| tl!("Apply SVG Filter…").trim_end_matches('…').to_string(), - body, - confirm, - ok_enabled: Some(|app, d| d.str("editing").is_empty() && filters(app).0.iter().any(|f| f.filter.name == d.str("selected"))), - min_width: 440.0, - ..DialogSpec::FORM -}; +const LIST: &str = "list"; +const CODE: &str = "code"; +const SELECTED: &str = "selected"; +const EDITING: &str = "editing"; +const MISSING: &str = "missing"; +const ACTION: &str = "__action"; +const LISTING: &str = "__list"; + +/// The dialog's heading. +fn heading() -> String { + tl!("Apply SVG Filter…").trim_end_matches('…').to_string() +} /// The filters the dialog lists for the active document, and how many come first as the defaults. fn filters(app: &VectorcraftApp) -> (Vec, usize) { listed(app.session.active().map(|st| st.doc.svg_filters.as_slice()).unwrap_or_default()) } +/// The listing the dialog keeps in `__list`: each filter's name, whether it is a default the +/// document holds no copy of, and how many come first as the defaults. +fn listing(app: &VectorcraftApp) -> Value { + let (list, defaults) = filters(app); + let filters: Vec = list.iter().map(|f| json!({ "name": f.filter.name, "default": f.default })).collect(); + json!({ "filters": filters, "defaults": defaults }) +} + +/// The text under `key`, whether the dialog or automation set it. +fn text(fields: &DialogFields, key: &str) -> String { + match fields.get(key) { + Some(FieldValue::Text(s)) => s.clone(), + Some(FieldValue::Json(Value::String(s))) => s.clone(), + _ => String::new(), + } +} + +/// The value under `key` as JSON, for the keys that are not text. +fn json_field(fields: &DialogFields, key: &str) -> Option { + match fields.get(key) { + Some(FieldValue::Json(v)) => Some(v.clone()), + _ => None, + } +} + +/// A field value for `v`: text stays text, anything else is kept as JSON. +fn field_value(v: Value) -> FieldValue { + match v { + Value::String(s) => FieldValue::Text(s), + v => FieldValue::Json(v), + } +} + +/// The filters of `__list`: `(name, default)` each, and how many come first as the defaults. +fn listed_entries(fields: &DialogFields) -> (Vec<(String, bool)>, usize) { + let Some(l) = json_field(fields, LISTING) else { return (Vec::new(), 0) }; + let entries = l["filters"] + .as_array() + .map(|a| a.iter().map(|f| (f["name"].as_str().unwrap_or_default().to_string(), f["default"].as_bool().unwrap_or(false))).collect()) + .unwrap_or_default(); + (entries, l["defaults"].as_u64().unwrap_or(0) as usize) +} + +/// What a row of the list is. +enum Kind { + /// The name of a deleted filter the effect names: dimmed, not selectable. + Missing, + /// The line between the defaults and the document's own filters. + Section, + Filter, +} + +/// The rows of the list in display order, keyed by the filter's name. +struct Rows(Vec<(String, Kind)>); + +impl Rows { + fn of(fields: &DialogFields) -> Rows { + let (entries, defaults) = listed_entries(fields); + let missing = text(fields, MISSING); + let mut rows = Vec::new(); + if !missing.is_empty() { + rows.push((missing, Kind::Missing)); + } + for (i, (name, _)) in entries.into_iter().enumerate() { + // The document's other filters follow the defaults, below a line. + if i == defaults && i > 0 { + rows.push((String::new(), Kind::Section)); + } + rows.push((name, Kind::Filter)); + } + Rows(rows) + } +} + +impl DisplayOrder for Rows { + fn len(&self) -> usize { + self.0.len() + } + fn key(&self, at: usize) -> String { + self.0.get(at).map(|r| r.0.clone()).unwrap_or_default() + } + fn is_section(&self, at: usize) -> bool { + matches!(self.0.get(at), Some((_, Kind::Section))) + } + fn enabled(&self, at: usize) -> bool { + matches!(self.0.get(at), Some((_, Kind::Filter))) + } +} + +impl ItemSource for Rows {} + +/// The button `label`, enabled or not; true when pressed. +fn pressed(ui: &mut egui::Ui, label: String, enabled: bool) -> bool { + Button::new(label).enabled(enabled).show(ui).clicks > 0 +} + +/// The list and its buttons. A button sets `__action` for [`pump`], which has the app. +fn list_row(ui: &mut egui::Ui, fields: &mut DialogFields, _cx: &mut craft_ui::patterns::form_dialogs::CustomContext) -> egui::Response { + let rows = Rows::of(fields); + let (entries, _) = listed_entries(fields); + let selected = text(fields, SELECTED); + let editing = text(fields, EDITING); + let idle = editing.is_empty(); + let mut selection = if selected.is_empty() { Selection::new(vec![], None, None) } else { Selection::new(vec![selected.clone()], Some(selected.clone()), Some(selected.clone())) }; + let mut action = None; + let mut edit = None; + let shown = Stack::new().show(ui, |ui, _| { + let out = List::::new(heading()) + .select(SelectMode::Single) + .activate(ActivateOn::DoubleClick) + .selection(Bind::bound(&mut selection)) + .show_items(ui, &rows, &mut |key, icx| match rows.0.get(icx.at) { + Some((_, Kind::Missing)) => ListRow::new(key.clone()).ink(RowInk::Dimmed), + Some((_, Kind::Section)) => ListRow::new(Label::Empty), + _ => ListRow::new(key.clone()), + }); + for event in out.events { + // A double click edits, only while no code is open. + if let CollectionEvent::Activate(key) = event + && idle + { + edit = Some(key); + } + } + let chosen = entries.iter().find(|(name, _)| *name == selected); + let held = entries.iter().filter(|(_, default)| !default).count(); + Row::new().show(ui, |ui, _| { + if pressed(ui, tl!("New"), idle && held < SvgFilter::MAX) { + action = Some("new"); + } + if pressed(ui, tl!("Edit"), idle && chosen.is_some()) { + action = Some("edit"); + } + // A default the document holds no copy of has nothing to delete. + if pressed(ui, tl!("Delete"), idle && chosen.is_some_and(|(_, default)| !default)) { + action = Some("delete"); + } + if pressed(ui, tl!("Import…"), idle) { + action = Some("import"); + } + }); + }); + let now = selection.items.first().cloned().unwrap_or_default(); + if let Some(name) = edit { + fields.set(SELECTED, name); + action = Some("edit"); + } else if now != selected { + fields.set(SELECTED, now); + } + if let Some(a) = action { + fields.set(ACTION, a); + } + shown.response +} + +/// The code box with Save and Cancel, shown while a filter's code is open. +fn code_row(ui: &mut egui::Ui, fields: &mut DialogFields, _cx: &mut craft_ui::patterns::form_dialogs::CustomContext) -> egui::Response { + if text(fields, EDITING).is_empty() { + return ui.allocate_response(egui::Vec2::ZERO, egui::Sense::hover()); + } + let mut code = text(fields, CODE); + let before = code.clone(); + let mut action = None; + let shown = Stack::new().show(ui, |ui, _| { + // The filter's code is data: edited as it is written. + TextField::new(tl!("Code"), Bind::bound(&mut code)).mono().lines(10).tab_inserts(true).show(ui); + Row::new().show(ui, |ui, _| { + if pressed(ui, tl!("Save"), !before.trim().is_empty()) { + action = Some("save"); + } + if pressed(ui, tl!("Cancel"), true) { + action = Some("discard"); + } + }); + }); + if code != before { + fields.set(CODE, code); + } + if let Some(a) = action { + fields.set(ACTION, a); + } + shown.response +} + /// `ui.svgFilterDialog {index?, item?}`: open Apply SVG Filter for the active document; with /// `index`, for the applied SVG filter at `index` of `item` (null: the object's effects): the /// document's filter of its name selected, or the default of its name when the effect draws with @@ -50,7 +248,7 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { if app.session.active().is_none() { return Err("no document open".into()); } - let mut fields = json!({ "selected": "", "code": "", "editing": "" }); + let mut fields = json!({ "selected": "", "code": "", "editing": "", "__action": "" }); if let Some(k) = p.get("index").and_then(Value::as_u64) { let mut q = json!({}); if let Some(item) = p.get("item") { @@ -80,235 +278,162 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { fields["__item"] = item.clone(); } } - app.ui.dialog = Some(Dialog::new(KIND, fields)); + fields[LISTING] = listing(app); + let pairs = fields.as_object().into_iter().flatten().map(|(k, v)| (k.clone(), field_value(v.clone()))); + let ctx = app.ctx().clone(); + // PLAN: assumed `VectorcraftApp::ctx(&self) -> &egui::Context`. + let dialog = HostedParamDialog::new(&ctx, KIND, DialogFields::from_pairs(pairs), build); + crate::dialogs::host::open_held(app, dialog, |app, result: &ParamResult| confirm(app, &result.fields)).ok_or("no window")?; Ok(Value::Null) } +/// The dialog over its values. +fn build(fields: &mut DialogFields) -> ParamDialog<'_> { + let (entries, _) = listed_entries(fields); + let selected = text(fields, SELECTED); + let ok = text(fields, EDITING).is_empty() && entries.iter().any(|(name, _)| *name == selected); + let params = vec![ + Param::custom(LIST, Label::Empty, list_row).companions([SELECTED, EDITING, MISSING, ACTION, LISTING]), + Param::custom(CODE, Label::Empty, code_row), + ]; + ParamDialog::new(KIND, heading(), params).values(Bind::bound(fields)).ok_enabled(ok).ok_reason(tl!("Select an SVG filter")) +} + /// OK: the selected filter to the selection, or in the applied effect's place. A default the /// document holds no copy of is copied into it in the same undo step. -fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { - let name = d.str("selected"); +fn confirm(app: &mut VectorcraftApp, fields: &DialogFields) -> Result<(), String> { + let name = text(fields, SELECTED); let f = filters(app).0.into_iter().find(|f| f.filter.name == name).ok_or("select an SVG filter")?; let mut p = json!({ "name": f.filter.name }); - if let Some(k) = d.fields.get("__index").and_then(Value::as_u64) { + if let Some(k) = json_field(fields, "__index").and_then(|v| v.as_u64()) { p["index"] = json!(k); - if let Some(item) = d.fields.get("__item") { - p["item"] = item.clone(); + if let Some(item) = json_field(fields, "__item") { + p["item"] = item; } } - let r = app.run("svgFilter.apply", p)?; - app.ui.dialog = None; - Ok(r) + app.run("svgFilter.apply", p).map(|_| ()) } -/// The names of the active document's filters that are not a default's, mirrored for the (static) -/// menu tree ([`sync`]). -fn names_store() -> &'static std::sync::RwLock> { - static NAMES: std::sync::OnceLock>> = std::sync::OnceLock::new(); - NAMES.get_or_init(Default::default) +/// The open dialog, when it is. +fn dialog(app: &mut VectorcraftApp) -> Option<&mut HostedParamDialog> { + app.dialogs().get_mut_by_id::(dialog_id(KIND)) + // PLAN: assumed `VectorcraftApp::dialogs(&mut self) -> &mut DialogHost`. +} + +/// Sets `fields` on the open dialog. +fn set(app: &mut VectorcraftApp, fields: &[(&str, Value)]) { + if let Some(d) = dialog(app) { + for (k, v) in fields { + d.set_field(*k, field_value(v.clone())); + } + } +} + +/// Each frame: pushes the listed filters into the dialog (the build closure has no app) and runs the +/// button pressed, once. +pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { + let listing = listing(app); + let Some(d) = dialog(app) else { return }; + if json_field(d.fields(), LISTING).as_ref() != Some(&listing) { + d.set_field(LISTING, FieldValue::Json(listing)); + } + let action = text(d.fields(), ACTION); + if action.is_empty() { + return; + } + let (selected, editing, code) = (text(d.fields(), SELECTED), text(d.fields(), EDITING), text(d.fields(), CODE)); + d.set_field(ACTION, FieldValue::Text(String::new())); + let result: Result<(), String> = match action.as_str() { + "new" => Ok(set(app, &[(EDITING, json!("new")), (CODE, json!(""))])), + "edit" => { + if let Some(f) = filters(app).0.iter().map(|f| &f.filter).find(|f| f.name == selected) { + let (name, xml) = (f.name.clone(), f.xml.clone()); + set(app, &[(SELECTED, json!(name)), (EDITING, json!(name)), (CODE, json!(xml))]); + } + Ok(()) + } + "delete" => app.run("svgFilter.delete", json!({ "name": selected })).map(|_| set(app, &[(SELECTED, json!(""))])), + "save" => { + let mut p = json!({ "xml": code }); + if editing != "new" { + p["replace"] = json!(editing); + } + app.run("svgFilter.define", p).map(|r| set(app, &[(SELECTED, r["name"].clone()), (EDITING, json!("")), (CODE, json!(""))])) + } + "discard" => Ok(set(app, &[(EDITING, json!("")), (CODE, json!(""))])), + "import" => { + ask_import(app); + Ok(()) + } + _ => Ok(()), + }; + if let Err(e) = result { + app.status(e); + } } /// The names of `held`, a document's filters, that are not a default's, in their order. -fn own_names(held: &[SvgFilter]) -> Vec { +pub(crate) fn own_names(held: &[SvgFilter]) -> Vec { held.iter().filter(|f| default_filter(&f.name).is_none()).map(|f| f.name.clone()).collect() } -/// Mirror the active document's SVG filter names for the menu tree (cheap when unchanged). A -/// filter with a default's name is the menu's entry of that name already. -pub fn sync(app: &VectorcraftApp) { - let names: Vec = app.session.active().map(|st| own_names(&st.doc.svg_filters)).unwrap_or_default(); - let same = names_store().read().map(|v| v.len() == names.len() && v.iter().zip(&names).all(|(a, b)| *a == b)).unwrap_or(true); - if !same { - if let Ok(mut v) = names_store().write() { - *v = names.iter().map(|n| crate::shortcut_editor::intern(n)).collect(); - } - crate::shortcut_editor::GENERATION.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - } -} - /// Effect › SVG Filters: Apply SVG Filter…, Import SVG Filter…, the default filters and then the -/// active document's other filters, each applying itself to the selection. -pub fn menu_items() -> Vec { - items_for(names_store().read().map(|v| v.clone()).unwrap_or_default()) -} - -/// [`menu_items`] for a document whose filters that are not a default's are named `names`. -fn items_for(names: Vec<&'static str>) -> Vec { - use crate::menus::Item; - let apply = |n: &'static str| Item::Cmd(n, "svgFilter.apply", json!({ "name": n })); - let mut v = - vec![Item::Cmd("Apply SVG Filter…", "ui.svgFilterDialog", Value::Null), Item::Cmd("Import SVG Filter…", "ui.importSvgFilter", Value::Null)]; - v.push(Item::Sep); +/// document's other filters (`names`, from [`own_names`]), each applying itself to the selection. +pub(crate) fn menu_items(names: &[String]) -> Vec { + use crate::menus::cp; + // PLAN: assumed `menus::cp(label: impl Into, id: &'static str, params: Value) -> Node` and `Node::Separator`. + let apply = |n: &str| cp(n, "svgFilter.apply", json!({ "name": n })); + let mut v = vec![cp("Apply SVG Filter…", "ui.svgFilterDialog", Value::Null), cp("Import SVG Filter…", "ui.importSvgFilter", Value::Null)]; + v.push(Node::Separator); v.extend(DEFAULT_FILTERS.iter().map(|d| apply(d.name))); if !names.is_empty() { - v.push(Item::Sep); - v.extend(names.into_iter().map(apply)); + v.push(Node::Separator); + v.extend(names.iter().map(|n| apply(n))); } v } /// `ui.importSvgFilter {}`: add the filters of an SVG file the user picks to the active document. +/// The answer is `{pending: "fileDialog"}`; the continuation runs the import. pub fn import_file(app: &mut VectorcraftApp) -> Result { - let path = crate::picks::open(app, &pick()).ok_or("cancelled")?; - let r = app.run("svgFilter.import", json!({ "path": path }))?; - let added = r["added"].as_array().map_or(0, Vec::len); - app.status(crate::i18n::msg_n(added as u64, "Imported {n} SVG filter", "Imported {n} SVG filters", &[])); - Ok(r) + Ok(ask_import(app)) } /// The extensions of the SVG files Import… reads. const SVG_EXTS: &[&str] = &["svg"]; /// The open dialog for an SVG file. -pub(crate) fn pick() -> FilePick { - FilePick { filters: vec![FilePick::filter("SVG", SVG_EXTS)], ..Default::default() } +pub(crate) fn pick() -> PickRequest { + PickRequest::open("svg-filter").filter("SVG", SVG_EXTS) } -/// Import…: the filters of the SVG file picked in the open dialog, added to the document, the first -/// one added selected. -fn import(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - let Some(path) = crate::picks::open(app, &pick()) else { return Ok(()) }; - let r = app.run("svgFilter.import", json!({ "path": path }))?; - if let Some(first) = r["added"].get(0).and_then(Value::as_str) { - d.fields.insert("selected".into(), json!(first)); - } - Ok(()) -} - -/// What a button asked for, done once the dialog is drawn. -enum Action { - Select(String), - New, - /// Open this filter's code (a double-clicked row, or the selected one). - Edit(String), - Delete, - Import, - Save, - Discard, -} - -fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { - let (list, defaults) = filters(app); - let held = list.iter().filter(|f| !f.default).count(); - let selected = d.str("selected"); - let editing = d.str("editing"); - let mut act = None; - widgets::list_box(ui, |ui| { - egui::ScrollArea::vertical().id_salt("svg-filters").min_scrolled_height(160.0).max_height(160.0).show(ui, |ui| { - ui.set_width(ui.available_width()); - ui.set_min_height(160.0); - // Filter names are data: shown as they are. A deleted filter the effect still names - // comes first, dimmed and not selectable. - let missing = d.str("missing"); - if !missing.is_empty() { - ui.add_enabled_ui(false, |ui| ui.selectable_label(false, missing.as_str())); +/// Asks for an SVG file; once picked, its filters are added to the document and the first one +/// added is selected in the dialog when it is open. +fn ask_import(app: &mut VectorcraftApp) -> Value { + file_asks::ask(app, pick(), Owner::Document, |app, picked| { + let params = match picked { + // The browser answers with the file's contents, not a path. + Picked::Contents(files) => { + let (_, bytes) = files.first().ok_or("cancelled")?; + json!({ "svg": String::from_utf8_lossy(bytes) }) } - for (i, Listed { filter: f, .. }) in list.iter().enumerate() { - // The document's other filters follow the defaults, below a line. - if i == defaults && i > 0 { - ui.separator(); - } - let r = ui.selectable_label(f.name == selected, f.name.as_str()); - if r.double_clicked() && editing.is_empty() { - act = Some(Action::Edit(f.name.clone())); - } else if r.clicked() { - act = Some(Action::Select(f.name.clone())); - } - } - }); - }); - ui.add_space(6.0); - let chosen = list.iter().find(|f| f.filter.name == selected); - let has = chosen.is_some(); - // A default the document holds no copy of has nothing to delete. - let deletable = chosen.is_some_and(|f| !f.default); - ui.horizontal(|ui| { - ui.spacing_mut().item_spacing.x = 4.0; - let idle = editing.is_empty(); - if ui.add_enabled_ui(idle && held < SvgFilter::MAX, |ui| widgets::flat_button(ui, tl!("New"), 64.0)).inner.clicked() { - act = Some(Action::New); + other => json!({ "path": other.first_path().ok_or("cancelled")?.to_string_lossy() }), + }; + let r = app.run("svgFilter.import", params)?; + let added = r["added"].as_array().map_or(0, Vec::len); + app.status(crate::i18n::msg_n(added as u64, "Imported {n} SVG filter", "Imported {n} SVG filters", &[])); + if let Some(first) = r["added"].get(0) { + set(app, &[(SELECTED, first.clone())]); } - if ui.add_enabled_ui(idle && has, |ui| widgets::flat_button(ui, tl!("Edit"), 64.0)).inner.clicked() { - act = Some(Action::Edit(selected.clone())); - } - if ui.add_enabled_ui(idle && deletable, |ui| widgets::flat_button(ui, tl!("Delete"), 64.0)).inner.clicked() { - act = Some(Action::Delete); - } - if crate::picks::can(app, &PickRequest::Open(pick())) - && ui.add_enabled_ui(idle, |ui| widgets::flat_button(ui, tl!("Import…"), 64.0)).inner.clicked() - { - act = Some(Action::Import); - } - }); - if !editing.is_empty() { - ui.add_space(8.0); - let mut code = d.str("code"); - // The filter's code is data: edited as it is written. - let r = ui.add(egui::TextEdit::multiline(&mut code).font(egui::TextStyle::Monospace).desired_width(f32::INFINITY).desired_rows(10)); - if r.changed() { - d.fields.insert("code".into(), json!(code)); - } - ui.add_space(6.0); - ui.horizontal(|ui| { - ui.spacing_mut().item_spacing.x = 4.0; - if ui.add_enabled_ui(!code.trim().is_empty(), |ui| widgets::flat_button(ui, tl!("Save"), 64.0)).inner.clicked() { - act = Some(Action::Save); - } - if widgets::flat_button(ui, tl!("Cancel"), 64.0).clicked() { - act = Some(Action::Discard); - } - }); - } - let r = match act { - None => Ok(()), - Some(Action::Select(name)) => { - d.fields.insert("selected".into(), json!(name)); - Ok(()) - } - Some(Action::New) => { - d.fields.insert("editing".into(), json!("new")); - d.fields.insert("code".into(), json!("")); - Ok(()) - } - Some(Action::Edit(name)) => { - if let Some(f) = list.iter().map(|f| &f.filter).find(|f| f.name == name) { - d.fields.insert("selected".into(), json!(f.name)); - d.fields.insert("editing".into(), json!(f.name)); - d.fields.insert("code".into(), json!(f.xml)); - } - Ok(()) - } - Some(Action::Delete) => app.run("svgFilter.delete", json!({ "name": selected })).map(|_| { - d.fields.insert("selected".into(), json!("")); - }), - // The open dialog, shown off the UI thread, fills the dialog as it is then. - Some(Action::Import) => crate::picks::in_dialog(app, d, import), - Some(Action::Save) => { - let mut p = json!({ "xml": d.str("code") }); - if editing != "new" { - p["replace"] = json!(editing); - } - app.run("svgFilter.define", p).map(|r| { - d.fields.insert("selected".into(), r["name"].clone()); - d.fields.insert("editing".into(), json!("")); - d.fields.insert("code".into(), json!("")); - }) - } - Some(Action::Discard) => { - d.fields.insert("editing".into(), json!("")); - d.fields.insert("code".into(), json!("")); - Ok(()) - } - }; - if let Err(e) = r { - app.status(e); - } - false + Ok(r) + }) } #[cfg(test)] mod tests { use super::*; + use crate::tests::picks::{answer_dialog, open_dialogs, poll, scripted}; + // PLAN: assumed fl.A20's helpers live in `crate::tests::picks`. use vectorcraft_engine::Session; const SHIFT: &str = r#""#; @@ -319,23 +444,17 @@ mod tests { app } - /// One headless frame of the dialog's body: the texts drawn. - fn frame(app: &mut VectorcraftApp, d: &mut Dialog) -> Vec { - fn texts(s: &egui::Shape, out: &mut Vec) { - match s { - egui::Shape::Text(t) => out.push(t.galley.text().to_string()), - egui::Shape::Vec(v) => v.iter().for_each(|s| texts(s, out)), - _ => {} - } - } - let ctx = egui::Context::default(); - let mut out = ctx.run_ui(egui::RawInput::default(), |ui| { - body(app, ui, d); - }); - out.textures_delta.clear(); - let mut v = vec![]; - out.shapes.iter().for_each(|c| texts(&c.shape, &mut v)); - v + fn rect(app: &mut VectorcraftApp) -> u64 { + app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap() + } + + /// The fields of the open dialog, as automation lists them. + fn fields(app: &VectorcraftApp) -> Value { + open_dialogs(app).last().and_then(|d| d.fields.clone()).unwrap_or(Value::Null) + } + + fn effects(app: &VectorcraftApp, id: u64) -> Vec { + app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects.clone() } #[test] @@ -344,20 +463,19 @@ mod tests { assert!(open(&mut app, &json!({})).is_err(), "no document"); let mut app = self::app(); app.session.execute("svgFilter.define", &json!({"xml": SHIFT})).unwrap(); - let id = app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let id = rect(&mut app); crate::menus::invoke(&mut app, "ui.svgFilterDialog", json!({})); - let mut d = app.ui.dialog.clone().unwrap(); - assert_eq!(d.kind, KIND); - // Below the defaults, which fill the rows the list shows at first. - assert_eq!(filters(&app).0.last().map(|f| f.filter.name.as_str()), Some("Shift"), "the filter's name, as it is"); - assert!(!frame(&mut app, &mut d).is_empty()); - assert!(!(SPEC.ok_enabled.unwrap())(&app, &d), "nothing selected"); - d.fields.insert("selected".into(), json!("Shift")); - assert!((SPEC.ok_enabled.unwrap())(&app, &d)); - confirm(&mut app, &d).unwrap(); - assert!(app.ui.dialog.is_none()); - let fx = &app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects; + let dialog = dialog_id(KIND); + let names: Vec = fields(&app)[LISTING]["filters"].as_array().unwrap().iter().map(|f| f["name"].as_str().unwrap().to_string()).collect(); + assert_eq!((names.len(), names.last().map(String::as_str)), (DEFAULT_FILTERS.len() + 1, Some("Shift")), "the document's own after the defaults"); + // Nothing selected: OK is off and the dialog stays. + answer_dialog(&mut app, dialog, &[], true); + assert!(effects(&app, id).is_empty()); + assert_eq!(open_dialogs(&app).len(), 1); + answer_dialog(&mut app, dialog, &[(SELECTED, json!("Shift"))], true); + let fx = effects(&app, id); assert_eq!((fx.len(), fx[0].params["name"].as_str()), (1, Some("Shift"))); + assert!(open_dialogs(&app).is_empty()); } #[test] @@ -365,97 +483,64 @@ mod tests { let mut app = app(); app.session.execute("svgFilter.define", &json!({"xml": SHIFT})).unwrap(); app.session.execute("svgFilter.define", &json!({"xml": ""})).unwrap(); - let id = app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let id = rect(&mut app); app.session.execute("svgFilter.apply", &json!({"name": "Shift"})).unwrap(); - // With a fill row active, the object's own effect is still the one edited. app.session.execute("appearance.setActiveItem", &json!({"index": 0})).unwrap(); - // The Appearance panel opens the effect's dialog: this one. - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let mut d = app.ui.dialog.clone().unwrap(); - assert_eq!((d.kind.as_str(), d.str("selected").as_str()), (KIND, "Shift")); - d.fields.insert("selected".into(), json!("Other")); - confirm(&mut app, &d).unwrap(); - let fx = &app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects; + let dialog = dialog_id(KIND); + let reopen = |app: &mut VectorcraftApp| app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); + // A held filter is selected; OK puts the other in its place. + reopen(&mut app); + assert_eq!(fields(&app)[SELECTED], "Shift"); + answer_dialog(&mut app, dialog, &[(SELECTED, json!("Other"))], true); + let fx = effects(&app, id); assert_eq!((fx.len(), fx[0].params["name"].as_str()), (1, Some("Other")), "in its place, not added"); - // Edited since it was applied: the effect follows the edit, and the filter stays selected. - app.session.execute("svgFilter.define", &json!({"xml": "", "replace": "Other"})).unwrap(); - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let d = app.ui.dialog.clone().unwrap(); - assert_eq!((d.str("selected").as_str(), d.str("editing").as_str()), ("Other", "")); - // Deleted: its name shows above the list, nothing is selected, and OK waits for a choice. + // A default the document holds no copy of is selected. + app.session.execute("svgFilter.apply", &json!({"name": "Thin", "index": 0})).unwrap(); + app.session.execute("svgFilter.delete", &json!({"name": "Thin"})).unwrap(); + reopen(&mut app); + assert_eq!((fields(&app)[SELECTED].as_str(), fields(&app)[EDITING].as_str()), (Some("Thin"), Some(""))); + answer_dialog(&mut app, dialog, &[(SELECTED, json!("Other"))], false); + // Deleted: shown as missing, nothing selected, OK off. app.session.execute("svgFilter.delete", &json!({"name": "Other"})).unwrap(); - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let d = app.ui.dialog.clone().unwrap(); - assert_eq!((d.str("missing").as_str(), d.str("selected").as_str(), d.str("editing").as_str()), ("Other", "", "")); - assert!(!(SPEC.ok_enabled.unwrap())(&app, &d)); - // An applied filter the document never held (from an opened file) opens as code to save. - let e = vectorcraft_doc::Effect { + app.session.execute("svgFilter.apply", &json!({"name": "Other", "index": 0, "xml": ""})).ok(); + let gone = vectorcraft_doc::Effect { id: "svg.filter".into(), params: json!({"name": "Gone"}), visible: true }; + app.session + .edit("Plant", |d, _| { + d.node_mut(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects = vec![gone]; + Ok(()) + }) + .unwrap(); + reopen(&mut app); + assert_eq!((fields(&app)[MISSING].as_str(), fields(&app)[SELECTED].as_str()), (Some("Gone"), Some(""))); + answer_dialog(&mut app, dialog, &[], true); + assert_eq!(open_dialogs(&app).len(), 1, "OK waits for a choice"); + // An applied filter the document never held opens as code to save. + let kept = vectorcraft_doc::Effect { id: "svg.filter".into(), params: json!({"name": "Kept", "xml": ""}), visible: true, }; app.session .edit("Plant", |d, _| { - d.node_mut(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects = vec![e]; + d.node_mut(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects = vec![kept]; Ok(()) }) .unwrap(); - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let d = app.ui.dialog.clone().unwrap(); - assert_eq!(d.str("editing"), "new"); - assert!(d.str("code").contains("id=\"Kept\"")); - } - - /// Edit, started from one object's effect in the Appearance panel, changes every object that uses - /// the filter; a deleted filter's name is drawn above the list. - #[test] - fn an_edit_from_one_objects_effect_changes_every_user() { - let mut app = app(); - app.session.execute("svgFilter.define", &json!({"xml": SHIFT})).unwrap(); - let rect = |app: &mut VectorcraftApp, x: f64| { - app.session.execute("shape.rectangle", &json!({"x": x, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap() - }; - let (a, b) = (rect(&mut app, 10.0), rect(&mut app, 50.0)); - app.session.execute("svgFilter.apply", &json!({"name": "Shift", "ids": [a, b]})).unwrap(); - app.session.execute("select.set", &json!({"ids": [a]})).unwrap(); - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let mut d = app.ui.dialog.clone().unwrap(); - // Edit opens the selected filter's code; Save replaces it, as its button does. - d.fields.insert("editing".into(), json!(d.str("selected"))); - let edited = r#""#; - app.run("svgFilter.define", json!({"xml": edited, "replace": d.str("editing")})).unwrap(); - let xml = |app: &VectorcraftApp, id: u64| { - app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects[0].params["xml"].clone() - }; - assert_eq!((xml(&app, a), xml(&app, b)), (json!(edited), json!(edited))); - app.session.execute("svgFilter.delete", &json!({"name": "Shift"})).unwrap(); - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let mut d = app.ui.dialog.clone().unwrap(); - assert!(frame(&mut app, &mut d).iter().any(|t| t == "Shift"), "the deleted filter's name"); + reopen(&mut app); + assert_eq!(fields(&app)[EDITING], "new"); + assert!(fields(&app)[CODE].as_str().unwrap().contains("id=\"Kept\"")); } #[test] fn the_submenu_lists_the_documents_filters_below_its_commands() { - use crate::menus::Item; - let ids = - |items: &[Item]| items.iter().map(|i| if let Item::Cmd(l, c, p) = i { format!("{l} {c} {p}") } else { "—".into() }).collect::>(); - // The defaults follow the commands in every document, and with no document. - let plain = ids(&items_for(vec![])); - assert_eq!( - plain[..4], - [ - "Apply SVG Filter… ui.svgFilterDialog null", - "Import SVG Filter… ui.importSvgFilter null", - "—", - "Soft Blur svgFilter.apply {\"name\":\"Soft Blur\"}" - ] - ); + let plain = menu_items(&[]); assert_eq!(plain.len(), 3 + DEFAULT_FILTERS.len()); - assert_eq!(plain.last().map(String::as_str), Some("Hue Shift svgFilter.apply {\"name\":\"Hue Shift\"}")); - // The document's other filters come below them. - let with = ids(&items_for(vec!["Shift", "A (1)"])); + assert_eq!(plain[2], Node::Separator); + let own = vec!["Shift".to_string(), "A (1)".to_string()]; + let with = menu_items(&own); assert_eq!(with[..plain.len()], plain[..]); - assert_eq!(with[plain.len()..], ["—", "Shift svgFilter.apply {\"name\":\"Shift\"}", "A (1) svgFilter.apply {\"name\":\"A (1)\"}"]); + assert_eq!(with[plain.len()], Node::Separator); + assert_eq!(with.len(), plain.len() + 1 + own.len()); // A document's filter with a default's name is that default's entry. let held = [ SvgFilter { name: "Glow".into(), xml: String::new() }, @@ -463,120 +548,50 @@ mod tests { SvgFilter { name: "glow".into(), xml: String::new() }, ]; assert_eq!(own_names(&held), ["Shift", "glow"]); - let mut app = app(); - assert!(!crate::menus::enabled(&app, "svgFilter.apply"), "nothing selected"); - app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); - assert!(crate::menus::enabled(&app, "svgFilter.apply")); - assert_eq!(crate::menus::display_label(&app, "svgFilter.apply", "Shift"), "Shift", "a filter's name as it is"); - } - - /// The steps in the active document's undo history. - fn steps(app: &VectorcraftApp) -> usize { - app.session.active().unwrap().history.undo.len() - } - - #[test] - fn a_default_is_listed_first_and_ok_copies_it_into_the_document() { - let mut app = app(); - app.session.execute("svgFilter.define", &json!({"xml": SHIFT})).unwrap(); - let id = app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap(); - open(&mut app, &json!({})).unwrap(); - let mut d = app.ui.dialog.clone().unwrap(); - let (list, defaults) = filters(&app); - let names: Vec<&str> = list.iter().map(|f| f.filter.name.as_str()).collect(); - assert_eq!((defaults, names.len()), (DEFAULT_FILTERS.len(), DEFAULT_FILTERS.len() + 1)); - assert_eq!((names[0], names[defaults - 1], names[defaults]), ("Soft Blur", "Hue Shift", "Shift"), "the defaults, then the document's own"); - assert!(frame(&mut app, &mut d).iter().any(|t| t == "Soft Blur"), "a default's name, as it is"); - d.fields.insert("selected".into(), json!("Soft Shadow")); - assert!((SPEC.ok_enabled.unwrap())(&app, &d), "a default can be applied"); - let before = steps(&app); - confirm(&mut app, &d).unwrap(); - assert_eq!(steps(&app), before + 1, "one undo step"); - let doc = &app.session.active().unwrap().doc; - assert_eq!(doc.svg_filters.iter().map(|f| f.name.as_str()).collect::>(), ["Shift", "Soft Shadow"]); - let fx = &doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects; - assert_eq!((fx.len(), fx[0].params["name"].as_str()), (1, Some("Soft Shadow"))); - assert_eq!(fx[0].params["xml"].as_str(), default_filter("Soft Shadow").map(|f| f.xml)); - // Opened for that effect, the document's copy is selected; OK with another default puts it - // in the effect's place and copies it, in one step. - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let mut d = app.ui.dialog.clone().unwrap(); - assert_eq!((d.str("selected").as_str(), d.str("editing").as_str()), ("Soft Shadow", "")); - d.fields.insert("selected".into(), json!("Invert")); - let before = steps(&app); - confirm(&mut app, &d).unwrap(); - assert_eq!(steps(&app), before + 1); - let doc = &app.session.active().unwrap().doc; - let fx = &doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects; - assert_eq!((fx.len(), fx[0].params["name"].as_str()), (1, Some("Invert")), "in its place, not added"); - assert_eq!(doc.svg_filters.len(), 3); - } - - /// Edit on a default opens its code, and Save keeps the edited copy in the document under the - /// default's name; an effect that names a default the document no longer holds selects it. - #[test] - fn an_edited_default_is_saved_in_the_document() { - let mut app = app(); - let id = app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap(); - // Save, as its button does for the default being edited. - let edited = r#""#; - let r = app.run("svgFilter.define", json!({"xml": edited, "replace": "Thin"})).unwrap(); - assert_eq!(r["name"], "Thin"); - assert_eq!(filters(&app).0.iter().filter(|f| !f.default).map(|f| f.filter.xml.as_str()).collect::>(), [edited]); - assert_eq!(filters(&app).0.len(), DEFAULT_FILTERS.len(), "in the default's place"); - app.session.execute("svgFilter.apply", &json!({"name": "Thin"})).unwrap(); - // Deleted: the default is listed again, and the effect that names it selects the default. - app.session.execute("svgFilter.delete", &json!({"name": "Thin"})).unwrap(); - assert!(filters(&app).0.iter().all(|f| f.default)); - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let d = app.ui.dialog.clone().unwrap(); - assert_eq!((d.str("selected").as_str(), d.str("missing").as_str(), d.str("editing").as_str()), ("Thin", "", "")); - confirm(&mut app, &d).unwrap(); - let doc = &app.session.active().unwrap().doc; - let fx = &doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects; - assert_eq!(fx[0].params["xml"].as_str(), default_filter("Thin").map(|f| f.xml), "the default again"); - // An applied filter with a default's name and other code opens as code to save. - let e = vectorcraft_doc::Effect { - id: "svg.filter".into(), - params: json!({"name": "Glow", "xml": ""}), - visible: true, - }; - app.session - .edit("Plant", |d, _| { - d.node_mut(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects = vec![e]; - Ok(()) - }) - .unwrap(); - app.run("effect.dialog", json!({"effect": "svg.filter", "index": 0, "item": null})).unwrap(); - let d = app.ui.dialog.clone().unwrap(); - assert_eq!((d.str("editing").as_str(), d.str("selected").as_str()), ("new", "")); } #[test] fn new_saves_the_code_and_selects_the_filter() { let mut app = app(); open(&mut app, &json!({})).unwrap(); - let mut d = app.ui.dialog.clone().unwrap(); - d.fields.insert("editing".into(), json!("new")); - d.fields.insert("code".into(), json!(SHIFT)); - assert!(!(SPEC.ok_enabled.unwrap())(&app, &d), "not while the code is open"); - // Save, as its button does. - let r = app.run("svgFilter.define", json!({"xml": d.str("code")})).unwrap(); - assert_eq!(r["name"], "Shift"); + let dialog = dialog_id(KIND); + answer_dialog(&mut app, dialog, &[(ACTION, json!("new"))], false); + assert_eq!(fields(&app)[EDITING], "new"); + // OK is off while the code is open. + answer_dialog(&mut app, dialog, &[(SELECTED, json!("Soft Blur")), (CODE, json!(SHIFT))], true); + assert_eq!(open_dialogs(&app).len(), 1); + answer_dialog(&mut app, dialog, &[(ACTION, json!("save"))], false); + assert_eq!(app.session.active().unwrap().doc.svg_filters.len(), 1); + assert_eq!((fields(&app)[SELECTED].as_str(), fields(&app)[EDITING].as_str()), (Some("Shift"), Some(""))); + // Cancel discards. + answer_dialog(&mut app, dialog, &[(ACTION, json!("new")), (CODE, json!(""))], false); + answer_dialog(&mut app, dialog, &[(ACTION, json!("discard"))], false); + assert_eq!((fields(&app)[EDITING].as_str(), fields(&app)[CODE].as_str()), (Some(""), Some(""))); assert_eq!(app.session.active().unwrap().doc.svg_filters.len(), 1); } #[test] fn import_adds_the_picked_files_filters() { let mut app = app(); + let picker = scripted(&mut app); + let ctx = egui::Context::default(); let path = std::env::temp_dir().join(format!("vc-ui-svgfilters-{}.svg", std::process::id())); std::fs::write(&path, format!("{SHIFT}")).unwrap(); - let picked = path.to_string_lossy().into_owned(); - app.services.pick_open = Some(Box::new(move |_: &FilePick| Some(picked.clone()))); - let mut d = Dialog::new(KIND, json!({ "selected": "", "code": "", "editing": "" })); - import(&mut app, &mut d).unwrap(); + open(&mut app, &json!({})).unwrap(); + let dialog = dialog_id(KIND); + // A cancelled pick adds nothing. + answer_dialog(&mut app, dialog, &[(ACTION, json!("import"))], false); + assert_eq!(picker.requests()[0].filters[0].extensions, ["svg"]); + picker.cancel(); + poll(&mut app, &ctx); + assert!(app.session.active().unwrap().doc.svg_filters.is_empty()); + // Through the command, as the menu runs it. + let r = import_file(&mut app).unwrap(); + assert_eq!(r["pending"], "fileDialog"); + picker.answer(&[path.to_str().unwrap()]); + poll(&mut app, &ctx); std::fs::remove_file(&path).ok(); - assert_eq!(d.str("selected"), "Shift"); assert_eq!(app.session.active().unwrap().doc.svg_filters[0].name, "Shift"); + assert_eq!(fields(&app)[SELECTED], "Shift"); } }