diff --git a/crates/ui-egui/src/addons/packaging/preview.rs b/crates/ui-egui/src/addons/packaging/preview.rs index 7fde5f2f4b..5af2119835 100644 --- a/crates/ui-egui/src/addons/packaging/preview.rs +++ b/crates/ui-egui/src/addons/packaging/preview.rs @@ -562,13 +562,13 @@ fn toolbar(ui: &mut Ui, words: &FoldPreviewWords, doc: &vectorcraft_engine::doc: .collect(); Row::new().wrap(true).show(ui, |ui, _| { let current = set.view().map_or("", |v| v.1); - if let Some(&id) = Select::new(words.view.clone(), Bind::given(current), &views).show(ui).value() + if let Some(&id) = Select::new(words.view.clone(), Bind::given(current), &views).show(ui).events.committed() && let Some(&(_, _, x, y)) = VIEWS.iter().find(|v| v.1 == id) { (set.rotate_x, set.rotate_y, set.pan) = (x, y, [0.0; 2]); } let inside = Select::new(words.inside.clone(), Bind::given(set.inside.clone()), &insides).placeholder(set.inside.clone()).show(ui); - if let Some(name) = inside.value() { + if let Some(name) = inside.events.committed() { set.inside.clone_from(name); } Checkbox::new(Bind::bound(&mut set.show_fold_lines)).label(words.show_fold_lines.clone()).show(ui); diff --git a/crates/ui-egui/src/addons/packaging/preview/tests.rs b/crates/ui-egui/src/addons/packaging/preview/tests.rs index 63f7bcc3a3..5c729aae39 100644 --- a/crates/ui-egui/src/addons/packaging/preview/tests.rs +++ b/crates/ui-egui/src/addons/packaging/preview/tests.rs @@ -565,3 +565,37 @@ fn the_status_line_reports_open_joints_crossing_panels_and_other_blanks() { // With no fold there is the one line that says why. assert_eq!(status(0, None, false).len(), 1); } + +/// View and Inside: an entry highlighted with the arrow keys changes no setting, Escape leaves the settings as they +/// were, and Enter applies the entry. +#[test] +fn a_highlighted_view_or_inside_applies_only_once_it_is_chosen() { + use craft::ui::contract::Role; + + use crate::tests::panel_harness::Panel; + + crate::i18n::set_current(crate::i18n::Lang::EN); + let (mut app, _) = app(); + app.run("ui.foldPreview", json!({"view": "front", "inside": ""})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(vec2(520.0, 440.0)); + let settings = |panel: &Panel| panel.app().ui.fold_preview.clone(); + for name in ["View", "Inside"] { + let before = settings(&panel); + panel.click_role(name, Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(settings(&panel), before, "a highlighted {name} entry changes no setting"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(settings(&panel), before, "Escape leaves the {name} setting as it was"); + panel.click_role(name, Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + assert_ne!(settings(&panel), before, "Enter applies the {name} entry"); + } + let set = settings(&panel); + assert_eq!((set.rotate_x, set.rotate_y), (0.0, 180.0), "the entry after Front is Back"); + assert!(!set.inside.is_empty(), "the entry after White is a swatch"); +} diff --git a/crates/ui-egui/src/dialogs/perspective_grid.rs b/crates/ui-egui/src/dialogs/perspective_grid.rs index f899a9d7e9..9c3dfcb00e 100644 --- a/crates/ui-egui/src/dialogs/perspective_grid.rs +++ b/crates/ui-egui/src/dialogs/perspective_grid.rs @@ -371,11 +371,12 @@ fn row(ui: &mut egui::Ui, name: &Label, salt: &str, mut body: impl FnMut(&mut eg Field::new(name.clone()).id_salt(salt).show(ui, |ui, _| body(ui)); } -/// A select of `entries` named `name` (an English label), over `value`; `set` takes a pick. +/// A select of `entries` named `name` (an English label), over `value`; `set` takes the entry chosen, not one only +/// highlighted. fn choose(ui: &mut egui::Ui, name: &str, salt: &str, entries: &[Entry], value: T, mut set: impl FnMut(T)) { let label = word(name); row(ui, &label, salt, |ui| { - if let Some(picked) = Select::new(label.clone(), Bind::given(value.clone()), entries).show(ui).value().cloned() { + if let Some(picked) = Select::new(label.clone(), Bind::given(value.clone()), entries).show(ui).events.committed().cloned() { set(picked); } }); diff --git a/crates/ui-egui/src/dialogs/png_options.rs b/crates/ui-egui/src/dialogs/png_options.rs index 3a69a42763..cafd0f6a12 100644 --- a/crates/ui-egui/src/dialogs/png_options.rs +++ b/crates/ui-egui/src/dialogs/png_options.rs @@ -223,7 +223,7 @@ fn resolution_row() -> Param { let entries: Vec> = RESOLUTION_LABELS.iter().enumerate().map(|(i, label)| Entry::new(i, tl!(*label))).collect(); let (mut picked, mut typed) = (None, None); let shown = Row::new().show(ui, |ui, _| { - picked = Select::new(tl!("Resolution:"), Bind::given(preset.unwrap_or(other)), &entries).show(ui).value().copied(); + picked = Select::new(tl!("Resolution:"), Bind::given(preset.unwrap_or(other)), &entries).show(ui).events.committed().copied(); if preset.is_none() { let field = NumberField::new(tl!("Resolution:"), Bind::given(Some(ppi))) .range(1.0..=4608.0) @@ -274,7 +274,7 @@ fn color_choice( let color = (at == ids.len()).then(|| Srgba::from(Rgba::from_hex(¤t).unwrap_or(Rgba::rgb(128, 128, 128)))); let (mut picked, mut pick_color) = (None, false); let shown = Row::new().show(ui, |ui, _| { - picked = Select::new(tl!(label), Bind::given(at.max(start)), &entries).show(ui).value().copied(); + picked = Select::new(tl!(label), Bind::given(at.max(start)), &entries).show(ui).events.committed().copied(); if let Some(color) = color { pick_color = Swatch::new(tl!(label), color).show(ui).clicked; } diff --git a/crates/ui-egui/src/dialogs/revolve.rs b/crates/ui-egui/src/dialogs/revolve.rs index 2d9c252ef3..6eab0d5404 100644 --- a/crates/ui-egui/src/dialogs/revolve.rs +++ b/crates/ui-egui/src/dialogs/revolve.rs @@ -130,14 +130,15 @@ fn set_value(fields: &mut DialogFields, key: &str, value: &Value) { } } -/// A select over `titles`: the index picked this pass. `at` is the shown entry (none shows a placeholder). +/// A select over `titles`: the index chosen this pass, not one only highlighted. `at` is the shown entry (none shows a +/// placeholder). fn pick(ui: &mut egui::Ui, label: &str, titles: &[String], at: Option, default: Option) -> Option { let entries: Vec> = titles.iter().enumerate().map(|(i, t)| Entry::new(i, t.clone())).collect(); let mut bind = Bind::given(at.unwrap_or(usize::MAX)); if let Some(d) = default { bind = bind.reset(d); } - Select::new(label.to_owned(), bind, &entries).id_salt(label).show(ui).value().copied() + Select::new(label.to_owned(), bind, &entries).id_salt(label).show(ui).events.committed().copied() } /// A select row of JSON `options` (value, title) for `key`. diff --git a/crates/ui-egui/src/dialogs/tests_export.rs b/crates/ui-egui/src/dialogs/tests_export.rs index f11039d48a..1a1ce7db43 100644 --- a/crates/ui-egui/src/dialogs/tests_export.rs +++ b/crates/ui-egui/src/dialogs/tests_export.rs @@ -224,6 +224,47 @@ fn png_options_name_the_pixel_size_in_the_accessibility_tree() { assert_eq!(sizes(&mut app), ["120 × 80 px"]); } +/// PNG Options' Resolution and Background Color, and PNG-8 Options' Matte: an entry highlighted with the arrow keys +/// changes no field, Escape closes the list with the dialog open and its fields as they were, and Enter applies the +/// entry. +#[test] +fn a_highlighted_raster_option_applies_only_once_it_is_chosen() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + use vectorcraft_engine::cmd::fileio::FORMATS; + + use crate::tests::panel_harness::Panel; + + for (id, name) in [("png", "Resolution:"), ("png", "Background Color:"), ("png8", "Matte:")] { + let mut app = VectorcraftApp::new(Session::new(), Services::default()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let mut panel = Panel::new(app, crate::tests::picks::dialogs); + panel.room(egui::vec2(1280.0, 1100.0)); + let format = FORMATS.iter().find(|f| f.id == id).unwrap(); + super::png_options::open(&mut panel.app(), format, json!({"format": id, "path": format!("/out/a.{id}")})); + panel.frame(); + panel.frame(); + let fields = + |panel: &Panel| on_top(&panel.app(), |h, _, at| h.open_dialogs().into_iter().find(|d| d.id == at).and_then(|d| d.fields)).flatten(); + let before = fields(&panel); + assert!(before.is_some(), "{id} options are open"); + panel.click_role(name, Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(fields(&panel), before, "a highlighted {name} entry changes no field"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(fields(&panel), before, "Escape closes the {name} list with the fields as they were"); + panel.click_role(name, Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + let after = fields(&panel); + assert!(after.is_some(), "Enter in the {name} list keeps the dialog open"); + assert_ne!(after, before, "Enter applies the {name} entry"); + } +} + /// The role and name of each node of the window's accessibility tree, in a frame drawn once the dialogs have settled. fn access_names(app: &mut VectorcraftApp) -> Vec<(egui::accesskit::Role, String)> { super::tests::settle(app); diff --git a/crates/ui-egui/src/dialogs/tests_perspective.rs b/crates/ui-egui/src/dialogs/tests_perspective.rs index ce286be347..9f6d312b8f 100644 --- a/crates/ui-egui/src/dialogs/tests_perspective.rs +++ b/crates/ui-egui/src/dialogs/tests_perspective.rs @@ -223,6 +223,42 @@ fn enter(app: &mut VectorcraftApp) { frame(app); } +/// Define Grid's Type, Units and Scale: an entry highlighted with the arrow keys changes no field, Escape closes the +/// list with the dialog open and its fields as they were, and Enter applies the entry. +#[test] +fn a_highlighted_define_grid_choice_applies_only_once_it_is_chosen() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + + use crate::tests::panel_harness::Panel; + + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 800, "height": 600})).unwrap(); + let mut panel = Panel::new(app, crate::tests::picks::dialogs); + panel.room(egui::vec2(1280.0, 1100.0)); + panel.app().run("ui.perspectiveGridDialog", json!({})).unwrap(); + panel.frame(); + panel.frame(); + let fields = |panel: &Panel| host(&panel.app(), |h| h.open_dialogs().pop().and_then(|d| d.fields)); + for name in ["Type:", "Units:", "Scale:"] { + let before = fields(&panel); + panel.click_role(name, Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(fields(&panel), before, "a highlighted {name} entry changes no field"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(kind(&panel.app()).as_deref(), Some(super::perspective_grid::KIND), "Escape closes the list, not the dialog"); + assert_eq!(fields(&panel), before, "Escape leaves the fields as they were"); + panel.click_role(name, Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + assert_eq!(kind(&panel.app()).as_deref(), Some(super::perspective_grid::KIND), "Enter in the list keeps the dialog open"); + assert_ne!(fields(&panel), before, "Enter applies the {name} entry"); + } +} + #[test] fn enter_is_ok_in_define_grid_and_save_grid_as_preset() { let mut app = app(); diff --git a/crates/ui-egui/src/panels/separations.rs b/crates/ui-egui/src/panels/separations.rs index 96d558186a..691bd720fc 100644 --- a/crates/ui-egui/src/panels/separations.rs +++ b/crates/ui-egui/src/panels/separations.rs @@ -66,7 +66,7 @@ fn check(ui: &mut Ui, label: &'static str, on: bool, enabled: bool) -> Option( ui: &mut Ui, name: &str, @@ -82,7 +82,8 @@ fn pick( if caption { Text::new(label.clone()).show(ui); } - picked = Select::new(label.clone(), Bind::given(current.clone()), &entries).enabled(enabled).id_salt(name).show(ui).value().cloned(); + picked = + Select::new(label.clone(), Bind::given(current.clone()), &entries).enabled(enabled).id_salt(name).show(ui).events.committed().cloned(); }); picked } @@ -541,6 +542,45 @@ mod tests { assert_eq!(none.params(AssignChoice::Profile, None), None, "no other profile to pick"); } + /// A profile highlighted in the Assign rows' Profile list assigns nothing, Escape leaves the document as it was, + /// and the profile chosen with Enter is assigned in one undo step. + #[test] + fn a_highlighted_profile_is_assigned_only_once_it_is_chosen() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + + // Assigning a profile to the front document changes the process's active color settings. + if !crate::testing::in_own_process("panels::separations::tests::a_highlighted_profile_is_assigned_only_once_it_is_chosen") { + return; + } + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({})).unwrap(); + app.run("edit.assignProfile", json!({"rgb": null, "cmyk": null})).unwrap(); + let row = AssignRow::of(&app.session.active().unwrap().doc); + assert!(row.others.len() >= 2, "{row:?}"); + let mut panel = Panel::new(app, show); + let state = |panel: &Panel| { + let app = panel.app(); + let st = app.session.active().unwrap(); + (vectorcraft_engine::cmd::colormgmt::doc_profiles(&st.doc), st.history.undo.len()) + }; + let before = state(&panel); + panel.click_role("Profile:", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "a highlighted profile assigns nothing"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "Escape leaves the document's profiles as they were"); + panel.click_role("Profile:", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + let ((rgb, _), undo) = state(&panel); + assert_eq!(rgb.as_deref(), Some(row.others[1].as_str()), "Enter assigns the chosen profile"); + assert_eq!(undo, before.1 + 1, "the choice is one undo step"); + } + /// The Assign rows follow the document's color mode: an RGB document lists the RGB working space /// and the RGB profiles and assigns its RGB profile, a CMYK document the CMYK ones, and the other /// model's profile stays as it was. diff --git a/crates/ui-egui/src/panels/tests_three_d.rs b/crates/ui-egui/src/panels/tests_three_d.rs index c253ba9747..f6b92fd209 100644 --- a/crates/ui-egui/src/panels/tests_three_d.rs +++ b/crates/ui-egui/src/panels/tests_three_d.rs @@ -237,6 +237,39 @@ fn a_drag_previews_and_records_one_undo_step_when_released() { assert_eq!(undo_len(&app), history + 1); } +/// A bevel shape highlighted with the arrow keys leaves the effect as it was, Escape closes the list with nothing +/// changed, and the shape chosen with Enter applies in one undo step. +#[test] +fn a_highlighted_bevel_shape_applies_only_once_it_is_chosen() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + + use crate::tests::panel_harness::Panel; + + let mut app = app(); + set_type(&mut app, "extrude").unwrap(); + let mut panel = Panel::new(app, show); + let state = |panel: &Panel| { + let app = panel.app(); + (effect(&app).1, undo_len(&app)) + }; + let before = state(&panel); + panel.click_role("Bevel Shape", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "a highlighted bevel shape changes nothing"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "Escape leaves the effect as it was"); + panel.click_role("Bevel Shape", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + let (params, undo) = state(&panel); + assert_ne!(params["bevel"], before.0["bevel"], "Enter applies the chosen bevel shape"); + assert_eq!(undo, before.1 + 1, "the choice is one undo step"); +} + #[test] fn a_change_reaches_each_selected_3d_effect_and_no_other_effect() { let mut app = app(); diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs index 9a3c6c2139..be89cd34c1 100644 --- a/crates/ui-egui/src/toolbar.rs +++ b/crates/ui-egui/src/toolbar.rs @@ -645,7 +645,9 @@ pub fn control_bar_options(app: &mut VectorcraftApp, ui: &mut Ui) { let entries: Vec> = choices.iter().map(|&(value, name)| Entry::new(value, tl!(name))).collect(); let current = opts[key].as_str().and_then(|v| choices.iter().find(|(value, _)| *value == v)).map(|(value, _)| *value); let picked = Field::new(tl!(label)) - .show(ui, |ui, _| Select::new(tl!(label), Bind::given(current.map_or(Mixed::Mixed, Mixed::Same)), &entries).show(ui).value().copied()) + .show(ui, |ui, _| { + Select::new(tl!(label), Bind::given(current.map_or(Mixed::Mixed, Mixed::Same)), &entries).show(ui).events.committed().copied() + }) .inner .flatten(); if let Some(value) = picked { @@ -691,7 +693,7 @@ fn crop_options(app: &mut VectorcraftApp, ui: &mut Ui) { let label = |v: f64| crate::i18n::fmt(tl!("{n} ppi"), &[("n", &format!("{}", (v * 100.0).round() / 100.0))]); let choices: Vec> = PPI_CHOICES.iter().map(|&v| Entry::new(v, label(v)).enabled(v <= max + 1e-6)).collect(); Field::new(tl!("PPI")).show(ui, |ui, _| { - if let Some(v) = Select::new(tl!("PPI"), Bind::given(ppi), &choices).placeholder(label(ppi)).show(ui).value().copied() { + if let Some(v) = Select::new(tl!("PPI"), Bind::given(ppi), &choices).placeholder(label(ppi)).show(ui).events.committed().copied() { edits.push(("ppi", json!(v))); } // An image's own resolution above the limit (a linked image's, or one past 2400 ppi) still diff --git a/crates/ui-egui/src/toolbar/tests.rs b/crates/ui-egui/src/toolbar/tests.rs index 243cde1c7f..5671cbe62e 100644 --- a/crates/ui-egui/src/toolbar/tests.rs +++ b/crates/ui-egui/src/toolbar/tests.rs @@ -508,6 +508,60 @@ fn a_tool_card_shows_what_the_tool_does_and_what_its_keys_do() { assert!(!card::Cards::new(&app).has(&"selection".to_string()), "tool tips off: no card"); } +/// The Control bar's lists of the Mirror & Cut tool (Axis, Keep) and of Crop Image (PPI): an entry highlighted with the +/// arrow keys leaves the tool's options as they were, Escape closes the list with nothing changed, and the entry +/// chosen with Enter sets the option without touching the drawing. +#[test] +fn a_highlighted_control_bar_option_applies_only_once_it_is_chosen() { + use craft::ui::contract::Role as Kind; + use egui::{Key, Modifiers}; + + use crate::tests::panel_harness::Panel; + + let png = vectorcraft_testkit::temp_dir("toolbar-crop-ppi").join("red.png"); + let image = craft_image_codecs::Image::from_raw( + 40, + 40, + craft_image_codecs::ChannelLayout::Rgba, + craft_image_codecs::SampleType::U8, + [200u8, 40, 40, 255].repeat(40 * 40), + ) + .unwrap(); + let encoded = craft_image_codecs::encode(&image, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); + std::fs::write(&png, encoded).unwrap(); + for (tool, name, key) in [("mirrorCut", "Axis:", "axis"), ("mirrorCut", "Keep:", "keep"), (vectorcraft_tools::cropimage::ID, "PPI", "ppi")] { + let mut app = app_with(300, 300); + if tool == vectorcraft_tools::cropimage::ID { + app.run("file.place", json!({"path": png, "link": true, "at": [100.0, 100.0]})).unwrap(); + app.run("ui.cropImage", json!({})).unwrap(); + } else { + app.run("tool.select", json!({ "tool": tool })).unwrap(); + } + assert_eq!(app.session.tool_id(), tool); + let mut panel = Panel::new(app, control_bar_options); + let state = |panel: &Panel| { + let app = panel.app(); + let st = app.session.active().unwrap(); + (app.session.tool_options()[key].clone(), st.doc.clone(), st.history.undo.len()) + }; + let before = state(&panel); + panel.click_role(name, Kind::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "a highlighted {name} entry changes no option"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "Escape leaves {name} as it was"); + panel.click_role(name, Kind::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + let (option, doc, undo) = state(&panel); + assert_ne!(option, before.0, "Enter sets {name}"); + assert_eq!((doc, undo), (before.1, before.2), "{name} only changes the tool's options"); + } +} + #[test] fn the_rail_builds_its_slots_only_when_its_tool_groups_change() { let mut app = app_with(300, 300);