Merge pull request #9750 from dotCooCoo/fix/option-form-cancel-drafts-w4

Keep canceled tool option values out of the form
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-27 22:14:42 -08:00
commit a346c170ee
1 file changed
+105 -4
+105 -4
View File
@@ -76,7 +76,7 @@ pub(super) fn is_on(values: &Map<String, Value>, key: &str) -> bool {
/// A select of `labels` showing the entry at `current`; the entry picked this pass.
pub(super) fn pick(ui: &mut Ui, label: &str, current: usize, labels: &[&str]) -> Option<usize> {
let entries: Vec<Entry<usize>> = labels.iter().enumerate().map(|(at, text)| Entry::new(at, word(text))).collect();
Select::new(word(label), Bind::given(current), &entries).show(ui).value().copied()
Select::new(word(label), Bind::given(current), &entries).show(ui).events.committed().copied()
}
impl Rows<'_> {
@@ -108,7 +108,7 @@ impl Rows<'_> {
/// A check box over `values[key]`.
pub(super) fn check(&mut self, ui: &mut Ui, key: &str, label: &str, enabled: bool) {
let shown = Checkbox::new(Bind::given(self.flag(key))).label(word(label)).enabled(enabled).show(ui);
if let Some(on) = shown.value().copied() {
if let Some(on) = shown.events.landed().copied() {
self.set(key, json!(on));
}
}
@@ -142,7 +142,7 @@ impl Rows<'_> {
let current = self.float(key, *range.start());
let symbol = Env::of(ui).units().symbol(unit);
let shown = NumberField::new(word(label), Bind::given(Some(current))).range(range).unit_kind(unit).unit(symbol).enabled(enabled).show(ui);
if let Some(Some(number)) = shown.value().copied() {
if let Some(Some(number)) = shown.edit.events.landed().copied() {
self.set(key, json!(number));
}
}
@@ -151,7 +151,7 @@ impl Rows<'_> {
pub(super) fn length(&mut self, ui: &mut Ui, key: &str, label: &str, range: RangeInclusive<f64>, enabled: bool) {
let current = self.values.get(key).and_then(Value::as_f64);
let shown = NumberField::length(word(label), Bind::given(current), self.lengths).range(range).enabled(enabled).show(ui);
if let Some(Some(length)) = shown.value().copied() {
if let Some(length) = shown.edit.events.landed().copied() {
self.set(key, json!(length));
}
}
@@ -396,6 +396,9 @@ fn accept(app: &mut VectorcraftApp, spec: &OptionSpec, answer: &Map<String, Valu
#[cfg(test)]
pub(super) mod tests {
use crate::tests::panel_harness::Panel;
use craft::ui::contract::Role;
use egui::{Key, Modifiers};
use vectorcraft_engine::Session;
use super::*;
@@ -456,6 +459,104 @@ pub(super) mod tests {
pump_all(app);
}
#[test]
fn cancelling_a_shape_builder_gap_preset_keeps_the_custom_length_on_ok() {
let mut app = app();
app.run("tool.setOption", json!({"tool": "shapeBuilder", "values": {"gapDetection": true, "gapLength": 17}})).unwrap();
let original = app.session.tool_options_of("shapeBuilder");
let document = app.session.active().unwrap().doc.clone();
let undo = app.session.active().unwrap().history.undo.len();
let mut panel = Panel::new(app, crate::tests::picks::dialogs);
panel.room(egui::vec2(1000.0, 1000.0));
crate::toolbar::open_options(&mut panel.app(), "shapeBuilder").unwrap();
panel.frame();
let before = fields(&panel.app());
panel.click_role("Gap Length", Role::COMBO_BOX);
// Custom is the last entry: Up highlights the preceding Large preset.
panel.key(Key::ArrowUp, Modifiers::NONE);
panel.key(Key::Escape, Modifiers::NONE);
assert_eq!(fields(&panel.app()), before, "the preset popup must not replace a custom numeric length");
assert_eq!(panel.app().session.tool_options_of("shapeBuilder"), original);
panel.click("OK");
panel.frame();
panel.frame();
assert!(!is_open(&panel.app()));
assert_eq!(panel.app().session.tool_options_of("shapeBuilder"), original);
assert_eq!(panel.app().session.active().unwrap().doc, document);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo);
}
#[test]
fn bucket_width_escape_restores_the_draft_and_only_ok_sets_accepted_typing() {
for commit in [false, true] {
let app = app();
let original = app.session.tool_options_of("livePaintBucket");
let document = app.session.active().unwrap().doc.clone();
let undo = app.session.active().unwrap().history.undo.len();
let mut panel = Panel::new(app, crate::tests::picks::dialogs);
panel.room(egui::vec2(900.0, 900.0));
crate::toolbar::open_options(&mut panel.app(), "livePaintBucket").unwrap();
panel.frame();
let before = fields(&panel.app());
panel.click_role("Width", Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text("9");
panel.key(Key::Escape, Modifiers::NONE);
assert!(!is_open(&panel.app()), "Escape from the Width draft cancels the modal");
assert_eq!(panel.app().session.tool_options_of("livePaintBucket"), original);
crate::toolbar::open_options(&mut panel.app(), "livePaintBucket").unwrap();
panel.frame();
assert_eq!(fields(&panel.app()), before);
if commit {
panel.click_role("Width", Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text("9");
panel.key(Key::Tab, Modifiers::NONE);
assert_eq!(fields(&panel.app())["width"], 9.0);
}
assert_eq!(panel.app().session.tool_options_of("livePaintBucket"), original, "field edits remain draft values until OK");
assert_eq!(panel.app().session.active().unwrap().doc, document);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo);
panel.click("OK");
panel.frame();
panel.frame();
assert!(!is_open(&panel.app()));
assert_eq!(panel.app().session.tool_options_of("livePaintBucket")["width"], if commit { 9.0 } else { 4.0 });
assert_eq!(panel.app().session.active().unwrap().doc, document);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo);
}
}
/// Normal Gap Options supplies numeric points. This hosted row fixture separately checks
/// the nullable length helper's contract, including restoring an empty opening value.
#[test]
fn a_nullable_length_row_restores_null_on_escape() {
static NULL_LENGTH: OptionSpec = OptionSpec {
key: "nullableLengthTest",
title: || "Nullable Length".into(),
keys: &["length"],
colors: &[],
preview: false,
body: |ui, rows| rows.length(ui, "length", "Length", 0.0..=72.0, true),
inactive: |_, _| false,
run: |_, _, _| Ok(Value::Null),
cancelled: None,
};
let mut panel = Panel::new(app(), crate::tests::picks::dialogs);
panel.room(egui::vec2(900.0, 900.0));
open(&mut panel.app(), &NULL_LENGTH, Map::from_iter([("length".into(), Value::Null)]), Value::Null).unwrap();
panel.frame();
panel.click_role("Length", Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text("9 pt");
assert_eq!(fields(&panel.app())["length"], 9.0, "the row still supports live proposals");
panel.key(Key::Escape, Modifiers::NONE);
assert!(!is_open(&panel.app()));
open(&mut panel.app(), &NULL_LENGTH, Map::from_iter([("length".into(), Value::Null)]), Value::Null).unwrap();
panel.frame();
assert_eq!(fields(&panel.app())["length"], Value::Null);
}
/// Whether `a` and `b` are the same value, numbers by their value.
fn same(a: &Value, b: &Value) -> bool {
match (a.as_f64(), b.as_f64()) {