diff --git a/crates/ui-egui/src/canvas/task_bar.rs b/crates/ui-egui/src/canvas/task_bar.rs index a1613565bb..56657ef670 100644 --- a/crates/ui-egui/src/canvas/task_bar.rs +++ b/crates/ui-egui/src/canvas/task_bar.rs @@ -464,10 +464,12 @@ pub(super) fn task_bar(app: &mut VectorcraftApp, ui: &mut Ui, space: &CanvasSpac .show(ui, space.view.rect, |ui, _| { for (label, icon, cmd, params) in &offer.actions { if *cmd == TASK_BAR_FIDELITY { - // The Smooth tool's Fidelity, labeled, sets the tool's option as it is typed or stepped. + // The Smooth tool's Fidelity, labeled, sets the tool's option once a typed or stepped value is + // committed; Escape leaves the option as it was. if let Some(v) = offer.fidelity { Text::new(*label).show(ui); - if let Some(x) = crate::dialogs::freehand::fidelity_field(ui, "task-bar-fidelity", v, "smooth").filter(|x| *x != v) { + let events = crate::dialogs::freehand::fidelity_field(ui, "task-bar-fidelity", v, "smooth"); + if let Some(x) = events.committed().and_then(|typed| typed.to_owned()).filter(|x| *x != v) { run = Some(("tool.setOption", json!({"tool": "smooth", "values": {"fidelity": x}}))); } } diff --git a/crates/ui-egui/src/dialogs/freehand.rs b/crates/ui-egui/src/dialogs/freehand.rs index 34d4e39fe5..7de36ba00e 100644 --- a/crates/ui-egui/src/dialogs/freehand.rs +++ b/crates/ui-egui/src/dialogs/freehand.rs @@ -19,6 +19,7 @@ // PLAN: the imports below assume `craft_ui` paths as the framework's own code uses them; `ui::brush_settings` // of the draw crate is where `TipEllipse` and `TipAngle` live. +use craft::behavior::EditEvents; use craft::draw::ui::brush_settings::{TipAngle, TipEllipse}; use craft::ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; use craft::ui::display::{Picture, PictureSize}; @@ -116,9 +117,10 @@ fn fidelity_round(v: f64) -> f64 { } /// The Fidelity field of tool `tool`, showing `v` to two decimals from the tool's lowest Fidelity to -/// 100 and stepping by [`fidelity_step`], named by the label drawn just before it. Returns the value -/// typed or stepped to. The Contextual Task Bar shows the Smooth tool's. -pub(crate) fn fidelity_field(ui: &mut Ui, id: &str, v: f64, tool: &str) -> Option { +/// 100 and stepping by [`fidelity_step`], named by the label drawn just before it. Returns the field's +/// edit events: the value typed or stepped to as a proposal, then its commit, or the cancel that +/// restores `v`. The Contextual Task Bar shows the Smooth tool's. +pub(crate) fn fidelity_field(ui: &mut Ui, id: &str, v: f64, tool: &str) -> EditEvents> { let min = vectorcraft_tools::draw2::min_fidelity(tool); NumberField::new(tl!("Fidelity"), Bind::given(Some(v))) .id_salt(id.to_owned()) @@ -128,8 +130,7 @@ pub(crate) fn fidelity_field(ui: &mut Ui, id: &str, v: f64, tool: &str) -> Optio .stepper(|v, dir, scale| fidelity_step(v, f64::from(dir) * scale)) .show(ui) .edit - .value() - .and_then(|typed| typed.to_owned()) + .events } /// The Fidelity row: a logarithmic slider from the tool's lowest Fidelity to [`FIDELITY_MAX`] with the field at its @@ -145,10 +146,17 @@ fn fidelity_row(ui: &mut Ui, fields: &mut DialogFields, tool: &str) -> Response .label(tl!("Fidelity")) .id_salt("fidelity-slider") .show(ui); - if let Some(x) = slid.value() { + // The draft follows a drag unrounded, as a rounded value would end the slider's gesture, and takes the rounded + // value once the drag ends. Escape puts back the value from before the drag or the typing, which the field + // allows above the slider's end. + if let Some(x) = slid.events.committed() { set = Some(fidelity_round(*x).clamp(min, FIDELITY_MAX)); + } else if let Some(x) = slid.events.value() { + set = Some(x.clamp(min, FIDELITY_MAX)); + } else if let Some(x) = slid.events.landed() { + set = Some(*x); } - if let Some(x) = fidelity_field(ui, "fidelity-field", v, tool) { + if let Some(x) = fidelity_field(ui, "fidelity-field", v, tool).landed().and_then(|typed| typed.to_owned()) { set = Some(x); } }); diff --git a/crates/ui-egui/src/dialogs/freehand/tests.rs b/crates/ui-egui/src/dialogs/freehand/tests.rs index d9e4e32dfe..790d5742e1 100644 --- a/crates/ui-egui/src/dialogs/freehand/tests.rs +++ b/crates/ui-egui/src/dialogs/freehand/tests.rs @@ -416,3 +416,76 @@ fn the_blob_brush_size_offers_the_stylus_wheel() { let text = shown_text(&mut app); assert!(text.contains("Stylus Wheel"), "it shows again on the next open: {text}"); } + +/// The Fidelity the open Tool Options dialog holds, `None` once it is closed. +fn draft_fidelity(panel: &crate::tests::panel_harness::Panel) -> Option { + panel.app().dialogs().and_then(|d| d.with(|host| host.get::().and_then(|dialog| dialog.fields().number("fidelity")))) +} + +/// The Smooth tool's Fidelity slider and field: a drag or a typed value that Escape cancels leaves the dialog's Fidelity +/// and the tool's as they were; a finished drag or a typed value left with Tab sets the dialog's, and OK sets the tool's. +#[test] +fn a_canceled_fidelity_drag_or_typed_value_leaves_the_dialog_and_the_tool() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers, vec2}; + let mut app = app(); + app.run("tool.setOption", json!({"tool": "smooth", "values": {"fidelity": 2.5}})).unwrap(); + let original = app.session.tool_options_of("smooth"); + let document = app.session.active().unwrap().doc.clone(); + let mut panel = crate::tests::panel_harness::Panel::new(app, crate::tests::picks::dialogs); + panel.room(vec2(1000.0, 1200.0)); + crate::toolbar::open_options(&mut panel.app(), "smooth").unwrap(); + panel.frame(); + assert_eq!(draft_fidelity(&panel), Some(2.5)); + + // On the track, above the Accurate and Smooth marks. + let track = |panel: &crate::tests::panel_harness::Panel| { + let r = panel.rect_role("Fidelity", Role::SLIDER); + egui::pos2(r.center().x, r.top() + 5.0) + }; + let from = track(&panel); + panel.drag_hold_between(from, from + vec2(60.0, 0.0)); + assert_ne!(draft_fidelity(&panel), Some(2.5), "the dialog's Fidelity follows the drag"); + panel.key(Key::Escape, Modifiers::NONE); + panel.release(); + assert_eq!(draft_fidelity(&panel), Some(2.5), "Escape puts back the Fidelity from before the drag"); + + panel.click_role("Fidelity", Role::SPIN_BUTTON); + panel.key(Key::A, Modifiers::COMMAND); + panel.type_text("7"); + panel.key(Key::Escape, Modifiers::NONE); + assert_eq!(panel.app().session.tool_options_of("smooth"), original, "the canceled typed value leaves the tool"); + if draft_fidelity(&panel).is_none() { + crate::toolbar::open_options(&mut panel.app(), "smooth").unwrap(); + panel.frame(); + } + assert_eq!(draft_fidelity(&panel), Some(2.5), "the dialog shows the Fidelity from before the typing"); + + let from = track(&panel); + panel.drag_hold_between(from, from + vec2(60.0, 0.0)); + panel.release(); + let dragged = draft_fidelity(&panel).unwrap(); + assert!(dragged > 2.5, "a finished drag sets the dialog's Fidelity: {dragged}"); + assert_eq!(dragged, fidelity_round(dragged), "to the slider's step"); + panel.click_role("Fidelity", Role::SPIN_BUTTON); + panel.key(Key::A, Modifiers::COMMAND); + panel.type_text("7"); + panel.key(Key::Tab, Modifiers::NONE); + assert_eq!(draft_fidelity(&panel), Some(7.0), "a typed value left with Tab sets the dialog's Fidelity"); + assert_eq!(panel.app().session.tool_options_of("smooth"), original, "the tool keeps its Fidelity until OK"); + panel.click("OK"); + panel.frame(); + assert_eq!(draft_fidelity(&panel), None, "OK closes the dialog"); + assert_eq!(panel.app().session.tool_options_of("smooth")["fidelity"].as_f64(), Some(7.0)); + assert_eq!(panel.app().session.active().unwrap().doc, document); + + panel.app().run("tool.setOption", json!({"tool": "smooth", "values": {"fidelity": 50}})).unwrap(); + crate::toolbar::open_options(&mut panel.app(), "smooth").unwrap(); + panel.frame(); + let from = track(&panel); + panel.drag_hold_between(from, from - vec2(60.0, 0.0)); + assert!(draft_fidelity(&panel).is_some_and(|x| x <= FIDELITY_MAX), "the drag runs along the slider"); + panel.key(Key::Escape, Modifiers::NONE); + panel.release(); + assert_eq!(draft_fidelity(&panel), Some(50.0), "Escape puts back a Fidelity above the slider's end"); +} diff --git a/crates/ui-egui/src/tests/taskbar.rs b/crates/ui-egui/src/tests/taskbar.rs index 3a56b8dad8..1995432740 100644 --- a/crates/ui-egui/src/tests/taskbar.rs +++ b/crates/ui-egui/src/tests/taskbar.rs @@ -1584,6 +1584,32 @@ fn the_smooth_tool_shows_fidelity_and_tool_options_for_a_path() { }); } +/// The Smooth tool's Fidelity field sets the tool's Fidelity only once a typed value is committed: while it is typed +/// and after Escape the tool keeps its Fidelity, and Enter sets it without an undo step. +#[test] +fn the_smooth_tools_fidelity_is_set_only_when_the_typed_value_is_committed() { + let mut app = app(); + let path = id(app.run("shape.ellipse", json!({"x": 60, "y": 60, "width": 120, "height": 80})).unwrap()); + select(&mut app, &[path]); + app.run("tool.select", json!({"tool": "smooth"})).unwrap(); + app.run("tool.setOption", json!({"tool": "smooth", "values": {"fidelity": 2.5}})).unwrap(); + let undo = app.session.active().unwrap().history.undo.len(); + on_canvas(&mut app, |rig| { + let field = rig.field_after("Fidelity:"); + rig.fx.click_at(field); + rig.fx.key(Key::A, Modifiers::COMMAND); + rig.fx.type_text("7"); + rig.settle(); + assert_eq!(rig.app().session.tool_options()["fidelity"].as_f64(), Some(2.5), "a value being typed leaves the tool"); + rig.fx.key(Key::Escape, Modifiers::NONE); + rig.settle(); + assert_eq!(rig.app().session.tool_options()["fidelity"].as_f64(), Some(2.5), "Escape leaves the tool's Fidelity"); + rig.enter_value(field, "7"); + }); + assert_eq!(app.session.tool_options()["fidelity"].as_f64(), Some(7.0), "Enter sets it"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); +} + /// The Smooth tool's "Fidelity:" label is a framework text in the bar: a label node in the access tree, drawn on one /// line with its baseline on the Fidelity field's. #[test]