diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 79208a1..bd60616 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -473,6 +473,25 @@ struct ClipDrag { clip: Clip, } +/// A track *header* being dragged: which track the hand took hold of, to be +/// let go over the header of the one whose place it is to take +/// ([`Player::reorder_lane`]). The lane alone -- a track carries its own clips +/// wherever it goes, so unlike a [`ClipDrag`] there is nothing else to name. +#[derive(Clone, Copy)] +struct LaneDrag(Lane); + +/// Where a header drag in flight would leave the track in the hand: the lane +/// whose slot it is about to take, and whether the line is drawn at that lane's +/// top edge (it is coming up from below) or its bottom one. The drop indicator +/// every editor draws between two tracks, and the header answer to the ghost a +/// clip drag lays on a lane ([`Ghost`]) -- stale between gestures, which costs +/// nothing because it is drawn only while one is live. +#[derive(Clone, Copy, PartialEq)] +struct LaneDrop { + lane: Lane, + above: bool, +} + /// Where the drag in flight would leave what it is carrying: the lane the /// pointer is over, the snapped head the release will commit ([`landing`]), and /// how long the thing is. Drawn on that lane as a translucent box the size of @@ -1315,6 +1334,11 @@ struct Player { /// line above does not answer -- and drawn only while a drag is live, for /// [`Player::snap_cue`]'s reason. ghost: Option, + /// The slot a track header being dragged is about to drop into, or `None` + /// while the pointer is over no lane: the line drawn between two headers, + /// for the reason [`Player::ghost`] draws a shadow -- where a gesture lands + /// is seen before the release. Drawn only while a drag is live. + lane_drop: Option, last_scrub: Instant, last_target: u32, /// The running export. While it owns the UI the editor is read-only. @@ -3948,6 +3972,33 @@ impl Player { self.set_ghost(ghost, cx); } + /// The line the track in the hand would drop into, on the row the pointer + /// is over: at that row's top edge when the header is coming up from below + /// and at its bottom edge when it is going down, which is the slot + /// [`Player::reorder_lane`] commits to at the release. Nothing at all over + /// its own row, where a release changes nothing. + fn preview_lane_drop(&mut self, from: Lane, onto: Lane, cx: &mut Context) { + let lanes = self + .session + .as_ref() + .map_or_else(Vec::new, PlaybackSession::lanes); + let at = |lane: Lane| lanes.iter().position(|&l| l == lane); + let next = match (at(from), at(onto)) { + (Some(i), Some(j)) if i != j => Some(LaneDrop { + lane: onto, + above: j < i, + }), + _ => None, + }; + // Only when it has actually changed: a drag move fires on every painted + // frame, and a redraw per frame that draws the same line is a redraw + // for nothing. + if self.lane_drop != next { + self.lane_drop = next; + cx.notify(); + } + } + /// The same shadow for a library row: its head at [`Player::place_frame`], /// which is where the drop inserts it, and the file's own length for its /// width -- the length the library row already reports. A file this lane @@ -5206,6 +5257,58 @@ impl Player { cx.notify(); } + /// A header let go over another header: the track in the hand takes that + /// one's place in the stack, clips and all + /// ([`engine::Project::move_lane`]), one undo step. The gesture every + /// editor reorders tracks with, and the only way the order is ever changed + /// -- there is no second list of it to keep in step. + /// + /// Display order is the stack, so moving a video track past another video + /// track changes which picture wins, here and in an export alike; audio is + /// summed and does not care, which is what makes `A1` above `V1` a purely + /// visual arrangement. A label is a position among the tracks of its kind, + /// so a track that crossed one of its own kind comes back under a different + /// name -- and everything holding a `(lane, idx)` is dropped exactly then, + /// for [`Player::remove_lane`]'s reason: those handles now name another + /// track's clip. A move that crossed only the other kind renames nothing + /// and keeps the selection. + fn reorder_lane(&mut self, lane: Lane, onto: Lane, cx: &mut Context) { + self.lane_drop = None; + if self.exporting().is_some() { + return; + } + let Some(session) = self.session.as_mut() else { + return; + }; + let Some(to) = session.lanes().iter().position(|&l| l == onto) else { + return; + }; + // Picked up and put back down where it was is a click, and a click says + // nothing -- `move_lane` refuses it and every other no-op. + let Some(moved) = session.move_lane(lane, to) else { + cx.notify(); + return; + }; + if moved != lane { + self.selected = None; + self.context_menu = None; + self.eq_open = None; + self.color_open = None; + self.speed_open = None; + self.close_silence(); + } + self.notify_user( + format!( + "{} IS TRACK {} NOW — {} puts it back", + moved.label(), + to + 1, + self.keymap.display(ActionId::Undo) + ) + .into(), + ); + cx.notify(); + } + /// What the remove keys act on: the last track of that kind, which is the /// one the matching add key appended. Nothing at all before a file is open, /// where the timeline drawn is a placeholder pair. @@ -16398,6 +16501,7 @@ fn main() { sub_track: 0, snap_cue: None, ghost: None, + lane_drop: None, last_scrub: Instant::now(), last_target: 0, export: None, diff --git a/crates/app/src/ui/timeline.rs b/crates/app/src/ui/timeline.rs index 3628ecc..16f5bf3 100644 --- a/crates/app/src/ui/timeline.rs +++ b/crates/app/src/ui/timeline.rs @@ -761,6 +761,18 @@ impl Player { let name = lane.label(); let row_id: SharedString = format!("{name}-clip").into(); let remove_id: SharedString = format!("{name}-remove").into(); + let header_id: SharedString = format!("{name}-header").into(); + let header_tip: SharedString = + format!("{name} — drag this header onto another to reorder the tracks").into(); + // The header ghost, the same shape a clip's is: the track's own name + // under the pointer, so what is in the hand is legible while it moves. + let header_ghost: SharedString = name.clone().into(); + // The line this row draws between itself and its neighbour while a + // header is being carried -- and, like every other drag cue here, only + // while the gesture is still in flight. + let drop = self + .lane_drop + .filter(|d| d.lane == lane && cx.has_active_drag()); let remove_tip: SharedString = format!( "Remove {name} — it must be empty first, and {} brings it back", self.keymap.display(ActionId::Undo) @@ -800,10 +812,35 @@ impl Player { .h(px(LANE_H)) .flex() .gap(px(HEADER_GAP)) + // A header let go anywhere along this row lands the track in the + // hand in this row's place: the whole width is the target, not the + // 40 px column, because a slot is what is being aimed at. The bed's + // own drops carry other payloads and never see this one. + .relative() + .on_drop(cx.listener(move |this, drag: &LaneDrag, _, cx| { + this.reorder_lane(drag.0, lane, cx); + })) + .on_drag_move(cx.listener( + move |this, event: &DragMoveEvent, _, cx| { + if !event.bounds.contains(&event.event.position) { + return; + } + this.preview_lane_drop(event.drag(cx).0, lane, cx); + }, + )) // The fixed column the ruler above is offset by as well. Full lane // height, so it reads as the bed continuing rather than as a chip. .child( div() + // Dragged, the whole track moves in the stack -- the + // gesture every editor reorders tracks with, and the reason + // the column is a handle rather than a caption. + .id(header_id) + .cursor(CursorStyle::OpenHand) + .tooltip(move |_, cx| cx.new(|_| Tip(header_tip.clone())).into()) + .on_drag(LaneDrag(lane), move |_, _, _, cx| { + cx.new(|_| Tip(header_ghost.clone())) + }) .flex_none() .w(px(HEADER_W)) .h_full() @@ -1326,5 +1363,24 @@ impl Player { .bg(rgb(ACCENT_PLAYHEAD())), ), ) + // The drop indicator, last of all so it is over the header and the + // bed both: a line along the edge the track in the hand comes in + // at, which is the slot the release commits to. *Inside* the row + // rather than in the gutter between two, because the lane column + // scrolls ([`Player::lanes_scroll`]) and anything drawn past the + // first row's top edge is clipped away by it -- an indicator that + // vanished on exactly the drop everybody tries first. + .children(drop.map(|d| { + let line = div() + .absolute() + .left_0() + .w_full() + .h(px(3.)) + .bg(rgb(ACCENT_PRIMARY())); + match d.above { + true => line.top_0(), + false => line.bottom_0(), + } + })) } } diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 90dcd14..f44a313 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -1435,6 +1435,22 @@ impl PlaybackSession { self.project.remove_lane(lane) } + /// Moves a whole track to display position `to`, clips and all + /// ([`Project::move_lane`]), and hands back the handle it answers to + /// afterwards -- `None` when nothing moved. One undo step. + /// + /// A *video* track that moves rebuilds the picture where the playhead + /// stands, because display order is the stack and the frame on screen may + /// be another lane's from here on; the sound plays on through it, untouched + /// -- a mix is a sum, and a lane's gain travelled with it. + pub fn move_lane(&mut self, lane: Lane, to: usize) -> Option { + let moved = self.project.move_lane(lane, to)?; + if lane.kind == LaneKind::Video { + self.invalidate(Dirty::Picture); + } + Some(moved) + } + /// Splits every lane at `timeline_secs`, so the two sides become two /// groups. Metadata only: a split never changes the timeline->source /// mapping, so the running decoder stays correct and playback does not diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 21a690e..efe579f 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -901,6 +901,41 @@ impl Project { Ok(()) } + /// Moves `lane` to display position `to` -- an index into + /// [`Project::lanes`], the slot the lane there gives up -- and hands back + /// the handle it answers to from now on. `None`, changing nothing and + /// snapshotting nothing, for a lane that is not there, a position that is + /// not, and a lane already standing in it. + /// + /// The whole track travels: its clips, its gain, its kind. Nothing is + /// remapped, so no take changes lane and no group is broken -- this is the + /// *order* of the lanes and nothing else, which is why one call is one undo + /// step like an add or a removal. + /// + /// Display order **is** the stack: [`Project::composite_span_at`] shows the + /// last video lane covering a frame, so moving one video lane past another + /// changes which picture wins, in the preview and in an export alike. The + /// audio lanes are summed and their sum does not care about order, so an + /// audio track moved among the video ones is a rearrangement of the screen + /// alone. + /// + /// A label is a *position* ([`Lane::label`] reads `ord`, which is the + /// position among the lanes of that kind), so a lane that crosses one of + /// its own kind swaps names with it -- `V2` dragged above `V1` becomes + /// `V1`, clips and all -- while one that crosses only lanes of the other + /// kind keeps its name. That is the returned handle: unchanged means every + /// handle a caller holds still names the track it named before. + pub fn move_lane(&mut self, lane: Lane, to: usize) -> Option { + let i = self.index(lane)?; + if to >= self.lanes.len() || to == i { + return None; + } + self.snapshot(); + let data = self.lanes.remove(i); + self.lanes.insert(to, data); + Some(handles(&self.lanes)[to]) + } + /// Where `lane` sits in [`Project::lanes`], or `None` for a lane that is /// not there. The one place a handle becomes a position. fn index(&self, lane: Lane) -> Option { @@ -5707,6 +5742,86 @@ mod tests { assert_eq!(p.lane_spans(v3), vec![(0, 3)]); } + /// A track dragged to another place in the stack: the whole lane travels -- + /// clips, gain and kind -- nothing is remapped, an audio track may sit above + /// a video one, a lane that crosses one of its own kind swaps names with it + /// (and swaps the composite with it), one undo puts the order back, and a + /// save carries it. + #[test] + fn a_track_is_dragged_to_another_place_in_the_stack() { + let mut p = three(); + assert!(p.set_lane_gain_db(Lane::A1, -6.0)); + let before = shape(&p); + let history = p.history.len(); + + // A1 over V1: the screen is rearranged and nothing else is. Both keep + // their names -- neither crossed a lane of its own kind -- so every + // handle a front-end holds still names the track it named before. + assert_eq!(p.move_lane(Lane::A1, 0), Some(Lane::A1)); + assert_eq!(p.lanes(), vec![Lane::A1, Lane::V1], "A1 is on top now"); + assert_eq!( + shape(&p), + vec![before[1].clone(), before[0].clone()], + "each lane's clips travelled with it, and none was remapped" + ); + assert_eq!(p.lane_gain_db(Lane::A1), -6.0, "so did its gain"); + assert_eq!(p.history.len(), history + 1, "one snapshot per move"); + invariants_hold(&p, "A1 over V1"); + + // Nothing moves for a lane that is not there, a slot that is not, or a + // lane already standing in the one it was pointed at. + assert_eq!(p.move_lane(Lane::new(LaneKind::Video, 9), 0), None); + assert_eq!(p.move_lane(Lane::V1, 2), None, "there is no third slot"); + assert_eq!(p.move_lane(Lane::A1, 0), None, "already there"); + assert_eq!(p.history.len(), history + 1, "and none of those snapshot"); + + // ...and one stroke puts the order back. + assert!(p.undo()); + assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1]); + assert_eq!(shape(&p), before); + + // A second video lane over the first: display order *is* the stack, so + // the last video lane covering a frame is the picture that shows. + let v2 = p.add_lane(LaneKind::Video); + assert!(p.place(v2, 0, clip(0, 0, 5, 0))); + let over = shape(&p)[2].clone(); + assert_eq!( + p.composite_span_at(0).map(|s| s.len), + Some(5), + "V2's five frames are what frame 0 shows" + ); + + // V2 dragged to the top takes V1's slot -- and V1's name, a label being + // a position among the lanes of its kind. The picture that wins changes + // with it: the take that was covering is the covered one now. + assert_eq!(p.move_lane(v2, 0), Some(Lane::V1), "a label is a position"); + assert_eq!(p.lanes(), vec![Lane::V1, v2, Lane::A1]); + assert_eq!( + shape(&p), + vec![over.clone(), before[0].clone(), before[1].clone()], + "V2's clips answer to V1 now, the old V1's to V2, and A1 kept its own" + ); + assert_eq!( + p.composite_span_at(0).map(|s| s.len), + Some(3), + "the lower video lane wins, and it is the old V1 now" + ); + invariants_hold(&p, "V2 over V1"); + + // A save writes that order and a load takes it back. + let (sources, lanes, eq, color) = p.without_orphan_sources(); + assert_eq!( + lanes.iter().map(|(k, _)| *k).collect::>(), + [LaneKind::Video, LaneKind::Video, LaneKind::Audio] + ); + let back = Project::from_parts(sources, lanes, eq, color).expect("three lanes load"); + assert_eq!(back.lanes(), p.lanes()); + assert_eq!(shape(&back), shape(&p), "the same lane is still on top"); + + assert!(p.undo(), "and the stack goes back to how it stood"); + assert_eq!(shape(&p), vec![before[0].clone(), before[1].clone(), over]); + } + /// The grouping rule across more than two lanes: a link id is one *span*, /// on however many lanes carry it -- not a video/audio pair and not a /// pairing by `ord`. So a split shares an id with every lane whose half