forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-a5b80eb9ab60336fe'

Browse files at this commit

b128a34df8abdba4e7431a6c2f84ce20e0517f4eAhmet Kaan GümüşParent: 2c7cf3730e8aae
crates/app/src/keymap.rsmodified+24 -4
@@ -48,6 +48,8 @@ pub enum ActionId {
4848 Delete,
4949 Lift,
5050 Undo,
51+ AddVideoLane,
52+ AddAudioLane,
5153 ToggleMute,
5254 VolumeUp,
5355 VolumeDown,
@@ -57,7 +59,7 @@ pub enum ActionId {
5759 impl ActionId {
5860 /// Display order everywhere -- the editor lists them in it and
5961 /// [`Keymap::defaults`] binds them in it.
60- pub const ALL: [ActionId; 14] = [
62+ pub const ALL: [ActionId; 16] = [
6163 ActionId::Play,
6264 ActionId::Export,
6365 ActionId::Save,
@@ -68,6 +70,8 @@ impl ActionId {
6870 ActionId::Delete,
6971 ActionId::Lift,
7072 ActionId::Undo,
73+ ActionId::AddVideoLane,
74+ ActionId::AddAudioLane,
7175 ActionId::ToggleMute,
7276 ActionId::VolumeUp,
7377 ActionId::VolumeDown,
@@ -87,6 +91,8 @@ impl ActionId {
8791 ActionId::Delete => "Delete",
8892 ActionId::Lift => "Lift (leave a gap)",
8993 ActionId::Undo => "Undo",
94+ ActionId::AddVideoLane => "Add a video track",
95+ ActionId::AddAudioLane => "Add an audio track",
9096 ActionId::ToggleMute => "Mute / Unmute",
9197 ActionId::VolumeUp => "Volume up",
9298 ActionId::VolumeDown => "Volume down",
@@ -108,6 +114,8 @@ impl ActionId {
108114 ActionId::Delete => "delete",
109115 ActionId::Lift => "lift",
110116 ActionId::Undo => "undo",
117+ ActionId::AddVideoLane => "add-video-lane",
118+ ActionId::AddAudioLane => "add-audio-lane",
111119 ActionId::ToggleMute => "toggle-mute",
112120 ActionId::VolumeUp => "volume-up",
113121 ActionId::VolumeDown => "volume-down",
@@ -126,7 +134,11 @@ impl ActionId {
126134 match self {
127135 ActionId::Play => Category::Playback,
128136 ActionId::Copy | ActionId::Paste | ActionId::Delete | ActionId::Lift => Category::Clips,
129- ActionId::Cut | ActionId::Regroup | ActionId::Undo => Category::Editing,
137+ ActionId::Cut
138+ | ActionId::Regroup
139+ | ActionId::Undo
140+ | ActionId::AddVideoLane
141+ | ActionId::AddAudioLane => Category::Editing,
130142 ActionId::ToggleMute | ActionId::VolumeUp | ActionId::VolumeDown => Category::Audio,
131143 ActionId::Save | ActionId::Export | ActionId::CancelExport => Category::File,
132144 }
@@ -310,6 +322,11 @@ impl Keymap {
310322 b(ActionId::Lift, "l", false),
311323 b(ActionId::Undo, "z", false),
312324 b(ActionId::Undo, "z", true),
325+ // The unshifted initials of what they add. Both were free --
326+ // the copy and paste chords are the *ctrl* ones -- and a track
327+ // is added often enough to deserve a key that is one press.
328+ b(ActionId::AddVideoLane, "v", false),
329+ b(ActionId::AddAudioLane, "a", false),
313330 b(ActionId::ToggleMute, "m", false),
314331 // The unshifted pair, which is what gpui reports for those two
315332 // keys ("=" and "-", platform.rs:862): the volume keys every
@@ -542,7 +559,7 @@ mod tests {
542559 #[test]
543560 fn every_default_stroke_reaches_its_action() {
544561 let k = Keymap::defaults();
545- assert_eq!(k.entries().len(), 16);
562+ assert_eq!(k.entries().len(), 18);
546563 assert_eq!(k.lookup("space", false), Some(ActionId::Play));
547564 assert_eq!(k.lookup("e", false), Some(ActionId::Export));
548565 assert_eq!(k.lookup("s", true), Some(ActionId::Save));
@@ -555,6 +572,9 @@ mod tests {
555572 assert_eq!(k.lookup("l", false), Some(ActionId::Lift));
556573 assert_eq!(k.lookup("z", false), Some(ActionId::Undo));
557574 assert_eq!(k.lookup("z", true), Some(ActionId::Undo));
575+ // The track keys are the bare letters; the ctrl ones stay copy/paste.
576+ assert_eq!(k.lookup("v", false), Some(ActionId::AddVideoLane));
577+ assert_eq!(k.lookup("a", false), Some(ActionId::AddAudioLane));
558578 assert_eq!(k.lookup("m", false), Some(ActionId::ToggleMute));
559579 // The volume pair is the unshifted one, so neither needs a modifier
560580 // and neither is the "+" gpui reports for shift+=.
@@ -689,7 +709,7 @@ mod tests {
689709 );
690710 // The label is the editor's column, and never the file's word for it.
691711 assert_eq!(ActionId::CancelExport.label(), "Cancel export");
692- assert_eq!(ActionId::ALL.len(), 14);
712+ assert_eq!(ActionId::ALL.len(), 16);
693713 }
694714
695715 #[test]
crates/app/src/main.rsmodified+307 -70
@@ -50,6 +50,10 @@ const HEADER_H: f32 = 32.;
5050 // between them, with a few px of slack so a taller text line cannot push a lane
5151 // off the bottom.
5252 const PANEL_H: f32 = 220.;
53+/// How many lane rows are drawn before the lane column starts scrolling: past
54+/// this the panel would be taller than the picture it belongs under, and a
55+/// timeline that pushes the video off the window is not a timeline.
56+const LANES_MAX: usize = 6;
5357 const LANE_H: f32 = 48.;
5458 /// The lane header column: wide enough for `V1`/`A1` and fixed, so both lanes
5559 /// and the ruler above them start at the same pixel and are the same width --
@@ -428,6 +432,8 @@ impl Player {
428432 ActionId::Delete => self.delete_selected(cx),
429433 ActionId::Lift => self.lift_selected(cx),
430434 ActionId::Undo => self.undo(cx),
435+ ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx),
436+ ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx),
431437 ActionId::ToggleMute => self.set_volume(|volume| volume.muted = !volume.muted, cx),
432438 ActionId::VolumeUp => self.set_volume(|volume| volume.step(true), cx),
433439 ActionId::VolumeDown => self.set_volume(|volume| volume.step(false), cx),
@@ -502,32 +508,20 @@ impl Player {
502508 return;
503509 }
504510 let selected = self.selected.take();
505- // Exhaustive over the kind, and explicit about the ord: both engine
506- // calls below take a *first*-lane index (`delete_clip` deletes the V1
507- // clip at `idx`, `video_half` reads A1), so a selection on a second
508- // lane of either kind would delete a different clip than the one
509- // clicked. It is refused by name until the add-track slice teaches
510- // those two a lane -- a wildcard here would drop it silently.
511+ // Whichever lane it was clicked in: the index is that lane's own, and
512+ // the ripple cuts the clip's span out of every lane -- a group covers
513+ // one span, so deleting a take by its audio half is the same edit as by
514+ // its picture. What is not a whole take is lifted instead, which is what
515+ // reaches a clip on an added track ([`whole_take`]).
511516 let deleted = match (&mut self.session, selected) {
512- (Some(session), Some((lane, idx))) if lane.ord == 0 => match lane.kind {
513- LaneKind::Video => session.delete_clip(idx),
514- LaneKind::Audio => match video_half(session, idx) {
515- Some(video) => session.delete_clip(video),
516- None => session.lift_clip(lane, idx),
517- },
517+ (Some(session), Some((lane, idx))) => match whole_take(session, lane, idx) {
518+ true => session.delete_clip(lane, idx),
519+ false => session.lift_clip(lane, idx),
518520 },
519521 _ => false,
520522 };
521- if let Some((lane, _)) = selected.filter(|_| !deleted) {
522- if lane.ord > 0 {
523- self.notice = Some(
524- format!(
525- "NOTHING DELETED — delete does not reach {} yet",
526- lane.label()
527- )
528- .into(),
529- );
530- }
523+ if selected.is_some() && !deleted {
524+ self.notice = Some("NOTHING DELETED — the timeline cannot be emptied".into());
531525 }
532526 if deleted {
533527 self.reset_after_reseek();
@@ -677,14 +671,15 @@ impl Player {
677671 if self.exporting().is_some() {
678672 return;
679673 }
680- // A file with no picture belongs on the audio lane and nowhere else:
681- // dropped on the video lane it is refused by name, and asked for by the
674+ // A file with no picture belongs on an audio lane and nowhere else:
675+ // dropped on a video lane it is refused by name, and asked for by the
682676 // Add button (which names no lane) it goes to the audio one -- which is
683677 // the engine's choice, in `place_stream_at`, not one made twice here.
684- if engine::is_audio(path) && onto == Some(Lane::V1) {
678+ if engine::is_audio(path) && onto.is_some_and(|lane| lane.kind == LaneKind::Video) {
685679 let name = file_name(path);
680+ let lane = onto.expect("checked above").label();
686681 self.notice = Some(
687- format!("NOT ON THE VIDEO LANE — {name} has no picture; drop it on A1").into(),
682+ format!("NOT ON {lane} — {name} has no picture; drop it on an audio lane").into(),
688683 );
689684 cx.notify();
690685 return;
@@ -696,7 +691,9 @@ impl Player {
696691 // -- an undone import leaves the source entry behind (project.rs:264)
697692 // -- so there is no length to insert and no file to ask for one.
698693 let placed = match (&mut self.session, frames) {
699- (Some(session), 1..) => session.place_stream_at(session.now(), path, stream, frames),
694+ (Some(session), 1..) => {
695+ session.place_stream_at(session.now(), path, stream, frames, onto)
696+ }
700697 _ => Ok(false),
701698 };
702699 match placed {
@@ -875,6 +872,31 @@ impl Player {
875872 cx.notify();
876873 }
877874
875+ /// A new empty track under the ones already there. One undo step in the
876+ /// engine, so the stroke that takes back an edit takes back a track too, and
877+ /// no reseek: nothing plays differently until something is dropped on it.
878+ /// The selection stays -- the lanes it indexes into have not moved.
879+ fn add_lane(&mut self, kind: LaneKind, cx: &mut Context<Self>) {
880+ if self.exporting().is_some() {
881+ return;
882+ }
883+ match &mut self.session {
884+ Some(session) => {
885+ let lane = session.add_lane(kind);
886+ self.notice = Some(
887+ format!(
888+ "{} ADDED — drag a clip onto it, {} takes it back",
889+ lane.label(),
890+ self.keymap.display(ActionId::Undo)
891+ )
892+ .into(),
893+ );
894+ }
895+ None => self.notice = Some("NO TRACK ADDED — open a file first".into()),
896+ }
897+ cx.notify();
898+ }
899+
878900 fn undo(&mut self, cx: &mut Context<Self>) {
879901 if self.session.as_mut().is_some_and(PlaybackSession::undo) {
880902 self.reset_after_reseek();
@@ -998,6 +1020,12 @@ impl Player {
9981020 /// written to a path ending in `.mp4` is a file every player will lie
9991021 /// about -- keeping whatever stem the save dialog last left there.
10001022 fn set_format(&mut self, format: Format) {
1023+ // The one door both the row and its initial go through, so a format the
1024+ // card greys out cannot be picked by keyboard either.
1025+ if let (Format::Mp4, Some(why)) = (format, self.session.as_ref().and_then(mp4_refusal)) {
1026+ self.notice = Some(format!("NOT MP4 — {why}").into());
1027+ return;
1028+ }
10011029 self.format = format;
10021030 self.export_path = retarget(&self.export_path, format);
10031031 }
@@ -1591,6 +1619,18 @@ impl Player {
15911619 // open they are dimmed rather than silently doing nothing.
15921620 let live = self.session.is_some() && !exporting;
15931621 let key = |action| self.keymap.display(action);
1622+ // The lanes the project has, or the pair a fresh one starts with so the
1623+ // panel reads the same before a file is open as after.
1624+ let lanes = self
1625+ .session
1626+ .as_ref()
1627+ .map_or_else(|| vec![Lane::V1, Lane::A1], PlaybackSession::lanes);
1628+ // A loop rather than a `map`: each row takes `cx` in turn, where a
1629+ // closure would hold it for as long as the iterator lives.
1630+ let mut rows = Vec::new();
1631+ for &lane in &lanes {
1632+ rows.push(self.lane_row(lane, duration, filled, cx));
1633+ }
15941634 let (hint, filled) = if let Some(export) = self.exporting() {
15951635 let progress = export.progress();
15961636 (
@@ -1618,7 +1658,7 @@ impl Player {
16181658 };
16191659 div()
16201660 .flex_none()
1621- .h(px(PANEL_H))
1661+ .h(px(panel_h(lanes.len())))
16221662 .flex()
16231663 .flex_col()
16241664 .gap(px(8.))
@@ -1665,6 +1705,36 @@ impl Player {
16651705 live && self.selected.is_some(),
16661706 cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)),
16671707 ))
1708+ // With the edit group, beside the buttons that change the
1709+ // edit list: a track is a row of it, and adding one is an
1710+ // undoable edit like the rest. Two buttons rather than one
1711+ // with a choice, because the choice is the whole action.
1712+ .child(control(
1713+ "add-video-lane",
1714+ None,
1715+ "+ V",
1716+ format!(
1717+ "{} — adds a video track under the ones there",
1718+ key(ActionId::AddVideoLane)
1719+ ),
1720+ live,
1721+ cx.listener(|this, _: &ClickEvent, _, cx| {
1722+ this.add_lane(LaneKind::Video, cx)
1723+ }),
1724+ ))
1725+ .child(control(
1726+ "add-audio-lane",
1727+ None,
1728+ "+ A",
1729+ format!(
1730+ "{} — adds an audio track under the ones there",
1731+ key(ActionId::AddAudioLane)
1732+ ),
1733+ live,
1734+ cx.listener(|this, _: &ClickEvent, _, cx| {
1735+ this.add_lane(LaneKind::Audio, cx)
1736+ }),
1737+ ))
16681738 // With the transport, not the edit group: it changes what
16691739 // the file sounds like, never what it is. Reads as its own
16701740 // state, so muted is legible without a tooltip.
@@ -1799,8 +1869,22 @@ impl Player {
17991869 ),
18001870 ),
18011871 )
1802- .child(self.lane_row(Lane::V1, "V1", duration, filled, cx))
1803- .child(self.lane_row(Lane::A1, "A1", duration, filled, cx))
1872+ // Every lane the project has, in its own order -- and its own
1873+ // column, so a project with more lanes than the panel is tall
1874+ // scrolls its tracks instead of pushing the picture off the window.
1875+ // The gap is the panel's own, so two lanes lay out exactly as they
1876+ // did when they were two children of it.
1877+ .child(
1878+ div()
1879+ .id("lanes")
1880+ .flex_none()
1881+ .flex()
1882+ .flex_col()
1883+ .gap(px(8.))
1884+ .max_h(px(lanes_h(LANES_MAX)))
1885+ .overflow_y_scroll()
1886+ .children(rows),
1887+ )
18041888 }
18051889
18061890 /// A notice holds its own bar, full width, until it is answered: any key
@@ -2027,11 +2111,21 @@ impl Player {
20272111 };
20282112 // The formats first: the quality rows below are *video* bitrate, so
20292113 // which file is being written decides whether they mean anything.
2114+ // An mp4 this timeline cannot be written as reads exactly like the
2115+ // formats there is no encoder for: dimmed, unclickable, and carrying its
2116+ // own reason instead of the detail line.
2117+ let refused = self.session.as_ref().and_then(mp4_refusal);
20302118 let formats: Vec<_> = FORMATS
20312119 .into_iter()
20322120 .enumerate()
20332121 .map(|(i, (format, label, detail))| {
2034- let picked = format == Some(self.format);
2122+ let blocked = format == Some(Format::Mp4) && refused.is_some();
2123+ let detail: SharedString = match &refused {
2124+ Some(why) if blocked => why.clone().into(),
2125+ _ => detail.into(),
2126+ };
2127+ let picked = format == Some(self.format) && !blocked;
2128+ let format = format.filter(|_| !blocked);
20352129 live(row(("format", i)), format.is_some())
20362130 .when(picked, |d| d.bg(rgb(SELECTED)))
20372131 .when_some(format, |d, format| {
@@ -2363,27 +2457,36 @@ impl Player {
23632457 fn lane_row(
23642458 &self,
23652459 lane: Lane,
2366- name: &'static str,
23672460 duration: f64,
23682461 filled: f32,
23692462 cx: &mut Context<Self>,
2370- ) -> impl IntoElement {
2463+ // Borrows nothing it was given (`use<>`): the rows are built one after
2464+ // another into a list, and a row still holding `cx` would be the only
2465+ // one that could be built.
2466+ ) -> impl IntoElement + use<> {
23712467 // The bed's own width, measured last repaint off the ruler's bar: the
23722468 // two are laid out identically (same header offset, same `flex_1`), so
23732469 // one probe answers for both. Zero before the first paint, which only
23742470 // costs the labels one frame.
23752471 let bed_w = f32::from(self.ruler.get().size.width);
2376- let (clips, others) = match &self.session {
2377- Some(session) => (
2378- session.lane_clips(lane),
2379- session.lane_clips(if lane.kind == LaneKind::Video {
2380- Lane::A1
2381- } else {
2382- Lane::V1
2383- }),
2384- ),
2385- None => (&[][..], &[][..]),
2386- };
2472+ let clips = self
2473+ .session
2474+ .as_ref()
2475+ .map_or(&[][..], |session| session.lane_clips(lane));
2476+ // The group ids some *other* lane carries: a clip whose id is in here
2477+ // has a half elsewhere, and one whose is not is a detached half however
2478+ // many lanes there are.
2479+ let others: Vec<u32> = self.session.as_ref().map_or_else(Vec::new, |session| {
2480+ session
2481+ .lanes()
2482+ .into_iter()
2483+ .filter(|&other| other != lane)
2484+ .flat_map(|other| session.lane_clips(other))
2485+ .filter_map(|clip| clip.link)
2486+ .collect()
2487+ });
2488+ let name = lane.label();
2489+ let row_id: SharedString = format!("{name}-clip").into();
23872490 let sources = self
23882491 .session
23892492 .as_ref()
@@ -2447,8 +2550,7 @@ impl Player {
24472550 // A group with a half in the other lane wears its tint;
24482551 // one without is outlined, so a detached half is visible
24492552 // as detached before anyone clicks it.
2450- let grouped =
2451- clip.link.is_some() && others.iter().any(|o| o.link == clip.link);
2553+ let grouped = clip.link.is_some_and(|link| others.contains(&link));
24522554 // Tinted by *file*, not by source entry: two audio
24532555 // streams of one file are two sources, and the library
24542556 // gives them one swatch because they are one file.
@@ -2470,7 +2572,9 @@ impl Player {
24702572 );
24712573 let tip = tip.clone();
24722574 div()
2473- .id((if audio { "aclip" } else { "vclip" }, i))
2575+ // Named per lane: two rows numbering their clips
2576+ // from zero would hand gpui the same id twice.
2577+ .id((row_id.clone(), i))
24742578 .absolute()
24752579 .top_0()
24762580 .h_full()
@@ -2708,12 +2812,14 @@ fn unseen_paths(sources: &[Source], streams: &HashMap<PathBuf, Vec<StreamInfo>>)
27082812 out
27092813 }
27102814
2711-/// Both lanes' clips, which is everything the timeline knows about its sources.
2815+/// Every lane's clips, which is everything the timeline knows about its sources.
2816+/// Every lane, not the first two: a clip that plays only on `V2` is still a clip
2817+/// the source is used by, and a walk that missed it would call the file unused.
27122818 fn lane_clips(session: &PlaybackSession) -> impl Iterator<Item = &Clip> {
27132819 session
2714- .lane_clips(Lane::V1)
2715- .iter()
2716- .chain(session.lane_clips(Lane::A1))
2820+ .lanes()
2821+ .into_iter()
2822+ .flat_map(|lane| session.lane_clips(lane))
27172823 }
27182824
27192825 /// How long a source is, as the timeline knows it: the furthest frame any clip
@@ -3121,6 +3227,37 @@ fn is_bare_modifier(key: &str) -> bool {
31213227
31223228 /// A clip's share of the lane. A timeline with no length reads as one full-width
31233229 /// box rather than as NaN, which gpui would carry into layout.
3230+/// Why this timeline cannot be written as an mp4, if it cannot: the mp4 path
3231+/// *copies* one AAC track and there is no encoder here to mix several with, so a
3232+/// timeline whose sound is spread over more than one audio lane is refused by
3233+/// the engine (`export::run`). Said on the row, before a destination has been
3234+/// picked, rather than after the write has started.
3235+fn mp4_refusal(session: &PlaybackSession) -> Option<String> {
3236+ let lanes = session
3237+ .lanes()
3238+ .into_iter()
3239+ .filter(|&lane| lane.kind == LaneKind::Audio && !session.lane_clips(lane).is_empty())
3240+ .count();
3241+ (lanes > 1).then(|| format!("{lanes} audio lanes — an mp4 copies one"))
3242+}
3243+
3244+/// How tall a column of `lanes` rows is, gaps included -- the panel's own gap
3245+/// between them, since the rows sit in it.
3246+fn lanes_h(lanes: usize) -> f32 {
3247+ match lanes {
3248+ 0 => 0.,
3249+ n => n as f32 * LANE_H + (n - 1) as f32 * 8.,
3250+ }
3251+}
3252+
3253+/// How tall the panel is with `lanes` tracks in it: [`PANEL_H`] is sized for the
3254+/// two a project starts with, and every further one adds its own row -- up to
3255+/// [`LANES_MAX`], past which the lane column scrolls instead and the panel stops
3256+/// growing.
3257+fn panel_h(lanes: usize) -> f32 {
3258+ PANEL_H + lanes_h(lanes.clamp(2, LANES_MAX)) - lanes_h(2)
3259+}
3260+
31243261 fn width_frac(len: f64, total: f64) -> f32 {
31253262 if total > 0. { (len / total) as f32 } else { 1. }
31263263 }
@@ -3136,15 +3273,31 @@ fn start_frac(start: f64, total: f64) -> f32 {
31363273 }
31373274 }
31383275
3139-/// The video-lane index of the take an audio clip belongs to, if the video lane
3140-/// still holds that half. `None` for a half whose picture was lifted -- which is
3141-/// what makes it a thing of its own to delete.
3142-fn video_half(session: &PlaybackSession, audio: usize) -> Option<usize> {
3143- let link = session.lane_clips(Lane::A1).get(audio)?.link?;
3144- session
3145- .lane_clips(Lane::V1)
3146- .iter()
3147- .position(|clip| clip.link == Some(link))
3276+/// Whether this clip is a whole take, i.e. whether deleting it may close the
3277+/// hole under it: a take is what the first pair of lanes carries between them,
3278+/// `V1`'s picture and the sound grouped with it, and dropping one moves the
3279+/// frames after it on every lane.
3280+///
3281+/// Everything else is a half or a layer, and is *lifted* instead: a half whose
3282+/// picture was lifted (what a lift leaves behind) has no take to ripple, and a
3283+/// clip on a further lane is laid over the timeline rather than part of it --
3284+/// closing a hole under it would drag the take beneath out of step with it.
3285+fn whole_take(session: &PlaybackSession, lane: Lane, idx: usize) -> bool {
3286+ let Some(clip) = session.lane_clips(lane).get(idx) else {
3287+ return false;
3288+ };
3289+ match (lane.kind, lane.ord) {
3290+ (_, 1..) => false,
3291+ (LaneKind::Video, _) => true,
3292+ // The sound of a take, only while the take is still there: its group is
3293+ // carried by a clip on some other lane.
3294+ (LaneKind::Audio, _) => session
3295+ .lanes()
3296+ .into_iter()
3297+ .filter(|&other| other != lane)
3298+ .flat_map(|other| session.lane_clips(other))
3299+ .any(|o| o.link.is_some() && o.link == clip.link),
3300+ }
31483301 }
31493302
31503303 /// Whether a click marks this clip: the clip that was clicked always, and the
@@ -3428,14 +3581,14 @@ fn timecode(t: f64, fps: f64) -> String {
34283581 mod tests {
34293582 use super::{
34303583 ACCENT, CONTROL_H, Clip, EXPORT_ROWS_H, FORMATS, Format, HEADER_GAP, HEADER_W, HIT_MIN,
3431- INK, INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LETTERBOX,
3432- LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H, Quality, ROW_H,
3433- RULER_HIT_H, SELECTED, SOURCE_TINTS, SURFACE, SWATCH_W, Source, StreamInfo, Volume,
3434- WAVE_BPS, WAVE_COL, Wave, applicable, can_add, cancels_export, envelope, export_path,
3435- export_settings, format_line, frac_along, frame_at, is_bare_modifier, is_project, keymap,
3436- lane_clips, marked, menu_at, normalise, project_path, push_digit, retarget, scrub_due,
3437- show_label, source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources,
3438- width_frac, window_title,
3584+ INK, INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX,
3585+ LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H,
3586+ Quality, ROW_H, RULER_HIT_H, SELECTED, SOURCE_TINTS, SURFACE, SWATCH_W, Source, StreamInfo,
3587+ Volume, WAVE_BPS, WAVE_COL, Wave, applicable, can_add, cancels_export, envelope,
3588+ export_path, export_settings, format_line, frac_along, frame_at, is_bare_modifier,
3589+ is_project, keymap, lane_clips, lanes_h, marked, menu_at, mp4_refusal, normalise, panel_h,
3590+ project_path, push_digit, retarget, scrub_due, show_label, source_frames, source_tint,
3591+ start_frac, timecode, unseen_paths, unseen_sources, whole_take, width_frac, window_title,
34393592 };
34403593 use super::{file_name, library_rows};
34413594 use engine::PlaybackSession;
@@ -3740,7 +3893,7 @@ mod tests {
37403893 // stream: the button, the drop and this are one call.
37413894 assert!(
37423895 session
3743- .place_stream_at(2.0, &second, 0, frames)
3896+ .place_stream_at(2.0, &second, 0, frames, None)
37443897 .expect("av2 is already on the timeline")
37453898 );
37463899 // The whole of source 1 went in and nothing was painted over: the
@@ -3772,7 +3925,7 @@ mod tests {
37723925 let end = session.timeline_duration();
37733926 assert!(
37743927 session
3775- .place_stream_at(end, &path, 1, frames)
3928+ .place_stream_at(end, &path, 1, frames, None)
37763929 .expect("the French track shares the timeline's parameters")
37773930 );
37783931 assert_eq!(session.sources()[1].audio_stream, 1);
@@ -3785,6 +3938,74 @@ mod tests {
37853938 );
37863939 }
37873940
3941+ /// The add-a-track path end to end through the doors the buttons and the
3942+ /// drop use: `+ V` adds a row, a library row let go over it lands there and
3943+ /// nowhere else, Delete on it leaves the lanes under it where they are, and
3944+ /// undo takes the whole thing back one step at a time.
3945+ #[test]
3946+ fn a_track_can_be_added_dropped_on_edited_and_taken_back() {
3947+ use engine::project::LaneKind;
3948+
3949+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
3950+ session.set_gain(0.0);
3951+ assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1]);
3952+ // What the `+ V` button asks for, and what the row it draws is called.
3953+ let v2 = session.add_lane(LaneKind::Video);
3954+ assert_eq!(v2.label(), "V2");
3955+ assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1, v2]);
3956+ assert!(session.lane_clips(v2).is_empty());
3957+ // A library row let go over that row: the same door the Add button
3958+ // uses, told which lane it was let go over.
3959+ let path = session.sources()[0].path.clone();
3960+ let frames = source_frames(lane_clips(&session), session.sources(), &path);
3961+ assert!(
3962+ session
3963+ .place_stream_at(1.0, &path, 0, frames, Some(v2))
3964+ .expect("its own file is on this timeline")
3965+ );
3966+ assert_eq!(session.lane_clips(v2).len(), 1, "the drop landed on V2");
3967+ assert_eq!(session.lane_clips(v2)[0].start, 30, "at the playhead");
3968+ // And nowhere else: the first pair is exactly as it was, one take each.
3969+ assert_eq!(session.lane_clips(Lane::V1).len(), 1);
3970+ assert_eq!(session.lane_clips(Lane::A1).len(), 1);
3971+ // Delete on the layer is a lift: it is laid over the timeline, so
3972+ // closing a hole under it would drag the take beneath out of step with
3973+ // it. The take on the first pair is still a take, and still ripples.
3974+ assert!(!whole_take(&session, v2, 0));
3975+ assert!(whole_take(&session, Lane::V1, 0));
3976+ assert!(whole_take(&session, Lane::A1, 0));
3977+ assert!(session.lift_clip(v2, 0));
3978+ assert!(session.lane_clips(v2).is_empty());
3979+ assert_eq!(session.lane_clips(Lane::V1).len(), 1, "V1 stayed put");
3980+
3981+ // A second audio lane carrying sound is what an mp4 export cannot
3982+ // write, so the card greys that row before a destination is picked.
3983+ assert_eq!(mp4_refusal(&session), None);
3984+ let a2 = session.add_lane(LaneKind::Audio);
3985+ assert_eq!(a2.label(), "A2");
3986+ assert_eq!(mp4_refusal(&session), None, "an empty lane carries nothing");
3987+ assert!(
3988+ session
3989+ .place_stream_at(0.0, &path, 0, frames, Some(a2))
3990+ .expect("its own file is on this timeline")
3991+ );
3992+ assert_eq!(
3993+ mp4_refusal(&session).as_deref(),
3994+ Some("2 audio lanes — an mp4 copies one")
3995+ );
3996+
3997+ // Undo, one edit at a time and backwards: the drop on A2, the lane A2
3998+ // itself, the lift, the drop on V2, and last the lane V2 -- an added
3999+ // track is one step like every other edit.
4000+ for lanes in [4, 3, 3, 3, 2] {
4001+ assert!(session.undo());
4002+ assert_eq!(session.lanes().len(), lanes);
4003+ }
4004+ assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1]);
4005+ assert_eq!(session.lane_clips(Lane::V1).len(), 1);
4006+ assert_eq!(mp4_refusal(&session), None);
4007+ }
4008+
37884009 #[test]
37894010 fn a_source_is_only_ever_asked_for_its_peaks_once() {
37904011 let (a, b) = (asset("test_av.mp4"), asset("test_av2.mp4"));
@@ -4301,6 +4522,22 @@ mod tests {
43014522 assert!(LANE_H >= HIT_MIN);
43024523 // A label row that ate the whole lane would leave no waveform.
43034524 assert!(LABEL_H < LANE_H / 2.);
4525+ // An added track adds its own row to the panel, and the two a project
4526+ // starts with leave it exactly the height it has always been.
4527+ assert_eq!(panel_h(2), PANEL_H);
4528+ assert_eq!(panel_h(1), PANEL_H);
4529+ assert_eq!(panel_h(3), PANEL_H + LANE_H + 8.);
4530+ assert_eq!(
4531+ panel_h(LANES_MAX),
4532+ PANEL_H + lanes_h(LANES_MAX) - lanes_h(2)
4533+ );
4534+ // Past the cap the column scrolls instead: the panel stops growing, so
4535+ // no number of tracks can push the picture off the window.
4536+ assert_eq!(panel_h(LANES_MAX + 1), panel_h(LANES_MAX));
4537+ assert_eq!(panel_h(50), panel_h(LANES_MAX));
4538+ assert_eq!(lanes_h(0), 0.);
4539+ assert_eq!(lanes_h(1), LANE_H);
4540+ assert_eq!(lanes_h(2), 2. * LANE_H + 8.);
43044541 }
43054542
43064543 #[test]
crates/engine/src/playback.rsmodified+48 -12
@@ -453,6 +453,19 @@ impl PlaybackSession {
453453 self.project.lane(lane)
454454 }
455455
456+ /// Every lane's handle, in display order -- what a front-end lays out top to
457+ /// bottom, and the list [`add_lane`](Self::add_lane) grows.
458+ pub fn lanes(&self) -> Vec<Lane> {
459+ self.project.lanes()
460+ }
461+
462+ /// Appends an empty lane and hands back its handle. One undo step
463+ /// ([`Project::add_lane`]), and no reseek: an empty lane changes nothing
464+ /// that plays until something is placed on it.
465+ pub fn add_lane(&mut self, kind: LaneKind) -> Lane {
466+ self.project.add_lane(kind)
467+ }
468+
456469 /// Splits every lane at `timeline_secs`, so the two sides become two
457470 /// groups. Metadata only: a split never changes the timeline->source
458471 /// mapping, so the running decoder stays correct and playback does not
@@ -510,16 +523,21 @@ impl PlaybackSession {
510523 /// A front-end greys such a row out; this is the backstop that keeps a
511524 /// stale one from making the whole timeline silent.
512525 ///
513- /// Which lane it lands on is decided here, from the file: one with a
514- /// picture is pasted across both lanes as a grouped take, one without
515- /// ([`crate::is_audio`]) is *placed* on the audio lane alone, overwriting
516- /// what it lands on and rippling nothing. A caller never has to ask.
526+ /// Which lane it lands on is decided here, from the file and from `onto` --
527+ /// the lane it was let go over, if it was let go over one at all: a file
528+ /// with a picture asked for by no lane, or for one of the first pair, is
529+ /// pasted across `V1` and `A1` as a grouped take; asked for by any further
530+ /// lane it is *placed* there alone, overwriting what it lands on and
531+ /// rippling nothing. A file with no picture ([`crate::is_audio`]) is placed
532+ /// on the audio lane it was asked for, or on `A1`, and never on a video
533+ /// lane. A caller never has to ask.
517534 pub fn place_stream_at(
518535 &mut self,
519536 timeline_secs: f64,
520537 path: &Path,
521538 stream: usize,
522539 frames: u32,
540+ onto: Option<Lane>,
523541 ) -> crate::Result<bool> {
524542 let wanted = Source::new(path, stream);
525543 // Another stream of a file whose picture is *already* on the timeline:
@@ -542,12 +560,26 @@ impl PlaybackSession {
542560 };
543561 // Which lane a source may land on is decided here and only here, so a
544562 // front-end never has to make the same call twice: a file with no
545- // picture goes on the audio lane alone, overwriting and rippling
563+ // picture goes on an audio lane alone, overwriting and rippling
546564 // nothing, because there is nothing on the video lane to move along
547- // with it.
548- Ok(match crate::is_audio(path) {
549- true => self.place_at(Lane::A1, timeline_secs, clip),
550- false => self.paste_at(timeline_secs, clip),
565+ // with it. A lane of the source's own kind takes it as it is dropped;
566+ // only `V1` means the grouped take, since that is the pair the paste
567+ // spans and a second video lane is a layer of its own.
568+ let onto = match (crate::is_audio(path), onto) {
569+ // No picture: an audio lane and nothing else, whichever one was
570+ // asked for -- `A1` when none was.
571+ (true, Some(lane)) if lane.kind == LaneKind::Audio => Some(lane),
572+ (true, _) => Some(Lane::A1),
573+ // A picture: the lane it was let go over, unless that is one of the
574+ // first pair. Those two are the grouped take a paste spans, and a
575+ // further lane is a layer of its own -- its picture on `V2`, its
576+ // sound on `A2`.
577+ (false, Some(lane)) if lane.ord > 0 => Some(lane),
578+ (false, _) => None,
579+ };
580+ Ok(match onto {
581+ Some(lane) => self.place_at(lane, timeline_secs, clip),
582+ None => self.paste_at(timeline_secs, clip),
551583 })
552584 }
553585
@@ -568,13 +600,17 @@ impl PlaybackSession {
568600 self.edit(|p| p.place(lane, at, clip))
569601 }
570602
571- /// Removes the video clip at `idx` and everything under it, closing the gap
603+ /// Removes `lane`'s clip at `idx` and everything under it, closing the gap
572604 /// on every lane. Unlike a split this *does* move every following frame, so
573605 /// the session reseeks to wherever the playhead now points.
574606 /// [`lift_clip`](Self::lift_clip) is the one that leaves a hole instead.
575607 /// `false` for a bad index or the last remaining clip.
576- pub fn delete_clip(&mut self, idx: usize) -> bool {
577- self.edit(|p| p.delete(idx))
608+ ///
609+ /// The lane travels because the index is a lane's own: `V2`'s third clip is
610+ /// not `V1`'s, and a front-end that could only say "the third clip" would
611+ /// delete the wrong one the moment a second lane exists.
612+ pub fn delete_clip(&mut self, lane: Lane, idx: usize) -> bool {
613+ self.edit(|p| p.delete_in(lane, idx))
578614 }
579615
580616 /// Undoes the last successful edit, and reseeks like a delete.
crates/engine/src/project.rsmodified+30 -9
@@ -919,13 +919,20 @@ impl Project {
919919 true
920920 }
921921
922- /// Insert `clip` into *every* lane at `timeline_frame` as one new group,
923- /// pushing everything from there on later by its length -- the grouped,
924- /// rippling paste a clipboard does. Mid-clip the clip it lands in is split
925- /// around it; at or past the end of the timeline it is appended, because a
926- /// paste means "put it here", not "put it here and leave black in front".
922+ /// Insert `clip` into the first lane of each kind at `timeline_frame` as one
923+ /// new group, pushing everything from there on later by its length in
924+ /// *every* lane -- the grouped, rippling paste a clipboard does. Mid-clip
925+ /// the clip it lands in is split around it; at or past the end of the
926+ /// timeline it is appended, because a paste means "put it here", not "put it
927+ /// here and leave black in front".
927928 /// Use [`place`](Project::place) to paste into one lane, or to make a gap.
928929 ///
930+ /// `V1` and `A1` and no other lane, because a take is one picture and one
931+ /// sound: copying it onto every lane there is would play the same audio
932+ /// twice over (and leave an mp4 export with two tracks to copy). The room is
933+ /// still opened everywhere, or the lanes it was not inserted into would slide
934+ /// out of step with the ones it was.
935+ ///
929936 /// Exactly one history snapshot, so one [`Project::undo`] takes it back.
930937 /// Changes the timeline->source mapping: the caller must reseek. Refused
931938 /// only for an empty `clip`, which the never-empty invariant forbids but the
@@ -941,10 +948,16 @@ impl Project {
941948 link: Some(self.new_link()),
942949 ..clip
943950 };
944- for data in &mut self.lanes {
951+ let takes: Vec<usize> = [LaneKind::Video, LaneKind::Audio]
952+ .into_iter()
953+ .filter_map(|kind| self.index(Lane::new(kind, 0)))
954+ .collect();
955+ for (i, data) in self.lanes.iter_mut().enumerate() {
945956 open_room(&mut data.clips, at, clip.len());
946- let idx = data.clips.partition_point(|c| c.start < at);
947- data.clips.insert(idx, clip);
957+ if takes.contains(&i) {
958+ let idx = data.clips.partition_point(|c| c.start < at);
959+ data.clips.insert(idx, clip);
960+ }
948961 debug_assert!(sorted_disjoint(&data.clips));
949962 }
950963 true
@@ -1001,7 +1014,15 @@ impl Project {
10011014 /// -- the whole-group delete a single-lane front-end means. `false` for a
10021015 /// bad index. Changes the mapping: the caller must reseek.
10031016 pub fn delete(&mut self, idx: usize) -> bool {
1004- let Some(clip) = self.clips().get(idx).copied() else {
1017+ self.delete_in(Lane::V1, idx)
1018+ }
1019+
1020+ /// [`delete`](Project::delete) for the lane the clip was picked on: the
1021+ /// clip's own span is cut out of *every* lane, so a take deletes whole from
1022+ /// whichever half of it was clicked. `false` for a bad index, and for a lane
1023+ /// that is not there.
1024+ pub fn delete_in(&mut self, lane: Lane, idx: usize) -> bool {
1025+ let Some(clip) = self.lane(lane).get(idx).copied() else {
10051026 return false;
10061027 };
10071028 self.ripple_delete(clip.start, clip.len())
crates/engine/tests/export.rsmodified+1 -1
@@ -74,7 +74,7 @@ fn edited(source: &Path) -> PlaybackSession {
7474 let mut session = PlaybackSession::open(source).expect("open source");
7575 assert!(session.cut_at(30.0 / FPS), "cut at frame 30");
7676 assert!(session.cut_at(60.0 / FPS), "cut at frame 60");
77- assert!(session.delete_clip(1), "drop the middle clip");
77+ assert!(session.delete_clip(Lane::V1, 1), "drop the middle clip");
7878 assert_eq!(session.clip_spans().len(), 2);
7979 session
8080 }
crates/engine/tests/playback.rsmodified+3 -3
@@ -343,7 +343,7 @@ fn edits_traverse_cuts() {
343343 assert!((session.timeline_duration() - whole).abs() < 1e-9);
344344
345345 let (cut, hole_end) = ((2.0 * fps) as u32, (3.5 * fps) as u32);
346- assert!(session.delete_clip(1), "drop the middle clip");
346+ assert!(session.delete_clip(Lane::V1, 1), "drop the middle clip");
347347 let kept = total - (hole_end - cut);
348348 assert_eq!(session.clip_spans().len(), 2);
349349 assert!(
@@ -597,7 +597,7 @@ fn edits_keep_the_audio_clock() {
597597 // Deleting the head clip moves everything after it, so the session reseeks
598598 // under the playhead: same timeline position, new source frame, and the
599599 // frame numbering restarts at the landing frame.
600- assert!(session.delete_clip(0), "drop the head clip");
600+ assert!(session.delete_clip(Lane::V1, 0), "drop the head clip");
601601 let landed = session.now();
602602 assert!(
603603 (landed - after_cut).abs() < 0.2,
@@ -695,7 +695,7 @@ fn import_appends_a_second_source_and_plays_across_the_join() {
695695 // second into the imported clip, then drop the whole original.
696696 assert!(session.cut_at(6.0), "cut 1 s into the imported clip");
697697 assert_eq!(session.clip_spans().len(), 3);
698- assert!(session.delete_clip(0), "drop the first source");
698+ assert!(session.delete_clip(Lane::V1, 0), "drop the first source");
699699 assert_eq!(session.clip_spans_by_source()[0].2, 1);
700700 assert!(
701701 (session.timeline_duration() - f64::from(second) / fps).abs() < 1e-9,
crates/engine/tests/saveload.rsmodified+8 -5
@@ -10,7 +10,7 @@
1010 use std::path::{Path, PathBuf};
1111
1212 use engine::PlaybackSession;
13-use engine::project::Source;
13+use engine::project::{Lane, Source};
1414
1515 fn asset(name: &str) -> PathBuf {
1616 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
@@ -51,7 +51,7 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() {
5151 // can join this timeline; it lands as a second source of the same file.
5252 assert!(
5353 session
54- .place_stream_at(end, &media, 1, frames)
54+ .place_stream_at(end, &media, 1, frames, None)
5555 .expect("stream 1 matches the timeline")
5656 );
5757 assert_eq!(
@@ -85,13 +85,13 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() {
8585 session.set_gain(0.0);
8686 let frames = session.clip_at(0).expect("one clip").out_frame;
8787 let err = session
88- .place_stream_at(0.0, &other, 1, frames)
88+ .place_stream_at(0.0, &other, 1, frames, None)
8989 .expect_err("22.05 kHz mono cannot join a 44.1 kHz stereo timeline")
9090 .to_string();
9191 assert!(err.contains("22050"), "unhelpful refusal: {err}");
9292 assert!(
9393 session
94- .place_stream_at(0.0, &media, 1, frames)
94+ .place_stream_at(0.0, &media, 1, frames, None)
9595 .expect_err("a file that is not a source")
9696 .to_string()
9797 .contains("not on this timeline")
@@ -109,7 +109,10 @@ fn edited(dir: &Path) -> PlaybackSession {
109109 .expect("import the second file");
110110 assert!(session.cut_at(2.0), "cut inside the first file");
111111 assert!(session.cut_at(6.5), "cut inside the second file");
112- assert!(session.delete_clip(1), "drop the second half of the first");
112+ assert!(
113+ session.delete_clip(Lane::V1, 1),
114+ "drop the second half of the first"
115+ );
113116 session.seek(3.0);
114117 assert_eq!(session.timeline_duration(), 6.0);
115118 session