FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
FONT-UNDO-001: merge selected-character property drags
jackcanon204505bcf5d1
diff --git a/crates/engine/src/graphic_templates.rs b/crates/engine/src/graphic_templates.rsindex f5053756b..00b66c72f 100644--- a/crates/engine/src/graphic_templates.rs+++ b/crates/engine/src/graphic_templates.rs@@ -775,7 +775,12 @@ fn set_char_style(s: &mut Session, p: &Value) -> Result<Value> {};let tl = s.playhead();let (start, end) = (u64_p(p, "start").map(|v| v as usize), u64_p(p, "end").map(|v| v as usize));- let runs = s.edit_sequence("Character Style", move |q, _, _| {+ // A pointer gesture shares one undo step only within the same clip, layer and range.+ let merge = bool_p(p, "merge").unwrap_or(false).then(|| format!("graphics.setCharStyle:{}:{ei}:{start:?}:{end:?}", clip.0));+ if bool_p(p, "begin").unwrap_or(false) {+ s.history.merge_key = None;+ }+ let runs = s.edit_sequence_as("Character Style", merge.as_deref(), move |q, _, _| {let (_, it) = q.find_item_mut(clip).ok_or(filmcraft_edit::EditError::NoItem(clip))?;let mt = it.source_time_at(tl.clamp(it.start, it.end() - Tick(1)));let e = &mut it.effects[ei];@@ -1217,7 +1222,7 @@ pub fn commands() -> Vec<CommandSpec> {"graphics.setCharStyle","Character Style",&[],- r##"{"clip":id?,"layer":n|name?,"start":char=0,"end":char=len,"style":{"font":str,"fontStyle":str,"size":px,"color":"#rrggbb","bold":bool,"italic":bool,"underline":bool,"tracking":n,"baselineShift":px,"caps":"normal|all caps|small caps"},"clear":bool? (remove styles from the range)}"##,+ r##"{"clip":id?,"layer":n|name?,"start":char=0,"end":char=len,"style":{"font":str,"fontStyle":str,"size":px,"color":"#rrggbb","bold":bool,"italic":bool,"underline":bool,"tracking":n,"baselineShift":px,"caps":"normal|all caps|small caps"},"clear":bool? (remove styles from the range),"merge":bool?,"begin":bool? (start a new undo gesture)}"##,has_graphic,set_char_style,),diff --git a/crates/engine/src/graphic_templates_tests.rs b/crates/engine/src/graphic_templates_tests.rsindex 98a55a588..e49c0ec8f 100644--- a/crates/engine/src/graphic_templates_tests.rs+++ b/crates/engine/src/graphic_templates_tests.rs@@ -380,3 +380,52 @@ fn copy_paste_duplicates_a_graphic_clip() {assert!((pasted[0].start.seconds() - 620.0).abs() < 0.05, "at the playhead: {:?}", pasted[0].start);assert_eq!(texts(&s, pasted[0].id), vec!["Lyric".to_string()]);}++#[test]+fn character_style_drag_merges_only_its_target_and_range() {+ let mut s = demo("gt-char-drag");+ let c = clip_of(&s.execute("graphics.newText", json!({"text":"Alpha Bravo"})).unwrap());+ let before = s.project.clone();+ let n = s.history.undo.len();+ for i in 0..8 {+ s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":0,"end":5,"style":{"size":105+i*5},"merge":true,"begin":i==0})).unwrap();+ }+ assert_eq!(s.history.undo.len(), n + 1, "one drag, one undo step");+ let after = s.project.clone();+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(*s.project, *before);+ s.execute("edit.redo", json!({})).unwrap();+ assert_eq!(*s.project, *after);+ s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":0,"end":5,"style":{"size":150},"merge":true,"begin":true})).unwrap();+ assert_eq!(s.history.undo.len(), n + 2, "a new gesture on the same range starts a new step");+ s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":6,"end":11,"style":{"size":160},"merge":true})).unwrap();+ assert_eq!(s.history.undo.len(), n + 3, "another character range does not merge");+ let before = s.project.clone();+ assert!(s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":3,"end":3,"style":{"size":170},"merge":true})).is_err());+ assert_eq!(*s.project, *before);+ assert_eq!(s.history.undo.len(), n + 3, "an invalid edit adds no undo step");+}++#[test]+fn character_style_merge_preserves_default_calls_and_separate_clips() {+ let mut s = demo("gt-char-merge-targets");+ let a = clip_of(&s.execute("graphics.newText", json!({"text":"First", "newClip":true})).unwrap());+ let b = clip_of(&s.execute("graphics.newText", json!({"text":"Other", "newClip":true})).unwrap());+ assert_ne!(a, b);+ let n = s.history.undo.len();+ for c in [a, b] {+ s.execute("graphics.setCharStyle", json!({"clip":c.0,"start":0,"end":5,"style":{"size":120},"merge":true})).unwrap();+ }+ assert_eq!(s.history.undo.len(), n + 2, "distinct clips do not merge");+ for size in [130, 140] {+ s.execute("graphics.setCharStyle", json!({"clip":b.0,"style":{"size":size}})).unwrap();+ }+ assert_eq!(s.history.undo.len(), n + 4, "default calls retain separate undo steps");+ let added = s.execute("graphics.newText", json!({"clip":b.0,"text":"Layer"})).unwrap();+ assert_eq!(clip_of(&added), b);+ let n = s.history.undo.len();+ for layer in [0, 1] {+ s.execute("graphics.setCharStyle", json!({"clip":b.0,"layer":layer,"start":0,"end":5,"style":{"size":150},"merge":true})).unwrap();+ }+ assert_eq!(s.history.undo.len(), n + 2, "distinct layers do not merge");+}diff --git a/crates/ui-egui/src/panels/graphics.rs b/crates/ui-egui/src/panels/graphics.rsindex 1b9e5438c..9ce2718d5 100644--- a/crates/ui-egui/src/panels/graphics.rs+++ b/crates/ui-egui/src/panels/graphics.rs@@ -1810,7 +1810,7 @@ pub fn properties(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {for (cmd, mut p) in actions {// a drag of a property is one undo step: the first change of each press begins a new one,// the rest fold into it (like Effect Controls, #201 / #887)- if cmd == "graphics.set" && down {+ if matches!(cmd.as_str(), "graphics.set" | "graphics.setCharStyle") && down {p["merge"] = json!(true);p["begin"] = json!(begun != press);wrote = true;diff --git a/crates/ui-egui/tests/graphics_ui.rs b/crates/ui-egui/tests/graphics_ui.rsindex c7aa5138e..e4ae68b2c 100644--- a/crates/ui-egui/tests/graphics_ui.rs+++ b/crates/ui-egui/tests/graphics_ui.rs@@ -230,3 +230,29 @@ fn shadow_color_drag_is_one_undo_step() {d.exec("edit.undo", json!({}));assert_eq!(color(&mut d), before, "one undo restores the original color");}++/// Character ranges use setCharStyle, but must share the same one-gesture undo rule as a layer.+#[test]+fn selected_character_size_drag_is_one_undo_step() {+ let mut d = Driver::demo();+ d.ok("ui.set", json!({"workspace":"Captions and Graphics","tool":"Type"}));+ d.exec("playhead.set", json!({"seconds":2}));+ d.frames(3);+ let p = d.rect("program.picture");+ d.ok("ui.click", json!({"x":p[0]+p[2]*0.2,"y":p[1]+p[3]*0.35}));+ d.ok("ui.type", json!({"text":"Undo drag test"}));+ d.ok("ui.key", json!({"key":"Cmd+A"}));+ d.frames(3);+ let before = d.harness.state().session.project.clone();+ let n = d.harness.state().session.history.undo.len();+ let r = d.rect("graphics.prop.size");+ d.ok("ui.drag", json!({"from":{"x":r[0]+r[2]/2.0,"y":r[1]+r[3]/2.0},"to":{"x":r[0]+r[2]/2.0+80.0,"y":r[1]+r[3]/2.0},"steps":8}));+ d.frames(3);+ assert_eq!(d.harness.state().session.history.undo.len(), n + 1);+ let after = d.harness.state().session.project.clone();+ assert_ne!(*after, *before);+ d.exec("edit.undo", json!({}));+ assert_eq!(*d.harness.state().session.project, *before);+ d.exec("edit.redo", json!({}));+ assert_eq!(*d.harness.state().session.project, *after);+}