Plug-ins: typed values reach the preview and the rows they show

A plug-in's parameter dialog did not bind its values to the host fields
that the preview and the rows' visibleWhen read, so a check box or a
typed number stayed in the form: checking a box did not show the rows
that depend on it, and the preview kept running with the opening
values. The dialog now binds its values with Bind::bound(fields), as
Set Corners and the Arrange dialogs do.
This commit is contained in:
dotCooCoo committed 2026-11-30 00:14:10 -08:00
1 parent ad642cb34c
commit 0b469bb7b6
2 files changed
+66 -2

No files matched your search

+2 -2
View File
@@ -42,7 +42,7 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use craft::menu::Node;
use craft::ui::contract::Show as _;
use craft::ui::contract::{Bind, Show as _};
use craft::ui::dialog::{DialogId, DialogResult, DialogStack, Prompt, StackDialog, dialog_id};
use craft::ui::display::{Shade, Text};
use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog};
@@ -564,7 +564,7 @@ fn build(key: &'static str, m: Manifest, all: Arc<[Param]>, look: Look) -> impl
rows.push(Param::read_only_text("description", "", look.description.clone()).full_width());
}
rows.extend(all.iter().filter(|p| shown.contains(p.key.as_str())).cloned());
let mut dialog = ParamDialog::new(key, look.heading(), Cow::Owned(rows)).previews(look.previews);
let mut dialog = ParamDialog::new(key, look.heading(), Cow::Owned(rows)).values(Bind::bound(fields)).previews(look.previews);
if let Some(ok) = look.ok {
dialog = dialog.ok_label(ok.into());
}
@@ -257,3 +257,67 @@ fn an_open_plugin_dialog_follows_a_change_of_language() {
assert_eq!(shown, (false, true), "the label in German");
assert_eq!(values.unwrap()["size"].as_f64(), Some(7.0), "the value set before");
}
/// The width of every path in the active document together, in points.
fn paths_width(app: &VectorcraftApp) -> Option<f64> {
let doc = &app.session.doc().ok()?.doc;
let mut ids = Vec::new();
doc.walk(|n| {
if n.path_data().is_some() {
ids.push(n.id);
}
});
doc.bounds_of(&ids, false).map(|r| r.x1 - r.x0)
}
/// Values typed and clicked in a plug-in's dialog reach the plug-in: a check box shows the row
/// whose `visibleWhen` it meets, the number typed there previews on the canvas, and OK runs the
/// filter with it as one undo step.
#[test]
fn typed_plugin_values_show_rows_and_reach_the_preview_and_the_run() {
use crate::tests::panel_harness::Panel;
let id = "test.ui.fields.typed";
let manifest = format!(
r#"{{"id":"{id}","name":"Typed Values","version":"1","kind":"filter","params":{{
"grow":{{"type":"bool","default":false,"label":"Grow"}},
"n":{{"type":"int","min":0,"max":10,"default":3,"label":"Count","visibleWhen":{{"grow":[true]}}}}}}}}"#
);
// A 30 pt wide triangle when the filter is given `n` 7, a 50 pt square otherwise.
let triangle = r#"{"objects":[{"path":{"subpaths":[{"closed":true,"anchors":[{"p":[0,0]},{"p":[30,0]},{"p":[15,20]}]}]}}]}"#;
let square = r#"{"objects":[{"path":{"subpaths":[{"closed":true,"anchors":[{"p":[0,0]},{"p":[50,0]},{"p":[50,50]},{"p":[0,50]}]}]}}]}"#;
let wasm = wat::parse_str(tpl::when_given_else(&manifest, tpl::Given::Params, r#""n":7"#, triangle, square)).unwrap();
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
app.run("plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&wasm)})).unwrap();
let mut panel = Panel::new(app, crate::tests::picks::dialogs);
panel.app().run("plugin.dialog", json!({"id": id})).unwrap();
panel.frame();
panel.frame();
assert!(!panel.shows("Count"), "Count is hidden while Grow is off");
panel.click("Grow");
panel.frame();
assert_eq!(fields(&panel.app())["grow"], json!(true), "the click reaches the dialog's values");
assert!(panel.shows("Count"), "checking Grow shows Count");
panel.click("Count");
panel.key(egui::Key::A, egui::Modifiers::COMMAND);
panel.type_text("7");
panel.key(egui::Key::Tab, egui::Modifiers::NONE);
panel.frame();
panel.frame();
let typed = fields(&panel.app())["n"].clone();
let previewed = paths_width(&panel.app());
let undo = panel.app().session.doc().unwrap().history.undo.len();
panel.click("OK");
panel.frame();
panel.frame();
let (closed, steps, ran) = {
let app = panel.app();
(!app.any_dialog_open(), app.session.doc().unwrap().history.undo.len(), paths_width(&app))
};
panel.app().run("plugin.remove", json!({"id": id})).unwrap();
assert_eq!(typed.as_f64(), Some(7.0), "the typed count reaches the dialog's values");
assert_eq!(previewed, Some(30.0), "the typed count previewed");
assert!(closed, "OK closed the dialog");
assert_eq!(steps, undo + 1, "one undo step");
assert_eq!(ran, Some(30.0), "the filter ran with the typed count");
}