diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 23b044a..59a5b02 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -206,6 +206,16 @@ enum Wave { /// change what is inserted. struct AssetDrag(PathBuf, usize); +/// A clip already on the timeline being dragged: the lane it is on and its index +/// there, which is how every other edit names a clip. Unlike an [`AssetDrag`] +/// nothing is inserted -- the same clip changes lane and keeps its frames -- so +/// where along the bed it is let go says nothing here either. +#[derive(Clone, Copy)] +struct ClipDrag { + lane: Lane, + idx: usize, +} + /// An open clip menu: which clip it was opened on, where it hangs, and whether /// it has been turned over to show what that clip *is* instead of what can be /// done to it. The lane and index are the ones the same click selected, so @@ -1111,6 +1121,53 @@ impl Player { cx.notify(); } + /// A clip dragged off one lane and let go over another: it changes track and + /// keeps the frames it covers, as one undo step. Dropped back on its own + /// lane it is not an edit at all, so nothing is said about it. The engine + /// reseeks, so all this owes is the flag reset -- and the selection, whose + /// index was that lane's own and now names a different clip there. + fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, cx: &mut Context) { + if self.exporting().is_some() || from == to { + return; + } + let moved = self + .session + .as_mut() + .is_some_and(|session| session.move_clip_to_lane(from, idx, to)); + let (kind, lanes) = match from.kind { + LaneKind::Video => ("picture", "video"), + LaneKind::Audio => ("sound", "audio"), + }; + match moved { + true => { + self.selected = None; + self.reset_after_reseek(); + } + // The two ways a drag is refused, told apart by the one thing the + // front-end already knows: a lane's kind. Everything else that + // could refuse (a clip that is not there) cannot be dragged. + false if from.kind != to.kind => { + self.notice = Some( + format!( + "NOT ON {} — that is a {kind} clip; drop it on a {lanes} lane", + to.label() + ) + .into(), + ) + } + false => { + self.notice = Some( + format!( + "NOT MOVED — another clip already covers those frames on {}", + to.label() + ) + .into(), + ) + } + } + cx.notify(); + } + /// The one way a library row reaches the timeline: the Add button and a row /// dragged onto a lane both come here, so there is a single answer to what /// "add this source" does. The whole source goes in at the playhead as one @@ -3437,6 +3494,13 @@ impl Player { this.insert_source(&drag.0.clone(), drag.1, Some(lane), cx) })) .drag_over::(|s, _, _, _| s.bg(rgb(HOVER_DIM))) + // A clip let go over another lane changes track and keeps + // the frames it covers -- the pointer's position along the + // bed says nothing here either, for the reason above. + .on_drop(cx.listener(move |this, drag: &ClipDrag, _, cx| { + this.move_clip(drag.lane, drag.idx, lane, cx) + })) + .drag_over::(|s, _, _, _| s.bg(rgb(HOVER_DIM))) .children(clips.iter().enumerate().map(|(i, clip)| { let (start, len) = ( f64::from(clip.start) / self.fps, @@ -3467,6 +3531,12 @@ impl Player { f64::from(clip.out_frame) / self.fps, ); let tip = tip.clone(); + // What the pointer carries on the way to another lane: + // the file the box is showing, the same ghost a library + // row makes. A box too narrow for its own label still + // says what is moving. + let ghost: SharedString = + label.clone().unwrap_or_else(|| lane.label()).into(); div() // Named per lane: two rows numbering their clips // from zero would hand gpui the same id twice. @@ -3490,6 +3560,13 @@ impl Player { .cursor_pointer() .hover(|s| s.border_color(rgb(ACCENT))) .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) + // Dragged onto another lane it *moves* there; the + // click that starts the drag still selects, so + // picking a clip up and putting it back down where + // it was is exactly a click. + .on_drag(ClipDrag { lane, idx: i }, move |_, _, _, cx| { + cx.new(|_| Tip(ghost.clone())) + }) .on_mouse_down( MouseButton::Left, cx.listener(move |this, _: &MouseDownEvent, _, cx| { @@ -5102,6 +5179,48 @@ mod tests { ); } + /// The move-a-clip-between-tracks path through the door the drop uses + /// ([`Player::move_clip`] calls exactly this): the clip changes row, the + /// *picture* comes from the new row afterwards -- which is what "it plays + /// from there" means -- and one undo puts it back. + #[test] + fn a_clip_dragged_onto_another_track_plays_from_it() { + use engine::project::LaneKind; + + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + let v2 = session.add_lane(LaneKind::Video); + assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0))); + + assert!(session.move_clip_to_lane(Lane::V1, 0, v2), "V1 -> V2"); + assert!(session.lane_clips(Lane::V1).is_empty(), "it left V1"); + assert_eq!(session.lane_clips(v2).len(), 1, "and landed on V2"); + assert_eq!( + session.video_clip_at(0.0), + Some((v2, 0)), + "the picture now comes from V2" + ); + assert_eq!(session.lane_clips(Lane::A1).len(), 1, "its sound stayed"); + + // Dropped on a lane of the other kind it is refused and nothing moves -- + // the notice the front-end shows for it says which kind of lane to use. + // (The other refusal, landing on another clip, is the engine's own test + // `move_to_lane_keeps_the_frames_and_refuses_the_rest`.) + assert!(!session.move_clip_to_lane(v2, 0, Lane::A1), "picture on A1"); + assert_eq!(session.lane_clips(v2).len(), 1, "and it stayed on V2"); + assert!( + session.move_clip_to_lane(v2, 0, Lane::V1), + "dragged back down" + ); + + // One undo per move, and each is a single step. + assert!(session.undo(), "the drag back"); + assert_eq!(session.video_clip_at(0.0), Some((v2, 0))); + assert!(session.undo(), "the drag up"); + assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0))); + assert!(session.lane_clips(v2).is_empty()); + } + /// The add-a-track path end to end through the doors the buttons and the /// drop use: `+ V` adds a row, a library row let go over it lands there and /// nowhere else, Delete on it leaves the lanes under it where they are, and diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index ead2615..e0925df 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -581,6 +581,16 @@ impl PlaybackSession { self.edit(|p| p.lift(lane, idx)) } + /// Moves that clip onto another lane of the same kind, keeping the frames it + /// covers -- the drag between tracks. One undo step, and it reseeks like + /// every other edit: which lane a clip sits on is what the compositor's + /// topmost-lane-wins rule reads, so the frame on screen is recomposed at + /// once. `false` for a bad index, for a move across kinds and for one that + /// would land on another clip; nothing changes. + pub fn move_clip_to_lane(&mut self, from: Lane, idx: usize, to: Lane) -> bool { + self.edit(|p| p.move_to_lane(from, idx, to)) + } + /// The clip the picture is coming from at `timeline_secs`: the lane it sits /// on and its index there, by the same topmost-lane-wins rule the decoder /// follows. `None` over a gap and past the end. What a card that grades diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index aaabd70..011f4ac 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -992,6 +992,45 @@ impl Project { true } + /// Move the clip at `idx` of `from` onto `to`, keeping the timeline frames + /// it covers: the drag that rearranges takes across tracks. One snapshot, so + /// one [`Project::undo`] puts it back where it was. + /// + /// Its group id travels with it, and that is not a desync: a link means + /// "these cover one span on however many lanes" and names no lane at all + /// (see [`links_are_consistent`]), so a picture moved from `V1` to `V2` is + /// still the same take as the sound under it. The span never changes here -- + /// only which lane draws it. + /// + /// Refused, changing nothing, for a lane that is not there, an index that is + /// not there, a move onto the lane it is already on, a move across *kinds* + /// (a picture cannot play on an audio lane, and the save it wrote would not + /// open again), and a landing that would touch another clip -- a move that + /// overwrote what it landed on would destroy a take the pointer never named, + /// which is what [`place`](Project::place) may do and a drag may not. + pub fn move_to_lane(&mut self, from: Lane, idx: usize, to: Lane) -> bool { + if from.kind != to.kind || from == to || self.index(to).is_none() { + return false; + } + let Some(clip) = self.lane(from).get(idx).copied() else { + return false; + }; + if self + .lane(to) + .iter() + .any(|c| c.start < clip.end() && clip.start < c.end()) + { + return false; + } + self.snapshot(); + self.lane_mut(from).expect("checked above").remove(idx); + let clips = self.lane_mut(to).expect("checked above"); + let at = clips.partition_point(|c| c.start < clip.start); + clips.insert(at, clip); + debug_assert!(sorted_disjoint(clips)); + true + } + /// Insert `clip` into the first lane of each kind at `timeline_frame` as one /// new group, pushing everything from there on later by its length in /// *every* lane -- the grouped, rippling paste a clipboard does. Mid-clip @@ -2012,6 +2051,67 @@ mod tests { assert!(!p.place(Lane::V1, 0, clip(0, 7, 7, 0)), "empty clip"); } + /// The drag between tracks: the clip keeps its frames, one undo takes it + /// back, and every way it can be refused leaves the project untouched. + #[test] + fn move_to_lane_keeps_the_frames_and_refuses_the_rest() { + let v2 = Lane::new(LaneKind::Video, 1); + let mut p = three(); + assert_eq!(p.add_lane(LaneKind::Video), v2); + let before = shape(&p); + + assert!(p.move_to_lane(Lane::V1, 1, v2), "V1's middle clip moves up"); + assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 5, 9, 0)]); + assert_eq!(shape(&p)[2], vec![clip(3, 3, 5, 0)], "same frames on V2"); + assert_eq!(shape(&p)[1], before[1], "the audio lane is untouched"); + assert_eq!(p.timeline_frames(), 9, "a move is not an insert"); + // One snapshot: a single undo, and the lane list survives it. + assert!(p.undo()); + assert_eq!(shape(&p), before); + + // A lane that is not there, the lane it is already on, an index that is + // not there, and a move across kinds: all refused, nothing changed. + let history = p.history.len(); + for (from, idx, to) in [ + (Lane::V1, 1, Lane::new(LaneKind::Video, 7)), + (Lane::V1, 1, Lane::V1), + (Lane::V1, 9, v2), + (Lane::V1, 1, Lane::A1), + (Lane::A1, 1, v2), + ] { + assert!(!p.move_to_lane(from, idx, to), "{from:?} {idx} -> {to:?}"); + } + assert_eq!(shape(&p), before); + assert_eq!(p.history.len(), history, "a refusal snapshots nothing"); + + // Landing on another clip is refused rather than overwriting it: the + // pointer named the lane, never the take already sitting there. + assert!(p.place(v2, 3, clip(0, 100, 101, 0)), "V2 holds [3,4)"); + assert!(!p.move_to_lane(Lane::V1, 1, v2), "[3,5) would land on it"); + assert!(p.move_to_lane(Lane::V1, 0, v2), "[0,3) merely abuts it"); + assert_eq!(shape(&p)[0], vec![clip(3, 3, 5, 0), clip(5, 5, 9, 0)]); + assert_eq!(shape(&p)[2], vec![clip(0, 0, 3, 0), clip(3, 100, 101, 0)]); + } + + /// A moved half stays in its group: a link names a span, not a lane, so the + /// picture on `V2` is still the same take as the sound under it on `A1`. + #[test] + fn move_to_lane_keeps_the_group() { + let v2 = Lane::new(LaneKind::Video, 1); + let mut p = three(); + p.add_lane(LaneKind::Video); + let link = p.lane(Lane::V1)[1].link.expect("a split hands out ids"); + assert_eq!(p.lane(Lane::A1)[1].link, Some(link), "both halves grouped"); + assert!(p.move_to_lane(Lane::V1, 1, v2)); + assert_eq!(p.lane(v2)[0].link, Some(link), "the id travelled with it"); + links_are_consistent(&p.lanes).expect("one id per lane, one span"); + assert_eq!( + p.lane(v2)[0].start, + p.lane(Lane::A1)[1].start, + "and still covers the same span as its sound" + ); + } + #[test] fn ripple_delete_spans_partial_clips() { let mut p = three();