Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

#224: dragging a transition's end trims that end only

swillunderscoref61669d20884
diff --git a/crates/edit/src/transitions.rs b/crates/edit/src/transitions.rs
index d28c7f7f9..c487c7d24 100644
--- a/crates/edit/src/transitions.rs
+++ b/crates/edit/src/transitions.rs
@@ -154,12 +154,13 @@ pub fn set_span(seq: &mut Sequence, id: TransitionId, start: Tick, duration: Tic
}
/// Where dragging a transition by `delta` in the Timeline puts it, as `(start, duration)`. Its
-/// middle (`edge` None) slides it over the cut. An end changes its duration: a Center at Cut
-/// transition grows or shrinks on both sides and stays centred on the cut (its alignment says how
-/// it changes length), any other keeps its other end where it is. A fade to or from nothing keeps
+/// middle (`edge` None) slides it over the cut. An end trims that end only (Premiere shows the
+/// Trim-In / Trim-Out icon there): the other end stays, whatever the alignment, so a Center at
+/// Cut transition becomes Custom Start. (A typed duration, in Effect Controls or Set Transition
+/// Duration, follows the alignment instead: see [`start_for`].) A fade to or from nothing keeps
/// its clip-edge end, and does not slide. Always clamped to where it may be ([`bounds`], over the
-/// cut, at least one `frame`); `snap` puts a centred start on a frame. `None` when its clips are gone.
-pub fn drag_span(track: &Track, tr: &Transition, edge: Option<Edge>, delta: Tick, frame: Tick, snap: impl Fn(Tick) -> Tick) -> Option<(Tick, Tick)> {
+/// cut, at least one `frame`). `None` when its clips are gone.
+pub fn drag_span(track: &Track, tr: &Transition, edge: Option<Edge>, delta: Tick, frame: Tick) -> Option<(Tick, Tick)> {
let (lo, cut, hi) = bounds(track, tr)?;
let frame = frame.max(Tick(1));
let (s0, e0, d0) = (tr.start, tr.end(), tr.duration);
@@ -171,13 +172,6 @@ pub fn drag_span(track: &Track, tr: &Transition, edge: Option<Edge>, delta: Tick
let (min_s, max_s) = (lo.max(cut - d0), (hi - d0).min(cut));
if min_s > max_s { unchanged } else { Some(((s0 + delta).clamp(min_s, max_s), d0)) }
}
- Some(e) if !one_sided && alignment(tr, cut, frame) == Alignment::Center => {
- // dragging an end by n frames moves both ends by n: the duration changes by 2n
- let grow = if e == Edge::In { Tick(0) - delta } else { delta };
- let max_d = Tick((cut - lo).min(hi - cut).0.saturating_mul(2));
- let d = Tick(d0.0.saturating_add(grow.0.saturating_mul(2))).clamp(frame, max_d.max(frame));
- Some((snap(cut - d.mul_ratio(1, 2)), d))
- }
Some(Edge::In) if tr.from.is_none() => unchanged,
Some(Edge::In) => {
let max_s = cut.min(e0 - frame).max(lo);
@@ -349,18 +343,19 @@ mod tests {
fn drag(q: &Sequence, id: u64, edge: Option<Edge>, frames: i64) -> (i64, i64) {
let (t, x) = find(q, TransitionId(id)).unwrap();
- let (st, d) = drag_span(t, x, edge, f(frames), f(1), |t| R.snap(t)).unwrap();
+ let (st, d) = drag_span(t, x, edge, f(frames), f(1)).unwrap();
(R.frame_at(st), R.frame_at(d))
}
#[test]
fn dragging_a_transition() {
let mut q = seq();
- // centred (36..60 over the cut at 48): an end grows / shrinks it on both sides
- assert_eq!(drag(&q, 1, Some(Edge::In), -4), (32, 32));
- assert_eq!(drag(&q, 1, Some(Edge::Out), -10), (46, 4));
- assert_eq!(drag(&q, 1, Some(Edge::Out), -100), (47, 1), "at least a frame (centred: it starts on the frame before the cut)");
- assert_eq!(drag(&q, 1, Some(Edge::Out), 100), (0, 96), "at most both clips");
+ // centred (36..60 over the cut at 48): an end trims that end only, the other stays
+ assert_eq!(drag(&q, 1, Some(Edge::In), -4), (32, 28));
+ assert_eq!(drag(&q, 1, Some(Edge::Out), -10), (36, 14));
+ assert_eq!(drag(&q, 1, Some(Edge::Out), -100), (36, 12), "the end stops at the cut");
+ assert_eq!(drag(&q, 1, Some(Edge::Out), 100), (36, 60), "and at the incoming clip's end");
+ assert_eq!(drag(&q, 1, Some(Edge::In), -100), (0, 60), "the start at the outgoing clip's start");
// the middle slides it, staying over the cut and inside the clips
assert_eq!(drag(&q, 1, None, 5), (41, 24));
assert_eq!(drag(&q, 1, None, 100), (48, 24));
@@ -380,7 +375,7 @@ mod tests {
// whatever comes out is a span set_span accepts
for (id, edge, n) in [(1, Some(Edge::In), -70), (1, None, 33), (2, Some(Edge::Out), 99), (3, Some(Edge::In), -99)] {
let (t, x) = find(&q, TransitionId(id)).unwrap();
- let (st, d) = drag_span(t, x, edge, f(n), f(1), |t| R.snap(t)).unwrap();
+ let (st, d) = drag_span(t, x, edge, f(n), f(1)).unwrap();
let mut c = q.clone();
set_span(&mut c, TransitionId(id), st, d, f(1), f(1)).unwrap();
}
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index a826e9a43..a1b1d9268 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -1983,10 +1983,7 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
Drag::Transition { id, edge, from, start, duration } => {
let delta = rate.snap_nearest(t_here - from);
let span = seq.all_tracks().find_map(|tr| {
- tr.transitions
- .iter()
- .find(|x| x.id == id)
- .and_then(|x| filmcraft_edit::transitions::drag_span(tr, x, edge, delta, rate.frame_duration(), |t| rate.snap(t)))
+ tr.transitions.iter().find(|x| x.id == id).and_then(|x| filmcraft_edit::transitions::drag_span(tr, x, edge, delta, rate.frame_duration()))
});
let (start, duration) = span.unwrap_or((start, duration));
Some(Drag::Transition { id, edge, from, start, duration })
diff --git a/crates/ui-egui/tests/transitions_ui.rs b/crates/ui-egui/tests/transitions_ui.rs
index 0cd4db431..fbcbabb57 100644
--- a/crates/ui-egui/tests/transitions_ui.rs
+++ b/crates/ui-egui/tests/transitions_ui.rs
@@ -177,8 +177,8 @@ fn click_selects_a_transition_and_delete_removes_it() {
assert_eq!(d.exec("sequence.inspect", json!({}))["transitionSelection"], json!([]));
}
-/// Dragging a centred transition's end changes its duration on both sides of the cut (#224);
-/// dragging its middle slides it over the cut (Custom Start). Each drag is one undo step.
+/// Dragging a transition's end trims that end only (#224); dragging its middle slides it over the
+/// cut (Custom Start). Each drag is one undo step.
#[test]
fn dragging_a_transition_end_or_middle() {
let mut d = Driver::demo();
@@ -192,9 +192,9 @@ fn dragging_a_transition_end_or_middle() {
let x1 = d.transition(c.id).unwrap();
let (s1, d1) = (x1["start"].as_i64().unwrap(), x1["duration"].as_i64().unwrap());
assert!(d1 > c.duration, "dragging the end out lengthens it: {} -> {d1}", c.duration);
- assert_eq!(x1["align"], json!("center"), "a centred transition stays centred");
- assert!((s1 + d1 / 2 - c.cut).abs() <= frame, "still centred on the cut");
- assert_eq!((d1 - c.duration) % (2 * frame), 0, "both ends moved by whole frames");
+ assert_eq!(s1, c.start, "the start stays where it was");
+ assert_eq!((d1 - c.duration) % frame, 0, "by whole frames");
+ assert_eq!(x1["align"], json!("custom"), "no longer centred: Custom Start");
// the middle: slide it right of centre
let (x, y, w, h, _) = d.find(&format!("timeline.transition.{}", c.id)).unwrap();
let (mx, my) = (x + w / 2.0, y + h / 2.0);
@@ -205,7 +205,8 @@ fn dragging_a_transition_end_or_middle() {
assert_eq!(x2["duration"].as_i64(), Some(d1), "sliding keeps the duration");
assert!(x2["start"].as_i64().unwrap() > s1, "and moves it right");
assert!(x2["start"].as_i64().unwrap() <= c.cut, "still over the cut");
- assert_eq!(x2["align"], json!("custom"));
+ // off-centre (Custom Start), or Start at Cut if the slide reached the cut, where it stops
+ assert!(matches!(x2["align"].as_str(), Some("custom" | "start")), "{}", x2["align"]);
// two drags, two undo steps
d.exec("edit.undo", json!({}));
d.exec("edit.undo", json!({}));