Effect dialogs: a canceled Settings, Texture or Blur Center edit puts back the value from before

Scribble's Settings menu, the Texture menu of Glass, Texturizer and Rough
Pastels, and Radial Blur's Blur Center box and Horizontal and Vertical
fields read their control's latest proposed value, so a value the arrow
keys highlighted in an open list, or a drag or typing that previewed a
change, stayed in the dialog and the live effect after Escape or another
cancel. These rows now read `events.landed()`, as the Adjust rows do.

- Settings keeps the rows from the start of the open list and puts them
  back on a cancel, since Custom rows match no preset that could refill
  them.
- Blur Center holds the exact value a drag or typing proposes while it
  runs and rounds only a committed value. A rounded value ended the
  point's drag after its first move, and a cancel now restores a stored
  center unrounded.
This commit is contained in:
dotCooCoo committed 2026-11-28 02:41:24 -08:00
1 parent b6f01852b7
commit cab7f29505
1 file changed
+221 -8
+221 -8
View File
@@ -5,7 +5,7 @@
use std::ops::RangeInclusive;
use craft::behavior::Mixed;
use craft::behavior::{EditEvent, Mixed};
use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _};
use craft::ui::layout_components::{Row, Stack};
use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamKind};
@@ -146,7 +146,23 @@ pub(super) fn scribble(cx: &EffectCx, fields: &DialogFields) -> Vec<Param> {
.chain([Entry::new("custom".to_owned(), lbl!("Custom"))])
.collect();
let picked = Select::new(lbl!("Settings"), Bind::given(scribble_setting(fields).to_owned()), &entries).show(ui);
if let Some(id) = picked.value().cloned()
// The rows as the gesture found them, which a cancel puts back; Custom rows match no preset that could refill them.
let start = picked.response.id.with("rows before");
let saved = picked.events.ended().is_some().then(|| ui.data_mut(|d| d.remove_temp::<DialogFields>(start))).flatten();
if picked.events.began() {
let rows = fields.clone();
ui.data_mut(|d| d.insert_temp(start, rows));
}
if let Some(before) = saved.filter(|_| picked.events.cancel_reason().is_some()) {
for key in presets.first().and_then(|p| p.params.as_object()).into_iter().flat_map(|rows| rows.keys()) {
match before.get(key) {
Some(value) => fields.set(key.as_str(), value.clone()),
None => {
fields.remove(key);
}
}
}
} else if let Some(id) = picked.events.landed().cloned()
&& let Some(preset) = presets.iter().find(|p| p.id == id).and_then(|p| p.params.as_object())
{
for (key, value) in preset {
@@ -212,7 +228,7 @@ pub(super) fn textured(cx: &EffectCx, fields: &DialogFields) -> Vec<Param> {
}
});
let Some(picked) = picked else { return shown.output };
if let Some(id) = picked.value().cloned() {
if let Some(id) = picked.events.landed().cloned() {
fields.set("texture", id);
}
// The pick is an edit of the rows, which the Appearance panel's editor applies.
@@ -252,6 +268,11 @@ pub(super) fn clamp_blur_center(params: &mut Map<String, Value>) {
}
}
/// Whether an event leaves its control's gesture running.
fn runs<T>(event: Option<&EditEvent<T>>) -> bool {
matches!(event, Some(EditEvent::Begin { .. } | EditEvent::Update { .. }))
}
/// Paints Radial Blur's pattern about the centre: rays for Zoom, rings for Spin.
fn blur_pattern(cx: &ContentPainterCx, rect: Rect, center: [f64; 2], zoom: bool) {
let at = pos2(rect.left() + center[0] as f32 * rect.width(), rect.top() + center[1] as f32 * rect.height());
@@ -276,6 +297,7 @@ pub(super) fn radial_blur(cx: &EffectCx, fields: &DialogFields) -> Vec<Param> {
let c = [fields.number("centerX").unwrap_or(0.5).clamp(0.0, 1.0), fields.number("centerY").unwrap_or(0.5).clamp(0.0, 1.0)];
let zoom = fields.text("method").is_some_and(|m| m.eq_ignore_ascii_case("zoom"));
let mut next = c;
let (mut running, mut committed) = (false, false);
let unit: RangeInclusive<f64> = 0.0..=1.0;
let row = Row::new().show(ui, |ui, _| {
let picker = PointPicker::new(Bind::given([Mixed::Same(c[0]), Mixed::Same(c[1])]), unit.clone(), unit.clone())
@@ -283,7 +305,9 @@ pub(super) fn radial_blur(cx: &EffectCx, fields: &DialogFields) -> Vec<Param> {
.id_salt("point")
.backdrop(move |cx: &ContentPainterCx, rect: Rect| blur_pattern(cx, rect, c, zoom))
.show(ui);
if let Some([Mixed::Same(x), Mixed::Same(y)]) = picker.value().cloned() {
running |= runs(picker.events.last());
committed |= picker.events.committed().is_some();
if let Some([Mixed::Same(x), Mixed::Same(y)]) = picker.events.landed().cloned() {
next = [x, y];
}
Stack::new().show(ui, |ui, _| {
@@ -294,15 +318,21 @@ pub(super) fn radial_blur(cx: &EffectCx, fields: &DialogFields) -> Vec<Param> {
.commit_decimals(1)
.id_salt(axis)
.show(ui);
if let Some(v) = shown.edit.value().cloned().flatten() {
running |= runs(shown.edit.events.last());
committed |= shown.edit.events.committed().is_some();
if let Some(v) = shown.edit.events.landed().cloned().flatten() {
next[axis] = v / 100.0;
}
}
});
});
if next != c {
fields.set("centerX", center_round(next[0].clamp(0.0, 1.0)));
fields.set("centerY", center_round(next[1].clamp(0.0, 1.0)));
// Only a committed value is rounded. While a drag or typing runs, the rows hold the exact value it proposes (the
// control takes a rounded value for a change made outside it and ends the gesture), and a cancel puts back the
// exact value from before.
let settle = |v: f64| if committed && !running { center_round(v.clamp(0.0, 1.0)) } else { v.clamp(0.0, 1.0) };
if next != c || committed {
fields.set("centerX", settle(next[0]));
fields.set("centerY", settle(next[1]));
}
row.output
})
@@ -310,3 +340,186 @@ pub(super) fn radial_blur(cx: &EffectCx, fields: &DialogFields) -> Vec<Param> {
let rows = generic(cx, fields).into_iter().filter(|p| !matches!(p.key.as_str(), "centerX" | "centerY"));
rows.chain([center]).collect()
}
#[cfg(test)]
mod tests {
use super::super::effect_params::{RADIAL_BLUR, SCRIBBLE, TEXTURIZER};
use super::super::tests_warp::{json_fields, set};
use crate::tests::panel_harness::Panel;
use craft::ui::contract::Role;
use craft::ui::dialog::DialogId;
use egui::{Key, Modifiers, Ui};
use serde_json::{Value, json};
use vectorcraft_doc::NodeId;
fn draw(app: &mut crate::VectorcraftApp, ui: &mut Ui) {
if let Some(dialogs) = app.dialogs() {
dialogs.with(|host| host.run(ui));
}
super::super::host::pump(app, ui.ctx());
}
/// The dialog of `effect` opened live over a new rectangle, with the rectangle's id.
fn open(effect: &str) -> (Panel, DialogId, NodeId) {
let mut app = crate::testing::windowed(crate::VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()));
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = NodeId(app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 60, "height": 40})).unwrap()["id"].as_u64().unwrap());
let mut panel = Panel::new(app, draw);
panel.room(egui::vec2(900.0, 800.0));
panel.app().run("effect.dialog", json!({"effect": effect, "item": null})).unwrap();
panel.frame();
let dialog = panel.app().effect_dialogs.effect.as_ref().unwrap().id;
(panel, dialog, id)
}
/// The parameters of the live `effect` on the node `id`.
fn live(panel: &Panel, id: NodeId, effect: &str) -> Value {
let app = panel.app();
let node = app.session.active().unwrap().doc.node(id).unwrap().clone();
node.appearance.effects.iter().find(|e| e.id == effect).map(|e| e.params.clone()).unwrap_or(Value::Null)
}
fn still_open(panel: &Panel, dialog: DialogId) -> bool {
panel.app().effect_dialogs.effect.as_ref().is_some_and(|d| d.id == dialog)
}
#[test]
fn texture_escape_restores_the_texture_the_keys_highlighted() {
let (mut panel, dialog, id) = open(TEXTURIZER);
let original = json_fields(&panel.app(), dialog)["texture"].clone();
panel.click_role("Texture", Role::COMBO_BOX);
panel.key(Key::ArrowDown, Modifiers::NONE);
let highlighted = json_fields(&panel.app(), dialog)["texture"].clone();
assert_ne!(highlighted, original, "the key highlight previews the next texture");
panel.key(Key::Escape, Modifiers::NONE);
assert!(still_open(&panel, dialog), "Escape closes the list and leaves the dialog open");
assert_eq!(json_fields(&panel.app(), dialog)["texture"], original, "Escape restores the texture");
assert_eq!(live(&panel, id, TEXTURIZER)["texture"], original, "Escape restores the live effect's texture");
panel.click_role("Texture", Role::COMBO_BOX);
panel.key(Key::ArrowDown, Modifiers::NONE);
panel.key(Key::Enter, Modifiers::NONE);
assert!(still_open(&panel, dialog));
assert_eq!(json_fields(&panel.app(), dialog)["texture"], highlighted, "Enter keeps the picked texture");
}
/// The Scribble rows that a Settings preset fills.
fn scribble_rows(fields: &Value) -> Vec<Option<f64>> {
["angle", "overlap", "overlapVariation", "strokeWidth", "curviness", "curvinessVariation", "spacing", "spacingVariation"]
.iter()
.map(|key| fields[*key].as_f64())
.collect()
}
#[test]
fn scribble_settings_escape_restores_the_rows_the_keys_highlighted() {
let (mut panel, dialog, id) = open(SCRIBBLE);
let original = scribble_rows(&json_fields(&panel.app(), dialog));
panel.click_role("Settings", Role::COMBO_BOX);
panel.key(Key::ArrowDown, Modifiers::NONE);
let highlighted = scribble_rows(&json_fields(&panel.app(), dialog));
assert_ne!(highlighted, original, "the key highlight previews the next preset's rows");
panel.key(Key::Escape, Modifiers::NONE);
assert!(still_open(&panel, dialog), "Escape closes the list and leaves the dialog open");
assert_eq!(scribble_rows(&json_fields(&panel.app(), dialog)), original, "Escape restores the preset's rows");
assert_eq!(scribble_rows(&live(&panel, id, SCRIBBLE)), original, "Escape restores the live effect");
panel.click_role("Settings", Role::COMBO_BOX);
panel.key(Key::ArrowDown, Modifiers::NONE);
panel.key(Key::Enter, Modifiers::NONE);
assert!(still_open(&panel, dialog));
assert_eq!(scribble_rows(&json_fields(&panel.app(), dialog)), highlighted, "Enter keeps the picked preset's rows");
}
#[test]
fn scribble_settings_escape_restores_custom_rows() {
let (mut panel, dialog, id) = open(SCRIBBLE);
set(&panel.app(), dialog, "angle", json!(37.0));
panel.frame();
let original = scribble_rows(&json_fields(&panel.app(), dialog));
assert_eq!(original[0], Some(37.0));
panel.click_role("Settings", Role::COMBO_BOX);
panel.key(Key::ArrowUp, Modifiers::NONE);
assert_ne!(scribble_rows(&json_fields(&panel.app(), dialog)), original, "the key highlight previews a preset's rows");
panel.key(Key::Escape, Modifiers::NONE);
assert!(still_open(&panel, dialog), "Escape closes the list and leaves the dialog open");
assert_eq!(scribble_rows(&json_fields(&panel.app(), dialog)), original, "Escape restores the custom rows");
assert_eq!(scribble_rows(&live(&panel, id, SCRIBBLE)), original, "Escape restores the live effect");
}
fn center(fields: &Value) -> (Value, Value) {
(fields["centerX"].clone(), fields["centerY"].clone())
}
#[test]
fn blur_center_escape_restores_the_center_during_a_held_drag() {
let (mut panel, dialog, id) = open(RADIAL_BLUR);
// A click on the well puts the point there, so the drags below start on the point.
panel.click_role("Blur Center", Role::GROUP);
let original = center(&json_fields(&panel.app(), dialog));
panel.drag_hold_role("Blur Center", Role::GROUP, egui::vec2(20.0, 15.0));
assert_ne!(center(&json_fields(&panel.app(), dialog)), original, "a held drag previews the new center");
panel.key(Key::Escape, Modifiers::NONE);
panel.release();
assert!(still_open(&panel, dialog), "Escape ends the drag and leaves the dialog open");
assert_eq!(center(&json_fields(&panel.app(), dialog)), original, "Escape restores the center");
assert_eq!(center(&live(&panel, id, RADIAL_BLUR)), original, "Escape restores the live effect's center");
panel.drag_hold_role("Blur Center", Role::GROUP, egui::vec2(20.0, 15.0));
let (x, y) = center(&json_fields(&panel.app(), dialog));
panel.release();
let dragged = (json!(super::center_round(x.as_f64().unwrap())), json!(super::center_round(y.as_f64().unwrap())));
assert_ne!(dragged, original);
assert_eq!(center(&json_fields(&panel.app(), dialog)), dragged, "releasing the drag keeps the new center, rounded");
}
#[test]
fn blur_center_cancel_restores_a_stored_center_without_rounding_it() {
let mut app = crate::testing::windowed(crate::VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()));
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
let id = NodeId(app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 60, "height": 40})).unwrap()["id"].as_u64().unwrap());
app.run("effect.apply", json!({"effect": RADIAL_BLUR, "params": {"centerX": 0.123456, "centerY": 0.654321}})).unwrap();
let mut panel = Panel::new(app, draw);
panel.room(egui::vec2(900.0, 800.0));
panel.app().run("effect.dialog", json!({"effect": RADIAL_BLUR, "index": 0})).unwrap();
panel.frame();
let dialog = panel.app().effect_dialogs.effect.as_ref().unwrap().id;
let original = center(&json_fields(&panel.app(), dialog));
assert_eq!(original, (json!(0.123456), json!(0.654321)));
panel.click_role("Horizontal", Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text("80");
assert_eq!(json_fields(&panel.app(), dialog)["centerX"], json!(0.8), "typing previews the new center");
panel.type_text("q");
panel.key(Key::Tab, Modifiers::NONE);
assert!(still_open(&panel, dialog));
assert_eq!(center(&json_fields(&panel.app(), dialog)), original, "the cancel restores the stored center exactly");
assert_eq!(center(&live(&panel, id, RADIAL_BLUR)), original);
}
#[test]
fn blur_center_fields_leaving_unreadable_text_restores_the_center_typing_previewed() {
for (label, key) in [("Horizontal", "centerX"), ("Vertical", "centerY")] {
let (mut panel, dialog, id) = open(RADIAL_BLUR);
let original = json_fields(&panel.app(), dialog)[key].clone();
let shown = panel.value_role(label, Role::SPIN_BUTTON);
panel.click_role(label, Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text("80");
assert_eq!(json_fields(&panel.app(), dialog)[key], json!(0.8), "{label}: typing previews the new center");
panel.type_text("q");
panel.key(Key::Tab, Modifiers::NONE);
assert!(still_open(&panel, dialog), "{label}: Tab leaves the field and the dialog open");
assert_eq!(panel.value_role(label, Role::SPIN_BUTTON), shown, "{label}: the field shows the center again");
assert_eq!(json_fields(&panel.app(), dialog)[key], original, "{label}: leaving unreadable text restores the center");
assert_eq!(live(&panel, id, RADIAL_BLUR)[key], original, "{label}: leaving unreadable text restores the live effect's center");
panel.click_role(label, Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text("80");
panel.key(Key::Tab, Modifiers::NONE);
assert!(still_open(&panel, dialog));
assert_eq!(json_fields(&panel.app(), dialog)[key], json!(0.8), "{label}: leaving the field keeps the typed center");
}
}
}