diff --git a/crates/ui-egui/src/dialogs/effect_rows.rs b/crates/ui-egui/src/dialogs/effect_rows.rs
index ef91055575..e8eeacd29c 100644
--- a/crates/ui-egui/src/dialogs/effect_rows.rs
+++ b/crates/ui-egui/src/dialogs/effect_rows.rs
@@ -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 {
.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::(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 {
}
});
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) {
}
}
+/// Whether an event leaves its control's gesture running.
+fn runs(event: Option<&EditEvent>) -> 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 {
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 = 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 {
.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 {
.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 {
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