Restore canceled brush option edits

This commit is contained in:
dotCooCoo committed 2026-11-26 22:25:09 -08:00
1 parent f566e5689f
commit cd76355164
3 files changed
+137 -9

No files matched your search

@@ -434,7 +434,7 @@ fn number_cell(ui: &mut Ui, f: &mut DialogFields, key: &str, label: &'static str
.enabled(enabled)
.id_salt(key)
.show(ui);
if let Some(Some(value)) = typed.edit.value() {
if let Some(Some(value)) = typed.edit.events.landed() {
f.set(key, *value);
}
typed.edit.response.clone()
@@ -448,7 +448,7 @@ fn clamped_number(key: &'static str, label: &'static str, range: (f64, f64), uni
/// A menu over `ids` (id, label) bound to the text field `key`, showing `current`.
fn id_select(ui: &mut Ui, f: &mut DialogFields, key: &str, label: &'static str, entries: &[Entry<String>], current: String) {
if let Some(picked) = Select::new(label, Bind::given(current), entries).id_salt(key).show(ui).value() {
if let Some(picked) = Select::new(label, Bind::given(current), entries).id_salt(key).show(ui).events.committed() {
f.set(key, picked.as_str());
}
}
@@ -501,8 +501,9 @@ fn art_rows(object: bool) -> Vec<Param> {
.show(ui, |ui, _| {
let slid =
Slider::new(Bind::given(clamped(f, "width", range)), range.0..=range.1).step(1.0).label(tl!("Width")).id_salt("width").show(ui);
if let Some(value) = slid.value() {
f.set("width", value.round());
if let Some(value) = slid.events.landed() {
// The slider quantizes new edits; Cancel restores the exact opening width.
f.set("width", *value);
}
number_cell(ui, f, "widthMax", tl!("Width"), range, UnitKind::Percent, mode.follows_pen());
id_select(ui, f, "widthMode", tl!("Width"), &entries, mode.id().to_string());
@@ -541,7 +542,7 @@ fn direction_row() -> Param {
let current = DIRECTIONS.iter().position(|d| *d == parse_field::<Direction>(f, "direction")).unwrap_or(0);
let entries: Vec<Entry<usize>> = arrows.iter().enumerate().map(|(i, (icon, tip))| Entry::new(i, *tip).icon(IconId::new(icon))).collect();
let shown = Segmented::new(tl!("Direction"), Bind::given(current), &entries).show(ui);
if let Some(dir) = shown.value().and_then(|i| DIRECTIONS.get(*i)) {
if let Some(dir) = shown.events.committed().and_then(|i| DIRECTIONS.get(*i)) {
f.set("direction", id_of(dir));
}
shown.response().clone()
+4 -4
View File
@@ -538,7 +538,7 @@ fn bristle_slider(key: &'static str, label: &'static str, range: (f64, f64)) ->
Param::custom(key, label, move |ui, d, _| {
let shown =
SliderField::new(label, Bind::given(clamped(d, key, range)), range.0..=range.1).layout(SliderFieldLayout::Inline).id_salt(key).show(ui);
if let Some(v) = shown.edit.value() {
if let Some(v) = shown.edit.events.landed() {
d.set(key, *v);
}
shown.edit.response.clone()
@@ -562,10 +562,10 @@ fn variation_row(key: &'static str, label: Label, suffix: &'static str, decimals
.fixed_decimals(decimals as _)
.id_salt("value")
.show(ui);
if let Some(Some(v)) = typed.edit.value() {
if let Some(Some(v)) = typed.edit.events.landed() {
edits.push((key.to_string(), FieldValue::Number(v.clamp(range.0, range.1))));
}
if let Some(picked) = Select::new(name.clone(), Bind::given(mode.clone()), &entries).id_salt("mode").show(ui).value() {
if let Some(picked) = Select::new(name.clone(), Bind::given(mode.clone()), &entries).id_salt("mode").show(ui).events.committed() {
edits.push((mode_id.clone(), FieldValue::Text(picked.clone())));
}
Field::new(tl!("Variation:")).id_salt("variation").show(ui, |ui, _| {
@@ -576,7 +576,7 @@ fn variation_row(key: &'static str, label: Label, suffix: &'static str, decimals
.enabled(mode != Variation::Fixed.id())
.id_salt("amount")
.show(ui);
if let Some(Some(v)) = typed.edit.value() {
if let Some(Some(v)) = typed.edit.events.landed() {
edits.push((variation_id.clone(), FieldValue::Number(v.clamp(0.0, max))));
}
});
@@ -87,6 +87,133 @@ fn escape(app: &mut VectorcraftApp) {
frame(app, vec![key(false)]);
}
fn draw_hosted_options(app: &mut VectorcraftApp, ui: &mut egui::Ui) {
if let Some(dialogs) = app.dialogs() {
dialogs.with(|host| host.run(ui));
}
super::super::host::pump(app, ui.ctx());
}
#[test]
fn canceled_custom_numbers_and_variation_choices_do_not_reach_the_brush() {
use crate::tests::panel_harness::Panel;
use craft::ui::contract::Role;
use egui::{Key, Modifiers};
for (brush, key, label, typed, value, pointer) in [
("6 pt. Flat", "angle", "Angle:", "30", 30.0, "/angle"),
("Bristle Round", "density", "Bristle Density:", "88", 88.0, "/density"),
("Confetti", "hueJitter", "Hue Jitter", "30", 30.0, "/hue_jitter"),
] {
let mut app = app();
app.run("brush.options", json!({"name": brush, "params": {}})).unwrap();
let original = app.run("brush.get", json!({"name": brush})).unwrap();
let undo = app.session.active().unwrap().history.undo.len();
let mut panel = Panel::new(app, draw_hosted_options);
panel.room(egui::vec2(1000.0, 1000.0));
open(&mut panel.app(), brush);
panel.frame();
let initial = fields_of(&panel.app()).number(key).unwrap();
panel.click_role(label, Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text(typed);
assert_eq!(fields_of(&panel.app()).number(key), Some(value), "typing previews the {brush} field");
panel.key(Key::Escape, Modifiers::NONE);
assert!(!is_open(&panel.app()));
assert_eq!(panel.app().run("brush.get", json!({"name": brush})).unwrap(), original);
open(&mut panel.app(), brush);
panel.frame();
assert_eq!(fields_of(&panel.app()).number(key), Some(initial));
if key == "angle" {
let mode = fields_of(&panel.app()).text("angleMode").unwrap().to_owned();
panel.click_role(label, Role::COMBO_BOX);
panel.key(Key::ArrowDown, Modifiers::NONE);
panel.key(Key::Escape, Modifiers::NONE);
assert_eq!(fields_of(&panel.app()).text("angleMode"), Some(mode.as_str()));
}
panel.click("Cancel");
panel.frame();
assert!(!is_open(&panel.app()));
assert_eq!(panel.app().run("brush.get", json!({"name": brush})).unwrap(), original);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo);
open(&mut panel.app(), brush);
panel.frame();
panel.click_role(label, Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text(typed);
panel.key(Key::Tab, Modifiers::NONE);
panel.click("Cancel");
assert_eq!(panel.app().run("brush.get", json!({"name": brush})).unwrap(), original);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo);
open(&mut panel.app(), brush);
panel.frame();
panel.click_role(label, Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text(typed);
panel.key(Key::Tab, Modifiers::NONE);
if key == "angle" {
panel.click_role(label, Role::COMBO_BOX);
panel.key(Key::ArrowDown, Modifiers::NONE);
panel.key(Key::Enter, Modifiers::NONE);
assert_eq!(fields_of(&panel.app()).text("angleMode"), Some("random"));
}
panel.click("OK");
panel.frame();
let after = panel.app().run("brush.get", json!({"name": brush})).unwrap();
assert_eq!(after.pointer(pointer).unwrap(), value);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo + 1);
panel.app().run("edit.undo", json!({})).unwrap();
assert_eq!(panel.app().run("brush.get", json!({"name": brush})).unwrap(), original);
}
}
#[test]
fn canceled_art_width_drag_restores_the_opening_width() {
use crate::tests::panel_harness::Panel;
use craft::ui::contract::Role;
use egui::{Key, Modifiers};
let mut app = app();
app.run("brush.options", json!({"name": "Arrow", "params": {"width": 100.25}})).unwrap();
let original = app.run("brush.get", json!({"name": "Arrow"})).unwrap();
let undo = app.session.active().unwrap().history.undo.len();
let mut panel = Panel::new(app, draw_hosted_options);
panel.room(egui::vec2(1000.0, 1000.0));
open(&mut panel.app(), "Arrow");
panel.frame();
let initial = fields_of(&panel.app()).number("width").unwrap();
assert_eq!(initial, 100.25, "Cancel must preserve a valid fractional stored width");
let mode = fields_of(&panel.app()).text("widthMode").map(str::to_owned);
panel.click_role("Width", Role::COMBO_BOX);
panel.key(Key::ArrowDown, Modifiers::NONE);
panel.key(Key::Escape, Modifiers::NONE);
assert_eq!(fields_of(&panel.app()).text("widthMode").map(str::to_owned), mode);
panel.drag_hold_role("Width", Role::SLIDER, egui::vec2(40.0, 0.0));
assert_ne!(fields_of(&panel.app()).number("width"), Some(initial), "the held drag previews another width");
panel.key(Key::Escape, Modifiers::NONE);
panel.release();
assert_eq!(fields_of(&panel.app()).number("width"), Some(initial));
panel.click("Cancel");
panel.frame();
assert_eq!(panel.app().run("brush.get", json!({"name": "Arrow"})).unwrap(), original);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo);
open(&mut panel.app(), "Arrow");
panel.frame();
panel.drag_hold_role("Width", Role::SLIDER, egui::vec2(40.0, 0.0));
panel.release();
let accepted = fields_of(&panel.app()).number("width").unwrap();
assert_ne!(accepted, initial);
panel.click("OK");
panel.frame();
assert_eq!(panel.app().run("brush.get", json!({"name": "Arrow"})).unwrap()["width"], accepted);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), undo + 1);
panel.app().run("edit.undo", json!({})).unwrap();
assert_eq!(panel.app().run("brush.get", json!({"name": "Arrow"})).unwrap(), original);
}
/// What a step of the table expects at a JSON pointer of the saved brush.
enum Expect {
Is(Value),