forge
Explore
Log in

Don't you dare go Hollow

A track is carried to its place in the stack

Browse files at this commit

9ddffc8e6f83e35f962ba67aa872ad084f978060Ahmet Kaan GümüşParent: 07f2cff
  • feat
    • a track is carried to its place in the stack
Show full message
✨ feat(timeline): a track is carried to its place in the stack

A header is a handle now: drag V1 or A1 onto another header and the whole
track takes its slot -- clips, gain and kind travelling with it, a ghost
under the pointer and a line along the edge it comes in at, one undo step.
Order lives in one place, the lane list, so the stack the preview
composites, the stack an export composites and the order a save writes are
the same list read three times.

Display order is that stack: moving a video track past another changes
which picture wins, in the preview and in an export alike, while audio is
summed and does not care -- which is what makes an audio track above a
video one a rearrangement of the screen alone. A label stays a position
among the tracks of its kind, so a track that crosses one of its own kind
swaps names with it, and everything holding a (lane, idx) is dropped
exactly then.
crates/app/src/main.rsmodified+104 -0
@@ -473,6 +473,25 @@ struct ClipDrag {
473473 clip: Clip,
474474 }
475475
476+/// A track *header* being dragged: which track the hand took hold of, to be
477+/// let go over the header of the one whose place it is to take
478+/// ([`Player::reorder_lane`]). The lane alone -- a track carries its own clips
479+/// wherever it goes, so unlike a [`ClipDrag`] there is nothing else to name.
480+#[derive(Clone, Copy)]
481+struct LaneDrag(Lane);
482+
483+/// Where a header drag in flight would leave the track in the hand: the lane
484+/// whose slot it is about to take, and whether the line is drawn at that lane's
485+/// top edge (it is coming up from below) or its bottom one. The drop indicator
486+/// every editor draws between two tracks, and the header answer to the ghost a
487+/// clip drag lays on a lane ([`Ghost`]) -- stale between gestures, which costs
488+/// nothing because it is drawn only while one is live.
489+#[derive(Clone, Copy, PartialEq)]
490+struct LaneDrop {
491+ lane: Lane,
492+ above: bool,
493+}
494+
476495 /// Where the drag in flight would leave what it is carrying: the lane the
477496 /// pointer is over, the snapped head the release will commit ([`landing`]), and
478497 /// how long the thing is. Drawn on that lane as a translucent box the size of
@@ -1315,6 +1334,11 @@ struct Player {
13151334 /// line above does not answer -- and drawn only while a drag is live, for
13161335 /// [`Player::snap_cue`]'s reason.
13171336 ghost: Option<Ghost>,
1337+ /// The slot a track header being dragged is about to drop into, or `None`
1338+ /// while the pointer is over no lane: the line drawn between two headers,
1339+ /// for the reason [`Player::ghost`] draws a shadow -- where a gesture lands
1340+ /// is seen before the release. Drawn only while a drag is live.
1341+ lane_drop: Option<LaneDrop>,
13181342 last_scrub: Instant,
13191343 last_target: u32,
13201344 /// The running export. While it owns the UI the editor is read-only.
@@ -3948,6 +3972,33 @@ impl Player {
39483972 self.set_ghost(ghost, cx);
39493973 }
39503974
3975+ /// The line the track in the hand would drop into, on the row the pointer
3976+ /// is over: at that row's top edge when the header is coming up from below
3977+ /// and at its bottom edge when it is going down, which is the slot
3978+ /// [`Player::reorder_lane`] commits to at the release. Nothing at all over
3979+ /// its own row, where a release changes nothing.
3980+ fn preview_lane_drop(&mut self, from: Lane, onto: Lane, cx: &mut Context<Self>) {
3981+ let lanes = self
3982+ .session
3983+ .as_ref()
3984+ .map_or_else(Vec::new, PlaybackSession::lanes);
3985+ let at = |lane: Lane| lanes.iter().position(|&l| l == lane);
3986+ let next = match (at(from), at(onto)) {
3987+ (Some(i), Some(j)) if i != j => Some(LaneDrop {
3988+ lane: onto,
3989+ above: j < i,
3990+ }),
3991+ _ => None,
3992+ };
3993+ // Only when it has actually changed: a drag move fires on every painted
3994+ // frame, and a redraw per frame that draws the same line is a redraw
3995+ // for nothing.
3996+ if self.lane_drop != next {
3997+ self.lane_drop = next;
3998+ cx.notify();
3999+ }
4000+ }
4001+
39514002 /// The same shadow for a library row: its head at [`Player::place_frame`],
39524003 /// which is where the drop inserts it, and the file's own length for its
39534004 /// width -- the length the library row already reports. A file this lane
@@ -5206,6 +5257,58 @@ impl Player {
52065257 cx.notify();
52075258 }
52085259
5260+ /// A header let go over another header: the track in the hand takes that
5261+ /// one's place in the stack, clips and all
5262+ /// ([`engine::Project::move_lane`]), one undo step. The gesture every
5263+ /// editor reorders tracks with, and the only way the order is ever changed
5264+ /// -- there is no second list of it to keep in step.
5265+ ///
5266+ /// Display order is the stack, so moving a video track past another video
5267+ /// track changes which picture wins, here and in an export alike; audio is
5268+ /// summed and does not care, which is what makes `A1` above `V1` a purely
5269+ /// visual arrangement. A label is a position among the tracks of its kind,
5270+ /// so a track that crossed one of its own kind comes back under a different
5271+ /// name -- and everything holding a `(lane, idx)` is dropped exactly then,
5272+ /// for [`Player::remove_lane`]'s reason: those handles now name another
5273+ /// track's clip. A move that crossed only the other kind renames nothing
5274+ /// and keeps the selection.
5275+ fn reorder_lane(&mut self, lane: Lane, onto: Lane, cx: &mut Context<Self>) {
5276+ self.lane_drop = None;
5277+ if self.exporting().is_some() {
5278+ return;
5279+ }
5280+ let Some(session) = self.session.as_mut() else {
5281+ return;
5282+ };
5283+ let Some(to) = session.lanes().iter().position(|&l| l == onto) else {
5284+ return;
5285+ };
5286+ // Picked up and put back down where it was is a click, and a click says
5287+ // nothing -- `move_lane` refuses it and every other no-op.
5288+ let Some(moved) = session.move_lane(lane, to) else {
5289+ cx.notify();
5290+ return;
5291+ };
5292+ if moved != lane {
5293+ self.selected = None;
5294+ self.context_menu = None;
5295+ self.eq_open = None;
5296+ self.color_open = None;
5297+ self.speed_open = None;
5298+ self.close_silence();
5299+ }
5300+ self.notify_user(
5301+ format!(
5302+ "{} IS TRACK {} NOW — {} puts it back",
5303+ moved.label(),
5304+ to + 1,
5305+ self.keymap.display(ActionId::Undo)
5306+ )
5307+ .into(),
5308+ );
5309+ cx.notify();
5310+ }
5311+
52095312 /// What the remove keys act on: the last track of that kind, which is the
52105313 /// one the matching add key appended. Nothing at all before a file is open,
52115314 /// where the timeline drawn is a placeholder pair.
@@ -16398,6 +16501,7 @@ fn main() {
1639816501 sub_track: 0,
1639916502 snap_cue: None,
1640016503 ghost: None,
16504+ lane_drop: None,
1640116505 last_scrub: Instant::now(),
1640216506 last_target: 0,
1640316507 export: None,
crates/app/src/ui/timeline.rsmodified+56 -0
@@ -761,6 +761,18 @@ impl Player {
761761 let name = lane.label();
762762 let row_id: SharedString = format!("{name}-clip").into();
763763 let remove_id: SharedString = format!("{name}-remove").into();
764+ let header_id: SharedString = format!("{name}-header").into();
765+ let header_tip: SharedString =
766+ format!("{name} — drag this header onto another to reorder the tracks").into();
767+ // The header ghost, the same shape a clip's is: the track's own name
768+ // under the pointer, so what is in the hand is legible while it moves.
769+ let header_ghost: SharedString = name.clone().into();
770+ // The line this row draws between itself and its neighbour while a
771+ // header is being carried -- and, like every other drag cue here, only
772+ // while the gesture is still in flight.
773+ let drop = self
774+ .lane_drop
775+ .filter(|d| d.lane == lane && cx.has_active_drag());
764776 let remove_tip: SharedString = format!(
765777 "Remove {name} — it must be empty first, and {} brings it back",
766778 self.keymap.display(ActionId::Undo)
@@ -800,10 +812,35 @@ impl Player {
800812 .h(px(LANE_H))
801813 .flex()
802814 .gap(px(HEADER_GAP))
815+ // A header let go anywhere along this row lands the track in the
816+ // hand in this row's place: the whole width is the target, not the
817+ // 40 px column, because a slot is what is being aimed at. The bed's
818+ // own drops carry other payloads and never see this one.
819+ .relative()
820+ .on_drop(cx.listener(move |this, drag: &LaneDrag, _, cx| {
821+ this.reorder_lane(drag.0, lane, cx);
822+ }))
823+ .on_drag_move(cx.listener(
824+ move |this, event: &DragMoveEvent<LaneDrag>, _, cx| {
825+ if !event.bounds.contains(&event.event.position) {
826+ return;
827+ }
828+ this.preview_lane_drop(event.drag(cx).0, lane, cx);
829+ },
830+ ))
803831 // The fixed column the ruler above is offset by as well. Full lane
804832 // height, so it reads as the bed continuing rather than as a chip.
805833 .child(
806834 div()
835+ // Dragged, the whole track moves in the stack -- the
836+ // gesture every editor reorders tracks with, and the reason
837+ // the column is a handle rather than a caption.
838+ .id(header_id)
839+ .cursor(CursorStyle::OpenHand)
840+ .tooltip(move |_, cx| cx.new(|_| Tip(header_tip.clone())).into())
841+ .on_drag(LaneDrag(lane), move |_, _, _, cx| {
842+ cx.new(|_| Tip(header_ghost.clone()))
843+ })
807844 .flex_none()
808845 .w(px(HEADER_W))
809846 .h_full()
@@ -1326,5 +1363,24 @@ impl Player {
13261363 .bg(rgb(ACCENT_PLAYHEAD())),
13271364 ),
13281365 )
1366+ // The drop indicator, last of all so it is over the header and the
1367+ // bed both: a line along the edge the track in the hand comes in
1368+ // at, which is the slot the release commits to. *Inside* the row
1369+ // rather than in the gutter between two, because the lane column
1370+ // scrolls ([`Player::lanes_scroll`]) and anything drawn past the
1371+ // first row's top edge is clipped away by it -- an indicator that
1372+ // vanished on exactly the drop everybody tries first.
1373+ .children(drop.map(|d| {
1374+ let line = div()
1375+ .absolute()
1376+ .left_0()
1377+ .w_full()
1378+ .h(px(3.))
1379+ .bg(rgb(ACCENT_PRIMARY()));
1380+ match d.above {
1381+ true => line.top_0(),
1382+ false => line.bottom_0(),
1383+ }
1384+ }))
13291385 }
13301386 }
crates/engine/src/playback.rsmodified+16 -0
@@ -1435,6 +1435,22 @@ impl PlaybackSession {
14351435 self.project.remove_lane(lane)
14361436 }
14371437
1438+ /// Moves a whole track to display position `to`, clips and all
1439+ /// ([`Project::move_lane`]), and hands back the handle it answers to
1440+ /// afterwards -- `None` when nothing moved. One undo step.
1441+ ///
1442+ /// A *video* track that moves rebuilds the picture where the playhead
1443+ /// stands, because display order is the stack and the frame on screen may
1444+ /// be another lane's from here on; the sound plays on through it, untouched
1445+ /// -- a mix is a sum, and a lane's gain travelled with it.
1446+ pub fn move_lane(&mut self, lane: Lane, to: usize) -> Option<Lane> {
1447+ let moved = self.project.move_lane(lane, to)?;
1448+ if lane.kind == LaneKind::Video {
1449+ self.invalidate(Dirty::Picture);
1450+ }
1451+ Some(moved)
1452+ }
1453+
14381454 /// Splits every lane at `timeline_secs`, so the two sides become two
14391455 /// groups. Metadata only: a split never changes the timeline->source
14401456 /// mapping, so the running decoder stays correct and playback does not
crates/engine/src/project.rsmodified+115 -0
@@ -901,6 +901,41 @@ impl Project {
901901 Ok(())
902902 }
903903
904+ /// Moves `lane` to display position `to` -- an index into
905+ /// [`Project::lanes`], the slot the lane there gives up -- and hands back
906+ /// the handle it answers to from now on. `None`, changing nothing and
907+ /// snapshotting nothing, for a lane that is not there, a position that is
908+ /// not, and a lane already standing in it.
909+ ///
910+ /// The whole track travels: its clips, its gain, its kind. Nothing is
911+ /// remapped, so no take changes lane and no group is broken -- this is the
912+ /// *order* of the lanes and nothing else, which is why one call is one undo
913+ /// step like an add or a removal.
914+ ///
915+ /// Display order **is** the stack: [`Project::composite_span_at`] shows the
916+ /// last video lane covering a frame, so moving one video lane past another
917+ /// changes which picture wins, in the preview and in an export alike. The
918+ /// audio lanes are summed and their sum does not care about order, so an
919+ /// audio track moved among the video ones is a rearrangement of the screen
920+ /// alone.
921+ ///
922+ /// A label is a *position* ([`Lane::label`] reads `ord`, which is the
923+ /// position among the lanes of that kind), so a lane that crosses one of
924+ /// its own kind swaps names with it -- `V2` dragged above `V1` becomes
925+ /// `V1`, clips and all -- while one that crosses only lanes of the other
926+ /// kind keeps its name. That is the returned handle: unchanged means every
927+ /// handle a caller holds still names the track it named before.
928+ pub fn move_lane(&mut self, lane: Lane, to: usize) -> Option<Lane> {
929+ let i = self.index(lane)?;
930+ if to >= self.lanes.len() || to == i {
931+ return None;
932+ }
933+ self.snapshot();
934+ let data = self.lanes.remove(i);
935+ self.lanes.insert(to, data);
936+ Some(handles(&self.lanes)[to])
937+ }
938+
904939 /// Where `lane` sits in [`Project::lanes`], or `None` for a lane that is
905940 /// not there. The one place a handle becomes a position.
906941 fn index(&self, lane: Lane) -> Option<usize> {
@@ -5707,6 +5742,86 @@ mod tests {
57075742 assert_eq!(p.lane_spans(v3), vec![(0, 3)]);
57085743 }
57095744
5745+ /// A track dragged to another place in the stack: the whole lane travels --
5746+ /// clips, gain and kind -- nothing is remapped, an audio track may sit above
5747+ /// a video one, a lane that crosses one of its own kind swaps names with it
5748+ /// (and swaps the composite with it), one undo puts the order back, and a
5749+ /// save carries it.
5750+ #[test]
5751+ fn a_track_is_dragged_to_another_place_in_the_stack() {
5752+ let mut p = three();
5753+ assert!(p.set_lane_gain_db(Lane::A1, -6.0));
5754+ let before = shape(&p);
5755+ let history = p.history.len();
5756+
5757+ // A1 over V1: the screen is rearranged and nothing else is. Both keep
5758+ // their names -- neither crossed a lane of its own kind -- so every
5759+ // handle a front-end holds still names the track it named before.
5760+ assert_eq!(p.move_lane(Lane::A1, 0), Some(Lane::A1));
5761+ assert_eq!(p.lanes(), vec![Lane::A1, Lane::V1], "A1 is on top now");
5762+ assert_eq!(
5763+ shape(&p),
5764+ vec![before[1].clone(), before[0].clone()],
5765+ "each lane's clips travelled with it, and none was remapped"
5766+ );
5767+ assert_eq!(p.lane_gain_db(Lane::A1), -6.0, "so did its gain");
5768+ assert_eq!(p.history.len(), history + 1, "one snapshot per move");
5769+ invariants_hold(&p, "A1 over V1");
5770+
5771+ // Nothing moves for a lane that is not there, a slot that is not, or a
5772+ // lane already standing in the one it was pointed at.
5773+ assert_eq!(p.move_lane(Lane::new(LaneKind::Video, 9), 0), None);
5774+ assert_eq!(p.move_lane(Lane::V1, 2), None, "there is no third slot");
5775+ assert_eq!(p.move_lane(Lane::A1, 0), None, "already there");
5776+ assert_eq!(p.history.len(), history + 1, "and none of those snapshot");
5777+
5778+ // ...and one stroke puts the order back.
5779+ assert!(p.undo());
5780+ assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1]);
5781+ assert_eq!(shape(&p), before);
5782+
5783+ // A second video lane over the first: display order *is* the stack, so
5784+ // the last video lane covering a frame is the picture that shows.
5785+ let v2 = p.add_lane(LaneKind::Video);
5786+ assert!(p.place(v2, 0, clip(0, 0, 5, 0)));
5787+ let over = shape(&p)[2].clone();
5788+ assert_eq!(
5789+ p.composite_span_at(0).map(|s| s.len),
5790+ Some(5),
5791+ "V2's five frames are what frame 0 shows"
5792+ );
5793+
5794+ // V2 dragged to the top takes V1's slot -- and V1's name, a label being
5795+ // a position among the lanes of its kind. The picture that wins changes
5796+ // with it: the take that was covering is the covered one now.
5797+ assert_eq!(p.move_lane(v2, 0), Some(Lane::V1), "a label is a position");
5798+ assert_eq!(p.lanes(), vec![Lane::V1, v2, Lane::A1]);
5799+ assert_eq!(
5800+ shape(&p),
5801+ vec![over.clone(), before[0].clone(), before[1].clone()],
5802+ "V2's clips answer to V1 now, the old V1's to V2, and A1 kept its own"
5803+ );
5804+ assert_eq!(
5805+ p.composite_span_at(0).map(|s| s.len),
5806+ Some(3),
5807+ "the lower video lane wins, and it is the old V1 now"
5808+ );
5809+ invariants_hold(&p, "V2 over V1");
5810+
5811+ // A save writes that order and a load takes it back.
5812+ let (sources, lanes, eq, color) = p.without_orphan_sources();
5813+ assert_eq!(
5814+ lanes.iter().map(|(k, _)| *k).collect::<Vec<_>>(),
5815+ [LaneKind::Video, LaneKind::Video, LaneKind::Audio]
5816+ );
5817+ let back = Project::from_parts(sources, lanes, eq, color).expect("three lanes load");
5818+ assert_eq!(back.lanes(), p.lanes());
5819+ assert_eq!(shape(&back), shape(&p), "the same lane is still on top");
5820+
5821+ assert!(p.undo(), "and the stack goes back to how it stood");
5822+ assert_eq!(shape(&p), vec![before[0].clone(), before[1].clone(), over]);
5823+ }
5824+
57105825 /// The grouping rule across more than two lanes: a link id is one *span*,
57115826 /// on however many lanes carry it -- not a video/audio pair and not a
57125827 /// pairing by `ord`. So a split shares an id with every lane whose half