FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#1124: ⌘D / ⇧⌘D apply transitions to the selected clips or edit points
NDelic24f4ed00a847
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 6609afddf..c68936a7f 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -3654,6 +3654,11 @@ fn set_transition(s: &mut Session, p: &Value) -> Result<Value> {}fn apply_transition(s: &mut Session, p: &Value, kind: TrackKind) -> Result<Value> {+ if p.get("clip").is_none()+ && let Some(r) = crate::sequence_extras::apply_to_selection(s, p, kind)?+ {+ return Ok(r);+ }let (track, tr) = preview_transition(s, p, kind)?;let label = format!("Apply {}", tr.effect.def().map_or("Transition", |d| d.name));let id = s.edit_sequence(&label, |q, ctx, _| Ok(edit::add_transition(q, track, tr, ctx)?))?;diff --git a/crates/engine/src/sequence_extras.rs b/crates/engine/src/sequence_extras.rsindex 389590c81..ba25116a3 100644--- a/crates/engine/src/sequence_extras.rs+++ b/crates/engine/src/sequence_extras.rs@@ -134,9 +134,35 @@ fn apply_default_transitions(s: &mut Session, p: &Value) -> Result<Value> {return crate::trim::apply_default_transitions(s);}let q = s.active_sequence().ok_or(EngineError::NoSequence)?;+ let edges = clip_edges(q, &clips);+ apply_at_edges(s, edges, &Value::Null, "Apply Default Transitions")+}++/// Apply Video / Audio Transition (⌘D / ⇧⌘D) with clips or edit points of `kind` selected: the+/// transition goes at the selected clips' edges (as Apply Default Transitions to Selection), or at+/// the selected edit points, instead of at the playhead (#1124). `None` when nothing of `kind` is+/// selected. `p`'s settings (`effect`, `frames`, `params`, `reverse`) apply to every edge.+pub(crate) fn apply_to_selection(s: &mut Session, p: &Value, kind: TrackKind) -> Result<Option<Value>> {+ let q = s.active_sequence().ok_or(EngineError::NoSequence)?;+ let on_kind = |c: ClipId| q.find_item(c).and_then(|(t, _)| q.track(t)).is_some_and(|t| t.kind == kind);+ let clips: Vec<ClipId> = s.state.selection.iter().copied().filter(|c| on_kind(*c)).collect();+ let edges = if clips.is_empty() {+ s.state.edit_points.iter().filter(|e| on_kind(e.clip)).map(|e| (kind, e.clip, if e.out { "out" } else { "in" })).collect()+ } else {+ clip_edges(q, &clips)+ };+ if clips.is_empty() && edges.is_empty() {+ return Ok(None);+ }+ let label = if kind == TrackKind::Video { "Apply Video Transition" } else { "Apply Audio Transition" };+ apply_at_edges(s, edges, p, label).map(Some)+}++/// The In and Out edges of `clips`, one per cut; edges that already have a transition keep it.+fn clip_edges(q: &Sequence, clips: &[ClipId]) -> Vec<(TrackKind, ClipId, &'static str)> {// (track, clip, edge) per distinct cutlet mut edges: Vec<(TrackKind, ClipId, &'static str, Tick, TrackId)> = Vec::new();- for c in &clips {+ for c in clips {let Some((tid, it)) = q.find_item(*c) else { continue };let Some(tr) = q.track(tid) else { continue };for (edge, at) in [("in", it.start), ("out", it.end())] {@@ -150,17 +176,25 @@ fn apply_default_transitions(s: &mut Session, p: &Value) -> Result<Value> {edges.push((tr.kind, *c, edge, at, tid));}}+ edges.into_iter().map(|(kind, clip, edge, _, _)| (kind, clip, edge)).collect()+}++/// Apply a transition at each (track kind, clip, edge) with `settings`, as one undo step `label`.+fn apply_at_edges(s: &mut Session, edges: Vec<(TrackKind, ClipId, &'static str)>, settings: &Value, label: &str) -> Result<Value> {let n0 = s.history.undo.len();let mut ids = Vec::new();let mut errors = Vec::new();- for (kind, clip, edge, _, _) in edges {+ for (kind, clip, edge) in edges {let cmd = if kind == TrackKind::Video { "sequence.applyVideoTransition" } else { "sequence.applyAudioTransition" };- match s.execute(cmd, json!({"clip": clip.0, "edge": edge})) {+ let mut args = settings.as_object().cloned().unwrap_or_default();+ args.insert("clip".into(), json!(clip.0));+ args.insert("edge".into(), json!(edge));+ match s.execute(cmd, Value::Object(args)) {Ok(r) => ids.push(r["transition"].clone()),Err(e) => errors.push(e.to_string()),}}- crate::clip_ops::collapse_history(s, n0, "Apply Default Transitions");+ crate::clip_ops::collapse_history(s, n0, label);if ids.is_empty() {return Err(EngineError::Other(errors.into_iter().next().unwrap_or_else(|| "no transition could be applied to the selection".into())));}diff --git a/crates/engine/src/sequence_extras_tests.rs b/crates/engine/src/sequence_extras_tests.rsindex 0f5753873..c3c4674a7 100644--- a/crates/engine/src/sequence_extras_tests.rs+++ b/crates/engine/src/sequence_extras_tests.rs@@ -83,6 +83,47 @@ fn apply_default_transitions_to_selected_clips() {assert_eq!(s.active_sequence().unwrap().video_tracks[0].transitions.len(), before_v);}+#[test]+fn apply_video_transition_goes_to_the_selection_not_the_playhead() {+ // #1124: ⌘D with a clip selected adds the transition at its free edges wherever the playhead is+ let mut s = demo();+ let q = s.active_sequence().unwrap();+ let c = q.video_tracks[0].items[2].clone();+ let before_v = q.video_tracks[0].transitions.len();+ let before_a = q.audio_tracks[0].transitions.len();+ s.execute("playhead.set", json!({"time": (c.start + Tick(c.duration.0 / 2)).0})).unwrap();+ assert!(s.execute("sequence.applyVideoTransition", json!({})).is_err(), "no selection, no edit point at the playhead");+ s.execute("timeline.select", json!({"clips": [c.id.0]})).unwrap();+ let n_undo = s.history.undo.len();+ let r = s.execute("sequence.applyVideoTransition", json!({"effect": "wipe", "frames": 4})).unwrap();+ assert_eq!(r["transitions"].as_array().map(Vec::len), Some(1), "{r}");+ assert_eq!(s.history.undo.len(), n_undo + 1, "one undo step");+ assert_eq!(s.history.undo.last().unwrap().0, "Apply Video Transition");+ let q = s.active_sequence().unwrap();+ assert_eq!(q.video_tracks[0].transitions.len(), before_v + 1, "the out edge keeps its dip to black");+ let t = q.video_tracks[0].transitions.iter().find(|t| t.to == Some(c.id)).expect("transition at the in point");+ assert_eq!(t.effect.effect, "wipe");+ assert_eq!(t.duration, s.sequence_rate().tick_of(4));+ assert_eq!(q.audio_tracks[0].transitions.len(), before_a, "video only, though the linked audio is selected");+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(s.active_sequence().unwrap().video_tracks[0].transitions.len(), before_v);++ // a selected edit point gets the transition at that edge only+ s.execute("trim.selectEditPoint", json!({"clip": c.id.0, "edge": "in"})).unwrap();+ s.execute("sequence.applyVideoTransition", json!({})).unwrap();+ let q = s.active_sequence().unwrap();+ assert_eq!(q.video_tracks[0].transitions.len(), before_v + 1);+ assert!(q.video_tracks[0].transitions.iter().any(|t| t.to == Some(c.id) && t.effect.effect == "cross_dissolve"));++ // ⇧⌘D with the clip selected: audio only+ s.execute("edit.undo", json!({})).unwrap();+ s.execute("timeline.select", json!({"clips": [c.id.0]})).unwrap();+ s.execute("sequence.applyAudioTransition", json!({})).unwrap();+ let q = s.active_sequence().unwrap();+ assert_eq!(q.video_tracks[0].transitions.len(), before_v);+ assert!(q.audio_tracks[0].transitions.len() > before_a);+}+#[test]fn normalize_mix_track_hits_the_target_peak() {let mut s = demo();diff --git a/crates/ui-egui/tests/scripted.rs b/crates/ui-egui/tests/scripted.rsindex 63903a485..b5f105c7f 100644--- a/crates/ui-egui/tests/scripted.rs+++ b/crates/ui-egui/tests/scripted.rs@@ -249,6 +249,21 @@ fn effects_panel_lists_the_premiere_transition_folders() {assert!(seq["video"][0]["transitions"].as_array().unwrap().iter().any(|t| t["effect"] == "iris_round"), "{seq}");}+#[test]+fn cmd_d_applies_the_default_transition_to_the_selected_clip() {+ // #1124: the playhead is mid-clip, away from every cut+ let mut d = Driver::demo();+ let clip = track_clips(&d.sequence(), 0)[2].clone();+ let id = clip["clip"].clone();+ let mid = (clip["start"].as_i64().unwrap() + clip["end"].as_i64().unwrap()) / 2;+ d.exec("playhead.set", json!({"time": mid}));+ d.exec("timeline.select", json!({"clips": [id]}));+ d.ok("ui.key", json!({"key": "Cmd+D"}));+ d.frames(2);+ let seq = d.sequence();+ assert!(seq["video"][0]["transitions"].as_array().unwrap().iter().any(|t| t["to"] == id && t["effect"] == "cross_dissolve"), "{seq}");+}+#[test]fn playback_toggle_and_stop() {let mut d = Driver::demo();