UI: a highlighted list entry applies only once it is chosen

Nine list reads took the entry the arrow keys highlighted as a choice:
Fold Preview's View and Inside, Define Grid's lists, the raster export
options' Resolution, Background Color and Matte, the 3D and Materials
rows' lists, the Separations Preview panel's lists, and the Control
bar's Mirror & Cut Axis and Keep and Crop Image PPI. A highlighted entry
applied at once, and Escape left it applied. These reads now take only
the entry chosen with Enter or a click, as Corner and Shared Edge do
since #9773.
This commit is contained in:
dotCooCoo committed 2026-11-28 11:18:28 -08:00
1 parent fe6bf49232
commit 13dfdc720a
11 files changed
+254 -12

No files matched your search

@@ -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);
@@ -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");
}
@@ -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<T: craft::behavior::Value>(ui: &mut egui::Ui, name: &str, salt: &str, entries: &[Entry<T>], 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);
}
});
+2 -2
View File
@@ -223,7 +223,7 @@ fn resolution_row() -> Param {
let entries: Vec<Entry<usize>> = 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(&current).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;
}
+3 -2
View File
@@ -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<usize>, default: Option<usize>) -> Option<usize> {
let entries: Vec<Entry<usize>> = 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`.
@@ -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);
@@ -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();
+42 -2
View File
@@ -66,7 +66,7 @@ fn check(ui: &mut Ui, label: &'static str, on: bool, enabled: bool) -> Option<bo
}
/// A select named `name` (shown before it when `caption`) over `items` of `(value, shown name)`; the
/// value the user picked this pass.
/// value the user chose this pass, not one only highlighted.
fn pick<T: Clone + PartialEq + Send + Sync + 'static>(
ui: &mut Ui,
name: &str,
@@ -82,7 +82,8 @@ fn pick<T: Clone + PartialEq + Send + Sync + 'static>(
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.
@@ -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();
+4 -2
View File
@@ -599,7 +599,9 @@ pub fn control_bar_options(app: &mut VectorcraftApp, ui: &mut Ui) {
let entries: Vec<Entry<&'static str>> = 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 {
@@ -645,7 +647,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<Entry<f64>> = 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
+54
View File
@@ -473,3 +473,57 @@ fn a_tool_card_shows_what_the_tool_does_and_what_its_keys_do() {
app.run("prefs.set", json!({"key": "showToolTips", "value": false})).unwrap();
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");
}
}