diff --git a/crates/engine/src/cmd/inline.rs b/crates/engine/src/cmd/inline.rs index db3e929952..bb4e4f2e8e 100644 --- a/crates/engine/src/cmd/inline.rs +++ b/crates/engine/src/cmd/inline.rs @@ -46,18 +46,22 @@ fn insert_inline(s: &mut Session, p: &Value) -> Result { }; let len = edit::runs_len(&t.runs); let from_tool = editing == Some(id); - let (a, b) = match p.get("at") { - None | Some(Value::Null) if from_tool => { - let g = |k: &str| tool.get(k).and_then(Value::as_u64).map_or(len, |v| (v as usize).min(len)); - let (x, y) = (g("start"), g("end")); - (x.min(y), x.max(y)) - } - None | Some(Value::Null) => (len, len), - Some(v) => { - let at = v.as_u64().ok_or_else(|| bad(C, "`at` must be a byte offset"))?.min(len as u64) as usize; - (at, at) - } + // Where the graphic goes in text `len` bytes long. + let range = |len: usize| -> Result<(usize, usize)> { + Ok(match p.get("at") { + None | Some(Value::Null) if from_tool => { + let g = |k: &str| tool.get(k).and_then(Value::as_u64).map_or(len, |v| (v as usize).min(len)); + let (x, y) = (g("start"), g("end")); + (x.min(y), x.max(y)) + } + None | Some(Value::Null) => (len, len), + Some(v) => { + let at = v.as_u64().ok_or_else(|| bad(C, "`at` must be a byte offset"))?.min(len as u64) as usize; + (at, at) + } + }) }; + let (mut a, mut b) = range(len)?; let symbol = match str_param(p, "symbol") { Some(n) if doc.symbols.iter().any(|s| s.name == n) => n.to_string(), Some(n) => return Err(bad(C, format!("no symbol named `{n}`"))), @@ -75,11 +79,16 @@ fn insert_inline(s: &mut Session, p: &Value) -> Result { None | Some(Value::Null) => 0.0, Some(v) => v.as_f64().filter(|k| k.is_finite() && k.abs() <= 1e5).ok_or_else(|| bad(C, "`shift` must be a number of points"))?, }; - // A typing session ends first: the insertion is a step of its own. + // A typing session ends first: the insertion is a step of its own. Typing that Real-time + // Drawing off left unapplied is applied first, and the caret is read in the typed text. let typing = tool.get("typing").and_then(Value::as_bool) == Some(true); if typing && from_tool { s.set_tool_option("commitTyping", &Value::Bool(true)); - s.commit_interaction()?; + super::settle_interaction(s)?; + let NodeKind::Text(t) = &s.doc()?.doc.node(id).ok_or(EngineError::NoNode(id))?.kind else { + return Err(bad(C, format!("node {} is not text", id.0))); + }; + (a, b) = range(edit::runs_len(&t.runs))?; } let caret = s.edit("Insert Inline Symbol", |d, _| { let mut art = InlineArt { scale, baseline_shift: shift, ..InlineArt::new(symbol.clone()) }; diff --git a/crates/engine/src/cmd/textedit.rs b/crates/engine/src/cmd/textedit.rs index 0fd7c7d97d..b4a5c9a6e1 100644 --- a/crates/engine/src/cmd/textedit.rs +++ b/crates/engine/src/cmd/textedit.rs @@ -1808,13 +1808,14 @@ enum Step { BaselineShift, } -/// The Type tool's selected text while it edits (its typing session ends first: one undo step). +/// The Type tool's selected text while it edits (its typing session ends first: one undo step, +/// with typing that Real-time Drawing off left unapplied applied first). pub(crate) fn editing_range(s: &mut Session) -> Result> { let o = s.tool_options(); let Some(id) = o.get("editing").and_then(Value::as_u64).map(NodeId) else { return Ok(None) }; if super::edit::typing_in_progress(s) { s.set_tool_option("commitTyping", &Value::Bool(true)); - s.commit_interaction()?; + super::edit::settle_interaction(s)?; } let at = |k: &str| byte_param(&o, k).unwrap_or(0); Ok(Some((id, at("start"), at("end")))) diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index 73977f7d64..9679bbe2a0 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -255,6 +255,21 @@ impl Session { (r, settled) } + /// [`Session::begin_interaction`] for a dialog or a panel that starts a preview → the warnings + /// of the preview that keeping typing in progress applied, for its status bar. + pub fn begin_interaction_warned(&mut self, label: &str) -> Result> { + let (r, settled) = self.collect_settled_warnings(|s| s.begin_interaction(label)); + r.map(|()| settled) + } + + /// Keep typing in progress as one undo step, applying the preview Real-time Drawing off left + /// unapplied, and end the Type tool's typing session → the applied preview's warnings. Nothing + /// changes when no typing is in progress. + pub fn keep_typing(&mut self) -> Result> { + let (r, settled) = self.collect_settled_warnings(|s| if cmd::typing_in_progress(s) { cmd::settle_interaction(s) } else { Ok(()) }); + r.map(|()| settled) + } + pub fn begin_interaction(&mut self, label: &str) -> Result<()> { if self.script_running() { return Err(EngineError::Busy); diff --git a/crates/engine/src/tests/inline.rs b/crates/engine/src/tests/inline.rs index f7c72956ad..4e815ddb94 100644 --- a/crates/engine/src/tests/inline.rs +++ b/crates/engine/src/tests/inline.rs @@ -142,6 +142,31 @@ fn type_tool_inserts_at_its_caret_and_deletes_the_graphic_whole() { assert_eq!(inline_count(&obj(&s, id)), 1); } +/// With Performance › Real-time Drawing and Editing off, the typing waits for its session to end: +/// a graphic inserted while typing applies it first, as a step of its own, and goes in at the +/// caret after it. +#[test] +fn inserting_while_deferred_typing_keeps_the_typing() { + let mut s = session(); + s.prefs.real_time_drawing = false; + // The new type starts empty: the caret after "Pay " is past the end of the text until the + // typing is applied. + s.prefs.placeholder_text = false; + circle_symbol(&mut s, "Mana", 10.0); + s.execute("symbol.setCurrent", &json!({"name": "Mana"})).unwrap(); + let v = ViewInfo::default(); + s.select_tool("type", v).unwrap(); + for k in [PointerKind::Down, PointerKind::Up] { + s.pointer(&PointerEvent::new(k, 50.0, 300.0), v).unwrap(); + } + let id = s.doc().unwrap().selection.objects[0]; + s.tool_text("Pay ", v).unwrap(); + s.execute("text.insertInline", &json!({})).unwrap(); + assert_eq!(obj(&s, id).plain_text(), format!("Pay {INLINE_CHAR}")); + let labels: Vec = s.doc().unwrap().history.undo.iter().map(|e| e.label.clone()).collect(); + assert_eq!(labels[labels.len() - 2..], ["Typing", "Insert Inline Symbol"], "{labels:?}"); +} + #[test] fn inline_graphics_follow_their_symbol() { let mut s = session(); diff --git a/crates/engine/src/tests/prefs.rs b/crates/engine/src/tests/prefs.rs index d88b8250a2..42809b6953 100644 --- a/crates/engine/src/tests/prefs.rs +++ b/crates/engine/src/tests/prefs.rs @@ -2586,6 +2586,35 @@ fn settling_typing_for_a_tool_shows_its_preview_warning() { s.pointer(&PointerEvent::new(PointerKind::Up, 50.0, 380.0).with_mods(cmd), ViewInfo::default()).unwrap(); } +/// Typing kept as its own step when a dialog or a panel begins its preview, or when the font menus +/// end the typing session, returns its applied preview's warning for the status bar. A preview +/// without a warning returns none, and with no typing in progress nothing is kept and no step is +/// added. +#[test] +fn settling_typing_for_a_dialog_returns_its_preview_warning() { + let warned = |w: &[String]| w.iter().any(|m| m.contains("type")); + let steps = |s: &Session| s.doc().unwrap().history.undo.len(); + let mut s = deferred_bloat("Typing", true, "selection"); + let before = steps(&s); + let w = s.begin_interaction_warned("Offset Path").unwrap(); + assert!(warned(&w), "{w:?}"); + assert_eq!(steps(&s), before + 1, "the typing is a step of its own"); + assert_eq!(s.doc().unwrap().interaction.as_ref().map(|it| it.label.as_str()), Some("Offset Path")); + + let mut s = deferred_bloat("Typing", true, "selection"); + let w = s.keep_typing().unwrap(); + assert!(warned(&w), "{w:?}"); + assert!(!s.in_interaction()); + assert_eq!(steps(&s), before + 1); + let w = s.keep_typing().unwrap(); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(steps(&s), before + 1, "nothing in progress adds no step"); + + let mut s = deferred_bloat("Typing", false, "selection"); + let w = s.begin_interaction_warned("Offset Path").unwrap(); + assert!(w.is_empty(), "{w:?}"); +} + /// File Handling › Use Low Resolution Proxy for Linked EPS (#394): off by default; toggling it does /// not replace full-resolution pixels already in the document. #[test] diff --git a/crates/engine/src/tests/textedit.rs b/crates/engine/src/tests/textedit.rs index adcc35d352..79f0503d1e 100644 --- a/crates/engine/src/tests/textedit.rs +++ b/crates/engine/src/tests/textedit.rs @@ -3038,6 +3038,41 @@ fn a_previewed_transform_while_typing_keeps_the_typing() { assert!((left(&s, id) - 400.0).abs() < 1e-6); } +/// With Performance › Real-time Drawing and Editing off, the typing is applied when its session +/// ends. Ending it for another edit applies it first, as a step of its own: a dialog or a panel +/// beginning its preview, the font menus ([`Session::keep_typing`]), a paint for the selected +/// characters. +#[test] +fn ending_deferred_typing_for_another_edit_keeps_the_typing() { + let typed = |end: &dyn Fn(&mut Session)| { + let mut s = session(); + s.prefs.real_time_drawing = false; + let v = ViewInfo::default(); + let id = text(&mut s, "Hello"); + s.select_tool("type", v).unwrap(); + let b = s.doc().unwrap().doc.bounds_of(&[id], false).unwrap(); + click(&mut s, b.x1 - 1.0, b.center().y); + s.tool_text(" world", v).unwrap(); + assert_eq!(obj(&s, id).plain_text(), "Hello", "the typing waits for its session to end"); + let steps = undo_labels(&s).len(); + end(&mut s); + assert_eq!(obj(&s, id).plain_text(), "Hello world"); + assert_eq!(undo_labels(&s)[steps], "Typing", "{:?}", undo_labels(&s)); + }; + typed(&|s| { + assert!(s.begin_interaction_warned("Fill Color").unwrap().is_empty()); + s.preview("paint.setFill", &json!({"color": "#00a000"})).unwrap(); + s.commit_interaction().unwrap(); + }); + typed(&|s| { + assert!(s.keep_typing().unwrap().is_empty()); + assert!(!s.in_interaction()); + }); + typed(&|s| { + s.execute("paint.setFill", &json!({"color": "#00a000"})).unwrap(); + }); +} + /// #1950: a colour previewed while typing (the Color Picker, a Color panel slider) colours the /// typed text: its interaction used to join the typing session and preview from the document /// before the typing, so the new type came back as its placeholder text. diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 50c7b1be30..ff2b5017bc 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -458,4 +458,30 @@ impl VectorcraftApp { pub fn status(&mut self, s: impl Into) { self.ui.status = s.into(); } + + /// Begin the interaction a dialog or a panel previews on. Typing in progress is kept as its own + /// undo step first, and the warning of the preview that keeping it applied shows in the status + /// bar, as it does when a tool's drag begins. On an error no interaction begins, so the caller + /// runs no preview. + pub(crate) fn begin_interaction(&mut self, label: &str) -> Result<(), String> { + let warnings = self.session.begin_interaction_warned(label).map_err(|e| e.to_string())?; + self.show_warnings(warnings); + Ok(()) + } + + /// Keep typing in progress as one undo step and end the Type tool's typing session, with the + /// typing that Real-time Drawing off left unapplied applied first; its warning shows in the + /// status bar. + pub(crate) fn keep_typing(&mut self) -> Result<(), String> { + let warnings = self.session.keep_typing().map_err(|e| e.to_string())?; + self.show_warnings(warnings); + Ok(()) + } + + /// Each warning in the status bar in turn, as the tool's status requests show; the last stays. + fn show_warnings(&mut self, warnings: Vec) { + for w in warnings { + self.status(w); + } + } } diff --git a/crates/ui-egui/src/dialogs/brush_replace_art.rs b/crates/ui-egui/src/dialogs/brush_replace_art.rs index cc1a08b251..f1ebf1083f 100644 --- a/crates/ui-egui/src/dialogs/brush_replace_art.rs +++ b/crates/ui-egui/src/dialogs/brush_replace_art.rs @@ -44,7 +44,7 @@ pub fn replace(app: &mut VectorcraftApp, name: &str, ids: &[u64]) { return; } // A preview on a copy of the document checks the art without changing anything. - if !app.session.in_interaction() && app.session.begin_interaction("Replace Brush Art").is_ok() { + if !app.session.in_interaction() && app.begin_interaction("Replace Brush Art").is_ok() { let checked = app.session.preview("brush.replaceArt", ¶ms); // The interaction began above, on the active document: ending it puts that document back // as it was, and can fail only when no document is open, when there is nothing to put back. diff --git a/crates/ui-egui/src/dialogs/bullets_numbering.rs b/crates/ui-egui/src/dialogs/bullets_numbering.rs index 43f15edbb8..5dd09dd8f3 100644 --- a/crates/ui-egui/src/dialogs/bullets_numbering.rs +++ b/crates/ui-egui/src/dialogs/bullets_numbering.rs @@ -85,8 +85,7 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { crate::panels::character::text_style(app).ok_or("select some type first")?; // While typing, the dialog acts on the paragraphs of the text being edited. let mut targets = json!({}); - if let Some((id, a, b)) = crate::panels::character::text_editing(app) { - crate::panels::character::end_typing(app); + if let Some((id, a, b)) = crate::panels::character::text_editing_kept(app) { targets = json!({"ids": [id.0], "start": a, "end": b}); } let mut values = Map::new(); diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index c4ef1a984d..9ce3085917 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -482,13 +482,18 @@ fn preview(app: &mut VectorcraftApp, d: &mut Dialog) { if unchanged || !selected { return; } - let _ = app.session.begin_interaction(if artboard { + let label = if artboard { "Artboard Options" } else if d.bool("stroke") { "Stroke Color" } else { "Fill Color" - }); + }; + // A failure to begin (typing that can't be kept) shows, and nothing previews. + if let Err(e) = app.begin_interaction(label) { + app.status(e); + return; + } if let Err(e) = app.session.preview(cmd, ¶ms) { app.status(e.to_string()); } diff --git a/crates/ui-egui/src/dialogs/effect.rs b/crates/ui-egui/src/dialogs/effect.rs index 777c872494..b5e3f5a174 100644 --- a/crates/ui-egui/src/dialogs/effect.rs +++ b/crates/ui-egui/src/dialogs/effect.rs @@ -41,7 +41,7 @@ fn three_d_refusal(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog, } else if !app.session.in_interaction() && d.fields.get(TRIED) != Some(¶ms) { // Waits for another interaction to end: trying inside it would replace its preview and // cancel it. A failure to begin leaves no interaction: the preview below then reports it. - let _ = app.session.begin_interaction(&d.str("__label")); + let _ = app.begin_interaction(&d.str("__label")); match app.session.preview(cmd, ¶ms) { Ok(_) => { d.fields.remove(form::REFUSED); @@ -253,9 +253,14 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { d.fields.insert("preview".into(), json!(pv)); if pv && (changed || pv_changed || !app.session.in_interaction()) { let label = d.str("__label"); - let _ = app.session.begin_interaction(&label); - let (cmd, p) = command(d); - let _ = app.session.preview(cmd, &p); + // A failure to begin (typing that can't be kept) shows, and nothing previews. + match app.begin_interaction(&label) { + Ok(()) => { + let (cmd, p) = command(d); + let _ = app.session.preview(cmd, &p); + } + Err(e) => app.status(e), + } } else if !pv && pv_changed { let _ = app.session.cancel_interaction(); } diff --git a/crates/ui-egui/src/dialogs/effect_gallery.rs b/crates/ui-egui/src/dialogs/effect_gallery.rs index 03c862171d..9b9851fb2e 100644 --- a/crates/ui-egui/src/dialogs/effect_gallery.rs +++ b/crates/ui-egui/src/dialogs/effect_gallery.rs @@ -215,11 +215,20 @@ fn stack_batch_params(stack: &[StackEntry]) -> Value { } /// Live canvas preview of the stack (`command.batch` would settle the dialog interaction each frame). +/// Typing in progress is kept as its own undo step first; only the gallery's own earlier preview is +/// canceled. fn preview_stack_on_canvas(app: &mut VectorcraftApp, stack: &[StackEntry]) { + if let Err(e) = app.keep_typing() { + app.status(e); + return; + } if app.session.in_interaction() { let _ = app.session.cancel_interaction(); } - let _ = app.session.begin_interaction("Effect Gallery"); + if let Err(e) = app.begin_interaction("Effect Gallery") { + app.status(e); + return; + } for (i, entry) in stack.iter().enumerate() { let params = Value::Object(entry.params.clone()); if app.run("effect.apply", json!({ "effect": entry.effect, "params": params })).is_err() { @@ -666,6 +675,39 @@ mod tests { assert!(app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects.is_empty()); } + /// With Real-time Drawing off, typing in progress when the stack previews is kept as a step of + /// its own, not canceled with the gallery's earlier preview. + #[test] + fn stack_preview_keeps_typing_in_progress() { + use vectorcraft_tools::{PointerEvent, PointerKind}; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + app.run("prefs.set", json!({"key": "realTimeDrawing", "value": false})).unwrap(); + let id = app.run("text.create", json!({"x": 100, "y": 100, "text": "Hello", "size": 20})).unwrap()["id"].as_u64().unwrap(); + let id = vectorcraft_doc::NodeId(id); + let v = vectorcraft_engine::ViewInfo::default(); + app.session.select_tool("type", v).unwrap(); + let b = app.session.active().unwrap().doc.bounds_of(&[id], false).unwrap(); + for k in [PointerKind::Down, PointerKind::Up] { + app.session.pointer(&PointerEvent::new(k, b.x1 - 1.0, b.center().y), v).unwrap(); + } + app.session.tool_text(" world", v).unwrap(); + let plain = |app: &VectorcraftApp| match &app.session.active().unwrap().doc.node(id).unwrap().kind { + vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), + _ => panic!("not text"), + }; + assert_eq!(plain(&app), "Hello", "the typing waits"); + let steps = app.session.active().unwrap().history.undo.len(); + app.run("ui.effectGalleryDialog", json!({})).unwrap(); + let d = app.ui.dialog.as_mut().unwrap(); + d.fields.insert("stack".into(), json!([{ "effect": "artistic.cutout", "visible": true, "params": { "levels": 3 } }])); + frame(&mut app); + let st = app.session.active().unwrap(); + assert_eq!(st.interaction.as_ref().map(|it| it.label.as_str()), Some("Effect Gallery")); + assert_eq!(st.history.undo.len(), steps + 1, "the typing is a step of its own"); + assert_eq!(plain(&app), "Hello world"); + } + #[test] fn stack_ok_is_one_undo_step() { let mut app = VectorcraftApp::new(Session::new(), Default::default()); diff --git a/crates/ui-egui/src/dialogs/form.rs b/crates/ui-egui/src/dialogs/form.rs index 033e194e50..45f73b12cb 100644 --- a/crates/ui-egui/src/dialogs/form.rs +++ b/crates/ui-egui/src/dialogs/form.rs @@ -1040,7 +1040,12 @@ fn preview_or_rollback(app: &mut crate::VectorcraftApp, d: &mut Dialog, label: & pub(super) fn preview_now(app: &mut crate::VectorcraftApp, d: &mut Dialog, label: &str, cmd: &str, params: Value) -> Option { let mut result = None; if d.fields.get(PREVIEWED) != Some(¶ms) || !app.session.in_interaction() { - let _ = app.session.begin_interaction(label); + // A failure to begin (typing that can't be kept) shows, and nothing previews on the + // interaction still in progress. + if let Err(e) = app.begin_interaction(label) { + app.status(e); + return None; + } match app.session.preview(cmd, ¶ms) { Ok(r) => { d.fields.remove(REFUSED); @@ -1167,6 +1172,98 @@ mod plugin_field_tests { } } +#[cfg(test)] +mod settled_typing_tests { + use super::*; + use vectorcraft_engine::{Session, ViewInfo}; + use vectorcraft_tools::{PointerEvent, PointerKind}; + + /// An app with Real-time Drawing off and " world" typed after "Hello", not yet applied → the + /// type's id. + fn typing(app: &mut crate::VectorcraftApp) -> u64 { + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + app.run("prefs.set", json!({"key": "realTimeDrawing", "value": false})).unwrap(); + let id = app.run("text.create", json!({"x": 100, "y": 100, "text": "Hello", "size": 20})).unwrap()["id"].as_u64().unwrap(); + let v = ViewInfo::default(); + app.session.select_tool("type", v).unwrap(); + let b = app.session.active().unwrap().doc.bounds_of(&[vectorcraft_doc::NodeId(id)], false).unwrap(); + for k in [PointerKind::Down, PointerKind::Up] { + app.session.pointer(&PointerEvent::new(k, b.x1 - 1.0, b.center().y), v).unwrap(); + } + app.session.tool_text(" world", v).unwrap(); + id + } + + fn plain(app: &crate::VectorcraftApp, id: u64) -> String { + match &app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind { + vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), + _ => panic!("not text"), + } + } + + /// A dialog's preview while typing is in progress keeps the typing as its own step and starts + /// from it; the status bar shows the warning of the typing's preview that keeping it applied. + #[test] + fn a_dialog_preview_keeps_the_typing_and_shows_its_warning() { + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + let id = typing(&mut app); + assert_eq!(plain(&app, id), "Hello", "the typing waits"); + let steps = app.session.active().unwrap().history.undo.len(); + let mut d = Dialog::new("test", json!({})); + assert!(preview_now(&mut app, &mut d, "Fill Color", "paint.setFill", json!({"color": "#00a000"})).is_some()); + assert_eq!(plain(&app, id), "Hello world"); + let st = app.session.active().unwrap(); + assert_eq!(st.history.undo.len(), steps + 1, "the typing is a step of its own"); + assert_eq!(st.interaction.as_ref().map(|it| it.label.as_str()), Some("Fill Color")); + + // The typing's deferred preview warns that type is skipped (as Bloat does with type selected). + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + let rect = app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 200, "height": 200})).unwrap(); + let text = app.run("text.create", json!({"x": 300, "y": 200, "text": "Hi"})).unwrap(); + app.run("select.set", json!({"ids": [rect["id"], text["id"]]})).unwrap(); + app.run("prefs.set", json!({"key": "realTimeDrawing", "value": false})).unwrap(); + app.session.begin_interaction("Typing").unwrap(); + let p = json!({"tool": "bloat", "points": [[290.0, 200.0]], "width": 200.0, "height": 200.0}); + app.session.doc_mut().unwrap().interaction.as_mut().unwrap().preview = Some(("object.liquify".into(), p)); + app.ui.status.clear(); + let mut d = Dialog::new("test", json!({})); + assert!(preview_now(&mut app, &mut d, "Fill Color", "paint.setFill", json!({"color": "#00a000"})).is_some()); + assert!(app.ui.status.contains("type"), "{:?}", app.ui.status); + } + + /// The font menus end the typing session before their preview: the typing Real-time Drawing + /// off left unapplied is kept as a step of its own. + #[test] + fn ending_typing_for_the_font_menus_keeps_the_typing() { + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + let id = typing(&mut app); + let steps = app.session.active().unwrap().history.undo.len(); + crate::panels::character::end_typing(&mut app); + assert_eq!(plain(&app, id), "Hello world"); + let st = app.session.active().unwrap(); + assert_eq!(st.history.undo.len(), steps + 1); + assert!(st.interaction.is_none()); + assert_ne!(app.session.tool_options().get("typing").and_then(Value::as_bool), Some(true), "the typing session is over"); + } + + /// A Character panel change keeps the typing first and acts on the characters the Type tool + /// selected in the typed text, not on a selection cut short to the text before the typing. + #[test] + fn a_character_change_acts_on_characters_selected_in_the_typing() { + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + let id = typing(&mut app); + app.session.set_tool_option("select", &json!({"start": 6, "end": 11})); + crate::panels::character::range_style(&mut app, json!({"underline": true})); + assert_eq!(plain(&app, id), "Hello world"); + let vectorcraft_doc::NodeKind::Text(t) = &app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind else { + panic!("not text") + }; + let underlined: String = t.runs.iter().filter(|r| r.style.underline).map(|r| r.text.as_str()).collect(); + assert_eq!(underlined, "world"); + } +} + #[cfg(test)] mod typed_number_tests { use super::*; diff --git a/crates/ui-egui/src/dialogs/justification.rs b/crates/ui-egui/src/dialogs/justification.rs index dc42dcff87..be997d7c75 100644 --- a/crates/ui-egui/src/dialogs/justification.rs +++ b/crates/ui-egui/src/dialogs/justification.rs @@ -90,8 +90,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result { // While typing, the dialog acts on the paragraphs of the text being edited. let mut targets = json!({}); let mut story = None; - if let Some((id, a, b)) = crate::panels::character::text_editing(app) { - crate::panels::character::end_typing(app); + if let Some((id, a, b)) = crate::panels::character::text_editing_kept(app) { targets = json!({"ids": [id.0], "start": a, "end": b}); // After Select All in threaded text: every paragraph of every frame ([`params`]). story = crate::panels::character::text_story(app).map(|_| id.0); diff --git a/crates/ui-egui/src/dialogs/simplify_slider.rs b/crates/ui-egui/src/dialogs/simplify_slider.rs index 88838585ee..4257c15177 100644 --- a/crates/ui-egui/src/dialogs/simplify_slider.rs +++ b/crates/ui-egui/src/dialogs/simplify_slider.rs @@ -87,7 +87,7 @@ impl Strip { /// The open strip `d` moves to the level `cmd {auto: true}` picks for the selection, and the /// canvas previews it → {level}. pub fn auto(&self, app: &mut VectorcraftApp, mut d: Dialog) -> Result { - let _ = app.session.begin_interaction(self.label); + app.begin_interaction(self.label)?; let r = app.session.preview(self.cmd, &json!({ "auto": true })).map_err(|e| e.to_string())?; let level = r["level"].as_f64().unwrap_or(self.default_level); d.fields.insert("level".into(), json!(level)); diff --git a/crates/ui-egui/src/dialogs/swatch_options.rs b/crates/ui-egui/src/dialogs/swatch_options.rs index 9583b893ef..913a9126a8 100644 --- a/crates/ui-egui/src/dialogs/swatch_options.rs +++ b/crates/ui-egui/src/dialogs/swatch_options.rs @@ -123,8 +123,10 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { d.fields.insert("preview".into(), json!(pv)); } if pv && (changed || pv_changed || !app.session.in_interaction()) { - let _ = app.session.begin_interaction("Swatch Options"); - if let Err(e) = app.session.preview("swatch.edit", &edit_params(d)) { + // A failure to begin (typing that can't be kept) shows, and nothing previews. + if let Err(e) = app.begin_interaction("Swatch Options") { + app.status(e); + } else if let Err(e) = app.session.preview("swatch.edit", &edit_params(d)) { app.status(e.to_string()); } } else if !pv && pv_changed { diff --git a/crates/ui-egui/src/font_menu.rs b/crates/ui-egui/src/font_menu.rs index 9b829114c2..fd78474f00 100644 --- a/crates/ui-egui/src/font_menu.rs +++ b/crates/ui-egui/src/font_menu.rs @@ -1698,7 +1698,8 @@ pub(crate) fn apply(app: &mut VectorcraftApp, ctx: &egui::Context, pick: FontPic FontPick::Preview(f, style) => { if !previewing { crate::panels::character::end_typing(app); - if app.session.begin_interaction("Font").is_err() { + if let Err(e) = app.begin_interaction("Font") { + app.status(e); return; } crate::panels::set_pstate(ctx, PREVIEWING, true); diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 71019385b6..f12a7f623a 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -478,12 +478,14 @@ pub(crate) fn live_run(app: &mut VectorcraftApp, label: &str, cmd: &str, params: match phase { Live::Idle => {} Live::Dragging | Live::Released => { - if app.session.begin_interaction(label).is_ok() { + if app.begin_interaction(label).is_ok() { if let Err(e) = app.session.preview(cmd, ¶ms) { app.ui.status = e.to_string(); } - if phase == Live::Released { - app.session.commit_interaction().ok(); + if phase == Live::Released + && let Err(e) = app.session.commit_interaction() + { + app.ui.status = e.to_string(); } app.sync_views(); } else if phase == Live::Released { diff --git a/crates/ui-egui/src/panels/character.rs b/crates/ui-egui/src/panels/character.rs index 0cb47e6542..dd6e230008 100644 --- a/crates/ui-egui/src/panels/character.rs +++ b/crates/ui-egui/src/panels/character.rs @@ -242,14 +242,13 @@ pub(crate) fn format(app: &mut VectorcraftApp, p: Value) { /// has selected (the whole text when nothing is selected) while it edits text, else /// `text.setFormat` on the selected stories ([`selected_stories`]). pub(crate) fn run_format(app: &mut VectorcraftApp, mut p: Value) -> Result { - let Some((id, a, b)) = text_editing(app) else { + let Some((id, a, b)) = text_editing_kept(app) else { target_selected_stories(app, &mut p); return app.run("text.setFormat", p); }; if let Some(first) = text_story(app).and_then(|story| story.first().copied()) { return story_style(app, first, p); } - end_typing(app); p["id"] = json!(id.0); if b > a { p["start"] = json!(a); @@ -300,7 +299,7 @@ fn char_cmd(app: &mut VectorcraftApp, cmd: &str, mut p: Value) { /// and Optical set no space (#5111); nowhere to set at an empty line's start. Else it is a /// character attribute like the others: the selected range, or all the text. fn set_kerning(app: &mut VectorcraftApp, value: Value) { - let Some((id, a, b)) = text_editing(app) else { + let Some((id, a, b)) = text_editing_kept(app) else { format(app, json!({"kerning": value})); return; }; @@ -310,7 +309,6 @@ fn set_kerning(app: &mut VectorcraftApp, value: Value) { } let Some(NodeKind::Text(t)) = app.session.active().and_then(|st| st.doc.node(id)).map(|n| &n.kind) else { return }; let Some((target, before)) = vectorcraft_text::edit::caret_kerning_target(&t.plain_text(), a) else { return }; - end_typing(app); let params = if before { json!({"id": id.0, "start": target.start, "end": target.end, "kerningBefore": value.as_f64().unwrap_or(0.0)}) } else { @@ -505,12 +503,11 @@ pub(crate) fn toggle_underline(app: &mut VectorcraftApp) -> Result a { @@ -534,6 +531,13 @@ pub(crate) fn text_editing(app: &VectorcraftApp) -> Option<(NodeId, usize, usize Some((id, g("start"), g("end"))) } +/// [`text_editing`] after [`end_typing`], for an edit of the text: the selection is read in the +/// text with the typing applied, which Real-time Drawing off leaves unapplied until then. +pub(crate) fn text_editing_kept(app: &mut VectorcraftApp) -> Option<(NodeId, usize, usize)> { + end_typing(app); + text_editing(app) +} + /// The frames of the threaded story whose whole text the Type tool has selected after Select All, /// in the order of the edited frame's thread as the document holds it now; `None` when its /// selection is in one type object. @@ -606,9 +610,15 @@ fn story_style(app: &mut VectorcraftApp, first: NodeId, mut p: Value) -> Result< app.run("text.setRangeStyle", p) } -/// Close the Type tool's typing session (one undo step) before another command edits the text. +/// Close the Type tool's typing session (one undo step) before another command edits the text. The +/// typing Real-time Drawing off left unapplied is applied first; when it can't be kept, the error +/// shows and the session goes on. pub(crate) fn end_typing(app: &mut VectorcraftApp) { if app.session.tool_options().get("typing").and_then(Value::as_bool) == Some(true) { + if let Err(e) = app.keep_typing() { + app.status(e); + return; + } app.session.set_tool_option("commitTyping", &json!(true)); app.session.commit_interaction().ok(); app.sync_views(); @@ -1196,9 +1206,8 @@ pub(crate) fn axis_sliders(app: &mut VectorcraftApp, ui: &mut Ui, face: &vectorc /// Set Variable Font settings `axes` on the text the panel acts on, live while a slider drags. fn axes_cmd(app: &mut VectorcraftApp, axes: Value, phase: widgets::Live) { - match text_editing(app) { + match text_editing_kept(app) { Some((id, a, b)) => { - end_typing(app); let mut q = json!({"id": id.0, "fontAxes": axes}); // After Select All in threaded text: every frame of the story. if let Some(first) = text_story(app).and_then(|story| story.first().copied()) { diff --git a/crates/ui-egui/src/panels/paragraph.rs b/crates/ui-egui/src/panels/paragraph.rs index e02d81e14c..232de0a65f 100644 --- a/crates/ui-egui/src/panels/paragraph.rs +++ b/crates/ui-egui/src/panels/paragraph.rs @@ -86,8 +86,7 @@ pub(crate) fn run_on_paragraphs(app: &mut VectorcraftApp, cmd: &str, p: Value) - /// [`run_on_paragraphs`], with the Selection tool on the selected type objects alone unless /// `stories`. fn run_on(app: &mut VectorcraftApp, cmd: &str, mut p: Value, stories: bool) -> Result { - if let Some((id, a, b)) = super::character::text_editing(app) { - super::character::end_typing(app); + if let Some((id, a, b)) = super::character::text_editing_kept(app) { match super::character::text_story(app) { // After Select All in threaded text: every paragraph of every frame. Some(story) => p["ids"] = json!(story.iter().map(|f| f.0).collect::>()), diff --git a/crates/ui-egui/src/panels/tabs.rs b/crates/ui-egui/src/panels/tabs.rs index 00814646c5..187ce21c42 100644 --- a/crates/ui-egui/src/panels/tabs.rs +++ b/crates/ui-egui/src/panels/tabs.rs @@ -423,8 +423,7 @@ fn stops_json(stops: &[TabStop]) -> Value { /// frame of the selected stories ([`super::character::selected_stories`]). Returns false when it /// fails. fn apply(app: &mut VectorcraftApp, stops: &[TabStop]) -> bool { - if let Some((id, a, b)) = super::character::text_editing(app) { - super::character::end_typing(app); + if let Some((id, a, b)) = super::character::text_editing_kept(app) { let p = match super::character::text_story(app) { // After Select All in threaded text: every paragraph of every frame. Some(story) => json!({"stops": stops_json(stops), "ids": story.iter().map(|f| f.0).collect::>()}), diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 7e36c7f318..d04b193f6d 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1981,6 +1981,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **The command sweep sends file and folder params into its temp folder by the command catalog:** the sweeps in `crates/engine/tests/command_sweep.rs` take each command's file and folder keys from the catalog (`cmd::location_keys`, the same key list the plug-in call rules use, less the keys that name part of the document in a command), read every key a params description names (`{path | name …}`, `{folder? (…)}` included) and its schema's params, and send every string under such a key, in arrays and in objects inside them (`paths: [{ids, path}]`), as a location in the temp folder, so a command added later is swept and checked without editing the sweep. `diffPath` counts as a file key, and Slice Options' `url` and Export for Screens' `folders` do not. Regressions: `sweep_cases_name_files_only_inside_the_temp_folder` and `sweep_cases_send_the_file_keys_of_the_command_catalog` in `command_sweep`, `location_keys_come_from_the_command_catalog` in `cmd::plugin_call`. Scores and hours unchanged. - **A drag ended by a command with Real-time Drawing off:** when Undo, Redo, a filter that calls commands or a plug-in command that calls commands ends a drag whose preview Real-time Drawing off has not applied yet, the preview is applied first and kept as an undo step of its own, as releasing the drag keeps it; a preview that fails cancels the drag. The filter or plug-in command is then given the objects where the drag left them. Regressions: `crates/engine/src/tests/plugins.rs`, `crates/engine/src/tests/prefs.rs`. Scores and hours unchanged. - **The warning of a preview applied when a command ends a drag:** a preview that Real-time Drawing off left unapplied, and that Undo, Clear History or another command applies before it runs, keeps its warning: the command's result carries it as `previewWarning`, and as `warning` when the command has none of its own, and the status bar shows it, as releasing the drag shows it. Typing kept as its own step when a tool's drag begins, or when Cmd lends the Selection tool, shows its applied preview's warning in the status bar too. Regressions: `settling_a_deferred_preview_keeps_its_warning` and `settling_typing_for_a_tool_shows_its_preview_warning` in `crates/engine/src/tests/prefs.rs`, `settled_warnings_join_the_result` in `crates/engine/src/session/execute.rs`. Scores and hours unchanged. +- **Typing kept when a dialog or a panel previews, with Real-time Drawing off:** a dialog's Preview, a panel's live slider, the Effect Gallery's preview, the font menus' preview, a paint for the characters the Type tool selected and an inline graphic inserted while typing first apply the typing that Real-time Drawing off left unapplied, as an undo step of its own, and the status bar shows that typing's preview warning. The Character, Paragraph and Tabs panels, Bullets and Numbering, Justification and the inline graphic then act on the characters the Type tool selected in the typed text. When the typing can't be kept, the status bar shows the error and the dialog's preview does not start. Regressions: `settling_typing_for_a_dialog_returns_its_preview_warning` in `crates/engine/src/tests/prefs.rs`, `ending_deferred_typing_for_another_edit_keeps_the_typing` in `crates/engine/src/tests/textedit.rs`, `inserting_while_deferred_typing_keeps_the_typing` in `crates/engine/src/tests/inline.rs`, `settled_typing_tests` in `crates/ui-egui/src/dialogs/form.rs`, `stack_preview_keeps_typing_in_progress` in `crates/ui-egui/src/dialogs/effect_gallery.rs`. Scores and hours unchanged. - **Plug-in progress, Cancel and log lines (G17):** at plug-in API level 2 a plug-in with the `progress` permission imports `env.vc_progress`, `env.vc_cancelled` and `env.vc_log`. A heavy filter's and an export's progress windows show its progress as a percentage bar, their Cancel reaches it through `vc_cancelled`, a light filter with `progress` and no `commandAllowlist` runs in the background with the same progress window when it runs at once or OK runs it with Preview off, and its log lines go to the app's log with its id, capped per line and per run. Installing checks every import and names the one it refuses. Regressions: `crates/plugins/src/host.rs`, `crates/plugins/tests/abi.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_fuzz.rs`, `crates/ui-egui/src/tests/plugin_runs.rs`. Scores and hours unchanged. - **Live-effect plug-ins paint their output (G17, #121):** a live effect whose manifest has `"api": 2` and `"paintOutput": true` returns pieces, each drawn by itself in order with fills and strokes of its own in the filter's paint forms (a piece without them keeps the object's; an empty list paints none; an effect on a fill returns only fills, one on a stroke only strokes). The whole result is checked first: limits, numbers in range with nothing clamped, known keys only, and the swatches and patterns it names in the document; one fault draws that step of the stack unchanged, keeps the effect record and puts the plug-in's name and the field in `plugin.info`'s `lastError`. The pieces are drawn, measured and exported to PDF and SVG from the appearance stack without changing the document, and Expand Appearance writes them as ordinary paths in one undo step. Effects without the key behave as before. Regressions: `crates/plugins/src/painted/tests.rs`, `crates/plugins/src/manifest.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_fuzz.rs`. Scores and hours unchanged. - **Later effects on every fill or stroke a painted effect returns (G17):** when a painted effect on a fill or stroke returns several fills or strokes, a later live-effect plug-in in that fill's or stroke's stack, and a later geometry effect on strokes, runs once on each of them in paint order, given its own paint and its own `_context.item` among them. Each run's pieces replace only that fill or stroke. A run that fails, is refused, is deferred or would pass the effects' budget leaves only its own fill or stroke as it was, and the others still run; a deferred stage is listed once, at the source fill's or stroke's place. Regressions: `crates/engine/src/tests/plugins.rs`, `crates/effects/src/tests/painted.rs`. Scores and hours unchanged.